diff --git a/abstract/wrapper_client.go b/abstract/wrapper_client.go index b046dab..3c7090a 100644 --- a/abstract/wrapper_client.go +++ b/abstract/wrapper_client.go @@ -8,8 +8,10 @@ package abstract import ( + "context" "git.zhangdeman.cn/zhangdeman/database/define" "go.uber.org/zap" + "gorm.io/gorm" ) // IWrapperClient 包装的客户端 @@ -24,4 +26,10 @@ type IWrapperClient interface { AddWithConfig(flag string, logInstance *zap.Logger, databaseConfig *define.Database, extraFieldList []string) error // BatchAddWithConfigDir 通过具体的配置列表增加数据库实例 BatchAddWithConfigDir(cfgDir string, logInstance *zap.Logger, extraFieldList []string) error + // GetDBClient 基于数据库标识获取数据库实例 + GetDBClient(dbFlag string) (*define.DBClient, error) + // GetMasterClient 获取主库连接 + GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB, error) + // GetSlaveClient 获取从库连接 + GetSlaveClient(ctx context.Context, dbFlag string) (*gorm.DB, error) } diff --git a/define/db_client.go b/define/db_client.go index c9603e0..8039393 100644 --- a/define/db_client.go +++ b/define/db_client.go @@ -9,8 +9,12 @@ package define import ( "context" + "fmt" + "git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/logger/wrapper" "go.uber.org/zap" + "gorm.io/driver/mysql" + "gorm.io/driver/sqlite" "gorm.io/gorm" gormLogger "gorm.io/gorm/logger" ) @@ -29,13 +33,29 @@ type DBClient struct { Cfg Driver // 数据库配置 } +// Init 初始化客户端 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:44 2024/8/20 +func (dc *DBClient) Init(databaseConfig *Database) error { + 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 + } + return nil +} + // SetFlag 设置数据库标识 // // Author : go_developer@163.com<白茶清欢> // // Date : 16:18 2022/6/5 func (dc *DBClient) SetFlag(dbFlag string) { - dc.dbFlag = dbFlag + dc.DbFlag = dbFlag } // GetMaster 获取主库连接 @@ -66,5 +86,55 @@ func (dc *DBClient) GetSlave(ctx context.Context) *gorm.DB { // // Date : 3:45 PM 2021/12/24 func (dc *DBClient) getLogger(ctx context.Context, dbClient *gorm.DB, node string) gormLogger.Interface { - return wrapper.NewGormLoggerWithInstance(ctx, dbClient, dc.loggerInstance, dc.dbFlag+"|"+node, dc.extraFieldList) + return wrapper.NewGormLoggerWithInstance(ctx, dbClient, dc.LoggerInstance, dc.DbFlag+"|"+node, dc.ExtraFieldList) +} + +// GetDatabaseClient 获取数据库连接 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:41 2022/6/11 +func (dc *DBClient) GetDatabaseClient(conf *Driver, logInstance *zap.Logger) (*gorm.DB, error) { + var ( + instance *gorm.DB + err error + ) + + 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 +func (dc *DBClient) buildConnectionDSN(conf *Driver) string { + 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, + ) } diff --git a/client.go b/wrapper_client.go similarity index 62% rename from client.go rename to wrapper_client.go index b5de29c..a199469 100644 --- a/client.go +++ b/wrapper_client.go @@ -12,33 +12,35 @@ import ( "errors" "fmt" "git.zhangdeman.cn/zhangdeman/consts" + "git.zhangdeman.cn/zhangdeman/database/abstract" "git.zhangdeman.cn/zhangdeman/database/define" "git.zhangdeman.cn/zhangdeman/serialize" "path/filepath" "strings" "sync" - "git.zhangdeman.cn/zhangdeman/logger/wrapper" "go.uber.org/zap" - "gorm.io/driver/mysql" - "gorm.io/driver/sqlite" "gorm.io/gorm" ) var ( - // Client mysql客户端 - Client *client + // WrapperClient 包装后的数据库客户端 + WrapperClient abstract.IWrapperClient ) func init() { - Client = &client{ + WrapperClient = NewWrapperClient() +} + +func NewWrapperClient() *wrapperClient { + return &wrapperClient{ lock: &sync.RWMutex{}, clientTable: make(map[string]*define.DBClient), } } -type client struct { +type wrapperClient struct { lock *sync.RWMutex clientTable map[string]*define.DBClient logger *zap.Logger @@ -49,7 +51,7 @@ type client struct { // Author : go_developer@163.com<白茶清欢> // // Date : 19:19 2022/6/5 -func (c *client) AddWithConfigFile(cfgFilePath string, logInstance *zap.Logger, extraFieldList []string) error { +func (c *wrapperClient) AddWithConfigFile(cfgFilePath string, logInstance *zap.Logger, extraFieldList []string) error { var ( err error cfg *define.CfgFile @@ -70,24 +72,19 @@ func (c *client) AddWithConfigFile(cfgFilePath string, logInstance *zap.Logger, // Author : go_developer@163.com<白茶清欢> // // Date : 20:41 2023/4/18 -func (c *client) 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{ DbFlag: flag, LoggerInstance: logInstance, - master: nil, - slave: nil, ExtraFieldList: extraFieldList, Cfg: define.Driver{}, } - var err error - if dbClient.master, err = c.GetDatabaseClient(databaseConfig.Master, logInstance); nil != err { - return err - } - if dbClient.slave, err = c.GetDatabaseClient(databaseConfig.Slave, logInstance); nil != err { + + if err := dbClient.Init(databaseConfig); nil != err { return err } c.lock.Lock() - c.clientTable[dbClient.dbFlag] = dbClient + c.clientTable[dbClient.DbFlag] = dbClient c.lock.Unlock() return nil } @@ -97,7 +94,7 @@ func (c *client) AddWithConfig(flag string, logInstance *zap.Logger, databaseCon // Author : go_developer@163.com<白茶清欢> // // Date : 19:19 2022/6/5 -func (c *client) BatchAddWithConfigDir(cfgDir string, logInstance *zap.Logger, extraFieldList []string) error { +func (c *wrapperClient) BatchAddWithConfigDir(cfgDir string, logInstance *zap.Logger, extraFieldList []string) error { filepathNames, _ := filepath.Glob(filepath.Join(cfgDir, "*")) for i := range filepathNames { if err := c.AddWithConfigFile(filepathNames[i], logInstance, extraFieldList); nil != err { @@ -112,7 +109,7 @@ func (c *client) BatchAddWithConfigDir(cfgDir string, logInstance *zap.Logger, e // Author : go_developer@163.com<白茶清欢> // // Date : 18:05 2022/6/11 -func (c *client) getCfg(cfgPath string) (*define.CfgFile, error) { +func (c *wrapperClient) getCfg(cfgPath string) (*define.CfgFile, error) { fileArr := strings.Split(cfgPath, ".") if len(fileArr) < 2 { // 获取不到类型 @@ -161,7 +158,7 @@ func (c *client) getCfg(cfgPath string) (*define.CfgFile, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 19:32 2022/6/5 -func (c *client) GetDBClient(dbFlag string) (*define.DBClient, error) { +func (c *wrapperClient) GetDBClient(dbFlag string) (*define.DBClient, error) { c.lock.RLock() defer c.lock.RUnlock() var ( @@ -179,7 +176,7 @@ func (c *client) GetDBClient(dbFlag string) (*define.DBClient, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 19:36 2022/6/5 -func (c *client) GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB, error) { +func (c *wrapperClient) GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB, error) { var ( err error dbClient *define.DBClient @@ -195,7 +192,7 @@ func (c *client) GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB, // Author : go_developer@163.com<白茶清欢> // // Date : 19:37 2022/6/5 -func (c *client) GetSlaveClient(ctx context.Context, dbFlag string) (*gorm.DB, error) { +func (c *wrapperClient) GetSlaveClient(ctx context.Context, dbFlag string) (*gorm.DB, error) { var ( err error dbClient *define.DBClient @@ -212,56 +209,6 @@ func (c *client) GetSlaveClient(ctx context.Context, dbFlag string) (*gorm.DB, e // Author : go_developer@163.com<白茶清欢> // // Date : 11:24 2022/6/6 -func (c *client) getGormClient() (*gorm.DB, error) { +func (c *wrapperClient) getGormClient() (*gorm.DB, error) { return nil, nil } - -// GetDatabaseClient 获取数据库连接 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:41 2022/6/11 -func (c *client) GetDatabaseClient(conf *define.Driver, logInstance *zap.Logger) (*gorm.DB, error) { - var ( - instance *gorm.DB - err error - ) - - if conf.DBType == consts.DatabaseDriverMysql { - if instance, err = gorm.Open(mysql.Open(c.buildConnectionDSN(conf)), &gorm.Config{}); nil != err { - return nil, err - } - } else if conf.DBType == consts.DatabaseDriverSqlite3 { - if instance, err = gorm.Open(sqlite.Open(c.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 -func (c *client) buildConnectionDSN(conf *define.Driver) string { - 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, - ) -}