允许设置loc

This commit is contained in:
Yun
2024-10-12 17:49:14 +08:00
parent de24304065
commit 6ae7e4223e
2 changed files with 12 additions and 3 deletions
+4 -2
View File
@@ -25,12 +25,14 @@ func NewGorm(opts ...Option) *gorm.DB {
panic("mysql is not supported") 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=utf8mb4&parseTime=True&loc=%s",
defaultOpts.Mysql.User, defaultOpts.Mysql.User,
defaultOpts.Mysql.Password, defaultOpts.Mysql.Password,
defaultOpts.Mysql.Host, defaultOpts.Mysql.Host,
defaultOpts.Mysql.Port, defaultOpts.Mysql.Port,
defaultOpts.Mysql.Database) defaultOpts.Mysql.Database,
defaultOpts.Loc,
)
dialector = mysql.Open(dsn) dialector = mysql.Open(dsn)
} else if defaultOpts.Db == "sqlite" { } else if defaultOpts.Db == "sqlite" {
+8 -1
View File
@@ -6,6 +6,7 @@ type clientOptions struct {
Sqlite *SqliteOptions Sqlite *SqliteOptions
Prefix string // 表名前缀,`Article` 的表名应该是 `it_articles` Prefix string // 表名前缀,`Article` 的表名应该是 `it_articles`
SingularTable bool // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article` SingularTable bool // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
Loc string // 时区设置 "Local" "Asia/Shanghai"
} }
type MysqlOptions struct { type MysqlOptions struct {
@@ -23,6 +24,7 @@ type SqliteOptions struct {
func defaultOptions() clientOptions { func defaultOptions() clientOptions {
return clientOptions{ return clientOptions{
SingularTable: true, SingularTable: true,
Loc: "Local",
} }
} }
@@ -57,10 +59,15 @@ func SetSqlite(dbPath string) Option {
} }
} }
// 表名前缀 // 表名前缀
func SetTablePrefix(perfix string) Option { func SetTablePrefix(perfix string) Option {
return func(o *clientOptions) { return func(o *clientOptions) {
o.Prefix = perfix o.Prefix = perfix
} }
} }
func SetLoc(loc string) Option {
return func(o *clientOptions) {
o.Loc = loc
}
}