2024-01-10 23:08:32 +08:00
|
|
|
package gormx
|
|
|
|
|
|
|
|
|
|
type clientOptions struct {
|
|
|
|
|
Db string
|
|
|
|
|
Mysql *MysqlOptions
|
|
|
|
|
Sqlite *SqliteOptions
|
|
|
|
|
Prefix string // 表名前缀,`Article` 的表名应该是 `it_articles`
|
2024-03-14 23:12:37 +08:00
|
|
|
SingularTable bool // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
|
2024-10-12 17:49:14 +08:00
|
|
|
Loc string // 时区设置 "Local" "Asia/Shanghai"
|
2024-01-10 23:08:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
2024-10-12 17:49:14 +08:00
|
|
|
Loc: "Local",
|
2024-01-10 23:08:32 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
2024-03-14 23:12:37 +08:00
|
|
|
if o.Mysql.Port == 0 {
|
|
|
|
|
o.Mysql.Port = 3306
|
|
|
|
|
}
|
2024-01-10 23:08:32 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 23:12:37 +08:00
|
|
|
// 使用sqlite数据库
|
2024-01-10 23:08:32 +08:00
|
|
|
func SetSqlite(dbPath string) Option {
|
|
|
|
|
return func(o *clientOptions) {
|
|
|
|
|
o.Db = "sqlite"
|
|
|
|
|
o.Sqlite = &SqliteOptions{
|
|
|
|
|
DbPath: dbPath,
|
|
|
|
|
}
|
2024-03-14 23:12:37 +08:00
|
|
|
if o.Sqlite.DbPath == "" {
|
|
|
|
|
o.Sqlite.DbPath = "sqlite.db"
|
|
|
|
|
}
|
2024-01-10 23:08:32 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 23:12:37 +08:00
|
|
|
// 表名前缀
|
2024-01-10 23:08:32 +08:00
|
|
|
func SetTablePrefix(perfix string) Option {
|
|
|
|
|
return func(o *clientOptions) {
|
|
|
|
|
o.Prefix = perfix
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-12 17:49:14 +08:00
|
|
|
|
|
|
|
|
func SetLoc(loc string) Option {
|
|
|
|
|
return func(o *clientOptions) {
|
|
|
|
|
o.Loc = loc
|
|
|
|
|
}
|
|
|
|
|
}
|