Skip to content

Commit

Permalink
重构mysql组件
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed Sep 15, 2023
1 parent b90b62e commit e22d28c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 31 deletions.
91 changes: 69 additions & 22 deletions extend/lcodec/src/mysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,48 @@ namespace lcodec {
}

size_t command_decode(lua_State* L) {
uint8_t type = *(uint8_t*)m_slice->read<uint8_t>();
if (type == 0x00) ok_packet_decode(L);
if (type == 0xff) err_packet_decode(L);
return 0;
}

void ok_packet_decode(lua_State* L) {
size_t data_len;
size_t affected_rows = decode_length_encoded_number();
size_t last_insert_id = decode_length_encoded_number();
uint16_t status_flags = *(uint16_t*)m_slice->read<uint16_t>();
uint16_t warnings = *(uint16_t*)m_slice->read<uint16_t>();
const char* info = read_cstring(m_slice, data_len);
lua_pushboolean(L, true);
lua_pushinteger(L, affected_rows);
lua_pushinteger(L, last_insert_id);
lua_pushinteger(L, warnings);
lua_pushstring(L, info);
}

void err_packet_decode(lua_State* L) {
uint16_t errnoo = *(uint16_t*)m_slice->read<uint16_t>();
//skip sql_state_marker
size_t data_len;
char* sql_state = (char*)m_slice->peek(5, 1);
const char* error_message = read_cstring(m_slice, data_len);
lua_pushboolean(L, false);
lua_pushinteger(L, errnoo);
lua_pushstring(L, sql_state);
lua_pushstring(L, error_message);
}

size_t prepare_decode(lua_State* L) {
uint8_t status = *(uint8_t*)m_slice->read<uint8_t>();
uint32_t statement_id = *(uint32_t*)m_slice->read<uint32_t>();
uint16_t num_columns = *(uint16_t*)m_slice->read<uint16_t>();
uint16_t num_params = *(uint16_t*)m_slice->read<uint16_t>();
uint8_t reserved_1 = *(uint8_t*)m_slice->read<uint8_t>();
uint16_t warn_params = *(uint16_t*)m_slice->read<uint16_t>();
return 0;
uint16_t num_params = *(uint16_t*)m_slice->read<uint16_t>();
int top = lua_gettop(L);
lua_pushinteger(L, statement_id);
lua_pushinteger(L, num_columns);
lua_pushinteger(L, num_params);
return lua_gettop(L) - top;
}

size_t auth_decode(lua_State* L) {
Expand Down Expand Up @@ -271,28 +302,44 @@ namespace lcodec {
lua_isinteger(L, index) ? m_buf->write<uint64_t>(lua_tointeger(L, index)) : m_buf->write<double>(lua_tonumber(L, index));
break;
case LUA_TSTRING: {
uint32_t data_len;
uint8_t* data = (uint8_t*)lua_tolstring(L, index, (size_t*)&data_len);
if (data_len < 0xfb) {
m_buf->write<uint8_t>(data_len);
}
else if (data_len < 0xffff) {
m_buf->write<uint8_t>(0xfc);
m_buf->write<uint16_t>(data_len);
}
else if (data_len < 0xffffff) {
m_buf->write<uint32_t>((0xfd << 24) | data_len);
}
else {
m_buf->write<uint8_t>(0xfe);
m_buf->write<uint64_t>(data_len);
uint32_t data_len;
uint8_t* data = (uint8_t*)lua_tolstring(L, index, (size_t*)&data_len);
if (data_len < 0xfb) {
m_buf->write<uint8_t>(data_len);
}
else if (data_len < 0xffff) {
m_buf->write<uint8_t>(0xfc);
m_buf->write<uint16_t>(data_len);
}
else if (data_len < 0xffffff) {
m_buf->write<uint32_t>((0xfd << 24) | data_len);
}
else {
m_buf->write<uint8_t>(0xfe);
m_buf->write<uint64_t>(data_len);
}
m_buf->push_data(data, data_len);
}
m_buf->push_data(data, data_len);
}
break;
break;
}
}

size_t decode_length_encoded_number() {
uint8_t nbyte = *(uint8_t*)m_slice->read<uint8_t>();
if (nbyte < 0xfb) return nbyte;
if (nbyte == 0xfc) return *(uint16_t*)m_slice->read<uint16_t>();
if (nbyte == 0xfd) return *(uint32_t*)m_slice->read<uint32_t>();
if (nbyte == 0xfe) return *(uint64_t*)m_slice->read<uint64_t>();
throw invalid_argument("invalid length coded number:" + nbyte);
}

