114 lines
3.6 KiB
Lua
114 lines
3.6 KiB
Lua
|
|
local skynet = require "skynet"
|
||
|
|
local mysql = require "skynet.db.mysql"
|
||
|
|
local oo = require "Class"
|
||
|
|
local log = require "Log"
|
||
|
|
local DB = oo.class()
|
||
|
|
|
||
|
|
--DB.MaxPlayerCount = 2
|
||
|
|
DB.database = {}
|
||
|
|
DB.playerDatabase = {}
|
||
|
|
|
||
|
|
|
||
|
|
DB.CheckDBExistSql = "show databases like '%s'"
|
||
|
|
DB.CreateDBSql = "create database if not exists `%s` default character set utf8mb4 default collate utf8mb4_general_ci" --创建数据库
|
||
|
|
DB.UseDBSql = "use %s" --选择数据库
|
||
|
|
DB.CreateTableUserSql = "create table if not exists `User` ( `id` int(10) not null auto_increment ,primary key (`id`) using btree) engine = innodb auto_increment = 100000 character set = utf8mb4 collate = utf8mb4_general_ci row_format = dynamic"
|
||
|
|
DB.DescTableSql = "desc %s" --查看表结构
|
||
|
|
DB.AddFieldsSql = "alter table %s add %s %s %s comment '%s'"
|
||
|
|
DB.GameName = "home"
|
||
|
|
|
||
|
|
function DB:Connect( host , port , user , password , dbName )
|
||
|
|
local function on_connect(db)
|
||
|
|
if self.database[ dbName ] then
|
||
|
|
self:InitDB( dbName )
|
||
|
|
log.info(string.format("数据库 %s 重新连接成功 主机 %s 端口 %s", dbName , host ,port ))
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
--连接数据库
|
||
|
|
dbName = self.GameName.."_"..dbName
|
||
|
|
self.database[ dbName ] = mysql.connect({ host=host, port=port, user=user,password=password,charset="utf8mb4",max_packet_size = 1024 * 1024, on_connect = on_connect})
|
||
|
|
|
||
|
|
if self.database[ dbName ] then
|
||
|
|
self:InitDB( dbName )
|
||
|
|
log.info(string.format("数据库 %s 启动成功 主机 %s 端口 %s", dbName , host ,port ))
|
||
|
|
return true
|
||
|
|
else
|
||
|
|
log.info(string.format("数据库 %s 启动成功 主机 %s 端口 %s", dbName , host ,port ))
|
||
|
|
return false
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
--初始化数据库
|
||
|
|
function DB:InitDB( dbName )
|
||
|
|
local sql = nil
|
||
|
|
local isExistDB = nil
|
||
|
|
isExistDB = self:CheckDBExist( dbName )
|
||
|
|
if not isExistDB then
|
||
|
|
self:CreateDB( dbName )
|
||
|
|
end
|
||
|
|
|
||
|
|
--选择数据库
|
||
|
|
sql = string.format( self.UseDBSql , dbName )
|
||
|
|
self:InitQuery( dbName, sql)
|
||
|
|
end
|
||
|
|
|
||
|
|
--初始化执行SQL语句
|
||
|
|
function DB:InitQuery( dbName, sql , isLog )
|
||
|
|
local res = self.database[ dbName ]:query(sql)
|
||
|
|
if res and res.errno and false ~= isLog then
|
||
|
|
log.info("DB执行语句 InitQuery", sql)
|
||
|
|
log.info("DB执行异常 InitQuery", skynet.server.common:TableToString(res))
|
||
|
|
end
|
||
|
|
return res
|
||
|
|
end
|
||
|
|
|
||
|
|
--执行SQL语句
|
||
|
|
function DB:Query( dbName, sql , isLog )
|
||
|
|
dbName = self.GameName.."_"..dbName
|
||
|
|
local res = self.database[ dbName ]:query(sql)
|
||
|
|
if res and res.errno and false ~= isLog then
|
||
|
|
log.info("DB执行语句 Query", sql)
|
||
|
|
log.info("DB执行异常 Query", skynet.server.common:TableToString(res))
|
||
|
|
end
|
||
|
|
return res
|
||
|
|
end
|
||
|
|
|
||
|
|
--玩家表查询语句
|
||
|
|
function DB:QueryPlayer( dbIndex , sql , isLog )
|
||
|
|
if not dbIndex then
|
||
|
|
return
|
||
|
|
end
|
||
|
|
|
||
|
|
local dbName = self.GameName.."_".."player_"..dbIndex
|
||
|
|
local res = self.database[ dbName ]:query(sql)
|
||
|
|
if res and res.errno and false ~= isLog then
|
||
|
|
log.info("DB执行语句 QueryPlayer", sql , dbIndex)
|
||
|
|
log.info("DB执行异常 QueryPlayer", skynet.server.common:TableToString(res))
|
||
|
|
end
|
||
|
|
return res
|
||
|
|
end
|
||
|
|
|
||
|
|
--检查数据库是否存在
|
||
|
|
function DB:CheckDBExist( dbName )
|
||
|
|
local sql = string.format( self.CheckDBExistSql , dbName )
|
||
|
|
local queryData = self:InitQuery( dbName , sql )
|
||
|
|
if #queryData > 0 then
|
||
|
|
return true
|
||
|
|
end
|
||
|
|
return false
|
||
|
|
end
|
||
|
|
|
||
|
|
--创建数据库
|
||
|
|
function DB:CreateDB( dbName )
|
||
|
|
local sql = string.format( self.CreateDBSql , dbName )
|
||
|
|
local queryData = self:InitQuery(dbName , sql )
|
||
|
|
if #queryData > 0 then
|
||
|
|
return true
|
||
|
|
end
|
||
|
|
return false
|
||
|
|
end
|
||
|
|
|
||
|
|
skynet.server.db = DB
|
||
|
|
return DB
|