db_client 面向接口实现

This commit is contained in:
白茶清欢 2024-08-20 18:14:57 +08:00
parent 5e87706d0a
commit 4ba14dc23d
4 changed files with 55 additions and 28 deletions

View File

@ -14,7 +14,7 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
// IWrapperClient 包装的客户端 // IWrapperClient 包装的客户端, 包装 IWrapperDatabaseClient
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
@ -27,7 +27,7 @@ type IWrapperClient interface {
// BatchAddWithConfigDir 通过具体的配置列表增加数据库实例 // BatchAddWithConfigDir 通过具体的配置列表增加数据库实例
BatchAddWithConfigDir(cfgDir string, logInstance *zap.Logger, extraFieldList []string) error BatchAddWithConfigDir(cfgDir string, logInstance *zap.Logger, extraFieldList []string) error
// GetDBClient 基于数据库标识获取数据库实例 // GetDBClient 基于数据库标识获取数据库实例
GetDBClient(dbFlag string) (*define.DBClient, error) GetDBClient(dbFlag string) (IWrapperDatabaseClient, error)
// GetMasterClient 获取主库连接 // GetMasterClient 获取主库连接
GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB, error) GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB, error)
// GetSlaveClient 获取从库连接 // GetSlaveClient 获取从库连接

View File

@ -0,0 +1,31 @@
// Package abstract ...
//
// Description : abstract ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-08-20 17:59
package abstract
import (
"context"
"git.zhangdeman.cn/zhangdeman/database/define"
"go.uber.org/zap"
"gorm.io/gorm"
)
// IWrapperDatabaseClient 包装gorm
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:00 2024/8/20
type IWrapperDatabaseClient interface {
// Init 初始化客户端连接
Init(databaseConfig *define.Database) error
// GetMaster 获取master连接
GetMaster(ctx context.Context) *gorm.DB
// GetSlave 获取slave连接
GetSlave(ctx context.Context) *gorm.DB
// GetDatabaseClient 获取数据库连接
GetDatabaseClient(conf *define.Driver, logInstance *zap.Logger) (*gorm.DB, error)
}

View File

