优化参数初始化方式
This commit is contained in:
@@ -5,6 +5,7 @@ go 1.20
|
|||||||
require (
|
require (
|
||||||
github.com/zeromicro/go-zero v1.6.1
|
github.com/zeromicro/go-zero v1.6.1
|
||||||
gorm.io/driver/mysql v1.5.2
|
gorm.io/driver/mysql v1.5.2
|
||||||
|
gorm.io/driver/sqlite v1.5.4
|
||||||
gorm.io/gorm v1.25.5
|
gorm.io/gorm v1.25.5
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,6 +16,7 @@ require (
|
|||||||
github.com/jinzhu/now v1.1.5 // indirect
|
github.com/jinzhu/now v1.1.5 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.17 // indirect
|
||||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||||
go.opentelemetry.io/otel v1.19.0 // indirect
|
go.opentelemetry.io/otel v1.19.0 // indirect
|
||||||
go.opentelemetry.io/otel/trace v1.19.0 // indirect
|
go.opentelemetry.io/otel/trace v1.19.0 // indirect
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
|
|||||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
|
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
|
||||||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||||
@@ -38,6 +40,8 @@ golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
|||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs=
|
gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs=
|
||||||
gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8=
|
gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8=
|
||||||
|
gorm.io/driver/sqlite v1.5.4 h1:IqXwXi8M/ZlPzH/947tn5uik3aYQslP9BVveoax0nV0=
|
||||||
|
gorm.io/driver/sqlite v1.5.4/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4=
|
||||||
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
||||||
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
|
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
|
||||||
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
||||||
|
|||||||
@@ -5,17 +5,40 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/schema"
|
"gorm.io/gorm/schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewGorm(prefix, user, password, host, database string, port int) *gorm.DB {
|
func NewGorm(opts ...Option) *gorm.DB {
|
||||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%v)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, port, database)
|
defaultOpts := defaultOptions()
|
||||||
|
for _, apply := range opts {
|
||||||
|
apply(&defaultOpts)
|
||||||
|
}
|
||||||
|
|
||||||
g, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
if defaultOpts.Db == "" {
|
||||||
|
panic("db is empty")
|
||||||
|
}
|
||||||
|
var dialector gorm.Dialector
|
||||||
|
if defaultOpts.Db == "mysql" {
|
||||||
|
if defaultOpts.Mysql == nil {
|
||||||
|
panic("mysql is not supported")
|
||||||
|
}
|
||||||
|
|
||||||
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%v)/%s?charset=utf8mb4&parseTime=True&loc=Local", defaultOpts.Mysql.User, defaultOpts.Mysql.Password, defaultOpts.Mysql.Host, defaultOpts.Mysql.Port, defaultOpts.Mysql.Database)
|
||||||
|
dialector = mysql.Open(dsn)
|
||||||
|
|
||||||
|
} else if defaultOpts.Db == "sqlite" {
|
||||||
|
if defaultOpts.Sqlite == nil {
|
||||||
|
panic("sqlite is not supported")
|
||||||
|
}
|
||||||
|
dialector = sqlite.Open(defaultOpts.Sqlite.DbPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := gorm.Open(dialector, &gorm.Config{
|
||||||
NamingStrategy: schema.NamingStrategy{
|
NamingStrategy: schema.NamingStrategy{
|
||||||
TablePrefix: prefix, // 表名前缀,`Article` 的表名应该是 `it_articles`
|
TablePrefix: defaultOpts.Prefix, // 表名前缀,`Article` 的表名应该是 `it_articles`
|
||||||
SingularTable: true, // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
|
SingularTable: defaultOpts.SingularTable, // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
|
||||||
},
|
},
|
||||||
Logger: NewGormxLogger(context.TODO()),
|
Logger: NewGormxLogger(context.TODO()),
|
||||||
})
|
})
|
||||||
@@ -23,5 +46,10 @@ func NewGorm(prefix, user, password, host, database string, port int) *gorm.DB {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return g
|
// 连接池设置
|
||||||
|
// sqlDB, _ := db.DB()
|
||||||
|
// sqlDB.SetMaxIdleConns(m.MaxIdleConns)
|
||||||
|
// sqlDB.SetMaxOpenConns(m.MaxOpenConns)
|
||||||
|
|
||||||
|
return db
|
||||||
}
|
}
|
||||||
|
|||||||
+57
@@ -0,0 +1,57 @@
|
|||||||
|
package gormx
|
||||||
|
|
||||||
|
type clientOptions struct {
|
||||||
|
Db string
|
||||||
|
Mysql *MysqlOptions
|
||||||
|
Sqlite *SqliteOptions
|
||||||
|
Prefix string // 表名前缀,`Article` 的表名应该是 `it_articles`
|
||||||
|
SingularTable bool // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
|
||||||
|
}
|
||||||
|
|
||||||
|
type MysqlOptions struct {
|
||||||
|
User string
|
||||||
|
Password string
|
||||||
|
Host string
|
||||||
|
Port int
|
||||||
|
Database string
|
||||||
|
}
|
||||||
|
|
||||||
|
type SqliteOptions struct {
|
||||||
|
DbPath string
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultOptions() clientOptions {
|
||||||
|
return clientOptions{
|
||||||
|
SingularTable: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(*clientOptions)
|
||||||
|
|
||||||
|
func SetMysql(user, password, host, database string, port int) Option {
|
||||||
|
return func(o *clientOptions) {
|
||||||
|
o.Db = "mysql"
|
||||||
|
o.Mysql = &MysqlOptions{
|
||||||
|
User: user,
|
||||||
|
Host: host,
|
||||||
|
Port: port,
|
||||||
|
Database: database,
|
||||||
|
Password: password,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetSqlite(dbPath string) Option {
|
||||||
|
return func(o *clientOptions) {
|
||||||
|
o.Db = "sqlite"
|
||||||
|
o.Sqlite = &SqliteOptions{
|
||||||
|
DbPath: dbPath,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetTablePrefix(perfix string) Option {
|
||||||
|
return func(o *clientOptions) {
|
||||||
|
o.Prefix = perfix
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user