From 64ce45cd0af0869bc335e62bb910a2f8edb31c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 15 Nov 2021 19:03:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0db=20wrapper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 - go.sum | 6 -- middleware/mysql/wrapper/define.go | 139 +++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 middleware/mysql/wrapper/define.go diff --git a/go.mod b/go.mod index 21a81fe..9b7fcb3 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,6 @@ require ( github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/golang/snappy v0.0.3 // indirect - github.com/google/gopacket v1.1.19 // indirect github.com/hashicorp/go-uuid v1.0.2 // indirect github.com/jcmturner/aescts/v2 v2.0.0 // indirect github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect diff --git a/go.sum b/go.sum index eb4cc35..6e620fb 100644 --- a/go.sum +++ b/go.sum @@ -73,8 +73,6 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8= -github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo= github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE= @@ -192,8 +190,6 @@ golang.org/x/crypto v0.0.0-20201112155050-0c6587e931a9/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -230,7 +226,6 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71 h1:ikCpsnYR+Ew0vu99XlDp55lGgDJdIMx3f4a18jfse/s= golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1UPo6RgF9rJFkTgZIxO4= golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -243,7 +238,6 @@ golang.org/x/text v0.3.6/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.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= 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= diff --git a/middleware/mysql/wrapper/define.go b/middleware/mysql/wrapper/define.go new file mode 100644 index 0000000..eadec11 --- /dev/null +++ b/middleware/mysql/wrapper/define.go @@ -0,0 +1,139 @@ +// 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 +}