string_view decode_length_encoded_string() {
size_t length = decode_length_encoded_number();
char* data = (char*)m_slice->peek(length);
if (!data) throw invalid_argument("invalid length coded string:" + length);
return string_view(data, length);
}

protected:
deque<mysql_cmd> sessions;
};
Expand Down
13 changes: 4 additions & 9 deletions quanta.sln
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "share", "share", "{D19A19B1
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mimalloc", "extend\mimalloc\mimalloc.vcxproj", "{6805C141-3788-A994-7DCA-ADD6574D1386}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "luabus", "core\luabus\luabus.vcxproj", "{EC8C4743-6D5F-E10D-3D10-98330150B2C9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Develop|x64 = Develop|x64
Expand Down Expand Up @@ -143,16 +141,12 @@ Global
{1BD1853E-C5E4-992D-7266-AA4AA72600AB}.Develop|x64.Build.0 = Develop|x64
{6805C141-3788-A994-7DCA-ADD6574D1386}.Develop|x64.ActiveCfg = Develop|x64
{6805C141-3788-A994-7DCA-ADD6574D1386}.Develop|x64.Build.0 = Develop|x64
{EC8C4743-6D5F-E10D-3D10-98330150B2C9}.Develop|x64.ActiveCfg = Develop|x64
{EC8C4743-6D5F-E10D-3D10-98330150B2C9}.Develop|x64.Build.0 = Develop|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{AB6A8F06-D7BB-8801-F776-B07145542020} = {1A3E0FD4-F765-A5C3-A989-0F78B8FA23A3}
{2CE1C7DB-EA72-ED68-C587-CA3383754760} = {AB6A8F06-D7BB-8801-F776-B07145542020}
{DEDA8889-2128-69C7-B2CC-3996BC8EB907} = {1A3E0FD4-F765-A5C3-A989-0F78B8FA23A3}
{DC8FEA7C-8450-92D3-4C20-3A6D15D99A08} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907}
{15BEA432-628B-E5CF-E5DE-A95D2DB86C4F} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907}
{E24CB1AD-193A-E647-45BE-564E7264711B} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907}
Expand All @@ -166,13 +160,14 @@ Global
{2967F038-B90B-EBF5-B268-BE3BAF66D417} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907}
{B2999D78-279A-1A53-CC82-74D494144722} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907}
{7186BCD3-4393-85B5-963F-880AC8A6F795} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907}
{EEE587B4-691E-274F-5E0D-3D4754F2B4B6} = {1A3E0FD4-F765-A5C3-A989-0F78B8FA23A3}
{B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} = {EEE587B4-691E-274F-5E0D-3D4754F2B4B6}
{F8DBBBDB-3B80-B4F1-70A8-272978F579EB} = {EEE587B4-691E-274F-5E0D-3D4754F2B4B6}
{1BD1853E-C5E4-992D-7266-AA4AA72600AB} = {EEE587B4-691E-274F-5E0D-3D4754F2B4B6}
{D19A19B1-8C4C-8963-0B32-A63B866BF960} = {1A3E0FD4-F765-A5C3-A989-0F78B8FA23A3}
{6805C141-3788-A994-7DCA-ADD6574D1386} = {D19A19B1-8C4C-8963-0B32-A63B866BF960}
{EC8C4743-6D5F-E10D-3D10-98330150B2C9} = {AB6A8F06-D7BB-8801-F776-B07145542020}
{AB6A8F06-D7BB-8801-F776-B07145542020} = {1A3E0FD4-F765-A5C3-A989-0F78B8FA23A3}
{DEDA8889-2128-69C7-B2CC-3996BC8EB907} = {1A3E0FD4-F765-A5C3-A989-0F78B8FA23A3}
{EEE587B4-691E-274F-5E0D-3D4754F2B4B6} = {1A3E0FD4-F765-A5C3-A989-0F78B8FA23A3}
{D19A19B1-8C4C-8963-0B32-A63B866BF960} = {1A3E0FD4-F765-A5C3-A989-0F78B8FA23A3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {63D02246-6BAD-4132-9325-6DDF29305452}
Expand Down

0 comments on commit e22d28c

Please sign in to comment.