api2sql基础的能力 #8

Merged
zhangdeman merged 26 commits from feature/api2sql into master 2024-08-24 12:33:47 +08:00
3 changed files with 100 additions and 75 deletions
Showing only changes of commit 5e87706d0a - Show all commits

View File

@ -8,8 +8,10 @@
package abstract package abstract
import ( import (
"context"
"git.zhangdeman.cn/zhangdeman/database/define" "git.zhangdeman.cn/zhangdeman/database/define"
"go.uber.org/zap" "go.uber.org/zap"
"gorm.io/gorm"
) )
// IWrapperClient 包装的客户端 // IWrapperClient 包装的客户端
@ -24,4 +26,10 @@ type IWrapperClient interface {
AddWithConfig(flag string, logInstance *zap.Logger, databaseConfig *define.Database, extraFieldList []string) error AddWithConfig(flag string, logInstance *zap.Logger, databaseConfig *define.Database, extraFieldList []string) error
// BatchAddWithConfigDir 通过具体的配置列表增加数据库实例 // BatchAddWithConfigDir 通过具体的配置列表增加数据库实例
BatchAddWithConfigDir(cfgDir string, logInstance *zap.Logger, extraFieldList []string) error 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)
} }

View File

@ -9,8 +9,12 @@ package define
import ( import (
"context" "context"
"fmt"
"git.zhangdeman.cn/zhangdeman/consts"
"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/sqlite"
"gorm.io/gorm" "gorm.io/gorm"
gormLogger "gorm.io/gorm/logger" gormLogger "gorm.io/gorm/logger"
) )
@ -29,13 +33,29 @@ type DBClient struct {
Cfg Driver // 数据库配置 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 设置数据库标识 // SetFlag 设置数据库标识
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:18 2022/6/5 // Date : 16:18 2022/6/5
func (dc *DBClient) SetFlag(dbFlag string) { func (dc *DBClient) SetFlag(dbFlag string) {
dc.dbFlag = dbFlag dc.DbFlag = dbFlag
} }
// GetMaster 获取主库连接 // GetMaster 获取主库连接
@ -66,5 +86,55 @@ func (dc *DBClient) GetSlave(ctx context.Context) *gorm.DB {
// //
// Date : 3:45 PM 2021/12/24 // Date : 3:45 PM 2021/12/24
func (dc *DBClient) getLogger(ctx context.Context, dbClient *gorm.DB, node string) gormLogger.Interface { 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,
)
} }

View File

@ -12,33 +12,35 @@ import (
"errors" "errors"
"fmt" "fmt"
"git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/database/abstract"
"git.zhangdeman.cn/zhangdeman/database/define" "git.zhangdeman.cn/zhangdeman/database/define"
"git.zhangdeman.cn/zhangdeman/serialize" "git.zhangdeman.cn/zhangdeman/serialize"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"git.zhangdeman.cn/zhangdeman/logger/wrapper"
"go.uber.org/zap" "go.uber.org/zap"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm" "gorm.io/gorm"
) )
var ( var (
// Client mysql客户端 // WrapperClient 包装后的数据库客户端
Client *client WrapperClient abstract.IWrapperClient
) )
func init() { func init() {
Client = &client{ WrapperClient = NewWrapperClient()
}
func NewWrapperClient() *wrapperClient {
return &wrapperClient{
lock: &sync.RWMutex{}, lock: &sync.RWMutex{},
clientTable: make(map[string]*define.DBClient), clientTable: make(map[string]*define.DBClient),
} }
} }
type client struct { type wrapperClient struct {
lock *sync.RWMutex lock *sync.RWMutex
clientTable map[string]*define.DBClient clientTable map[string]*define.DBClient
logger *zap.Logger logger *zap.Logger
@ -49,7 +51,7 @@ type client struct {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 19:19 2022/6/5 // 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 ( var (
err error err error
cfg *define.CfgFile cfg *define.CfgFile
@ -70,24 +72,19 @@ func (c *client) AddWithConfigFile(cfgFilePath string, logInstance *zap.Logger,
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 20:41 2023/4/18 // 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{ dbClient := &define.DBClient{
DbFlag: flag, DbFlag: flag,
LoggerInstance: logInstance, LoggerInstance: logInstance,
master: nil,
slave: nil,
ExtraFieldList: extraFieldList, ExtraFieldList: extraFieldList,
Cfg: define.Driver{}, Cfg: define.Driver{},
} }
var err error
if dbClient.master, err = c.GetDatabaseClient(databaseConfig.Master, logInstance); nil != err { if err := dbClient.Init(databaseConfig); nil != err {
return err
}
if dbClient.slave, err = c.GetDatabaseClient(databaseConfig.Slave, logInstance); nil != err {
return err return err
} }
c.lock.Lock() c.lock.Lock()
c.clientTable[dbClient.dbFlag] = dbClient c.clientTable[dbClient.DbFlag] = dbClient
c.lock.Unlock() c.lock.Unlock()
return nil return nil
} }
@ -97,7 +94,7 @@ func (c *client) AddWithConfig(flag string, logInstance *zap.Logger, databaseCon
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 19:19 2022/6/5 // 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, "*")) filepathNames, _ := filepath.Glob(filepath.Join(cfgDir, "*"))
for i := range filepathNames { for i := range filepathNames {
if err := c.AddWithConfigFile(filepathNames[i], logInstance, extraFieldList); nil != err { 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<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:05 2022/6/11 // 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, ".") fileArr := strings.Split(cfgPath, ".")
if len(fileArr) < 2 { if len(fileArr) < 2 {
// 获取不到类型 // 获取不到类型
@ -161,7 +158,7 @@ func (c *client) 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 *client) GetDBClient(dbFlag string) (*define.DBClient, error) { func (c *wrapperClient) GetDBClient(dbFlag string) (*define.DBClient, error) {
c.lock.RLock() c.lock.RLock()
defer c.lock.RUnlock() defer c.lock.RUnlock()
var ( var (
@ -179,7 +176,7 @@ func (c *client) GetDBClient(dbFlag string) (*define.DBClient, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 19:36 2022/6/5 // 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 ( var (
err error err error
dbClient *define.DBClient dbClient *define.DBClient
@ -195,7 +192,7 @@ func (c *client) GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB,
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 19:37 2022/6/5 // 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 ( var (
err error err error
dbClient *define.DBClient dbClient *define.DBClient
@ -212,56 +209,6 @@ func (c *client) GetSlaveClient(ctx context.Context, dbFlag string) (*gorm.DB, e
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 11:24 2022/6/6 // Date : 11:24 2022/6/6
func (c *client) getGormClient() (*gorm.DB, error) { func (c *wrapperClient) getGormClient() (*gorm.DB, error) {
return nil, nil 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,
)
}