Skip to content

Commit

Permalink
分离出mysqli
Browse files Browse the repository at this point in the history
  • Loading branch information
huahua132 committed Oct 10, 2024
1 parent 3dd2edf commit 89bbcd8
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 82 deletions.
68 changes: 1 addition & 67 deletions lualib/skynet-fly/db/mysqlf.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
local skynet = require "skynet"
local contriner_client = require "skynet-fly.client.contriner_client"
local mysql = require "skynet.db.mysql"
local log = require "skynet-fly.log"
local timer = require "skynet-fly.timer"

local assert = assert
local setmetatable = setmetatable
local pcall = pcall
local next = next

contriner_client:register("mysql_m", "share_config_m")
contriner_client:register("mysql_m")

local g_instance = nil
local g_instance_map = {}

local M = {}
local mt = {__index = M}

---------------------------------mysql_m--------------------------------------------

function M:new(db_name)
local client = contriner_client:new("mysql_m",db_name)
local t = {
Expand Down Expand Up @@ -59,66 +55,4 @@ function M:max_packet_size()
end
end

---------------------------------mysql_m--------------------------------------------

---------------------------------本服直连模式----------------------------------------
local function keep_alive(week_t)
local t = next(week_t)
if not t then return end
local conn = t.conn
if conn then
local ok,ret = pcall(conn.ping, conn)
if not ok then
log.error("keep_alive err ", ret)
end
else
log.error("keep_alive not conn ", conn)
end
end

local g_lmt = {__gc = function(t)
if t.keep_time then
t.keep_time:cancel()
end
end}

local week_mt = {__mode = "kv"}

function M.l_new_client(db_name)
local cli = contriner_client:new('share_config_m')
local conf_map = cli:mod_call('query','mysql')
assert(conf_map and conf_map[db_name],"not mysql conf")

local conf = conf_map[db_name]
local database = conf.database
conf.database = nil
local conn = mysql.connect(conf)
conn:query('CREATE DATABASE IF NOT EXISTS ' .. database .. ';')
conn:disconnect()
conf.database = database
conn = mysql.connect(conf)

local t = {
conf = conf,
conn = conn,
}

local week_t = setmetatable({}, week_mt)
week_t[t] = true

t.keep_time = timer:new(timer.second * 10,timer.loop, keep_alive, week_t)
t.keep_time:after_next()

setmetatable(t, g_lmt)
return t
end

function M.l_instance(db_name)
if not g_instance_map[db_name] then
g_instance_map[db_name] = M.l_new_client(db_name)
end

return g_instance_map[db_name]
end

return M
77 changes: 77 additions & 0 deletions lualib/skynet-fly/db/mysqli.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
local skynet = require "skynet"
local mysql = require "skynet.db.mysql"
local log = require "skynet-fly.log"
local timer = require "skynet-fly.timer"
local contriner_client = require "skynet-fly.client.contriner_client"

contriner_client:register("share_config_m")

local assert = assert
local setmetatable = setmetatable
local pcall = pcall
local next = next

local g_instance_map = {}

local M = {}

local function keep_alive(week_t)
local t = next(week_t)
if not t then return end
local conn = t.conn
if conn then
local ok,ret = pcall(conn.ping, conn)
if not ok then
log.error("keep_alive err ", ret)
end
else
log.error("keep_alive not conn ", conn)
end
end

local g_lmt = {__gc = function(t)
if t.keep_time then
t.keep_time:cancel()
end
end}

local week_mt = {__mode = "kv"}

function M.new_client(db_name)
local cli = contriner_client:new('share_config_m')
local conf_map = cli:mod_call('query','mysql')
assert(conf_map and conf_map[db_name],"not mysql conf")

local conf = conf_map[db_name]
local database = conf.database
conf.database = nil
local conn = mysql.connect(conf)
conn:query('CREATE DATABASE IF NOT EXISTS ' .. database .. ';')
conn:disconnect()
conf.database = database
conn = mysql.connect(conf)

local t = {
conf = conf,
conn = conn,
}

local week_t = setmetatable({}, week_mt)
week_t[t] = true

t.keep_time = timer:new(timer.second * 10,timer.loop, keep_alive, week_t)
t.keep_time:after_next()

setmetatable(t, g_lmt)
return t
end

function M.instance(db_name)
if not g_instance_map[db_name] then
g_instance_map[db_name] = M.new_client(db_name)
end

return g_instance_map[db_name]
end

return M
14 changes: 2 additions & 12 deletions lualib/skynet-fly/db/ormadapter/ormadapter_mysql.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local contriner_client = require "skynet-fly.client.contriner_client"
local table_util = require "skynet-fly.utils.table_util"
local string_util = require "skynet-fly.utils.string_util"
local mysqlf = require "skynet-fly.db.mysqlf"
local mysqli = require "skynet-fly.db.mysqli"
local log = require "skynet-fly.log"
local json = require "cjson"

Expand Down Expand Up @@ -48,16 +48,6 @@ local FIELD_TYPE_SQL_TYPE = {
[FIELD_TYPE.table] = "blob",
}

local IS_NUMBER_TYPE = {
[FIELD_TYPE.int8] = true,
[FIELD_TYPE.int16] = true,
[FIELD_TYPE.int32] = true,
[FIELD_TYPE.int64] = true,
[FIELD_TYPE.uint8] = true,
[FIELD_TYPE.uint16] = true,
[FIELD_TYPE.uint32] = true,
}

local FIELD_TYPE_LUA_TYPE = {}

do
Expand Down Expand Up @@ -104,7 +94,7 @@ local mata = {__index = M}
-- 新建适配对象
function M:new(db_name)
local t = {
_db = mysqlf.l_new_client(db_name),
_db = mysqli.new_client(db_name),
_tab_name = nil,
_field_list = nil,
_field_map = nil,
Expand Down
7 changes: 4 additions & 3 deletions test/module/mysql_test_m.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local skynet = require "skynet"
local log = require "skynet-fly.log"
local timer = require "skynet-fly.timer"
local mysqlf = require "skynet-fly.db.mysqlf"
local mysqli = require "skynet-fly.db.mysqli"
local CMD = {}

local function test()
Expand Down Expand Up @@ -43,8 +44,8 @@ local function test()

end

local function test_l_new_client()
local db = mysqlf.l_new_client("game")
local function test_new_client()
local db = mysqli.new_client("game")
db.conn:query("drop table if exists user")

local create_sql = [[
Expand Down Expand Up @@ -75,7 +76,7 @@ end

function CMD.start()
--skynet.fork(test)
skynet.fork(test_l_new_client)
skynet.fork(test_new_client)
return true
end

Expand Down

0 comments on commit 89bbcd8

Please sign in to comment.