mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
format up :
0. remove some useless multiple blank lines. 1. uniform indent as white-space. 2. add some expression white-space pad.
This commit is contained in:
@@ -23,15 +23,13 @@ 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 }
|
||||||
@@ -49,43 +47,41 @@ 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)
|
||||||
return strpack("<I2", n)
|
return strpack("<I2", n)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function _set_byte3(n)
|
local function _set_byte3(n)
|
||||||
return strpack("<I3", n)
|
return strpack("<I3", n)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function _set_byte4(n)
|
local function _set_byte4(n)
|
||||||
return strpack("<I4", n)
|
return strpack("<I4", n)
|
||||||
end
|
end
|
||||||
@@ -107,10 +103,9 @@ 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)
|
||||||
if password == "" then
|
if password == "" then
|
||||||
return ""
|
return ""
|
||||||
@@ -122,13 +117,13 @@ local function _compute_token(password, scramble)
|
|||||||
local stage2 = sha1(stage1)
|
local stage2 = sha1(stage1)
|
||||||
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)
|
||||||
@@ -138,17 +133,12 @@ local function _compose_packet(self, 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
|
||||||
|
|
||||||
|
|
||||||
local len, pos = _get_byte3(data, 1)
|
local len, pos = _get_byte3(data, 1)
|
||||||
|
|
||||||
|
|
||||||
if len == 0 then
|
if len == 0 then
|
||||||
return nil, nil, "empty packet"
|
return nil, nil, "empty packet"
|
||||||
end
|
end
|
||||||
@@ -156,12 +146,10 @@ local function _recv_packet(self,sock)
|
|||||||
self.packet_no = strbyte(data, pos)
|
self.packet_no = strbyte(data, pos)
|
||||||
|
|
||||||
data = sock:read(len)
|
data = sock:read(len)
|
||||||
|
|
||||||
if not data then
|
if not data then
|
||||||
return nil, nil, "failed to read packet content: "
|
return nil, nil, "failed to read packet content: "
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local field_count = strbyte(data, 1)
|
local field_count = strbyte(data, 1)
|
||||||
local typ
|
local typ
|
||||||
if field_count == 0x00 then
|
if field_count == 0x00 then
|
||||||
@@ -177,7 +165,6 @@ local function _recv_packet(self,sock)
|
|||||||
return data, typ
|
return data, typ
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function _from_length_coded_bin(data, pos)
|
local function _from_length_coded_bin(data, pos)
|
||||||
local first = strbyte(data, pos)
|
local first = strbyte(data, pos)
|
||||||
|
|
||||||
@@ -233,44 +220,32 @@ local function _from_length_coded_str(data, pos)
|
|||||||
if len == nil then
|
if len == nil then
|
||||||
return nil, pos
|
return nil, pos
|
||||||
end
|
end
|
||||||
|
|
||||||
return sub(data, pos, pos + len - 1), pos + len
|
return sub(data, pos, pos + len - 1), pos + len
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function _parse_ok_packet(packet)
|
local function _parse_ok_packet(packet)
|
||||||
local res = {}
|
local res = {}
|
||||||
local pos
|
local pos
|
||||||
|
|
||||||
res.affected_rows, pos = _from_length_coded_bin(packet, 2)
|
res.affected_rows, pos = _from_length_coded_bin(packet, 2)
|
||||||
|
|
||||||
res.insert_id, pos = _from_length_coded_bin(packet, pos)
|
res.insert_id, pos = _from_length_coded_bin(packet, pos)
|
||||||
|
|
||||||
res.server_status, pos = _get_byte2(packet, pos)
|
res.server_status, pos = _get_byte2(packet, pos)
|
||||||
|
|
||||||
res.warning_count, pos = _get_byte2(packet, pos)
|
res.warning_count, pos = _get_byte2(packet, pos)
|
||||||
|
|
||||||
|
|
||||||
local message = sub(packet, pos)
|
local message = sub(packet, pos)
|
||||||
if message and message ~= "" then
|
if message and message ~= "" then
|
||||||
res.message = message
|
res.message = message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return res
|
return res
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function _parse_eof_packet(packet)
|
local function _parse_eof_packet(packet)
|
||||||
local pos = 2
|
local pos = 2
|
||||||
|
|
||||||
local warning_count, pos = _get_byte2(packet, pos)
|
local warning_count, pos = _get_byte2(packet, pos)
|
||||||
local status_flags = _get_byte2(packet, pos)
|
local status_flags = _get_byte2(packet, pos)
|
||||||
|
|
||||||
return warning_count, status_flags
|
return warning_count, status_flags
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function _parse_err_packet(packet)
|
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)
|
||||||
@@ -281,48 +256,36 @@ local function _parse_err_packet(packet)
|
|||||||
sqlstate = sub(packet, pos, pos + 5 - 1)
|
sqlstate = sub(packet, pos, pos + 5 - 1)
|
||||||
pos = pos + 5
|
pos = pos + 5
|
||||||
end
|
end
|
||||||
|
|
||||||
local message = sub(packet, pos)
|
local message = sub(packet, pos)
|
||||||
return errno, message, sqlstate
|
return errno, message, sqlstate
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function _parse_result_set_header_packet(packet)
|
local function _parse_result_set_header_packet(packet)
|
||||||
local field_count, pos = _from_length_coded_bin(packet, 1)
|
local field_count, pos = _from_length_coded_bin(packet, 1)
|
||||||
return field_count, _from_length_coded_bin(packet, pos)
|
return field_count, _from_length_coded_bin(packet, pos)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function _parse_field_packet(data)
|
local function _parse_field_packet(data)
|
||||||
local col = {}
|
local col = {}
|
||||||
local catalog, db, table, orig_table, orig_name, charsetnr, length
|
local catalog, db, table, orig_table, orig_name, charsetnr, length
|
||||||
local pos
|
local pos
|
||||||
|
|
||||||
catalog, pos = _from_length_coded_str(data, 1)
|
catalog, pos = _from_length_coded_str(data, 1)
|
||||||
|
|
||||||
|
|
||||||
db, pos = _from_length_coded_str(data, pos)
|
db, pos = _from_length_coded_str(data, pos)
|
||||||
table, pos = _from_length_coded_str(data, pos)
|
table, pos = _from_length_coded_str(data, pos)
|
||||||
orig_table, pos = _from_length_coded_str(data, pos)
|
orig_table, pos = _from_length_coded_str(data, pos)
|
||||||
col.name, pos = _from_length_coded_str(data, pos)
|
col.name, pos = _from_length_coded_str(data, pos)
|
||||||
|
|
||||||
orig_name, pos = _from_length_coded_str(data, pos)
|
orig_name, pos = _from_length_coded_str(data, pos)
|
||||||
|
|
||||||
pos = pos + 1 -- ignore the filler
|
pos = pos + 1 -- ignore the filler
|
||||||
|
|
||||||
charsetnr, pos = _get_byte2(data, pos)
|
charsetnr, pos = _get_byte2(data, pos)
|
||||||
|
|
||||||
length, pos = _get_byte4(data, pos)
|
length, pos = _get_byte4(data, pos)
|
||||||
|
|
||||||
col.type = strbyte(data, pos)
|
col.type = strbyte(data, pos)
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
pos = pos + 1
|
pos = pos + 1
|
||||||
|
|
||||||
col.flags, pos = _get_byte2(data, pos)
|
col.flags, pos = _get_byte2(data, pos)
|
||||||
|
|
||||||
col.decimals = strbyte(data, pos)
|
col.decimals = strbyte(data, pos)
|
||||||
pos = pos + 1
|
pos = pos + 1
|
||||||
|
|
||||||
local default = sub(data, pos + 2)
|
local default = sub(data, pos + 2)
|
||||||
if default and default ~= "" then
|
if default and default ~= "" then
|
||||||
col.default = default
|
col.default = default
|
||||||
@@ -332,7 +295,6 @@ local function _parse_field_packet(data)
|
|||||||
return col
|
return col
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function _parse_row_data_packet(data, cols, compact)
|
local function _parse_row_data_packet(data, cols, compact)
|
||||||
local value, col, conv
|
local value, col, conv
|
||||||
local pos = 1
|
local pos = 1
|
||||||
@@ -360,7 +322,6 @@ local function _parse_row_data_packet(data, cols, compact)
|
|||||||
return row
|
return row
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function _recv_field_packet(self, sock)
|
local function _recv_field_packet(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
|
||||||
@@ -401,11 +362,9 @@ 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)
|
||||||
@@ -417,9 +376,7 @@ local function _mysql_login(self,user,password,database,on_connect)
|
|||||||
|
|
||||||
self._server_ver = server_ver
|
self._server_ver = server_ver
|
||||||
|
|
||||||
|
|
||||||
local thread_id, pos = _get_byte4(packet, pos)
|
local thread_id, pos = _get_byte4(packet, pos)
|
||||||
|
|
||||||
local scramble1 = sub(packet, pos, pos + 8 - 1)
|
local scramble1 = sub(packet, pos, pos + 8 - 1)
|
||||||
if not scramble1 then
|
if not scramble1 then
|
||||||
error "1st part of scramble not found"
|
error "1st part of scramble not found"
|
||||||
@@ -429,10 +386,8 @@ local function _mysql_login(self,user,password,database,on_connect)
|
|||||||
|
|
||||||
-- two lower bytes
|
-- two lower bytes
|
||||||
self._server_capabilities, pos = _get_byte2(packet, pos)
|
self._server_capabilities, pos = _get_byte2(packet, pos)
|
||||||
|
|
||||||
self._server_lang = strbyte(packet, pos)
|
self._server_lang = strbyte(packet, pos)
|
||||||
pos = pos + 1
|
pos = pos + 1
|
||||||
|
|
||||||
self._server_status, pos = _get_byte2(packet, pos)
|
self._server_status, pos = _get_byte2(packet, pos)
|
||||||
|
|
||||||
local more_capabilities
|
local more_capabilities
|
||||||
@@ -441,7 +396,6 @@ local function _mysql_login(self,user,password,database,on_connect)
|
|||||||
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
|
||||||
|
|
||||||
local scramble_part2 = sub(packet, pos, pos + len - 1)
|
local scramble_part2 = sub(packet, pos, pos + len - 1)
|
||||||
@@ -449,20 +403,16 @@ 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
|
||||||
@@ -513,32 +463,27 @@ local store_types = {
|
|||||||
|
|
||||||
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
|
||||||
|
|
||||||
@@ -574,7 +519,6 @@ local function read_result(self, sock)
|
|||||||
-- typ == 'DATA'
|
-- typ == 'DATA'
|
||||||
|
|
||||||
local field_count, extra = _parse_result_set_header_packet(packet)
|
local field_count, extra = _parse_result_set_header_packet(packet)
|
||||||
|
|
||||||
local cols = {}
|
local cols = {}
|
||||||
for i = 1, field_count do
|
for i = 1, field_count do
|
||||||
local col, err, errno, sqlstate = _recv_field_packet(self, sock)
|
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
|
return nil, err, errno, sqlstate
|
||||||
--error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate))
|
--error( strformat("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate))
|
||||||
end
|
end
|
||||||
|
|
||||||
cols[i] = col
|
cols[i] = col
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -600,7 +543,6 @@ local function read_result(self, sock)
|
|||||||
-- typ == 'EOF'
|
-- typ == 'EOF'
|
||||||
|
|
||||||
local compact = self.compact
|
local compact = self.compact
|
||||||
|
|
||||||
local rows = {}
|
local rows = {}
|
||||||
local i = 0
|
local i = 0
|
||||||
while true do
|
while true do
|
||||||
@@ -612,11 +554,9 @@ local function read_result(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
|
||||||
|
|
||||||
@@ -667,7 +607,6 @@ local function _query_resp(self)
|
|||||||
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
|
||||||
@@ -677,7 +616,6 @@ function _M.connect(opts)
|
|||||||
self._max_packet_size = max_packet_size
|
self._max_packet_size = max_packet_size
|
||||||
self.compact = opts.compact_arrays
|
self.compact = opts.compact_arrays
|
||||||
|
|
||||||
|
|
||||||
local database = opts.database or ""
|
local database = opts.database or ""
|
||||||
local user = opts.user or ""
|
local user = opts.user or ""
|
||||||
local password = opts.password or ""
|
local password = opts.password or ""
|
||||||
@@ -686,24 +624,20 @@ function _M.connect(opts)
|
|||||||
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
|
||||||
channel:connect(true)
|
channel:connect(true)
|
||||||
|
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function _M.disconnect(self)
|
function _M.disconnect(self)
|
||||||
self.sockchannel:close()
|
self.sockchannel:close()
|
||||||
setmetatable(self, nil)
|
setmetatable(self, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function _M.query(self, query)
|
function _M.query(self, query)
|
||||||
local querypacket = _compose_query(self, query)
|
local querypacket = _compose_query(self, query)
|
||||||
local sockchannel = self.sockchannel
|
local sockchannel = self.sockchannel
|
||||||
@@ -843,14 +777,12 @@ local function _parse_row_data_binary(data, cols, compact)
|
|||||||
local col = cols[i]
|
local col = cols[i]
|
||||||
local typ = col.type
|
local typ = col.type
|
||||||
local name = col.name
|
local name = col.name
|
||||||
|
|
||||||
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
|
||||||
@@ -896,17 +828,15 @@ local function read_execute_result(self,sock)
|
|||||||
local col
|
local col
|
||||||
while true do
|
while true do
|
||||||
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)
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
col = _parse_field_packet(packet)
|
col = _parse_field_packet(packet)
|
||||||
if not col then
|
if not col then
|
||||||
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
|
||||||
|
|
||||||
@@ -920,20 +850,17 @@ local function read_execute_result(self,sock)
|
|||||||
local row
|
local row
|
||||||
while true do
|
while true do
|
||||||
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
|
||||||
|
|
||||||
@@ -1032,24 +959,23 @@ 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)
|
||||||
self.compact = value
|
self.compact = value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return _M
|
return _M
|
||||||
|
|||||||
Reference in New Issue
Block a user