diff --git a/middleware/mysql/client.go b/middleware/mysql/client.go new file mode 100644 index 0000000..592cc35 --- /dev/null +++ b/middleware/mysql/client.go @@ -0,0 +1,64 @@ +// Package mysql ... +// +// Description : mysql客户端 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-03-01 9:20 下午 +package mysql + +import ( + "fmt" + + "github.com/go-developer/gopkg/logger/wrapper" + + "gorm.io/driver/mysql" + "gorm.io/gorm" +) + +// 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 + ) + + if instance, err = gorm.Open(mysql.Open(buildConnectionDSN(conf)), &gorm.Config{}); nil != err { + return nil, err + } + + if len(logConf.TraceFieldName) == 0 { + logConf.TraceFieldName = defaultTraceFieldName + } + + if instance.Logger, err = wrapper.NewGormV2( + logConf.Level, + logConf.ConsoleOutput, + logConf.Encoder, + logConf.SplitConfig, + logConf.TraceFieldName); nil != err { + return nil, CreateDBLogError(err) + } + 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, + ) +} diff --git a/middleware/mysql/define.go b/middleware/mysql/define.go new file mode 100644 index 0000000..9e47ab2 --- /dev/null +++ b/middleware/mysql/define.go @@ -0,0 +1,48 @@ +// Package mysql... +// +// Description : 数据定义 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-03-01 9:27 下午 +package mysql + +import ( + "github.com/go-developer/gopkg/logger" + "go.uber.org/zap/zapcore" +) + +// DBConfig 数据库连接的配置 +// +// Author : go_developer@163.com<张德满> +// +// Date : 9:32 下午 2021/3/1 +type DBConfig struct { + Host string // 主机 + Port uint // 端口 + Database string // 数据库 + Username string // 账号 + Password string // 密码 + Charset string // 编码 + MaxOpenConnection uint // 打开的最大连接数 + MaxIdleConnection uint // 最大空闲连接数 +} + +// 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 +} + +const ( + // defaultTraceFieldName 默认trace_id字段 + defaultTraceFieldName = "trace_id" +) diff --git a/middleware/mysql/error.go b/middleware/mysql/error.go new file mode 100644 index 0000000..04f236f --- /dev/null +++ b/middleware/mysql/error.go @@ -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, "数据库日志初始化失败") +}