Merge pull request '支持sqlite数据库驱动' (#1) from feture/upgrade into master
Reviewed-on: zhangdeman/mysql#1
This commit is contained in:
commit
e9076adecd
25
client.go
25
client.go
@ -24,6 +24,7 @@ import (
|
|||||||
gormLogger "gorm.io/gorm/logger"
|
gormLogger "gorm.io/gorm/logger"
|
||||||
|
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -70,7 +71,7 @@ func (c *client) AddWithConfigFile(cfgFilePath string, logInstance *zap.Logger)
|
|||||||
master: nil,
|
master: nil,
|
||||||
slave: nil,
|
slave: nil,
|
||||||
extraFieldList: nil,
|
extraFieldList: nil,
|
||||||
cfg: Mysql{},
|
cfg: Driver{},
|
||||||
}
|
}
|
||||||
if dbClient.master, err = c.GetDatabaseClient(cfg.Config.Master, logInstance); nil != err {
|
if dbClient.master, err = c.GetDatabaseClient(cfg.Config.Master, logInstance); nil != err {
|
||||||
return err
|
return err
|
||||||
@ -218,14 +219,22 @@ func (c *client) getGormClient() (*gorm.DB, error) {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 18:41 2022/6/11
|
// Date : 18:41 2022/6/11
|
||||||
func (c *client) GetDatabaseClient(conf *Mysql, logInstance *zap.Logger) (*gorm.DB, error) {
|
func (c *client) GetDatabaseClient(conf *Driver, logInstance *zap.Logger) (*gorm.DB, error) {
|
||||||
var (
|
var (
|
||||||
instance *gorm.DB
|
instance *gorm.DB
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
if instance, err = gorm.Open(mysql.Open(c.buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
|
if conf.DBType == DriverTypeMysql {
|
||||||
return nil, err
|
if instance, err = gorm.Open(mysql.Open(c.buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else if conf.DBType == DriverTypeSqlite3 {
|
||||||
|
if instance, err = gorm.Open(sqlite.Open(c.buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("%v : db driver is not support", conf.DBType)
|
||||||
}
|
}
|
||||||
|
|
||||||
instance.Logger = wrapper.NewGormLoggerWithInstance(nil, instance, logInstance, "", nil)
|
instance.Logger = wrapper.NewGormLoggerWithInstance(nil, instance, logInstance, "", nil)
|
||||||
@ -238,7 +247,11 @@ func (c *client) GetDatabaseClient(conf *Mysql, logInstance *zap.Logger) (*gorm.
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 18:42 2022/6/11
|
// Date : 18:42 2022/6/11
|
||||||
func (c *client) buildConnectionDSN(conf *Mysql) string {
|
func (c *client) buildConnectionDSN(conf *Driver) string {
|
||||||
|
if conf.DBType == DriverTypeSqlite3 {
|
||||||
|
// 兼容sqlite3
|
||||||
|
return conf.Host
|
||||||
|
}
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=%s",
|
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=%s",
|
||||||
conf.Username,
|
conf.Username,
|
||||||
@ -262,7 +275,7 @@ type DBClient struct {
|
|||||||
master *gorm.DB // 主库
|
master *gorm.DB // 主库
|
||||||
slave *gorm.DB // 从库
|
slave *gorm.DB // 从库
|
||||||
extraFieldList []string // 提取的字段
|
extraFieldList []string // 提取的字段
|
||||||
cfg Mysql // 数据库配置
|
cfg Driver // 数据库配置
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetFlag 设置数据库标识
|
// SetFlag 设置数据库标识
|
||||||
|
16
define.go
16
define.go
@ -49,16 +49,17 @@ const (
|
|||||||
//
|
//
|
||||||
// Date : 15:19 2022/6/9
|
// Date : 15:19 2022/6/9
|
||||||
type Database struct {
|
type Database struct {
|
||||||
Master *Mysql `json:"master" yaml:"master"` // 主库配置
|
Master *Driver `json:"master" yaml:"master"` // 主库配置
|
||||||
Slave *Mysql `json:"slave" yaml:"slave"` // 从库配置
|
Slave *Driver `json:"slave" yaml:"slave"` // 从库配置
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mysql ...
|
// Driver ...
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 18:44 2022/5/14
|
// Date : 18:44 2022/5/14
|
||||||
type Mysql struct {
|
type Driver struct {
|
||||||
|
DBType string `json:"db_type" yaml:"db_type"` // 数据库驱动类型
|
||||||
Host string `json:"host" yaml:"host"` // 数据库地址
|
Host string `json:"host" yaml:"host"` // 数据库地址
|
||||||
Port int `json:"port" yaml:"port"` // 数据库端口
|
Port int `json:"port" yaml:"port"` // 数据库端口
|
||||||
Username string `json:"username" yaml:"username"` // 用户名
|
Username string `json:"username" yaml:"username"` // 用户名
|
||||||
@ -69,6 +70,13 @@ type Mysql struct {
|
|||||||
Timezone string `json:"timezone" yaml:"timezone"` // 时区
|
Timezone string `json:"timezone" yaml:"timezone"` // 时区
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DriverTypeMysql mysql 驱动
|
||||||
|
DriverTypeMysql = "mysql"
|
||||||
|
// DriverTypeSqlite3 sqlite3
|
||||||
|
DriverTypeSqlite3 = "sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
// Connection 连接数配置
|
// Connection 连接数配置
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
4
go.mod
4
go.mod
@ -10,7 +10,7 @@ require (
|
|||||||
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
|
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
|
||||||
go.uber.org/zap v1.24.0
|
go.uber.org/zap v1.24.0
|
||||||
gorm.io/driver/mysql v1.4.7
|
gorm.io/driver/mysql v1.4.7
|
||||||
gorm.io/gorm v1.24.6
|
gorm.io/gorm v1.25.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@ -35,6 +35,7 @@ require (
|
|||||||
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect
|
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect
|
||||||
github.com/lestrrat-go/strftime v1.0.6 // indirect
|
github.com/lestrrat-go/strftime v1.0.6 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.18 // indirect
|
github.com/mattn/go-isatty v0.0.18 // indirect
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.16 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
|
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
|
||||||
@ -54,4 +55,5 @@ require (
|
|||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
google.golang.org/protobuf v1.30.0 // indirect
|
google.golang.org/protobuf v1.30.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
gorm.io/driver/sqlite v1.5.0 // indirect
|
||||||
)
|
)
|
||||||
|
8
go.sum
8
go.sum
@ -94,6 +94,9 @@ github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPn
|
|||||||
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
|
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
|
||||||
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
@ -253,10 +256,15 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gorm.io/driver/mysql v1.4.7 h1:rY46lkCspzGHn7+IYsNpSfEv9tA+SU4SkkB+GFX125Y=
|
gorm.io/driver/mysql v1.4.7 h1:rY46lkCspzGHn7+IYsNpSfEv9tA+SU4SkkB+GFX125Y=
|
||||||
gorm.io/driver/mysql v1.4.7/go.mod h1:SxzItlnT1cb6e1e4ZRpgJN2VYtcqJgqnHxWr4wsP8oc=
|
gorm.io/driver/mysql v1.4.7/go.mod h1:SxzItlnT1cb6e1e4ZRpgJN2VYtcqJgqnHxWr4wsP8oc=
|
||||||
|
gorm.io/driver/sqlite v1.5.0 h1:zKYbzRCpBrT1bNijRnxLDJWPjVfImGEn0lSnUY5gZ+c=
|
||||||
|
gorm.io/driver/sqlite v1.5.0/go.mod h1:kDMDfntV9u/vuMmz8APHtHF0b4nyBB7sfCieC6G8k8I=
|
||||||
gorm.io/gorm v1.23.6/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
gorm.io/gorm v1.23.6/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
||||||
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
||||||
gorm.io/gorm v1.24.5 h1:g6OPREKqqlWq4kh/3MCQbZKImeB9e6Xgc4zD+JgNZGE=
|
gorm.io/gorm v1.24.5 h1:g6OPREKqqlWq4kh/3MCQbZKImeB9e6Xgc4zD+JgNZGE=
|
||||||
gorm.io/gorm v1.24.5/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
|
gorm.io/gorm v1.24.5/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
|
||||||
gorm.io/gorm v1.24.6 h1:wy98aq9oFEetsc4CAbKD2SoBCdMzsbSIvSUUFJuHi5s=
|
gorm.io/gorm v1.24.6 h1:wy98aq9oFEetsc4CAbKD2SoBCdMzsbSIvSUUFJuHi5s=
|
||||||
gorm.io/gorm v1.24.6/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
gorm.io/gorm v1.24.6/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
||||||
|
gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
||||||
|
gorm.io/gorm v1.25.0 h1:+KtYtb2roDz14EQe4bla8CbQlmb9dN3VejSai3lprfU=
|
||||||
|
gorm.io/gorm v1.25.0/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
||||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||||
|
Loading…
Reference in New Issue
Block a user