140 lines
3.0 KiB
Go
140 lines
3.0 KiB
Go
|
// Package wrapper...
|
||
|
//
|
||
|
// Description : wrapper...
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 2021-11-15 6:02 下午
|
||
|
package wrapper
|
||
|
|
||
|
import (
|
||
|
"git.zhangdeman.cn/zhangdeman/gopkg/middleware/mysql"
|
||
|
"github.com/pkg/errors"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
// DBConfig ...
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 7:00 下午 2021/11/15
|
||
|
type DBConfig struct {
|
||
|
DBConfig *mysql.DBConfig
|
||
|
LogConfig *mysql.LogConfig
|
||
|
}
|
||
|
|
||
|
// DBWrapperConfig ...
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 6:58 下午 2021/11/15
|
||
|
type DBWrapperConfig struct {
|
||
|
Write *DBConfig
|
||
|
Read *DBConfig
|
||
|
}
|
||
|
|
||
|
// 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 = mysql.GetDatabaseClient(cf.Write.DBConfig, cf.Write.LogConfig); nil != err {
|
||
|
return nil, err
|
||
|
}
|
||
|
if wrapperInstance.read, err = mysql.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
|
||
|
}
|