@ -36,13 +36,13 @@ func init() {
func NewWrapperClient() *wrapperClient { func NewWrapperClient() *wrapperClient {
return &wrapperClient{ return &wrapperClient{
lock: &sync.RWMutex{}, lock: &sync.RWMutex{},
clientTable: make(map[string]*define.DBClient), clientTable: make(map[string]abstract.IWrapperDatabaseClient),
} }
} }
type wrapperClient struct { type wrapperClient struct {
lock *sync.RWMutex lock *sync.RWMutex
clientTable map[string]*define.DBClient clientTable map[string]abstract.IWrapperDatabaseClient
logger *zap.Logger logger *zap.Logger
} }
@ -73,7 +73,7 @@ func (c *wrapperClient) AddWithConfigFile(cfgFilePath string, logInstance *zap.L
// //
// Date : 20:41 2023/4/18 // Date : 20:41 2023/4/18
func (c *wrapperClient) AddWithConfig(flag string, logInstance *zap.Logger, databaseConfig *define.Database, extraFieldList []string) error { func (c *wrapperClient) AddWithConfig(flag string, logInstance *zap.Logger, databaseConfig *define.Database, extraFieldList []string) error {
dbClient := &define.DBClient{ dbClient := &DBClient{
DbFlag: flag, DbFlag: flag,
LoggerInstance: logInstance, LoggerInstance: logInstance,
ExtraFieldList: extraFieldList, ExtraFieldList: extraFieldList,
@ -158,12 +158,12 @@ func (c *wrapperClient) getCfg(cfgPath string) (*define.CfgFile, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 19:32 2022/6/5 // Date : 19:32 2022/6/5
func (c *wrapperClient) GetDBClient(dbFlag string) (*define.DBClient, error) { func (c *wrapperClient) GetDBClient(dbFlag string) (abstract.IWrapperDatabaseClient, error) {
c.lock.RLock() c.lock.RLock()
defer c.lock.RUnlock() defer c.lock.RUnlock()
var ( var (
exist bool exist bool
dbClient *define.DBClient dbClient abstract.IWrapperDatabaseClient
) )
if dbClient, exist = c.clientTable[dbFlag]; !exist { if dbClient, exist = c.clientTable[dbFlag]; !exist {
return nil, fmt.Errorf("%s 标识的数据库实例不存在! ", dbFlag) return nil, fmt.Errorf("%s 标识的数据库实例不存在! ", dbFlag)
@ -179,7 +179,7 @@ func (c *wrapperClient) GetDBClient(dbFlag string) (*define.DBClient, error) {
func (c *wrapperClient) GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB, error) { func (c *wrapperClient) GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB, error) {
var ( var (
err error err error
dbClient *define.DBClient dbClient abstract.IWrapperDatabaseClient
) )
if dbClient, err = c.GetDBClient(dbFlag); nil != err { if dbClient, err = c.GetDBClient(dbFlag); nil != err {
return nil, err return nil, err
@ -195,7 +195,7 @@ func (c *wrapperClient) GetMasterClient(ctx context.Context, dbFlag string) (*go
func (c *wrapperClient) GetSlaveClient(ctx context.Context, dbFlag string) (*gorm.DB, error) { func (c *wrapperClient) GetSlaveClient(ctx context.Context, dbFlag string) (*gorm.DB, error) {
var ( var (
err error err error
dbClient *define.DBClient dbClient abstract.IWrapperDatabaseClient
) )
if dbClient, err = c.GetDBClient(dbFlag); nil != err { if dbClient, err = c.GetDBClient(dbFlag); nil != err {
return nil, err return nil, err

View File

@ -5,12 +5,13 @@
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 2024-08-20 17:36 // Date : 2024-08-20 17:36
package define package database
import ( import (
"context" "context"
"fmt" "fmt"
"git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/database/define"
"git.zhangdeman.cn/zhangdeman/logger/wrapper" "git.zhangdeman.cn/zhangdeman/logger/wrapper"
"go.uber.org/zap" "go.uber.org/zap"
"gorm.io/driver/mysql" "gorm.io/driver/mysql"
@ -25,12 +26,12 @@ import (
// //
// Date : 3:09 PM 2021/12/24 // Date : 3:09 PM 2021/12/24
type DBClient struct { type DBClient struct {
DbFlag string // 数据库标识 DbFlag string // 数据库标识
LoggerInstance *zap.Logger // 日志实例 LoggerInstance *zap.Logger // 日志实例
master *gorm.DB // 主库 master *gorm.DB // 主库
slave *gorm.DB // 从库 slave *gorm.DB // 从库
ExtraFieldList []string // 提取的字段 ExtraFieldList []string // 提取的字段
Cfg Driver // 数据库配置 Cfg define.Driver // 数据库配置
} }
// Init 初始化客户端 // Init 初始化客户端
@ -38,7 +39,7 @@ type DBClient struct {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:44 2024/8/20 // Date : 17:44 2024/8/20
func (dc *DBClient) Init(databaseConfig *Database) error { func (dc *DBClient) Init(databaseConfig *define.Database) error {
var err error var err error
if dc.master, err = dc.GetDatabaseClient(databaseConfig.Master, dc.LoggerInstance); nil != err { if dc.master, err = dc.GetDatabaseClient(databaseConfig.Master, dc.LoggerInstance); nil != err {
return err return err
@ -49,15 +50,6 @@ func (dc *DBClient) Init(databaseConfig *Database) error {
return nil return nil
} }
// SetFlag 设置数据库标识
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:18 2022/6/5
func (dc *DBClient) SetFlag(dbFlag string) {
dc.DbFlag = dbFlag
}
// GetMaster 获取主库连接 // GetMaster 获取主库连接
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
@ -94,12 +86,16 @@ func (dc *DBClient) getLogger(ctx context.Context, dbClient *gorm.DB, node strin
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:41 2022/6/11 // Date : 18:41 2022/6/11
func (dc *DBClient) GetDatabaseClient(conf *Driver, logInstance *zap.Logger) (*gorm.DB, error) { func (dc *DBClient) GetDatabaseClient(conf *define.Driver, logInstance *zap.Logger) (*gorm.DB, error) {
var ( var (
instance *gorm.DB instance *gorm.DB
err error err error
) )
if nil == logInstance {
logInstance = dc.LoggerInstance
}
if conf.DBType == consts.DatabaseDriverMysql { if conf.DBType == consts.DatabaseDriverMysql {
if instance, err = gorm.Open(mysql.Open(dc.buildConnectionDSN(conf)), &gorm.Config{}); nil != err { if instance, err = gorm.Open(mysql.Open(dc.buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
return nil, err return nil, err
@ -122,7 +118,7 @@ func (dc *DBClient) GetDatabaseClient(conf *Driver, logInstance *zap.Logger) (*g
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:42 2022/6/11 // Date : 18:42 2022/6/11
func (dc *DBClient) buildConnectionDSN(conf *Driver) string { func (dc *DBClient) buildConnectionDSN(conf *define.Driver) string {
if conf.DBType == consts.DatabaseDriverSqlite3 { if conf.DBType == consts.DatabaseDriverSqlite3 {
// 兼容sqlite3 // 兼容sqlite3
return conf.Host return conf.Host