feat:完善入驻信息管理
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"SciencesServer/config"
|
||||
"SciencesServer/serve/orm/logic"
|
||||
"fmt"
|
||||
"log"
|
||||
@ -16,20 +15,18 @@ import (
|
||||
|
||||
var (
|
||||
orm *gorm.DB
|
||||
|
||||
engines = map[string]func() logic.IEngine{
|
||||
"mysql": mysql, "sqlite": sqlite,
|
||||
}
|
||||
)
|
||||
|
||||
type Instance struct {
|
||||
Engine *gorm.DB
|
||||
|
||||
debug bool
|
||||
dbMode string
|
||||
tablePrefix string
|
||||
singularTable bool
|
||||
maxIdleConns, maxOpenConns, maxLifetime int
|
||||
*logic.Mysql
|
||||
*logic.Sqlite
|
||||
mysql *logic.Mysql
|
||||
sqlite *logic.Sqlite
|
||||
}
|
||||
|
||||
type Option func(instance *Instance)
|
||||
@ -76,29 +73,36 @@ func WithMaxLifetime(maxLifetime int) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func WithMysqlOption(user string) Option {
|
||||
func WithMysqlOption(option *logic.Mysql) Option {
|
||||
return func(instance *Instance) {
|
||||
instance.Mysql.User = user
|
||||
instance.mysql = option
|
||||
}
|
||||
}
|
||||
|
||||
func WithSqliteOption(user string) Option {
|
||||
func WithSqliteOption(option *logic.Sqlite) Option {
|
||||
return func(instance *Instance) {
|
||||
instance.Mysql.User = user
|
||||
instance.sqlite = option
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Instance) Init() {
|
||||
handle, has := engines[this.dbMode]
|
||||
func (this *Instance) Init() *Instance {
|
||||
var engine logic.IEngine
|
||||
|
||||
if !has {
|
||||
panic(fmt.Sprintf("Unknown Engine Mode:%d", config.SettingInfo.Engine.DBMode))
|
||||
switch this.dbMode {
|
||||
case "mysql":
|
||||
engine = this.mysql
|
||||
break
|
||||
case "sqlite":
|
||||
engine = this.sqlite
|
||||
break
|
||||
default:
|
||||
panic(fmt.Sprintf("Unknown Engine Mode:%d", this.dbMode))
|
||||
}
|
||||
option := &gorm.Config{
|
||||
DisableForeignKeyConstraintWhenMigrating: true,
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: config.SettingInfo.Engine.TablePrefix,
|
||||
SingularTable: !config.SettingInfo.Engine.Complex,
|
||||
TablePrefix: this.tablePrefix,
|
||||
SingularTable: this.singularTable,
|
||||
},
|
||||
}
|
||||
if this.debug {
|
||||
@ -112,17 +116,23 @@ func (this *Instance) Init() {
|
||||
},
|
||||
)
|
||||
}
|
||||
db, err := gorm.Open(handle().DSN(), option)
|
||||
db, err := gorm.Open(engine.DSN(), option)
|
||||
|
||||
if err != nil {
|
||||
panic("Orm Open Error:" + err.Error())
|
||||
panic("Orm Init Error:" + err.Error())
|
||||
}
|
||||
_db, _ := db.DB()
|
||||
_db.SetMaxIdleConns(config.SettingInfo.Engine.MaxIdleConns)
|
||||
_db.SetMaxOpenConns(config.SettingInfo.Engine.MaxOpenConns)
|
||||
_db.SetConnMaxLifetime(time.Duration(config.SettingInfo.Engine.MaxLifetime) * time.Second)
|
||||
_db.SetMaxIdleConns(this.maxIdleConns)
|
||||
_db.SetMaxOpenConns(this.maxOpenConns)
|
||||
_db.SetConnMaxLifetime(time.Duration(this.maxLifetime) * time.Second)
|
||||
|
||||
orm = db
|
||||
this.Engine = db
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Instance) Local() *Instance {
|
||||
orm = this.Engine
|
||||
return this
|
||||
}
|
||||
|
||||
func NewInstance(option ...Option) *Instance {
|
||||
@ -134,18 +144,6 @@ func NewInstance(option ...Option) *Instance {
|
||||
return instance
|
||||
}
|
||||
|
||||
func mysql() logic.IEngine {
|
||||
return &logic.Mysql{
|
||||
User: config.SettingInfo.Engine.Mysql.User, Password: config.SettingInfo.Engine.Mysql.Password,
|
||||
Host: config.SettingInfo.Engine.Mysql.Host, Port: config.SettingInfo.Engine.Mysql.Port,
|
||||
DBName: config.SettingInfo.Engine.Mysql.DBName, Parameters: config.SettingInfo.Engine.Mysql.Parameters,
|
||||
}
|
||||
}
|
||||
|
||||
func sqlite() logic.IEngine {
|
||||
return &logic.Sqlite{Path: config.SettingInfo.Engine.Sqlite.Path, Name: config.SettingInfo.Engine.Sqlite.Name}
|
||||
}
|
||||
|
||||
func GetDB() *gorm.DB {
|
||||
return orm
|
||||
}
|
||||
|
Reference in New Issue
Block a user