优化参数初始化方式
This commit is contained in:
+57
@@ -0,0 +1,57 @@
|
||||
package gormx
|
||||
|
||||
type clientOptions struct {
|
||||
Db string
|
||||
Mysql *MysqlOptions
|
||||
Sqlite *SqliteOptions
|
||||
Prefix string // 表名前缀,`Article` 的表名应该是 `it_articles`
|
||||
SingularTable bool // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func SetSqlite(dbPath string) Option {
|
||||
return func(o *clientOptions) {
|
||||
o.Db = "sqlite"
|
||||
o.Sqlite = &SqliteOptions{
|
||||
DbPath: dbPath,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func SetTablePrefix(perfix string) Option {
|
||||
return func(o *clientOptions) {
|
||||
o.Prefix = perfix
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user