From fe3954fb041c676557acccf9c5148c20d4a771ba Mon Sep 17 00:00:00 2001 From: fztcjjl Date: Sat, 24 Oct 2015 01:35:16 +0800 Subject: [PATCH] add a callback for mysql auth --- lualib/mysql.lua | 13 ++++++++----- test/testmysql.lua | 6 ++---- 2 files changed, 10 insertions(+), 9 deletions(-) mode change 100755 => 100644 lualib/mysql.lua diff --git a/lualib/mysql.lua b/lualib/mysql.lua old mode 100755 new mode 100644 index 305e900b..331c7942 --- 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) +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 diff --git a/test/testmysql.lua b/test/testmysql.lua index 7bd54855..8679e7bc 100644 --- a/test/testmysql.lua +++ b/test/testmysql.lua @@ -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))")