database/wrapper_client.go

206 lines
5.1 KiB
Go
Raw Normal View History

2023-04-18 18:25:39 +08:00
// Package database ...
2022-05-15 11:27:28 +08:00
//
// Description : mysql客户端
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-03-01 9:20 下午
2023-04-18 18:25:39 +08:00
package database
2022-05-15 11:27:28 +08:00
import (
2024-06-24 17:36:32 +08:00
"context"
2022-06-11 18:25:00 +08:00
"errors"
2022-05-15 11:27:28 +08:00
"fmt"
2023-08-16 23:16:35 +08:00
"git.zhangdeman.cn/zhangdeman/consts"
2024-08-20 17:56:43 +08:00
"git.zhangdeman.cn/zhangdeman/database/abstract"
"git.zhangdeman.cn/zhangdeman/database/define"
2023-08-11 14:01:18 +08:00
"git.zhangdeman.cn/zhangdeman/serialize"
2024-08-23 18:17:31 +08:00
"go.uber.org/zap"
2022-06-09 14:58:01 +08:00
"path/filepath"
"strings"
2022-06-05 19:49:45 +08:00
"sync"
2022-06-25 18:46:28 +08:00
2022-05-15 11:27:28 +08:00
"gorm.io/gorm"
)
2022-06-05 19:49:45 +08:00
var (
2024-08-20 17:56:43 +08:00
// WrapperClient 包装后的数据库客户端
WrapperClient abstract.IWrapperClient
2022-06-05 19:49:45 +08:00
)
func init() {
2024-08-20 17:56:43 +08:00
WrapperClient = NewWrapperClient()
}
func NewWrapperClient() *wrapperClient {
return &wrapperClient{
2022-06-05 19:49:45 +08:00
lock: &sync.RWMutex{},
2024-08-20 18:14:57 +08:00
clientTable: make(map[string]abstract.IWrapperDatabaseClient),
2022-06-05 19:49:45 +08:00
}
}
2024-08-20 17:56:43 +08:00
type wrapperClient struct {
2022-06-05 19:49:45 +08:00
lock *sync.RWMutex
2024-08-20 18:14:57 +08:00
clientTable map[string]abstract.IWrapperDatabaseClient
2022-06-12 18:26:19 +08:00
logger *zap.Logger
2022-06-05 19:49:45 +08:00
}
// AddWithConfigFile 使用文件生成新的客户端文件名去掉后缀作为flag
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:19 2022/6/5
2024-08-20 17:56:43 +08:00
func (c *wrapperClient) AddWithConfigFile(cfgFilePath string, logInstance *zap.Logger, extraFieldList []string) error {
2022-06-11 19:05:54 +08:00
var (
err error
cfg *define.CfgFile
2022-06-11 19:05:54 +08:00
)
if cfg, err = c.getCfg(cfgFilePath); nil != err {
return err
}
if nil == cfg {
// 不支持的配置文件格式
return nil
}
2024-06-30 18:55:35 +08:00
return c.AddWithConfig(cfg.Flag, logInstance, cfg.Config, extraFieldList)
}
// AddWithConfig ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:41 2023/4/18
2024-08-20 17:56:43 +08:00
func (c *wrapperClient) AddWithConfig(flag string, logInstance *zap.Logger, databaseConfig *define.Database, extraFieldList []string) error {
2024-08-20 18:14:57 +08:00
dbClient := &DBClient{
DbFlag: flag,
LoggerInstance: logInstance,
ExtraFieldList: extraFieldList,
Cfg: define.Driver{},
2022-06-11 19:05:54 +08:00
}
2024-08-20 17:56:43 +08:00
2024-08-23 17:54:48 +08:00
if err := dbClient.Init(databaseConfig, nil); nil != err {
2022-06-11 19:05:54 +08:00
return err
}
c.lock.Lock()
2024-08-20 17:56:43 +08:00
c.clientTable[dbClient.DbFlag] = dbClient
2022-06-11 19:05:54 +08:00
c.lock.Unlock()
2022-06-05 19:49:45 +08:00
return nil
}
// BatchAddWithConfigDir 自动读取目录下配置文件, 生成客户端
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:19 2022/6/5
2024-08-20 17:56:43 +08:00
func (c *wrapperClient) BatchAddWithConfigDir(cfgDir string, logInstance *zap.Logger, extraFieldList []string) error {
2022-06-11 19:18:42 +08:00
filepathNames, _ := filepath.Glob(filepath.Join(cfgDir, "*"))
for i := range filepathNames {
2024-06-30 18:55:35 +08:00
if err := c.AddWithConfigFile(filepathNames[i], logInstance, extraFieldList); nil != err {
2022-06-11 19:18:42 +08:00
return err
}
}
2022-06-05 19:49:45 +08:00
return nil
}
2022-06-11 18:25:00 +08:00
// getCfg 读取配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:05 2022/6/11
2024-08-20 17:56:43 +08:00
func (c *wrapperClient) getCfg(cfgPath string) (*define.CfgFile, error) {
2022-06-11 18:25:00 +08:00
fileArr := strings.Split(cfgPath, ".")
if len(fileArr) < 2 {
// 获取不到类型
return nil, errors.New("文件格式必须是JSON或者YAML")
}
fileType := strings.ToLower(fileArr[len(fileArr)-1])
2022-06-11 19:05:54 +08:00
fileFlagArr := strings.Split(fileArr[0], string(filepath.Separator))
result := &define.CfgFile{
2022-06-11 19:05:54 +08:00
Path: cfgPath,
Type: "",
Flag: fileFlagArr[len(fileFlagArr)-1],
Config: &define.Database{},
2022-06-11 19:05:54 +08:00
}
2022-06-11 18:25:00 +08:00
var (
err error
cfgInfo define.Database
2022-06-11 18:25:00 +08:00
)
switch fileType {
2023-08-16 23:16:35 +08:00
case consts.FileTypeYaml:
2022-06-11 18:25:00 +08:00
fallthrough
2023-08-16 23:16:35 +08:00
case consts.FileTypeYml:
result.Type = consts.FileTypeYaml
2023-08-11 14:01:18 +08:00
if err = serialize.File.ReadYmlContent(cfgPath, &result.Config); nil != err {
2022-06-11 18:25:00 +08:00
return nil, fmt.Errorf("%s 配置文件解析失败, 原因 : %s", cfgPath, err.Error())
}
2023-08-16 23:16:35 +08:00
case consts.FileTypeJson:
result.Type = consts.FileTypeJson
2023-08-11 14:01:18 +08:00
if err = serialize.File.ReadJSONContent(cfgPath, &cfgInfo); nil != err {
2022-06-11 18:25:00 +08:00
return nil, fmt.Errorf("%s 配置文件解析失败, 原因 : %s", cfgPath, err.Error())
}
default:
// 不是JSON , 也不是YML, 跳过
return nil, nil
}
2022-06-11 19:05:54 +08:00
if len(result.Config.Master.Timezone) == 0 {
// 默认使用本地时区
result.Config.Master.Timezone = "Local"
} else {
result.Config.Slave.Timezone = result.Config.Master.Timezone
}
return result, nil
2022-06-11 18:25:00 +08:00
}
2022-06-05 19:49:45 +08:00
// GetDBClient 获取db client
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:32 2022/6/5
2024-08-20 18:14:57 +08:00
func (c *wrapperClient) GetDBClient(dbFlag string) (abstract.IWrapperDatabaseClient, error) {
2022-06-05 19:49:45 +08:00
c.lock.RLock()
defer c.lock.RUnlock()
var (
exist bool
2024-08-20 18:14:57 +08:00
dbClient abstract.IWrapperDatabaseClient
2022-06-05 19:49:45 +08:00
)
if dbClient, exist = c.clientTable[dbFlag]; !exist {
return nil, fmt.Errorf("%s 标识的数据库实例不存在! ", dbFlag)
}
return dbClient, nil
}
// GetMasterClient 获取主库客户端
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:36 2022/6/5
2024-08-20 17:56:43 +08:00
func (c *wrapperClient) GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB, error) {
2022-06-05 19:49:45 +08:00
var (
err error
2024-08-20 18:14:57 +08:00
dbClient abstract.IWrapperDatabaseClient
2022-06-05 19:49:45 +08:00
)
2022-06-11 18:00:47 +08:00
if dbClient, err = c.GetDBClient(dbFlag); nil != err {
2022-06-05 19:49:45 +08:00
return nil, err
}
2024-08-23 18:17:31 +08:00
return dbClient.GetMaster(ctx), nil
2022-06-05 19:49:45 +08:00
}
// GetSlaveClient 获取从库客户端
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:37 2022/6/5
2024-08-20 17:56:43 +08:00
func (c *wrapperClient) GetSlaveClient(ctx context.Context, dbFlag string) (*gorm.DB, error) {
2022-06-05 19:49:45 +08:00
var (
err error
2024-08-20 18:14:57 +08:00
dbClient abstract.IWrapperDatabaseClient
2022-06-05 19:49:45 +08:00
)
2022-06-11 18:00:47 +08:00
if dbClient, err = c.GetDBClient(dbFlag); nil != err {
2022-06-05 19:49:45 +08:00
return nil, err
}
return dbClient.GetSlave(ctx), nil
2022-06-05 19:49:45 +08:00
}