搬迁mysql相关操作
This commit is contained in:
commit
815806777e
22
.gitignore
vendored
Normal file
22
.gitignore
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
# Created by .ignore support plugin (hsz.mobi)
|
||||||
|
### Go template
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.xlsx
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
# Dependency directories (remove the comment below to include it)
|
||||||
|
# vendor/
|
||||||
|
.idea
|
||||||
|
.vscode
|
||||||
|
mail_test.go
|
||||||
|
|
147
base.go
Normal file
147
base.go
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
// Package mysql ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 8:04 下午 2021/8/8
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import "gorm.io/gorm"
|
||||||
|
|
||||||
|
// Option 扩展选项
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 8:05 下午 2021/8/8
|
||||||
|
type Option struct {
|
||||||
|
DBTable string `json:"db_table"` // 数据表
|
||||||
|
Limit int `json:"limit"` // 限制数量
|
||||||
|
Offset int `json:"offset"` // 偏移量
|
||||||
|
}
|
||||||
|
|
||||||
|
type BaseDao struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateNewData 创建新的数据
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 8:06 下午 2021/8/8
|
||||||
|
func (b *BaseDao) CreateNewData(dbInstance *gorm.DB, data interface{}, o *Option) error {
|
||||||
|
if nil != o {
|
||||||
|
if o.Limit > 0 {
|
||||||
|
dbInstance.Limit(o.Limit)
|
||||||
|
}
|
||||||
|
|
||||||
|
if o.Offset > 0 {
|
||||||
|
dbInstance.Offset(o.Offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dbInstance.Create(data).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateData 更新数据
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 8:12 下午 2021/8/8
|
||||||
|
func (b *BaseDao) UpdateData(dbInstance *gorm.DB, data interface{}, condition interface{}, o *Option) error {
|
||||||
|
if nil != o {
|
||||||
|
|
||||||
|
if o.Limit > 0 {
|
||||||
|
dbInstance.Limit(o.Limit)
|
||||||
|
}
|
||||||
|
|
||||||
|
if o.Offset > 0 {
|
||||||
|
dbInstance.Offset(o.Offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dbInstance.Where(condition).Updates(data).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select 查询数据列表
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 8:14 下午 2021/8/8
|
||||||
|
func (b *BaseDao) Select(dbInstance *gorm.DB, condition interface{}, result interface{}, o *Option) error {
|
||||||
|
if nil != o {
|
||||||
|
if o.Limit > 0 {
|
||||||
|
dbInstance.Limit(o.Limit)
|
||||||
|
}
|
||||||
|
|
||||||
|
if o.Offset > 0 {
|
||||||
|
dbInstance.Offset(o.Offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dbInstance.Find(result, condition).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detail 查询详情
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 8:25 下午 2021/8/8
|
||||||
|
func (b *BaseDao) Detail(dbInstance *gorm.DB, condition interface{}, result interface{}, o *Option) error {
|
||||||
|
if nil != o {
|
||||||
|
if len(o.DBTable) > 0 {
|
||||||
|
dbInstance.Table(o.DBTable)
|
||||||
|
}
|
||||||
|
if o.Limit > 0 {
|
||||||
|
dbInstance.Limit(o.Limit)
|
||||||
|
}
|
||||||
|
|
||||||
|
if o.Offset > 0 {
|
||||||
|
dbInstance.Offset(o.Offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dbInstance.First(result, condition).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count 查询数量
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 8:25 下午 2021/8/8
|
||||||
|
func (b *BaseDao) Count(dbInstance *gorm.DB, condition interface{}, o *Option) (int64, error) {
|
||||||
|
if nil != o {
|
||||||
|
if len(o.DBTable) > 0 {
|
||||||
|
dbInstance.Table(o.DBTable)
|
||||||
|
}
|
||||||
|
if o.Limit > 0 {
|
||||||
|
dbInstance.Limit(o.Limit)
|
||||||
|
}
|
||||||
|
|
||||||
|
if o.Offset > 0 {
|
||||||
|
dbInstance.Offset(o.Offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var cnt int64
|
||||||
|
return cnt, dbInstance.Where(condition).Count(&cnt).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Begin 开启事务
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 3:09 下午 2021/8/9
|
||||||
|
func (b *BaseDao) Begin(dbInstance *gorm.DB) *gorm.DB {
|
||||||
|
return dbInstance.Begin()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit 提交事务
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 3:10 下午 2021/8/9
|
||||||
|
func (b *BaseDao) Commit(db *gorm.DB) error {
|
||||||
|
return db.Commit().Error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rollback 回滚
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 3:12 下午 2021/8/9
|
||||||
|
func (b *BaseDao) Rollback(db *gorm.DB) error {
|
||||||
|
return db.Rollback().Error
|
||||||
|
}
|
155
client.go
Normal file
155
client.go
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
// Package mysql ...
|
||||||
|
//
|
||||||
|
// Description : mysql客户端
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2021-03-01 9:20 下午
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.zhangdeman.cn/zhangdeman/logger/wrapper"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
|
|
||||||
|
"git.zhangdeman.cn/zhangdeman/logger"
|
||||||
|
gormLogger "gorm.io/gorm/logger"
|
||||||
|
|
||||||
|
"gorm.io/driver/mysql"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewDBClient ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 3:09 PM 2021/12/24
|
||||||
|
func NewDBClient(masterConf *DBConfig, slaveConf *DBConfig, logConf *LogConfig, loggerInstance *zap.Logger, extraRequestFieldList []string) (*DBClient, error) {
|
||||||
|
client := &DBClient{
|
||||||
|
extraFieldList: extraRequestFieldList,
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
// 日志初始化失败
|
||||||
|
if client.loggerInstance, err = getLogInstance(logConf, loggerInstance); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if client.master, err = GetDatabaseClient(masterConf, nil); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if client.slave, err = GetDatabaseClient(slaveConf, nil); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DBClient 包装日志实例
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 3:09 PM 2021/12/24
|
||||||
|
type DBClient struct {
|
||||||
|
loggerInstance *zap.Logger
|
||||||
|
master *gorm.DB
|
||||||
|
slave *gorm.DB
|
||||||
|
extraFieldList []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMaster 获取主库连接
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 3:28 PM 2021/12/24
|
||||||
|
func (dc *DBClient) GetMaster(ctx *gin.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 *gin.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 *gin.Context, dbClient *gorm.DB, node string) gormLogger.Interface {
|
||||||
|
return wrapper.NewGormLoggerWithInstance(ctx, dbClient, dc.loggerInstance, node, dc.extraFieldList)
|
||||||
|
}
|
||||||
|
|
||||||
|
// getLogInstance 获取日志实例
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 3:20 PM 2021/12/24
|
||||||
|
func getLogInstance(logConf *LogConfig, loggerInstance *zap.Logger) (*zap.Logger, error) {
|
||||||
|
if nil != loggerInstance {
|
||||||
|
return loggerInstance, nil
|
||||||
|
}
|
||||||
|
logConfList := []logger.SetLoggerOptionFunc{logger.WithEncoder(logConf.Encoder), logger.WithCallerSkip(logConf.Skip), logger.WithCaller()}
|
||||||
|
if logConf.ConsoleOutput {
|
||||||
|
logConfList = append(logConfList, logger.WithConsoleOutput())
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if loggerInstance, err = logger.NewLogger(logConf.Level, logConf.SplitConfig, logConfList...); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return loggerInstance, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDatabaseClient 获取日志实例
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 10:49 下午 2021/3/1
|
||||||
|
func GetDatabaseClient(conf *DBConfig, logConf *LogConfig) (*gorm.DB, error) {
|
||||||
|
var (
|
||||||
|
instance *gorm.DB
|
||||||
|
err error
|
||||||
|
loggerInstance *zap.Logger
|
||||||
|
)
|
||||||
|
|
||||||
|
if instance, err = gorm.Open(mysql.Open(buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if nil != logConf {
|
||||||
|
if loggerInstance, err = getLogInstance(logConf, nil); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
instance.Logger = wrapper.NewGormLoggerWithInstance(nil, instance, loggerInstance, "", nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
return instance, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildConnectionDSN 构建建立连接的DSN
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 9:34 下午 2021/3/1
|
||||||
|
func buildConnectionDSN(conf *DBConfig) string {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
|
||||||
|
conf.Username,
|
||||||
|
conf.Password,
|
||||||
|
conf.Host,
|
||||||
|
conf.Port,
|
||||||
|
conf.Database,
|
||||||
|
conf.Charset,
|
||||||
|
)
|
||||||
|
}
|
44
define.go
Normal file
44
define.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
// Package mysql...
|
||||||
|
//
|
||||||
|
// Description : 数据定义
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2021-03-01 9:27 下午
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.zhangdeman.cn/zhangdeman/logger"
|
||||||
|
"go.uber.org/zap/zapcore"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DBConfig 数据库连接的配置
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 9:32 下午 2021/3/1
|
||||||
|
type DBConfig struct {
|
||||||
|
Host string `json:"host" yaml:"host"` // 主机
|
||||||
|
Port uint `json:"port" yaml:"port"` // 端口
|
||||||
|
Database string `json:"database" yaml:"database"` // 数据库
|
||||||
|
Username string `json:"username" yaml:"username"` // 账号
|
||||||
|
Password string `json:"password" yaml:"password"` // 密码
|
||||||
|
Charset string `json:"charset" yaml:"charset"` // 编码
|
||||||
|
MaxOpenConnection uint `json:"max_open_connection" yaml:"max_open_connection"` // 打开的最大连接数
|
||||||
|
MaxIdleConnection uint `json:"max_idle_connection" yaml:"max_idle_connection"` // 最大空闲连接数
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogConfig 日志配置
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 10:51 下午 2021/3/1
|
||||||
|
type LogConfig struct {
|
||||||
|
Level zapcore.Level
|
||||||
|
ConsoleOutput bool
|
||||||
|
Encoder zapcore.Encoder
|
||||||
|
SplitConfig *logger.RotateLogConfig
|
||||||
|
ExtractFieldList []string
|
||||||
|
TraceFieldName string
|
||||||
|
Skip int
|
||||||
|
}
|
28
error.go
Normal file
28
error.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// Package mysql...
|
||||||
|
//
|
||||||
|
// Description : 异常定义
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2021-03-01 9:42 下午
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import "github.com/pkg/errors"
|
||||||
|
|
||||||
|
// ConnectionOpenError 数据库连接失败
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 9:43 下午 2021/3/1
|
||||||
|
func ConnectionOpenError(err error) error {
|
||||||
|
return errors.WithMessage(err, "数据库连接失败")
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateDBLogError 打开日志失败
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 10:53 下午 2021/3/1
|
||||||
|
func CreateDBLogError(err error) error {
|
||||||
|
return errors.WithMessage(err, "数据库日志初始化失败")
|
||||||
|
}
|
36
go.mod
Normal file
36
go.mod
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
module git.zhangdeman.cn/zhangdeman/mysql
|
||||||
|
|
||||||
|
go 1.17
|
||||||
|
|
||||||
|
require (
|
||||||
|
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20220514052229-cf395d3dc4c3
|
||||||
|
github.com/gin-gonic/gin v1.7.7
|
||||||
|
github.com/pkg/errors v0.9.1
|
||||||
|
go.uber.org/zap v1.21.0
|
||||||
|
gorm.io/driver/mysql v1.3.3
|
||||||
|
gorm.io/gorm v1.23.5
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
|
github.com/go-playground/locales v0.13.0 // indirect
|
||||||
|
github.com/go-playground/universal-translator v0.17.0 // indirect
|
||||||
|
github.com/go-playground/validator/v10 v10.4.1 // indirect
|
||||||
|
github.com/go-sql-driver/mysql v1.6.0 // indirect
|
||||||
|
github.com/golang/protobuf v1.3.3 // indirect
|
||||||
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
|
github.com/jinzhu/now v1.1.4 // indirect
|
||||||
|
github.com/json-iterator/go v1.1.9 // indirect
|
||||||
|
github.com/leodido/go-urn v1.2.0 // indirect
|
||||||
|
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect
|
||||||
|
github.com/lestrrat-go/strftime v1.0.6 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.12 // indirect
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||||
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
|
||||||
|
github.com/ugorji/go/codec v1.1.7 // indirect
|
||||||
|
go.uber.org/atomic v1.7.0 // indirect
|
||||||
|
go.uber.org/multierr v1.6.0 // indirect
|
||||||
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
|
||||||
|
gopkg.in/yaml.v2 v2.2.8 // indirect
|
||||||
|
)
|
118
go.sum
Normal file
118
go.sum
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20220514052229-cf395d3dc4c3 h1:T41tE9F2Gy8eKVSKtTPhf7RaRT12qHEXTfx4IJuoIS4=
|
||||||
|
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20220514052229-cf395d3dc4c3/go.mod h1:0A5BV9pE31nuFE60TLbP7BIhhV/fcWoi+fHrcV2clJw=
|
||||||
|
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
||||||
|
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||||
|
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||||
|
github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs=
|
||||||
|
github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U=
|
||||||
|
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
|
||||||
|
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
|
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
|
||||||
|
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
|
||||||
|
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
|
||||||
|
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
|
||||||
|
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
|
||||||
|
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
|
||||||
|
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
|
||||||
|
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||||
|
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
|
||||||
|
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||||
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||||
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
|
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
|
||||||
|
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||||
|
github.com/jonboulle/clockwork v0.3.0 h1:9BSCMi8C+0qdApAp4auwX0RkLGUjs956h0EkuQymUhg=
|
||||||
|
github.com/jonboulle/clockwork v0.3.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
|
||||||
|
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
|
||||||
|
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
|
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||||
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
|
||||||
|
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
||||||
|
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8=
|
||||||
|
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
|
||||||
|
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkLibYKgg+SwmyFU9dF2hn6MdTj4=
|
||||||
|
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA=
|
||||||
|
github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ=
|
||||||
|
github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
|
||||||
|
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||||
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
||||||
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
|
||||||
|
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||||
|
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
|
||||||
|
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
|
||||||
|
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||||
|
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||||
|
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||||
|
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
|
||||||
|
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
||||||
|
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
|
||||||
|
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||||
|
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
|
||||||
|
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
|
||||||
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
|
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||||
|
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
|
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
|
||||||
|
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gorm.io/driver/mysql v1.3.3 h1:jXG9ANrwBc4+bMvBcSl8zCfPBaVoPyBEBshA8dA93X8=
|
||||||
|
gorm.io/driver/mysql v1.3.3/go.mod h1:ChK6AHbHgDCFZyJp0F+BmVGb06PSIoh9uVYKAlRbb2U=
|
||||||
|
gorm.io/gorm v1.23.1/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
||||||
|
gorm.io/gorm v1.23.5 h1:TnlF26wScKSvknUC/Rn8t0NLLM22fypYBlvj1+aH6dM=
|
||||||
|
gorm.io/gorm v1.23.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
138
logger.go
Normal file
138
logger.go
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
// Package mysql ...
|
||||||
|
//
|
||||||
|
// Description : wrapper...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2021-11-15 6:02 下午
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DBFullConfig ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 7:00 下午 2021/11/15
|
||||||
|
type DBFullConfig struct {
|
||||||
|
DBConfig *DBConfig
|
||||||
|
LogConfig *LogConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// DBWrapperConfig ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 6:58 下午 2021/11/15
|
||||||
|
type DBWrapperConfig struct {
|
||||||
|
Write *DBFullConfig
|
||||||
|
Read *DBFullConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDBWrapper 获取包装后的数据库实例
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 6:45 下午 2021/11/15
|
||||||
|
func NewDBWrapper(cf *DBWrapperConfig) (*DBWrapper, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if nil == cf {
|
||||||
|
return nil, errors.New("cf is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if nil == cf.Write {
|
||||||
|
return nil, errors.New("write cf is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if nil == cf.Read {
|
||||||
|
return nil, errors.New("read cf is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
wrapperInstance := &DBWrapper{}
|
||||||
|
if wrapperInstance.write, err = GetDatabaseClient(cf.Write.DBConfig, cf.Write.LogConfig); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if wrapperInstance.read, err = GetDatabaseClient(cf.Read.DBConfig, cf.Read.LogConfig); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return wrapperInstance, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DBWrapper 数据库包装
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 6:02 下午 2021/11/15
|
||||||
|
type DBWrapper struct {
|
||||||
|
read *gorm.DB // 读库实例
|
||||||
|
write *gorm.DB // 写库实例
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectDBOption 数据库实力选择时的选项
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 6:33 下午 2021/11/15
|
||||||
|
type SelectDBOption struct {
|
||||||
|
Table string
|
||||||
|
ForceMaster bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetSelectDBOption 设置选项
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 6:34 下午 2021/11/15
|
||||||
|
type SetSelectDBOption func(o *SelectDBOption)
|
||||||
|
|
||||||
|
// GetWriteInstance 获取主库实例
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 6:31 下午 2021/11/15
|
||||||
|
func (dw *DBWrapper) GetWriteInstance(instanceOptionList ...SetSelectDBOption) *gorm.DB {
|
||||||
|
option := dw.getOption(instanceOptionList...)
|
||||||
|
instance := dw.write
|
||||||
|
if len(option.Table) > 0 {
|
||||||
|
instance.Table(option.Table)
|
||||||
|
}
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetReadInstance 获取读库实例
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 6:32 下午 2021/11/15
|
||||||
|
func (dw *DBWrapper) GetReadInstance(instanceOptionList ...SetSelectDBOption) *gorm.DB {
|
||||||
|
option := dw.getOption(instanceOptionList...)
|
||||||
|
instance := dw.read
|
||||||
|
if option.ForceMaster || nil == dw.read {
|
||||||
|
instance = dw.write
|
||||||
|
}
|
||||||
|
if len(option.Table) > 0 {
|
||||||
|
instance.Table(option.Table)
|
||||||
|
}
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
|
||||||
|
// getOption 生成option
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 6:35 下午 2021/11/15
|
||||||
|
func (dw *DBWrapper) getOption(instanceOptionList ...SetSelectDBOption) *SelectDBOption {
|
||||||
|
o := &SelectDBOption{
|
||||||
|
Table: "",
|
||||||
|
ForceMaster: false,
|
||||||
|
}
|
||||||
|
for _, f := range instanceOptionList {
|
||||||
|
f(o)
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user