local skynet = require "skynet" local mysql = require "skynet.db.mysql" local oo = require "Class" local log = require "Log" local DB = oo.class() 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 = skynet.server.gameConfig.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:DisConnect( dbName ) if self.database[ dbName ] then self.database[ dbName ]:disconnect() log.info(string.format("数据库 %s 断开连接成功 ", dbName )) 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 ) dbName = skynet.server.gameConfig.GameName.."_"..dbName local res = self.database[ dbName ]:query(sql) if res and res.errno then log.info("DB执行语句 Query1", sql) log.info("DB执行异常 Query2", skynet.server.common:TableToString(res)) end if not res then log.info("DB执行语句 Query3", sql ) end return res end --玩家表查询语句 function DB:QueryPlayer( dbIndex , sql ) function Do() if not dbIndex then log.info("DB执行语句 QueryPlayer1", sql ) return nil end local dbName = skynet.server.gameConfig.GameName.."_".."player_"..dbIndex local res = self.database[ dbName ]:query(sql) return res end local isDo, res = pcall( Do ) if not isDo then local errorText = string.format("QueryPlayer 失败 %s", sql) log.warning( errorText , res) return nil 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