From 1653b3213a8faae7df79438f4c79e4c2157b3352 Mon Sep 17 00:00:00 2001 From: fztcjjl Date: Sat, 24 Oct 2015 06:37:25 +0800 Subject: [PATCH] add a callback for mysql auth --- lualib/mysql.lua | 10 +++++----- test/testmysql.lua | 8 ++++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lualib/mysql.lua b/lualib/mysql.lua index 331c7942..c3573ae2 100644 --- a/lualib/mysql.lua +++ b/lualib/mysql.lua @@ -418,7 +418,7 @@ local function _recv_auth_resp(self) end -local function _mysql_login(self,user,password,database,cb) +local function _mysql_login(self,user,password,database,on_connect) return function(sockchannel) local packet, typ, err = sockchannel:response( _recv_decode_packet_resp(self) ) @@ -491,8 +491,8 @@ local function _mysql_login(self,user,password,database,cb) local authpacket=_compose_packet(self,req,packet_len) sockchannel:request(authpacket,_recv_auth_resp(self)) - if cb then - cb(self) + if on_connect then + on_connect(self) end end end @@ -629,7 +629,7 @@ local function _query_resp(self) end end -function _M.connect(opts, cb) +function _M.connect(opts) local self = setmetatable( {}, mt) @@ -648,7 +648,7 @@ function _M.connect(opts, cb) local channel = socketchannel.channel { host = opts.host, port = opts.port or 3306, - auth = _mysql_login(self,user,password,database,cb), + auth = _mysql_login(self,user,password,database,opts.on_connect), } self.sockchannel = channel -- try connect first only once diff --git a/test/testmysql.lua b/test/testmysql.lua index 8679e7bc..e8b420aa 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -70,14 +70,18 @@ local function test3( db) end skynet.start(function() + local function on_connect(db) + db:query("set charset utf8"); + end 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) + max_packet_size = 1024 * 1024, + on_connect = on_connect + }) if not db then print("failed to connect") end