优化一些配置
This commit is contained in:
@@ -25,12 +25,15 @@ func NewGorm(opts ...Option) *gorm.DB {
|
||||
panic("mysql is not supported")
|
||||
}
|
||||
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=%s",
|
||||
defaultOpts.Mysql.User,
|
||||
defaultOpts.Mysql.Password,
|
||||
defaultOpts.Mysql.Host,
|
||||
defaultOpts.Mysql.Port,
|
||||
defaultOpts.Mysql.Database)
|
||||
defaultOpts.Mysql.Database,
|
||||
defaultOpts.Charset,
|
||||
defaultOpts.Loc,
|
||||
)
|
||||
dialector = mysql.Open(dsn)
|
||||
|
||||
} else if defaultOpts.Db == "sqlite" {
|
||||
|
||||
+10
-1
@@ -6,6 +6,8 @@ type clientOptions struct {
|
||||
Sqlite *SqliteOptions
|
||||
Prefix string // 表名前缀,`Article` 的表名应该是 `it_articles`
|
||||
SingularTable bool // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
|
||||
Loc string // 使用时区
|
||||
Charset string // 使用utf8mb4编码
|
||||
}
|
||||
|
||||
type MysqlOptions struct {
|
||||
@@ -23,6 +25,8 @@ type SqliteOptions struct {
|
||||
func defaultOptions() clientOptions {
|
||||
return clientOptions{
|
||||
SingularTable: true,
|
||||
Loc: "Local", //
|
||||
Charset: "utf8mb4",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,10 +61,15 @@ func SetSqlite(dbPath string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 表名前缀
|
||||
func SetTablePrefix(perfix string) Option {
|
||||
return func(o *clientOptions) {
|
||||
o.Prefix = perfix
|
||||
}
|
||||
}
|
||||
|
||||
func SetLoc(loc string) Option {
|
||||
return func(o *clientOptions) {
|
||||
o.Loc = loc
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user