Files
gormx/options.go
T
2024-10-27 22:56:26 +08:00

76 lines
1.5 KiB
Go

package gormx
type clientOptions struct {
Db string
Mysql *MysqlOptions
Sqlite *SqliteOptions
Prefix string // 表名前缀,`Article` 的表名应该是 `it_articles`
SingularTable bool // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
Loc string // 使用时区
Charset string // 使用utf8mb4编码
}
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,
Loc: "Local", //
Charset: "utf8mb4",
}
}
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,
}
if o.Mysql.Port == 0 {
o.Mysql.Port = 3306
}
}
}
// 使用sqlite数据库
func SetSqlite(dbPath string) Option {
return func(o *clientOptions) {
o.Db = "sqlite"
o.Sqlite = &SqliteOptions{
DbPath: dbPath,
}
if o.Sqlite.DbPath == "" {
o.Sqlite.DbPath = "sqlite.db"
}
}
}
// 表名前缀
func SetTablePrefix(perfix string) Option {
return func(o *clientOptions) {
o.Prefix = perfix
}
}
func SetLoc(loc string) Option {
return func(o *clientOptions) {
o.Loc = loc
}
}