From e22d28cc9c2fad52ef1b57d84a8680b7eb263457 Mon Sep 17 00:00:00 2001 From: xiyoo0812 Date: Fri, 15 Sep 2023 18:50:16 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84mysql=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/lcodec/src/mysql.h | 91 +++++++++++++++++++++++++++++---------- quanta.sln | 13 ++---- 2 files changed, 73 insertions(+), 31 deletions(-) diff --git a/extend/lcodec/src/mysql.h b/extend/lcodec/src/mysql.h index b470f1c0..477ed212 100644 --- a/extend/lcodec/src/mysql.h +++ b/extend/lcodec/src/mysql.h @@ -139,17 +139,48 @@ namespace lcodec { } size_t command_decode(lua_State* L) { + uint8_t type = *(uint8_t*)m_slice->read(); + 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 warnings = *(uint16_t*)m_slice->read(); + 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(); + //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(); uint32_t statement_id = *(uint32_t*)m_slice->read(); uint16_t num_columns = *(uint16_t*)m_slice->read(); - uint16_t num_params = *(uint16_t*)m_slice->read(); - uint8_t reserved_1 = *(uint8_t*)m_slice->read(); - uint16_t warn_params = *(uint16_t*)m_slice->read(); - return 0; + uint16_t num_params = *(uint16_t*)m_slice->read(); + 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) { @@ -271,28 +302,44 @@ namespace lcodec { lua_isinteger(L, index) ? m_buf->write(lua_tointeger(L, index)) : m_buf->write(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(data_len); - } - else if (data_len < 0xffff) { - m_buf->write(0xfc); - m_buf->write(data_len); - } - else if (data_len < 0xffffff) { - m_buf->write((0xfd << 24) | data_len); - } - else { - m_buf->write(0xfe); - m_buf->write(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(data_len); + } + else if (data_len < 0xffff) { + m_buf->write(0xfc); + m_buf->write(data_len); + } + else if (data_len < 0xffffff) { + m_buf->write((0xfd << 24) | data_len); + } + else { + m_buf->write(0xfe); + m_buf->write(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(); + if (nbyte < 0xfb) return nbyte; + if (nbyte == 0xfc) return *(uint16_t*)m_slice->read(); + if (nbyte == 0xfd) return *(uint32_t*)m_slice->read(); + if (nbyte == 0xfe) return *(uint64_t*)m_slice->read(); + 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 sessions; }; diff --git a/quanta.sln b/quanta.sln index a5d17b84..f4656f3c 100644 --- a/quanta.sln +++ b/quanta.sln @@ -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 @@ -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} @@ -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}