database/wrapper_db_client.go

228 lines
6.3 KiB
Go
Raw Normal View History

// Package database ...
//
// Description : define ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-08-20 17:36
2024-08-20 18:14:57 +08:00
package database
import (
"context"
"errors"
2024-08-20 17:56:43 +08:00
"fmt"
"git.zhangdeman.cn/zhangdeman/consts"
2024-08-20 18:14:57 +08:00
"git.zhangdeman.cn/zhangdeman/database/define"
"git.zhangdeman.cn/zhangdeman/logger/wrapper"
"go.uber.org/zap"
2024-08-20 17:56:43 +08:00
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
"sync"
"time"
)
// DBClient 包装日志实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:09 PM 2021/12/24
type DBClient struct {
DbFlag string // 数据库标识
LoggerInstance *zap.Logger // 日志实例
master *gorm.DB // 主库
slave *gorm.DB // 从库
ExtraFieldList []string // 提取的字段
Cfg define.Driver // 数据库配置
cacheTableStructureConfig *define.CacheTableStructureConfig // 缓存配置
lock *sync.RWMutex // 操作锁
tableStructureCache map[string][]*define.ColumnInfo // 表结构缓存
}
2024-08-20 17:56:43 +08:00
// Init 初始化客户端
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:44 2024/8/20
func (dc *DBClient) Init(databaseConfig *define.Database, cacheTableStructureConfig *define.CacheTableStructureConfig) error {
dc.lock = &sync.RWMutex{}
dc.cacheTableStructureConfig = cacheTableStructureConfig
2024-08-20 17:56:43 +08:00
var err error
if dc.master, err = dc.GetDatabaseClient(databaseConfig.Master, dc.LoggerInstance); nil != err {
return err
}
if dc.slave, err = dc.GetDatabaseClient(databaseConfig.Slave, dc.LoggerInstance); nil != err {
return err
}
if err = dc.syncDbTableStructure(false); nil != err { // 同步缓存表结构
return err
}
// 启动异步任务
go func() {
if !dc.CacheDataTableStructureConfig().Enable {
// 未启用
return
}
for {
select {
case <-time.After(time.Second * time.Duration(dc.CacheDataTableStructureConfig().SyncTimeInterval)):
_ = dc.syncDbTableStructure(true)
}
}
}()
2024-08-20 17:56:43 +08:00
return nil
}
// GetMaster 获取主库连接
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:28 PM 2021/12/24
func (dc *DBClient) GetMaster(ctx context.Context) *gorm.DB {
session := dc.master.Session(&gorm.Session{})
session.Logger = dc.getLogger(ctx, session, "master")
return session
}
// GetSlave 获取从库链接
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:29 PM 2021/12/24
func (dc *DBClient) GetSlave(ctx context.Context) *gorm.DB {
session := dc.slave.Session(&gorm.Session{})
session.Logger = dc.getLogger(ctx, session, "slave")
return session
}
// getLogger 获取日志实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:45 PM 2021/12/24
func (dc *DBClient) getLogger(ctx context.Context, dbClient *gorm.DB, node string) gormLogger.Interface {
2024-08-20 17:56:43 +08:00
return wrapper.NewGormLoggerWithInstance(ctx, dbClient, dc.LoggerInstance, dc.DbFlag+"|"+node, dc.ExtraFieldList)
}
// GetDatabaseClient 获取数据库连接
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:41 2022/6/11
2024-08-20 18:14:57 +08:00
func (dc *DBClient) GetDatabaseClient(conf *define.Driver, logInstance *zap.Logger) (*gorm.DB, error) {
2024-08-20 17:56:43 +08:00
var (
instance *gorm.DB
err error
)
2024-08-20 18:14:57 +08:00
if nil == logInstance {
logInstance = dc.LoggerInstance
}
2024-08-20 17:56:43 +08:00
if conf.DBType == consts.DatabaseDriverMysql {
if instance, err = gorm.Open(mysql.Open(dc.buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
return nil, err
}
} else if conf.DBType == consts.DatabaseDriverSqlite3 {
if instance, err = gorm.Open(sqlite.Open(dc.buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
return nil, err
}
} else {
return nil, fmt.Errorf("%v : db driver is not support", conf.DBType)
}
instance.Logger = wrapper.NewGormLoggerWithInstance(nil, instance, logInstance, "", nil)
return instance, nil
}
// buildConnectionDSN 构建连接信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:42 2022/6/11
2024-08-20 18:14:57 +08:00
func (dc *DBClient) buildConnectionDSN(conf *define.Driver) string {
2024-08-20 17:56:43 +08:00
if conf.DBType == consts.DatabaseDriverSqlite3 {
// 兼容sqlite3
return conf.Host
}
return fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=%s",
conf.Username,
conf.Password,
conf.Host,
conf.Port,
conf.Database,
conf.Charset,
conf.Timezone,
)
}
// CacheDataTableStructureConfig ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:03 2024/8/21
func (dc *DBClient) CacheDataTableStructureConfig() *define.CacheTableStructureConfig {
if nil == dc.cacheTableStructureConfig {
return &define.CacheTableStructureConfig{Enable: false, SyncTimeInterval: 3600}
}
if dc.cacheTableStructureConfig.SyncTimeInterval <= 0 {
dc.cacheTableStructureConfig.SyncTimeInterval = 3600
}
return dc.cacheTableStructureConfig
}
// GetTableFieldList 获取表结构配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:07 2024/8/21
func (dc *DBClient) GetTableFieldList(tableName string) ([]*define.ColumnInfo, error) {
if !dc.CacheDataTableStructureConfig().Enable {
// 未启用缓存, 返回空list
return make([]*define.ColumnInfo, 0), nil
}
dc.lock.RLock()
defer dc.lock.RUnlock()
if _, exist := dc.tableStructureCache[tableName]; !exist {
return nil, errors.New(tableName + " : cache result not found")
}
return dc.tableStructureCache[tableName], nil
}
// syncDbTableStructure 缓存表结构
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:17 2024/8/21
func (dc *DBClient) syncDbTableStructure(ignoreErr bool) error {
var (
err error
tableList []string
)
c := dc.GetMaster(context.Background())
systemDao := &SystemDao{}
if tableList, err = systemDao.GetTableList(c); nil != err {
return err
}
tableStructCache := make(map[string][]*define.ColumnInfo)
for _, itemTableName := range tableList {
fieldList, loadTableErr := systemDao.GetTableInfo(c, dc.Cfg.Database, itemTableName)
if nil != loadTableErr {
if ignoreErr {
continue
}
return loadTableErr
}
tableStructCache[itemTableName] = fieldList
}
// 更新缓存结果
dc.lock.Lock()
defer dc.lock.Unlock()
dc.tableStructureCache = tableStructCache
return nil
}