From 035aa5be5114dae865328e71c71c43bf9318e847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 12 Jun 2022 19:42:11 +0800 Subject: [PATCH] remove code --- logger.go | 138 ------------------------------------------------------ 1 file changed, 138 deletions(-) delete mode 100644 logger.go diff --git a/logger.go b/logger.go deleted file mode 100644 index ec9efad..0000000 --- a/logger.go +++ /dev/null @@ -1,138 +0,0 @@ -// 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 -}