api2sql基础的能力 #8
24
abstract/api_sql_manager.go
Normal file
24
abstract/api_sql_manager.go
Normal file
@ -0,0 +1,24 @@
|
||||
// Package abstract ...
|
||||
//
|
||||
// Description : abstract ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2024-08-20 17:17
|
||||
package abstract
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// IManager 接口 => sql 数据管理实例约束
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:17 2024/8/20
|
||||
type IManager interface {
|
||||
// SetDatabaseClient 设置数据库连接的客户端
|
||||
SetDatabaseClient(database IWrapperClient)
|
||||
// Execute 执行
|
||||
Execute(ctx context.Context, databaseFlag string) (any, error)
|
||||
}
|
27
abstract/wrapper_client.go
Normal file
27
abstract/wrapper_client.go
Normal file
@ -0,0 +1,27 @@
|
||||
// Package abstract ...
|
||||
//
|
||||
// Description : abstract ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2024-08-20 17:26
|
||||
package abstract
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/database/define"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// IWrapperClient 包装的客户端
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:26 2024/8/20
|
||||
type IWrapperClient interface {
|
||||
// AddWithConfigFile 通过配置文件增加数据库实例
|
||||
AddWithConfigFile(cfgFilePath string, logInstance *zap.Logger, extraFieldList []string) error
|
||||
// AddWithConfig 通过具体的配置增加数据库实例
|
||||
AddWithConfig(flag string, logInstance *zap.Logger, databaseConfig *define.Database, extraFieldList []string) error
|
||||
// BatchAddWithConfigDir 通过具体的配置列表增加数据库实例
|
||||
BatchAddWithConfigDir(cfgDir string, logInstance *zap.Logger, extraFieldList []string) error
|
||||
}
|
104
client.go
104
client.go
@ -12,6 +12,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/database/define"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -20,8 +21,6 @@ import (
|
||||
"git.zhangdeman.cn/zhangdeman/logger/wrapper"
|
||||
"go.uber.org/zap"
|
||||
|
||||
gormLogger "gorm.io/gorm/logger"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
@ -35,13 +34,13 @@ var (
|
||||
func init() {
|
||||
Client = &client{
|
||||
lock: &sync.RWMutex{},
|
||||
clientTable: make(map[string]*DBClient),
|
||||
clientTable: make(map[string]*define.DBClient),
|
||||
}
|
||||
}
|
||||
|
||||
type client struct {
|
||||
lock *sync.RWMutex
|
||||
clientTable map[string]*DBClient
|
||||
clientTable map[string]*define.DBClient
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
@ -53,7 +52,7 @@ type client struct {
|
||||
func (c *client) AddWithConfigFile(cfgFilePath string, logInstance *zap.Logger, extraFieldList []string) error {
|
||||
var (
|
||||
err error
|
||||
cfg *cfgFile
|
||||
cfg *define.CfgFile
|
||||
)
|
||||
|
||||
if cfg, err = c.getCfg(cfgFilePath); nil != err {
|
||||
@ -71,14 +70,14 @@ 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 *Database, extraFieldList []string) error {
|
||||
dbClient := &DBClient{
|
||||
dbFlag: flag,
|
||||
loggerInstance: logInstance,
|
||||
func (c *client) 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: Driver{},
|
||||
ExtraFieldList: extraFieldList,
|
||||
Cfg: define.Driver{},
|
||||
}
|
||||
var err error
|
||||
if dbClient.master, err = c.GetDatabaseClient(databaseConfig.Master, logInstance); nil != err {
|
||||
@ -113,7 +112,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) (*cfgFile, error) {
|
||||
func (c *client) getCfg(cfgPath string) (*define.CfgFile, error) {
|
||||
fileArr := strings.Split(cfgPath, ".")
|
||||
if len(fileArr) < 2 {
|
||||
// 获取不到类型
|
||||
@ -121,15 +120,15 @@ func (c *client) getCfg(cfgPath string) (*cfgFile, error) {
|
||||
}
|
||||
fileType := strings.ToLower(fileArr[len(fileArr)-1])
|
||||
fileFlagArr := strings.Split(fileArr[0], string(filepath.Separator))
|
||||
result := &cfgFile{
|
||||
result := &define.CfgFile{
|
||||
Path: cfgPath,
|
||||
Type: "",
|
||||
Flag: fileFlagArr[len(fileFlagArr)-1],
|
||||
Config: &Database{},
|
||||
Config: &define.Database{},
|
||||
}
|
||||
var (
|
||||
err error
|
||||
cfgInfo Database
|
||||
cfgInfo define.Database
|
||||
)
|
||||
switch fileType {
|
||||
case consts.FileTypeYaml:
|
||||
@ -162,12 +161,12 @@ func (c *client) getCfg(cfgPath string) (*cfgFile, error) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:32 2022/6/5
|
||||
func (c *client) GetDBClient(dbFlag string) (*DBClient, error) {
|
||||
func (c *client) GetDBClient(dbFlag string) (*define.DBClient, error) {
|
||||
c.lock.RLock()
|
||||
defer c.lock.RUnlock()
|
||||
var (
|
||||
exist bool
|
||||
dbClient *DBClient
|
||||
dbClient *define.DBClient
|
||||
)
|
||||
if dbClient, exist = c.clientTable[dbFlag]; !exist {
|
||||
return nil, fmt.Errorf("%s 标识的数据库实例不存在! ", dbFlag)
|
||||
@ -183,15 +182,12 @@ func (c *client) GetDBClient(dbFlag string) (*DBClient, error) {
|
||||
func (c *client) GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB, error) {
|
||||
var (
|
||||
err error
|
||||
dbClient *DBClient
|
||||
dbClient *define.DBClient
|
||||
)
|
||||
if dbClient, err = c.GetDBClient(dbFlag); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
session := dbClient.master.Session(&gorm.Session{})
|
||||
session.Logger = dbClient.getLogger(ctx, session, dbFlag+"-master")
|
||||
return session, nil
|
||||
return dbClient.GetMaster(ctx), nil
|
||||
}
|
||||
|
||||
// GetSlaveClient 获取从库客户端
|
||||
@ -202,15 +198,13 @@ func (c *client) GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB,
|
||||
func (c *client) GetSlaveClient(ctx context.Context, dbFlag string) (*gorm.DB, error) {
|
||||
var (
|
||||
err error
|
||||
dbClient *DBClient
|
||||
dbClient *define.DBClient
|
||||
)
|
||||
if dbClient, err = c.GetDBClient(dbFlag); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
session := dbClient.slave.Session(&gorm.Session{})
|
||||
session.Logger = dbClient.getLogger(ctx, session, dbFlag+"-slave")
|
||||
return session, nil
|
||||
return dbClient.GetSlave(ctx), nil
|
||||
}
|
||||
|
||||
// getGormClient 获取GORM client方法
|
||||
@ -227,7 +221,7 @@ func (c *client) getGormClient() (*gorm.DB, error) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:41 2022/6/11
|
||||
func (c *client) GetDatabaseClient(conf *Driver, logInstance *zap.Logger) (*gorm.DB, error) {
|
||||
func (c *client) GetDatabaseClient(conf *define.Driver, logInstance *zap.Logger) (*gorm.DB, error) {
|
||||
var (
|
||||
instance *gorm.DB
|
||||
err error
|
||||
@ -255,7 +249,7 @@ func (c *client) GetDatabaseClient(conf *Driver, logInstance *zap.Logger) (*gorm
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:42 2022/6/11
|
||||
func (c *client) buildConnectionDSN(conf *Driver) string {
|
||||
func (c *client) buildConnectionDSN(conf *define.Driver) string {
|
||||
if conf.DBType == consts.DatabaseDriverSqlite3 {
|
||||
// 兼容sqlite3
|
||||
return conf.Host
|
||||
@ -271,57 +265,3 @@ func (c *client) buildConnectionDSN(conf *Driver) string {
|
||||
conf.Timezone,
|
||||
)
|
||||
}
|
||||
|
||||
// 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 Driver // 数据库配置
|
||||
}
|
||||
|
||||
// SetFlag 设置数据库标识
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:18 2022/6/5
|
||||
func (dc *DBClient) SetFlag(dbFlag string) {
|
||||
dc.dbFlag = dbFlag
|
||||
}
|
||||
|
||||
// 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, "slave")
|
||||
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 {
|
||||
return wrapper.NewGormLoggerWithInstance(ctx, dbClient, dc.loggerInstance, dc.dbFlag+"|"+node, dc.extraFieldList)
|
||||
}
|
||||
|
70
define/db_client.go
Normal file
70
define/db_client.go
Normal file
@ -0,0 +1,70 @@
|
||||
// Package define ...
|
||||
//
|
||||
// Description : define ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2024-08-20 17:36
|
||||
package define
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.zhangdeman.cn/zhangdeman/logger/wrapper"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
gormLogger "gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// 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 Driver // 数据库配置
|
||||
}
|
||||
|
||||
// SetFlag 设置数据库标识
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:18 2022/6/5
|
||||
func (dc *DBClient) SetFlag(dbFlag string) {
|
||||
dc.dbFlag = dbFlag
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return wrapper.NewGormLoggerWithInstance(ctx, dbClient, dc.loggerInstance, dc.dbFlag+"|"+node, dc.extraFieldList)
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
// package database...
|
||||
// Package define ...
|
||||
//
|
||||
// Description : 数据定义
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-03-01 9:27 下午
|
||||
package database
|
||||
package define
|
||||
|
||||
// DBConfig 数据库连接的配置
|
||||
//
|
||||
@ -22,12 +22,12 @@ type DBConfig struct {
|
||||
Connection Connection `json:"connection" yaml:"connection"` // 连接数量配置
|
||||
}
|
||||
|
||||
// cfgFile 配置文件定义
|
||||
// CfgFile 配置文件定义
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:47 2022/6/9
|
||||
type cfgFile struct {
|
||||
type CfgFile struct {
|
||||
Flag string `json:"flag"` // 数据库标识
|
||||
Path string `json:"path"` // 配置文件路径
|
||||
Type string `json:"type"` // 配置文件类型
|
Loading…
Reference in New Issue
Block a user