From f977e9adfdbb9c92e6e195f21c0f677be6467588 Mon Sep 17 00:00:00 2001 From: Yun Date: Sun, 27 Oct 2024 22:56:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=80=E4=BA=9B=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gormx.go | 7 +++++-- options.go | 11 ++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/gormx.go b/gormx.go index 8a6292a..49eabb4 100644 --- a/gormx.go +++ b/gormx.go @@ -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" { diff --git a/options.go b/options.go index a4720ae..5a61670 100644 --- a/options.go +++ b/options.go @@ -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 + } +}