diff --git a/lualib/skynet/db/mysql.lua b/lualib/skynet/db/mysql.lua index f2e3db1c..73df35a6 100644 --- a/lualib/skynet/db/mysql.lua +++ b/lualib/skynet/db/mysql.lua @@ -23,15 +23,13 @@ local tonumber = tonumber local tointeger = math.tointeger local _M = { _VERSION = '0.14' } --- constants +-- constants local COM_QUERY = '\x03' local COM_PING = '\x0e' local COM_STMT_PREPARE = '\x16' local COM_STMT_EXECUTE = '\x17' - local CURSOR_TYPE_NO_CURSOR = 0x00 - local SERVER_MORE_RESULTS_EXISTS = 8 local mt = { __index = _M } @@ -49,43 +47,41 @@ converters[0x0d] = tonumber -- year converters[0xf6] = tonumber -- newdecimal local function _get_byte1(data, i) - return strbyte(data,i),i+1 + return strbyte(data,i),i+1 end local function _get_byte2(data, i) - return strunpack("0 then + if arg_num > 0 then local null_count=(arg_num+7)//8 - local f,ts,vs local types_buf="" local values_buf="" - for _,v in pairs(args) do f= store_types[type(v)] if not f then error("invalid parameter type",type(v)) end - ts,vs = f(v) - types_buf=types_buf..ts values_buf=values_buf..vs end - cmd_packet = cmd_packet..strrep("\0",null_count)..strchar(0x01)..types_buf..values_buf end @@ -574,7 +519,6 @@ local function read_result(self, sock) -- typ == 'DATA' local field_count, extra = _parse_result_set_header_packet(packet) - local cols = {} for i = 1, field_count do local col, err, errno, sqlstate = _recv_field_packet(self, sock) @@ -582,7 +526,6 @@ local function read_result(self, sock) return nil, err, errno, sqlstate --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end - cols[i] = col end @@ -600,7 +543,6 @@ local function read_result(self, sock) -- typ == 'EOF' local compact = self.compact - local rows = {} local i = 0 while true do @@ -612,11 +554,9 @@ local function read_result(self, sock) if typ == 'EOF' then local warning_count, status_flags = _parse_eof_packet(packet) - if status_flags&SERVER_MORE_RESULTS_EXISTS ~= 0 then return rows, "again" end - break end @@ -667,7 +607,6 @@ local function _query_resp(self) end function _M.connect(opts) - local self = setmetatable( {}, mt) local max_packet_size = opts.max_packet_size @@ -677,7 +616,6 @@ function _M.connect(opts) self._max_packet_size = max_packet_size self.compact = opts.compact_arrays - local database = opts.database or "" local user = opts.user or "" local password = opts.password or "" @@ -686,24 +624,20 @@ function _M.connect(opts) host = opts.host, port = opts.port or 3306, auth = _mysql_login(self,user,password,database,opts.on_connect), - overload = opts.overload, + overload = opts.overload, } self.sockchannel = channel -- try connect first only once channel:connect(true) - return self end - - function _M.disconnect(self) self.sockchannel:close() setmetatable(self, nil) end - function _M.query(self, query) local querypacket = _compose_query(self, query) local sockchannel = self.sockchannel @@ -843,14 +777,12 @@ local function _parse_row_data_binary(data, cols, compact) local col = cols[i] local typ = col.type local name = col.name - if not null_fields[i] then parser = _binary_parser[typ] if not parser then error("_parse_row_data_binary()error,unsupported field type "..typ) end value,pos = parser(data,pos) - if compact then row[i] = value else @@ -896,17 +828,15 @@ local function read_execute_result(self,sock) local col while true do packet, typ, err = _recv_packet(self, sock) - if typ=="EOF" then + if typ == "EOF" then local warning_count, status_flags = _parse_eof_packet(packet) break end - col = _parse_field_packet(packet) if not col then break --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) end - table.insert(cols,col) end @@ -920,20 +850,17 @@ local function read_execute_result(self,sock) local row while true do packet, typ, err = _recv_packet(self, sock) - - if typ=="EOF" then + if typ == "EOF" then local warning_count, status_flags = _parse_eof_packet(packet) if status_flags&SERVER_MORE_RESULTS_EXISTS ~= 0 then return rows, "again" end break end - row = _parse_row_data_binary(packet,cols,compact) if not col then break end - table.insert(rows,row) end @@ -1032,24 +959,23 @@ function _M.server_ver(self) end local escape_map = { - ['\0'] = "\\0", - ['\b'] = "\\b", - ['\n'] = "\\n", - ['\r'] = "\\r", - ['\t'] = "\\t", - ['\26'] = "\\Z", - ['\\'] = "\\\\", - ["'"] = "\\'", - ['"'] = '\\"', + ['\0'] = "\\0", + ['\b'] = "\\b", + ['\n'] = "\\n", + ['\r'] = "\\r", + ['\t'] = "\\t", + ['\26'] = "\\Z", + ['\\'] = "\\\\", + ["'"] = "\\'", + ['"'] = '\\"', } function _M.quote_sql_str( str) - return strformat("'%s'", strgsub(str, "[\0\b\n\r\t\26\\\'\"]", escape_map)) + return strformat("'%s'", strgsub(str, "[\0\b\n\r\t\26\\\'\"]", escape_map)) end function _M.set_compact_arrays(self, value) self.compact = value end - return _M