初始化项目
This commit is contained in:
225
trunk/center/common/connection/dal.go
Normal file
225
trunk/center/common/connection/dal.go
Normal file
@@ -0,0 +1,225 @@
|
||||
package connection
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"framework/sqlAsyncMgr"
|
||||
goredis "github.com/go-redis/redis/v8"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"goutil/logUtilPlus"
|
||||
"time"
|
||||
|
||||
// _ "github.com/go-sql-driver/mysql"
|
||||
config "common/configsYaml"
|
||||
"goutil/mysqlUtil"
|
||||
"goutil/redisUtil"
|
||||
)
|
||||
|
||||
var (
|
||||
adminDB *gorm.DB //管理员数据库对象
|
||||
userDB *gorm.DB //用户数据库对象
|
||||
gameModelDB *gorm.DB // 游戏模型数据库对象
|
||||
gameDB *gorm.DB // 游戏数据库
|
||||
gamePlayerDB *gorm.DB // 玩家库
|
||||
redisDB *goredis.Client // 游戏redis连接客户端
|
||||
syncMgr *sqlAsyncMgr.SqlAsyncUtil // 同步管理对象
|
||||
syncFileSize = 1024 * 1024 // 同步文件大小
|
||||
fileMaxSaveTime = 24 * 7 // 保留7天的sql文件
|
||||
)
|
||||
|
||||
var excuteSqlFunc func(*gorm.DB, ITable, string) error
|
||||
var loghandle func(s string, is ...interface{})
|
||||
var defaultType = SyncSql
|
||||
|
||||
type ITable interface {
|
||||
TableName() string
|
||||
}
|
||||
|
||||
type SqlExecuteType int32
|
||||
|
||||
const (
|
||||
SyncSql SqlExecuteType = iota
|
||||
ASyncSql
|
||||
Name = "BaseDal"
|
||||
)
|
||||
|
||||
// init
|
||||
// @description: init
|
||||
// parameter:
|
||||
// return:
|
||||
func init() {
|
||||
|
||||
//管理中心数据库配置
|
||||
adminDB = initMysql(config.GetDbConfig().GetAdminConfig())
|
||||
userDB = initMysql(config.GetDbConfig().GetUserConfig())
|
||||
|
||||
// 初始化游戏模型数据库
|
||||
gameModelDBConfig := config.GetDbConfig().GetGameModelConfig()
|
||||
gameModelDB = initMysql(gameModelDBConfig)
|
||||
|
||||
// 初始化crossSever 游戏数据库
|
||||
gameDBConfig := config.GetDbConfig().GetGameConfig()
|
||||
gameDB = initMysql(gameDBConfig)
|
||||
gamePlayerDB = initMysql(config.GetDbConfig().GetPlayerConfig())
|
||||
|
||||
// 初始化redis-client
|
||||
excuteSqlFunc = func(gdb *gorm.DB, table ITable, sql string) error {
|
||||
db := gdb.Exec(sql)
|
||||
if db.Error != nil {
|
||||
loghandle("sql执行错误:%s;错误信息:%v", sql, db.Error.Error())
|
||||
return db.Error
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
redisDB = initRedis(config.GetDbConfig().GetRedisConfig())
|
||||
}
|
||||
|
||||
// 初始化Mysql
|
||||
// initMysql
|
||||
// @description: 初始化Mysql
|
||||
// parameter:
|
||||
// @dbConfig: dbConfig
|
||||
// return:
|
||||
// @*gorm.DB: DB
|
||||
func initMysql(dbConfig *mysqlUtil.DBConfig) *gorm.DB {
|
||||
|
||||
//不存在配置
|
||||
if dbConfig == nil || dbConfig.ConnectionString == "" {
|
||||
return nil
|
||||
}
|
||||
logUtilPlus.WarnLog("开始连接mysql:%s", dbConfig.ConnectionString)
|
||||
dbObj, err := gorm.Open(mysql.Open(dbConfig.ConnectionString), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("初始化数据库:%s失败,错误信息为:%s", dbConfig.ConnectionString, err))
|
||||
}
|
||||
logUtilPlus.WarnLog("连接mysql:%s成功", dbConfig.ConnectionString)
|
||||
|
||||
return dbObj
|
||||
}
|
||||
|
||||
// initRedis
|
||||
// @description: 初始化redis客户端
|
||||
// parameter:
|
||||
// @redisConfig: redisConfig
|
||||
// return:
|
||||
// @*goredis.Client: Client
|
||||
func initRedis(redisConfig *redisUtil.RedisConfig) *goredis.Client {
|
||||
|
||||
client := goredis.NewClient(&goredis.Options{
|
||||
Addr: redisConfig.ConnectionString,
|
||||
Password: redisConfig.Password,
|
||||
DB: redisConfig.Database,
|
||||
IdleTimeout: redisConfig.IdleTimeout,
|
||||
DialTimeout: redisConfig.DialConnectTimeout,
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
ping, err := client.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("ping->redis:%s失败,DB:%s,错误信息为:%s", redisConfig.ConnectionString, redisConfig.Database, err))
|
||||
}
|
||||
|
||||
logUtilPlus.WarnLog("ping->redis:%s成功,信息为:%s", redisConfig.ConnectionString, ping)
|
||||
return client
|
||||
}
|
||||
|
||||
// GetAdminDB
|
||||
// @description: 获取admin库db实例
|
||||
// parameter:
|
||||
// return:
|
||||
// @*gorm.DB:admin库db实例
|
||||
func GetAdminDB() *gorm.DB {
|
||||
return adminDB
|
||||
}
|
||||
|
||||
// GetUserDB
|
||||
// @description: 获取user库db实例
|
||||
// parameter:
|
||||
// return:
|
||||
// @*gorm.DB:user库db实例
|
||||
func GetUserDB() *gorm.DB {
|
||||
return userDB
|
||||
}
|
||||
|
||||
// GetGameDB
|
||||
// @description: 获取game库db实例
|
||||
// parameter:
|
||||
// return:
|
||||
// @*gorm.DB:game库db实例
|
||||
func GetGameDB() *gorm.DB {
|
||||
return gameDB
|
||||
}
|
||||
|
||||
// GetPlayerDB
|
||||
// @description: 获取玩家库db实例
|
||||
// parameter:
|
||||
// return:
|
||||
// @*gorm.DB:玩家库db实例
|
||||
func GetPlayerDB() *gorm.DB {
|
||||
return gamePlayerDB
|
||||
}
|
||||
|
||||
// GetGameModelDB
|
||||
// @description: 获取model库db实例
|
||||
// parameter:
|
||||
// return:
|
||||
// @*gorm.DB:model库db实例
|
||||
func GetGameModelDB() *gorm.DB {
|
||||
return gameModelDB
|
||||
}
|
||||
|
||||
// GetRedisClient
|
||||
// @description: 获取redisdb实例
|
||||
// parameter:
|
||||
// return:
|
||||
// @*goredis.Client:redisdb实例
|
||||
func GetRedisClient() *goredis.Client {
|
||||
return redisDB
|
||||
}
|
||||
|
||||
// GetAll
|
||||
// @description: 获取model数据
|
||||
// parameter:
|
||||
// @dataList:数据列表
|
||||
// return:
|
||||
// @error:错误信息
|
||||
func GetAll(dataList interface{}) error {
|
||||
mysqlObj := GetGameModelDB()
|
||||
if mysqlObj = mysqlObj.Find(dataList); mysqlObj.Error != nil {
|
||||
WriteLog("dal.GetAll", mysqlObj.Error)
|
||||
return mysqlObj.Error
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetGameModelData
|
||||
// @description: 获取GameModel数据
|
||||
// parameter:
|
||||
// @moduleName:模块名称
|
||||
// @dataList:数据列表
|
||||
// return:
|
||||
// @error:错误信息
|
||||
func GetGameModelData(moduleName string, dataList interface{}) error {
|
||||
if result := gameModelDB.Find(dataList); result.Error != nil {
|
||||
WriteLog(moduleName+"GetGameModelData", result.Error)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteLog
|
||||
// @description: 记录错误日志
|
||||
// parameter:
|
||||
// @command:命令
|
||||
// @err:错误信息
|
||||
// return:
|
||||
func WriteLog(command string, err error) {
|
||||
logUtilPlus.ErrorLog(fmt.Sprintf("Scan失败,错误信息:%s,command:%s", err, command))
|
||||
}
|
||||
31
trunk/center/common/connection/dal_test.go
Normal file
31
trunk/center/common/connection/dal_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package connection
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type testDal struct{}
|
||||
|
||||
func (t testDal) TableName() string {
|
||||
return "test"
|
||||
}
|
||||
|
||||
var TestDal = testDal{}
|
||||
|
||||
type User struct {
|
||||
ID uint `gorm:"primary_key"`
|
||||
Name string `gorm:"column:name"`
|
||||
Age int `gorm:"column:age"`
|
||||
Birthday time.Time `gorm:"column:birthday"`
|
||||
}
|
||||
|
||||
func TestExecute(t *testing.T) {
|
||||
user := User{Name: "Jinzhu", Age: 18, Birthday: time.Now()}
|
||||
|
||||
result := adminDB.Create(&user) // 通过数据的指针来创建
|
||||
|
||||
_ = user.ID // 返回插入数据的主键
|
||||
_ = result.Error // 返回 error
|
||||
_ = result.RowsAffected // 返回插入记录的条数
|
||||
}
|
||||
41
trunk/center/common/connection/dbHead.go
Normal file
41
trunk/center/common/connection/dbHead.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package connection
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
var (
|
||||
// 存放实体结构
|
||||
dbModelMap = make([]interface{}, 0)
|
||||
|
||||
//当前管理数据库
|
||||
modelDB *gorm.DB
|
||||
)
|
||||
|
||||
// RegisterDBModel 注册数据库模型到全局变量dbModelMap中。
|
||||
// 这个函数接受一个interface{}类型的参数dbModel,表示数据库模型。
|
||||
// 函数的目的是将传入的数据库模型添加到全局变量dbModelMap中,
|
||||
// 以便在其他地方可以访问和使用这些数据库模型。
|
||||
func RegisterDBModel(dbModel interface{}) {
|
||||
// 将dbModel的地址添加到dbModelMap中。
|
||||
// 这里使用地址是因为数据库模型可能比较大,通过引用存储可以提高效率。
|
||||
dbModelMap = append(dbModelMap, &dbModel)
|
||||
}
|
||||
|
||||
// 设置modelDB 类型
|
||||
func SetModelDB(db *gorm.DB) {
|
||||
modelDB = db
|
||||
}
|
||||
|
||||
// BuildDB 用于遍历dbModelMap中的所有数据库模型,并检查每个模型对应的表是否存在于数据库中。
|
||||
// 如果表不存在,则自动迁移(创建)该表。这样可以确保数据库模式与程序中的模型保持同步。
|
||||
func BuildDB() {
|
||||
// 遍历dbModelMap中的每个元素
|
||||
for _, dbModel := range dbModelMap {
|
||||
|
||||
// 检查数据库中是否存在与dbModel对应的表
|
||||
tableExists := modelDB.Migrator().HasTable(dbModel)
|
||||
if !tableExists {
|
||||
// 如果表不存在,则进行自动迁移
|
||||
modelDB.AutoMigrate(dbModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user