add a callback for mysql auth

This commit is contained in:
fztcjjl
2015-10-24 01:35:16 +08:00
parent 13d920e185
commit fe3954fb04
2 changed files with 10 additions and 9 deletions

13
lualib/mysql.lua Executable file → Normal file
View File

@@ -418,7 +418,7 @@ local function _recv_auth_resp(self)
end
local function _mysql_login(self,user,password,database)
local function _mysql_login(self,user,password,database,cb)
return function(sockchannel)
local packet, typ, err = sockchannel:response( _recv_decode_packet_resp(self) )
@@ -490,7 +490,10 @@ local function _mysql_login(self,user,password,database)
local packet_len = #req
local authpacket=_compose_packet(self,req,packet_len)
return sockchannel:request(authpacket,_recv_auth_resp(self))
sockchannel:request(authpacket,_recv_auth_resp(self))
if cb then
cb(self)
end
end
end
@@ -626,7 +629,7 @@ local function _query_resp(self)
end
end
function _M.connect( opts)
function _M.connect(opts, cb)
local self = setmetatable( {}, mt)
@@ -645,11 +648,11 @@ function _M.connect( opts)
local channel = socketchannel.channel {
host = opts.host,
port = opts.port or 3306,
auth = _mysql_login(self,user,password,database ),
auth = _mysql_login(self,user,password,database,cb),
}
self.sockchannel = channel
-- try connect first only once
channel:connect(true)
self.sockchannel = channel
return self

View File

@@ -70,21 +70,19 @@ local function test3( db)
end
skynet.start(function()
local db=mysql.connect{
local db=mysql.connect({
host="127.0.0.1",
port=3306,
database="skynet",
user="root",
password="1",
max_packet_size = 1024 * 1024
}
}, function(db) db:query("set charset utf8") end)
if not db then
print("failed to connect")
end
print("testmysql success to connect to mysql server")
db:query("set names utf8")
local res = db:query("drop table if exists cats")
res = db:query("create table cats "
.."(id serial primary key, ".. "name varchar(5))")