2024-01-10 23:08:32 +08:00
|
|
|
package gormx
|
|
|
|
|
|
2025-07-12 17:12:24 +08:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/yuninks/loggerx"
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-10 23:08:32 +08:00
|
|
|
type clientOptions struct {
|
|
|
|
|
Db string
|
|
|
|
|
Mysql *MysqlOptions
|
|
|
|
|
Sqlite *SqliteOptions
|
2025-07-12 17:12:24 +08:00
|
|
|
Prefix string // 表名前缀,`Article` 的表名应该是 `it_articles`
|
|
|
|
|
SingularTable bool // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
|
|
|
|
|
Charset string // 使用utf8mb4编码
|
|
|
|
|
Location *time.Location // 时区设置 "Local" "Asia/Shanghai"
|
|
|
|
|
Logger loggerx.LoggerInterface // 日志记录器
|
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,
|
2025-07-12 17:12:24 +08:00
|
|
|
Location: time.Local, // 默认本地
|
2024-10-27 22:56:26 +08:00
|
|
|
Charset: "utf8mb4",
|
2025-07-12 17:12:24 +08:00
|
|
|
Logger: loggerx.NewLogger(context.Background()),
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-12 17:12:24 +08:00
|
|
|
// 使用postgres数据库
|
|
|
|
|
func SetPostgres() Option {
|
|
|
|
|
return func(o *clientOptions) {
|
|
|
|
|
o.Db = "postgres"
|
|
|
|
|
panic("postgres is not supported yet")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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-27 22:56:26 +08:00
|
|
|
|
2025-07-12 17:12:24 +08:00
|
|
|
// 设置时区,默认是本地
|
|
|
|
|
func SetLocation(loc *time.Location) Option {
|
|
|
|
|
return func(o *clientOptions) {
|
|
|
|
|
o.Location = loc
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置日志记录器
|
|
|
|
|
func SetLogger(logger loggerx.LoggerInterface) Option {
|
2024-10-27 22:56:26 +08:00
|
|
|
return func(o *clientOptions) {
|
2025-07-12 17:12:24 +08:00
|
|
|
o.Logger = logger
|
2024-10-27 22:56:26 +08:00
|
|
|
}
|
|
|
|
|
}
|