mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 04:33:05 +00:00
improve mysql lib
This commit is contained in:
128
lualib/mysql.lua
128
lualib/mysql.lua
@@ -605,46 +605,7 @@ local function _mysql_login(self,user,password,database)
|
|||||||
return sockchannel:request(authpacket,_recv_auth_resp(self))
|
return sockchannel:request(authpacket,_recv_auth_resp(self))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
function _M.connect( opts)
|
|
||||||
|
|
||||||
local self = setmetatable( {}, mt)
|
|
||||||
|
|
||||||
local max_packet_size = opts.max_packet_size
|
|
||||||
if not max_packet_size then
|
|
||||||
max_packet_size = 1024 * 1024 -- default 1 MB
|
|
||||||
end
|
|
||||||
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 ""
|
|
||||||
|
|
||||||
local channel = socketchannel.channel {
|
|
||||||
host = opts.host,
|
|
||||||
port = opts.port or 3306,
|
|
||||||
auth = _mysql_login(self,user,password,database ),
|
|
||||||
}
|
|
||||||
-- try connect first only once
|
|
||||||
channel:connect(true)
|
|
||||||
self.sockchannel = channel
|
|
||||||
|
|
||||||
|
|
||||||
return self
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function _M.close(self)
|
|
||||||
self.sockchannel:close()
|
|
||||||
setmetatable(self, nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function _M.server_ver(self)
|
|
||||||
return self._server_ver
|
|
||||||
end
|
|
||||||
|
|
||||||
local function _compose_query(self, query)
|
local function _compose_query(self, query)
|
||||||
|
|
||||||
@@ -665,14 +626,15 @@ local function read_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
|
||||||
--print("read result", err)
|
--print("read result", err)
|
||||||
error( err )
|
return nil, err
|
||||||
|
--error( 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)
|
||||||
--print("read result ", msg, errno, sqlstate)
|
--print("read result ", msg, errno, sqlstate)
|
||||||
--return nil, msg, errno, sqlstate
|
return nil, msg, errno, sqlstate
|
||||||
error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate))
|
--error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate))
|
||||||
end
|
end
|
||||||
|
|
||||||
if typ == 'OK' then
|
if typ == 'OK' then
|
||||||
@@ -687,7 +649,8 @@ local function read_result(self, sock)
|
|||||||
|
|
||||||
if typ ~= 'DATA' then
|
if typ ~= 'DATA' then
|
||||||
--print("read result", "packet type " ,typ , " not supported")
|
--print("read result", "packet type " ,typ , " not supported")
|
||||||
error( "packet type " .. typ .. " not supported" )
|
return nil, "packet type " .. typ .. " not supported"
|
||||||
|
--error( "packet type " .. typ .. " not supported" )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- typ == 'DATA'
|
-- typ == 'DATA'
|
||||||
@@ -702,8 +665,8 @@ local function read_result(self, sock)
|
|||||||
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)
|
||||||
if not col then
|
if not col then
|
||||||
--return nil, err, errno, sqlstate
|
return nil, err, errno, sqlstate
|
||||||
error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate))
|
--error( string.format("errno:%d, msg:%s,sqlstate:%s",errno,msg,sqlstate))
|
||||||
end
|
end
|
||||||
|
|
||||||
cols[i] = col
|
cols[i] = col
|
||||||
@@ -711,11 +674,13 @@ local function read_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
|
||||||
error( err)
|
--error( 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"
|
||||||
end
|
end
|
||||||
|
|
||||||
-- typ == 'EOF'
|
-- typ == 'EOF'
|
||||||
@@ -729,7 +694,8 @@ local function read_result(self, sock)
|
|||||||
|
|
||||||
packet, typ, err = _recv_packet(self, sock)
|
packet, typ, err = _recv_packet(self, sock)
|
||||||
if not packet then
|
if not packet then
|
||||||
error (err)
|
--error (err)
|
||||||
|
return nil, err
|
||||||
end
|
end
|
||||||
|
|
||||||
if typ == 'EOF' then
|
if typ == 'EOF' then
|
||||||
@@ -761,15 +727,25 @@ end
|
|||||||
local function _query_resp(self)
|
local function _query_resp(self)
|
||||||
return function(sock)
|
return function(sock)
|
||||||
--return true ,read_result(self,sock)
|
--return true ,read_result(self,sock)
|
||||||
local res, more = read_result(self,sock)
|
--local res, more = read_result(self,sock)
|
||||||
if more ~= "again" then
|
local res, err, errno, sqlstate = read_result(self,sock)
|
||||||
|
if not res then
|
||||||
|
local badresult ={}
|
||||||
|
badresult.badresult = true
|
||||||
|
badresult.err = err
|
||||||
|
badresult.errno = errno
|
||||||
|
badresult.sqlstate = sqlstate
|
||||||
|
return true , badresult
|
||||||
|
end
|
||||||
|
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 more =="again" do
|
while err =="again" do
|
||||||
res, more = read_result(self,sock)
|
--res, more = read_result(self,sock)
|
||||||
|
res, err, errno, sqlstate = read_result(self,sock)
|
||||||
if not res then
|
if not res then
|
||||||
return true, mulitresultset
|
return true, mulitresultset
|
||||||
end
|
end
|
||||||
@@ -779,6 +755,44 @@ local function _query_resp(self)
|
|||||||
return true, mulitresultset
|
return true, mulitresultset
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function _M.connect( opts)
|
||||||
|
|
||||||
|
local self = setmetatable( {}, mt)
|
||||||
|
|
||||||
|
local max_packet_size = opts.max_packet_size
|
||||||
|
if not max_packet_size then
|
||||||
|
max_packet_size = 1024 * 1024 -- default 1 MB
|
||||||
|
end
|
||||||
|
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 ""
|
||||||
|
|
||||||
|
local channel = socketchannel.channel {
|
||||||
|
host = opts.host,
|
||||||
|
port = opts.port or 3306,
|
||||||
|
auth = _mysql_login(self,user,password,database ),
|
||||||
|
}
|
||||||
|
-- try connect first only once
|
||||||
|
channel:connect(true)
|
||||||
|
self.sockchannel = channel
|
||||||
|
|
||||||
|
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function _M.disconnect(self)
|
||||||
|
self.sockchannel:close()
|
||||||
|
setmetatable(self, nil)
|
||||||
|
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
|
||||||
@@ -788,9 +802,8 @@ function _M.query(self, query)
|
|||||||
return sockchannel:request( querypacket, self.query_resp )
|
return sockchannel:request( querypacket, self.query_resp )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function _M.server_ver(self)
|
||||||
function _M.set_compact_arrays(self, value)
|
return self._server_ver
|
||||||
self.compact = value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -798,4 +811,9 @@ function _M.quote_sql_str( str)
|
|||||||
return mysqlaux.quote_sql_str(str)
|
return mysqlaux.quote_sql_str(str)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function _M.set_compact_arrays(self, value)
|
||||||
|
self.compact = value
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
return _M
|
return _M
|
||||||
|
|||||||
@@ -78,12 +78,13 @@ skynet.start(function()
|
|||||||
print ( mysql.quote_sql_str([[\mysql escape %string test'test"]]) )
|
print ( mysql.quote_sql_str([[\mysql escape %string test'test"]]) )
|
||||||
|
|
||||||
-- bad sql statement
|
-- bad sql statement
|
||||||
local ok, res = pcall( db.query, db, "select * from notexisttable" )
|
local res = db:query("select * from notexisttable" )
|
||||||
print( "ok= ",ok, dump(res) )
|
print( dump(res) )
|
||||||
|
|
||||||
res = db:query("select * from cats order by id asc")
|
res = db:query("select * from cats order by id asc")
|
||||||
print ( dump( res ) )
|
print ( dump( res ) )
|
||||||
|
|
||||||
|
db:disconnect()
|
||||||
skynet.exit()
|
skynet.exit()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user