format up by vscode plugin

This commit is contained in:
Hanlei Qin
2019-10-29 15:05:00 +08:00
committed by 云风
parent 6b4050bf2c
commit 49c21916a2

View File

@@ -7,7 +7,6 @@
local socketchannel = require "skynet.socketchannel" local socketchannel = require "skynet.socketchannel"
local crypt = require "skynet.crypt" local crypt = require "skynet.crypt"
local sub = string.sub local sub = string.sub
local strgsub = string.gsub local strgsub = string.gsub
local strformat = string.format local strformat = string.format
@@ -16,23 +15,23 @@ local strchar = string.char
local strrep = string.rep local strrep = string.rep
local strunpack = string.unpack local strunpack = string.unpack
local strpack = string.pack local strpack = string.pack
local sha1= crypt.sha1 local sha1 = crypt.sha1
local setmetatable = setmetatable local setmetatable = setmetatable
local error = error local error = error
local tonumber = tonumber local tonumber = tonumber
local tointeger = math.tointeger local tointeger = math.tointeger
local _M = { _VERSION = '0.14' } local _M = {_VERSION = "0.14"}
-- constants -- constants
local COM_QUERY = '\x03' local COM_QUERY = "\x03"
local COM_PING = '\x0e' local COM_PING = "\x0e"
local COM_STMT_PREPARE = '\x16' local COM_STMT_PREPARE = "\x16"
local COM_STMT_EXECUTE = '\x17' local COM_STMT_EXECUTE = "\x17"
local CURSOR_TYPE_NO_CURSOR = 0x00 local CURSOR_TYPE_NO_CURSOR = 0x00
local SERVER_MORE_RESULTS_EXISTS = 8 local SERVER_MORE_RESULTS_EXISTS = 8
local mt = { __index = _M } local mt = {__index = _M}
-- mysql field value type converters -- mysql field value type converters
local converters = {} local converters = {}
@@ -47,31 +46,31 @@ converters[0x0d] = tonumber -- year
converters[0xf6] = tonumber -- newdecimal converters[0xf6] = tonumber -- newdecimal
local function _get_byte1(data, i) local function _get_byte1(data, i)
return strbyte(data,i),i+1 return strbyte(data, i), i + 1
end end
local function _get_byte2(data, i) local function _get_byte2(data, i)
return strunpack("<I2",data,i) return strunpack("<I2", data, i)
end end
local function _get_byte3(data, i) local function _get_byte3(data, i)
return strunpack("<I3",data,i) return strunpack("<I3", data, i)
end end
local function _get_byte4(data, i) local function _get_byte4(data, i)
return strunpack("<I4",data,i) return strunpack("<I4", data, i)
end end
local function _get_byte8(data, i) local function _get_byte8(data, i)
return strunpack("<I8",data,i) return strunpack("<I8", data, i)
end end
local function _get_float(data, i) local function _get_float(data, i)
return strunpack("<f",data,i) return strunpack("<f", data, i)
end end
local function _get_double(data, i) local function _get_double(data, i)
return strunpack("<d",data,i) return strunpack("<d", data, i)
end end
local function _set_byte2(n) local function _set_byte2(n)
@@ -103,7 +102,13 @@ local function _from_cstring(data, i)
end end
local function _dumphex(bytes) local function _dumphex(bytes)
return strgsub(bytes, ".", function(x) return strformat("%02x ", strbyte(x)) end) return strgsub(
bytes,
".",
function(x)
return strformat("%02x ", strbyte(x))
end
)
end end
local function _compute_token(password, scramble) local function _compute_token(password, scramble)
@@ -118,22 +123,25 @@ local function _compute_token(password, scramble)
local stage3 = sha1(scramble .. stage2) local stage3 = sha1(scramble .. stage2)
local i = 0 local i = 0
return strgsub(stage3,".", return strgsub(
stage3,
".",
function(x) function(x)
i = i + 1 i = i + 1
-- ~ is xor in lua 5.3 -- ~ is xor in lua 5.3
return strchar(strbyte(x) ~ strbyte(stage1, i)) return strchar(strbyte(x) ~ strbyte(stage1, i))
end) end
)
end end
local function _compose_packet(self, req) local function _compose_packet(self, req)
self.packet_no = self.packet_no + 1 self.packet_no = self.packet_no + 1
local size = #req local size = #req
return strpack("<I3Bc"..size,size,self.packet_no,req) return strpack("<I3Bc" .. size, size, self.packet_no, req)
end end
local function _recv_packet(self,sock) local function _recv_packet(self, sock)
local data = sock:read( 4) local data = sock:read(4)
if not data then if not data then
return nil, nil, "failed to receive packet header: " return nil, nil, "failed to receive packet header: "
end end
@@ -199,19 +207,19 @@ local function _from_length_coded_bin(data, pos)
end end
local function _set_length_coded_bin(n) local function _set_length_coded_bin(n)
if n<251 then if n < 251 then
return strchar(n) return strchar(n)
end end
if n<(1<<16) then if n < (1 << 16) then
return strpack('<BI2',0xfc,n) return strpack("<BI2", 0xfc, n)
end end
if n<(1<<24) then if n < (1 << 24) then
return strpack('<BI3',0xfd,n) return strpack("<BI3", 0xfd, n)
end end
return strpack('<BI8',0xfe,n) return strpack("<BI8", 0xfe, n)
end end
local function _from_length_coded_str(data, pos) local function _from_length_coded_str(data, pos)
@@ -250,7 +258,7 @@ local function _parse_err_packet(packet)
local errno, pos = _get_byte2(packet, 2) local errno, pos = _get_byte2(packet, 2)
local marker = sub(packet, pos, pos) local marker = sub(packet, pos, pos)
local sqlstate local sqlstate
if marker == '#' then if marker == "#" then
-- with sqlstate -- with sqlstate
pos = pos + 1 pos = pos + 1
sqlstate = sub(packet, pos, pos + 5 - 1) sqlstate = sub(packet, pos, pos + 5 - 1)
@@ -291,7 +299,6 @@ local function _parse_field_packet(data)
col.default = default col.default = default
end end
--]] --]]
return col return col
end end
@@ -333,7 +340,7 @@ local function _recv_field_packet(self, sock)
return nil, msg, errno, sqlstate return nil, msg, errno, sqlstate
end end
if typ ~= 'DATA' then if typ ~= "DATA" then
return nil, "bad field packet type: " .. typ return nil, "bad field packet type: " .. typ
end end
@@ -344,17 +351,17 @@ end
local function _recv_decode_packet_resp(self) local function _recv_decode_packet_resp(self)
return function(sock) return function(sock)
local packet, typ, err = _recv_packet(self,sock) local packet, typ, err = _recv_packet(self, sock)
if not packet then if not packet then
return false, "failed to receive the result packet"..err return false, "failed to receive the result packet" .. err
end end
if typ == 'ERR' then if typ == "ERR" then
local errno, msg, sqlstate = _parse_err_packet(packet) local errno, msg, sqlstate = _parse_err_packet(packet)
return false, strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate) return false, strformat("errno:%d, msg:%s,sqlstate:%s", errno, msg, sqlstate)
end end
if typ == 'EOF' then if typ == "EOF" then
return false, "old pre-4.1 authentication protocol not supported" return false, "old pre-4.1 authentication protocol not supported"
end end
@@ -362,10 +369,10 @@ local function _recv_decode_packet_resp(self)
end end
end end
local function _mysql_login(self,user,password,database,on_connect) local function _mysql_login(self, user, password, database, on_connect)
return function(sockchannel) return function(sockchannel)
local dispatch_resp = _recv_decode_packet_resp(self) local dispatch_resp = _recv_decode_packet_resp(self)
local packet = sockchannel:response( dispatch_resp ) local packet = sockchannel:response(dispatch_resp)
self.protocol_ver = strbyte(packet) self.protocol_ver = strbyte(packet)
@@ -393,7 +400,7 @@ local function _mysql_login(self,user,password,database,on_connect)
local more_capabilities local more_capabilities
more_capabilities, pos = _get_byte2(packet, pos) more_capabilities, pos = _get_byte2(packet, pos)
self._server_capabilities = self._server_capabilities|more_capabilities<<16 self._server_capabilities = self._server_capabilities | more_capabilities << 16
local len = 21 - 8 - 1 local len = 21 - 8 - 1
pos = pos + 1 + 10 pos = pos + 1 + 10
@@ -403,17 +410,20 @@ local function _mysql_login(self,user,password,database,on_connect)
error "2nd part of scramble not found" error "2nd part of scramble not found"
end end
local scramble = scramble1..scramble_part2 local scramble = scramble1 .. scramble_part2
local token = _compute_token(password, scramble) local token = _compute_token(password, scramble)
local client_flags = 260047; local client_flags = 260047
local req = strpack("<I4I4c24zs1z", local req =
strpack(
"<I4I4c24zs1z",
client_flags, client_flags,
self._max_packet_size, self._max_packet_size,
strrep("\0", 24), -- TODO: add support for charset encoding strrep("\0", 24), -- TODO: add support for charset encoding
user, user,
token, token,
database) database
local authpacket=_compose_packet(self,req) )
local authpacket = _compose_packet(self, req)
sockchannel:request(authpacket, dispatch_resp) sockchannel:request(authpacket, dispatch_resp)
if on_connect then if on_connect then
on_connect(self) on_connect(self)
@@ -429,7 +439,7 @@ end
local function _compose_query(self, query) local function _compose_query(self, query)
self.packet_no = -1 self.packet_no = -1
local cmd_packet = COM_QUERY..query local cmd_packet = COM_QUERY .. query
return _compose_packet(self, cmd_packet) return _compose_packet(self, cmd_packet)
end end
@@ -441,50 +451,50 @@ end
--参数字段类型转换 --参数字段类型转换
local store_types = { local store_types = {
number=function(v) number = function(v)
if not tointeger(v) then if not tointeger(v) then
return _set_byte2(0x05),_set_double(v) return _set_byte2(0x05), _set_double(v)
else else
return _set_byte2(0x08),_set_byte8(v) return _set_byte2(0x08), _set_byte8(v)
end end
end, end,
string=function(v) string = function(v)
return _set_byte2(0x0f),_set_length_coded_bin(#v)..v return _set_byte2(0x0f), _set_length_coded_bin(#v) .. v
end, end,
--bool转换为0,1 --bool转换为0,1
boolean=function(v) boolean = function(v)
if v then if v then
return _set_byte2(0x01),strchar(1) return _set_byte2(0x01), strchar(1)
else else
return _set_byte2(0x01),strchar(0) return _set_byte2(0x01), strchar(0)
end
end end
end,
} }
local function _compose_stmt_execute(self,stmt,cursor_type,args) local function _compose_stmt_execute(self, stmt, cursor_type, args)
local arg_num = #args local arg_num = #args
if arg_num ~= stmt.param_count then if arg_num ~= stmt.param_count then
error("require stmt.param_count "..stmt.param_count.." get arg_num "..arg_num) error("require stmt.param_count " .. stmt.param_count .. " get arg_num " .. arg_num)
end end
self.packet_no = -1 self.packet_no = -1
local cmd_packet = strpack("<c1I4BI4",COM_STMT_EXECUTE,stmt.prepare_id,cursor_type,0x01) local cmd_packet = strpack("<c1I4BI4", COM_STMT_EXECUTE, stmt.prepare_id, cursor_type, 0x01)
if arg_num > 0 then if arg_num > 0 then
local null_count=(arg_num+7)//8 local null_count = (arg_num + 7) // 8
local f,ts,vs local f, ts, vs
local types_buf="" local types_buf = ""
local values_buf="" local values_buf = ""
for _,v in pairs(args) do for _, v in pairs(args) do
f= store_types[type(v)] f = store_types[type(v)]
if not f then if not f then
error("invalid parameter type",type(v)) error("invalid parameter type", type(v))
end end
ts,vs = f(v) ts, vs = f(v)
types_buf=types_buf..ts types_buf = types_buf .. ts
values_buf=values_buf..vs values_buf = values_buf .. vs
end end
cmd_packet = cmd_packet..strrep("\0",null_count)..strchar(0x01)..types_buf..values_buf cmd_packet = cmd_packet .. strrep("\0", null_count) .. strchar(0x01) .. types_buf .. values_buf
end end
return _compose_packet(self, cmd_packet) return _compose_packet(self, cmd_packet)
@@ -503,15 +513,15 @@ local function read_result(self, sock)
--error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate))
end end
if typ == 'OK' then if typ == "OK" then
local res = _parse_ok_packet(packet) local res = _parse_ok_packet(packet)
if res and res.server_status&SERVER_MORE_RESULTS_EXISTS ~= 0 then if res and res.server_status & SERVER_MORE_RESULTS_EXISTS ~= 0 then
return res, "again" return res, "again"
end end
return res return res
end end
if typ ~= 'DATA' then if typ ~= "DATA" then
return nil, "packet type " .. typ .. " not supported" return nil, "packet type " .. typ .. " not supported"
--error( "packet type " .. typ .. " not supported" ) --error( "packet type " .. typ .. " not supported" )
end end
@@ -535,9 +545,9 @@ local function read_result(self, sock)
return nil, err return nil, err
end end
if typ ~= 'EOF' then if typ ~= "EOF" then
--error ( "unexpected packet type " .. typ .. " while eof packet is ".. "expected" ) --error ( "unexpected packet type " .. typ .. " while eof packet is ".. "expected" )
return nil, "unexpected packet type " .. typ .. " while eof packet is ".. "expected" return nil, "unexpected packet type " .. typ .. " while eof packet is " .. "expected"
end end
-- typ == 'EOF' -- typ == 'EOF'
@@ -552,9 +562,9 @@ local function read_result(self, sock)
return nil, err return nil, err
end end
if typ == 'EOF' then if typ == "EOF" then
local warning_count, status_flags = _parse_eof_packet(packet) local warning_count, status_flags = _parse_eof_packet(packet)
if status_flags&SERVER_MORE_RESULTS_EXISTS ~= 0 then if status_flags & SERVER_MORE_RESULTS_EXISTS ~= 0 then
return rows, "again" return rows, "again"
end end
break break
@@ -575,23 +585,23 @@ end
local function _query_resp(self) local function _query_resp(self)
return function(sock) return function(sock)
local res, err, errno, sqlstate = read_result(self,sock) local res, err, errno, sqlstate = read_result(self, sock)
if not res then if not res then
local badresult ={} local badresult = {}
badresult.badresult = true badresult.badresult = true
badresult.err = err badresult.err = err
badresult.errno = errno badresult.errno = errno
badresult.sqlstate = sqlstate badresult.sqlstate = sqlstate
return true , badresult return true, badresult
end end
if err ~= "again" then if err ~= "again" then
return true, res return true, res
end end
local multiresultset = {res} local multiresultset = {res}
multiresultset.multiresultset = true multiresultset.multiresultset = true
local i =2 local i = 2
while err =="again" do while err == "again" do
res, err, errno, sqlstate = read_result(self,sock) res, err, errno, sqlstate = read_result(self, sock)
if not res then if not res then
multiresultset.badresult = true multiresultset.badresult = true
multiresultset.err = err multiresultset.err = err
@@ -599,15 +609,15 @@ local function _query_resp(self)
multiresultset.sqlstate = sqlstate multiresultset.sqlstate = sqlstate
return true, multiresultset return true, multiresultset
end end
multiresultset[i]=res multiresultset[i] = res
i=i+1 i = i + 1
end end
return true, multiresultset return true, multiresultset
end end
end end
function _M.connect(opts) function _M.connect(opts)
local self = setmetatable( {}, mt) local self = setmetatable({}, mt)
local max_packet_size = opts.max_packet_size local max_packet_size = opts.max_packet_size
if not max_packet_size then if not max_packet_size then
@@ -620,11 +630,12 @@ function _M.connect(opts)
local user = opts.user or "" local user = opts.user or ""
local password = opts.password or "" local password = opts.password or ""
local channel = socketchannel.channel { local channel =
socketchannel.channel {
host = opts.host, host = opts.host,
port = opts.port or 3306, port = opts.port or 3306,
auth = _mysql_login(self,user,password,database,opts.on_connect), auth = _mysql_login(self, user, password, database, opts.on_connect),
overload = opts.overload, overload = opts.overload
} }
self.sockchannel = channel self.sockchannel = channel
-- try connect first only once -- try connect first only once
@@ -644,7 +655,7 @@ function _M.query(self, query)
if not self.query_resp then if not self.query_resp then
self.query_resp = _query_resp(self) self.query_resp = _query_resp(self)
end end
return sockchannel:request( querypacket, self.query_resp ) return sockchannel:request(querypacket, self.query_resp)
end end
local function read_prepare_result(self, sock) local function read_prepare_result(self, sock)
@@ -670,61 +681,61 @@ local function read_prepare_result(self, sock)
if typ ~= "OK" then if typ ~= "OK" then
resp.badresult = true resp.badresult = true
resp.errno = 300201 resp.errno = 300201
resp.err = "first typ must be OK,now"..typ resp.err = "first typ must be OK,now" .. typ
return false,resp return false, resp
end end
resp.prepare_id,resp.field_count,resp.param_count,resp.warning_count = strunpack("<I4I2I2xI2",packet,2) resp.prepare_id, resp.field_count, resp.param_count, resp.warning_count = strunpack("<I4I2I2xI2", packet, 2)
resp.params = {} resp.params = {}
resp.fields = {} resp.fields = {}
if resp.param_count >0 then if resp.param_count > 0 then
local param = _recv_field_packet(self,sock) local param = _recv_field_packet(self, sock)
while param do while param do
table.insert(resp.params,param) table.insert(resp.params, param)
param = _recv_field_packet(self,sock) param = _recv_field_packet(self, sock)
end end
end end
if resp.field_count>0 then if resp.field_count > 0 then
local field = _recv_field_packet(self,sock) local field = _recv_field_packet(self, sock)
while field do while field do
table.insert(resp.fields,field) table.insert(resp.fields, field)
field = _recv_field_packet(self,sock) field = _recv_field_packet(self, sock)
end end
end end
return true,resp return true, resp
end end
local function _prepare_resp(self,sql) local function _prepare_resp(self, sql)
return function(sock) return function(sock)
return read_prepare_result(self,sock,sql) return read_prepare_result(self, sock, sql)
end end
end end
-- 注册预处理语句 -- 注册预处理语句
function _M.prepare(self,sql) function _M.prepare(self, sql)
local querypacket = _compose_stmt_prepare(self, sql) local querypacket = _compose_stmt_prepare(self, sql)
local sockchannel = self.sockchannel local sockchannel = self.sockchannel
if not self.prepare_resp then if not self.prepare_resp then
self.prepare_resp = _prepare_resp(self) self.prepare_resp = _prepare_resp(self)
end end
return sockchannel:request( querypacket, self.prepare_resp ) return sockchannel:request(querypacket, self.prepare_resp)
end end
local function _get_datetime(data,pos) local function _get_datetime(data, pos)
local len,year,month,day,hour,minute,second local len, year, month, day, hour, minute, second
local value local value
len,pos = _from_length_coded_bin(data,pos) len, pos = _from_length_coded_bin(data, pos)
if len==7 then if len == 7 then
year,month,day,hour,minute,second,pos=string.unpack("<I2BBBBB",data,pos) year, month, day, hour, minute, second, pos = string.unpack("<I2BBBBB", data, pos)
value = strformat("%04d-%02d-%02d %02d:%02d:%02d",year,month,day,hour,minute,second) value = strformat("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second)
else else
value = "2017-09-09 20:08:09" value = "2017-09-09 20:08:09"
--unsupported format --unsupported format
pos=pos+len pos = pos + len
end end
return value,pos return value, pos
end end
local _binary_parser = { local _binary_parser = {
@@ -743,31 +754,31 @@ local _binary_parser = {
[0xfb] = _from_length_coded_str, [0xfb] = _from_length_coded_str,
[0xfc] = _from_length_coded_str, [0xfc] = _from_length_coded_str,
[0xfd] = _from_length_coded_str, [0xfd] = _from_length_coded_str,
[0xfe] = _from_length_coded_str, [0xfe] = _from_length_coded_str
} }
local function _parse_row_data_binary(data, cols, compact) local function _parse_row_data_binary(data, cols, compact)
local ncols = #cols local ncols = #cols
-- 空位图,前两个bit系统保留 (列数量 + 7 + 2) / 8 -- 空位图,前两个bit系统保留 (列数量 + 7 + 2) / 8
local null_count=(ncols+9)//8 local null_count = (ncols + 9) // 8
local pos = 2+null_count local pos = 2 + null_count
local value local value
--空字段表 --空字段表
local null_fields= {} local null_fields = {}
local field_index=1 local field_index = 1
local byte local byte
for i=2,pos-1 do for i = 2, pos - 1 do
byte = strbyte(data,i) byte = strbyte(data, i)
for j=0,7 do for j = 0, 7 do
if field_index>2 then if field_index > 2 then
if byte&(1<<j)==0 then if byte & (1 << j) == 0 then
null_fields[field_index-2]=false null_fields[field_index - 2] = false
else else
null_fields[field_index-2]=true null_fields[field_index - 2] = true
end end
end end
field_index=field_index+1 field_index = field_index + 1
end end
end end
@@ -780,9 +791,9 @@ local function _parse_row_data_binary(data, cols, compact)
if not null_fields[i] then if not null_fields[i] then
parser = _binary_parser[typ] parser = _binary_parser[typ]
if not parser then if not parser then
error("_parse_row_data_binary()error,unsupported field type "..typ) error("_parse_row_data_binary()error,unsupported field type " .. typ)
end end
value,pos = parser(data,pos) value, pos = parser(data, pos)
if compact then if compact then
row[i] = value row[i] = value
else else
@@ -794,7 +805,7 @@ local function _parse_row_data_binary(data, cols, compact)
return row return row
end end
local function read_execute_result(self,sock) local function read_execute_result(self, sock)
local packet, typ, err = _recv_packet(self, sock) local packet, typ, err = _recv_packet(self, sock)
if not packet then if not packet then
return nil, err return nil, err
@@ -807,15 +818,15 @@ local function read_execute_result(self,sock)
--error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate))
end end
if typ == 'OK' then if typ == "OK" then
local res = _parse_ok_packet(packet) local res = _parse_ok_packet(packet)
if res and res.server_status&SERVER_MORE_RESULTS_EXISTS ~= 0 then if res and res.server_status & SERVER_MORE_RESULTS_EXISTS ~= 0 then
return res, "again" return res, "again"
end end
return res return res
end end
if typ ~= 'DATA' then if typ ~= "DATA" then
return nil, "packet type " .. typ .. " not supported" return nil, "packet type " .. typ .. " not supported"
--error( "packet type " .. typ .. " not supported" ) --error( "packet type " .. typ .. " not supported" )
end end
@@ -837,11 +848,11 @@ local function read_execute_result(self,sock)
break break
--error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate)) --error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate))
end end
table.insert(cols,col) table.insert(cols, col)
end end
--没有记录集返回 --没有记录集返回
if #cols<1 then if #cols < 1 then
return {} return {}
end end
@@ -852,16 +863,16 @@ local function read_execute_result(self,sock)
packet, typ, err = _recv_packet(self, sock) packet, typ, err = _recv_packet(self, sock)
if typ == "EOF" then if typ == "EOF" then
local warning_count, status_flags = _parse_eof_packet(packet) local warning_count, status_flags = _parse_eof_packet(packet)
if status_flags&SERVER_MORE_RESULTS_EXISTS ~= 0 then if status_flags & SERVER_MORE_RESULTS_EXISTS ~= 0 then
return rows, "again" return rows, "again"
end end
break break
end end
row = _parse_row_data_binary(packet,cols,compact) row = _parse_row_data_binary(packet, cols, compact)
if not col then if not col then
break break
end end
table.insert(rows,row) table.insert(rows, row)
end end
return rows return rows
@@ -869,23 +880,23 @@ end
local function _execute_resp(self) local function _execute_resp(self)
return function(sock) return function(sock)
local res, err, errno, sqlstate = read_execute_result(self,sock) local res, err, errno, sqlstate = read_execute_result(self, sock)
if not res then if not res then
local badresult ={} local badresult = {}
badresult.badresult = true badresult.badresult = true
badresult.err = err badresult.err = err
badresult.errno = errno badresult.errno = errno
badresult.sqlstate = sqlstate badresult.sqlstate = sqlstate
return true , badresult return true, badresult
end end
if err ~= "again" then if err ~= "again" then
return true, res return true, res
end end
local mulitresultset = {res} local mulitresultset = {res}
mulitresultset.mulitresultset = true mulitresultset.mulitresultset = true
local i =2 local i = 2
while err =="again" do while err == "again" do
res, err, errno, sqlstate = read_execute_result(self,sock) res, err, errno, sqlstate = read_execute_result(self, sock)
if not res then if not res then
mulitresultset.badresult = true mulitresultset.badresult = true
mulitresultset.err = err mulitresultset.err = err
@@ -893,8 +904,8 @@ local function _execute_resp(self)
mulitresultset.sqlstate = sqlstate mulitresultset.sqlstate = sqlstate
return true, mulitresultset return true, mulitresultset
end end
mulitresultset[i]=res mulitresultset[i] = res
i=i+1 i = i + 1
end end
return true, mulitresultset return true, mulitresultset
end end
@@ -908,50 +919,50 @@ end
sqlstate sqlstate
err err
]] ]]
function _M.execute(self, stmt,...) function _M.execute(self, stmt, ...)
-- 检查参数不能为nil -- 检查参数不能为nil
local p_n = select('#',...) local p_n = select("#", ...)
local p_v local p_v
for i=1,p_n do for i = 1, p_n do
p_v = select(i,...) p_v = select(i, ...)
if p_v==nil then if p_v == nil then
return { return {
badresult=true, badresult = true,
errno=30902, errno = 30902,
err="parameter "..i.." is nil", err = "parameter " .. i .. " is nil"
} }
end end
end end
local querypacket,er = _compose_stmt_execute(self,stmt,CURSOR_TYPE_NO_CURSOR,{...}) local querypacket, er = _compose_stmt_execute(self, stmt, CURSOR_TYPE_NO_CURSOR, {...})
if not querypacket then if not querypacket then
return { return {
badresult=true, badresult = true,
errno=30902, errno = 30902,
err=er, err = er
} }
end end
local sockchannel = self.sockchannel local sockchannel = self.sockchannel
if not self.execute_resp then if not self.execute_resp then
self.execute_resp = _execute_resp(self) self.execute_resp = _execute_resp(self)
end end
return sockchannel:request( querypacket, self.execute_resp ) return sockchannel:request(querypacket, self.execute_resp)
end end
function _M.ping(self) function _M.ping(self)
local querypacket,er = _compose_ping(self) local querypacket, er = _compose_ping(self)
if not querypacket then if not querypacket then
return { return {
badresult=true, badresult = true,
errno=30902, errno = 30902,
err=er, err = er
} }
end end
local sockchannel = self.sockchannel local sockchannel = self.sockchannel
if not self.query_resp then if not self.query_resp then
self.query_resp = _query_resp(self) self.query_resp = _query_resp(self)
end end
return sockchannel:request( querypacket, self.query_resp ) return sockchannel:request(querypacket, self.query_resp)
end end
function _M.server_ver(self) function _M.server_ver(self)
@@ -959,19 +970,19 @@ function _M.server_ver(self)
end end
local escape_map = { local escape_map = {
['\0'] = "\\0", ["\0"] = "\\0",
['\b'] = "\\b", ["\b"] = "\\b",
['\n'] = "\\n", ["\n"] = "\\n",
['\r'] = "\\r", ["\r"] = "\\r",
['\t'] = "\\t", ["\t"] = "\\t",
['\26'] = "\\Z", ["\26"] = "\\Z",
['\\'] = "\\\\", ["\\"] = "\\\\",
["'"] = "\\'", ["'"] = "\\'",
['"'] = '\\"', ['"'] = '\\"'
} }
function _M.quote_sql_str( str) 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 end
function _M.set_compact_arrays(self, value) function _M.set_compact_arrays(self, value)