2021-09-28 11:47:19 +08:00
|
|
|
|
package orm
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"SciencesServer/config"
|
|
|
|
|
"SciencesServer/serve/orm/logic"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm/logger"
|
|
|
|
|
"gorm.io/gorm/schema"
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
orm *gorm.DB
|
|
|
|
|
|
|
|
|
|
engines = map[string]func() logic.IEngine{
|
|
|
|
|
"mysql": mysql, "sqlite": sqlite,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2021-12-03 11:32:26 +08:00
|
|
|
|
type Instance struct {
|
2021-12-03 15:22:23 +08:00
|
|
|
|
debug bool
|
|
|
|
|
dbMode string
|
|
|
|
|
tablePrefix string
|
|
|
|
|
singularTable bool
|
|
|
|
|
maxIdleConns, maxOpenConns, maxLifetime int
|
2021-12-03 11:32:26 +08:00
|
|
|
|
*logic.Mysql
|
|
|
|
|
*logic.Sqlite
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Option func(instance *Instance)
|
|
|
|
|
|
|
|
|
|
func WithDebug(debug bool) Option {
|
|
|
|
|
return func(instance *Instance) {
|
|
|
|
|
instance.debug = debug
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 11:32:26 +08:00
|
|
|
|
func WithDBMode(dbMode string) Option {
|
|
|
|
|
return func(instance *Instance) {
|
|
|
|
|
instance.dbMode = dbMode
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 15:22:23 +08:00
|
|
|
|
func WithTablePrefix(tablePrefix string) Option {
|
2021-12-03 11:32:26 +08:00
|
|
|
|
return func(instance *Instance) {
|
2021-12-03 15:22:23 +08:00
|
|
|
|
instance.tablePrefix = tablePrefix
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithSingularTable(singularTable bool) Option {
|
|
|
|
|
return func(instance *Instance) {
|
|
|
|
|
instance.singularTable = singularTable
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithMaxIdleConns(maxIdleConns int) Option {
|
|
|
|
|
return func(instance *Instance) {
|
|
|
|
|
instance.maxIdleConns = maxIdleConns
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithMaxOpenConns(maxOpenConns int) Option {
|
|
|
|
|
return func(instance *Instance) {
|
|
|
|
|
instance.maxOpenConns = maxOpenConns
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithMaxLifetime(maxLifetime int) Option {
|
|
|
|
|
return func(instance *Instance) {
|
|
|
|
|
instance.maxLifetime = maxLifetime
|
2021-12-03 11:32:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 15:22:23 +08:00
|
|
|
|
func WithMysqlOption(user string) Option {
|
|
|
|
|
return func(instance *Instance) {
|
|
|
|
|
instance.Mysql.User = user
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-03 11:32:26 +08:00
|
|
|
|
|
2021-12-03 15:22:23 +08:00
|
|
|
|
func WithSqliteOption(user string) Option {
|
|
|
|
|
return func(instance *Instance) {
|
|
|
|
|
instance.Mysql.User = user
|
|
|
|
|
}
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 11:32:26 +08:00
|
|
|
|
func (this *Instance) Init() {
|
|
|
|
|
handle, has := engines[this.dbMode]
|
2021-09-28 11:47:19 +08:00
|
|
|
|
|
|
|
|
|
if !has {
|
|
|
|
|
panic(fmt.Sprintf("Unknown Engine Mode:%d", config.SettingInfo.Engine.DBMode))
|
|
|
|
|
}
|
|
|
|
|
option := &gorm.Config{
|
|
|
|
|
DisableForeignKeyConstraintWhenMigrating: true,
|
|
|
|
|
NamingStrategy: schema.NamingStrategy{
|
|
|
|
|
TablePrefix: config.SettingInfo.Engine.TablePrefix,
|
|
|
|
|
SingularTable: !config.SettingInfo.Engine.Complex,
|
|
|
|
|
},
|
|
|
|
|
}
|
2021-12-03 11:32:26 +08:00
|
|
|
|
if this.debug {
|
2021-09-28 11:47:19 +08:00
|
|
|
|
option.Logger = logger.New(
|
|
|
|
|
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
|
|
|
|
logger.Config{
|
|
|
|
|
SlowThreshold: time.Second,
|
|
|
|
|
LogLevel: logger.Info,
|
|
|
|
|
Colorful: false,
|
|
|
|
|
IgnoreRecordNotFoundError: true,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
db, err := gorm.Open(handle().DSN(), option)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic("Orm Open 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)
|
|
|
|
|
|
|
|
|
|
orm = db
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 11:32:26 +08:00
|
|
|
|
func NewInstance(option ...Option) *Instance {
|
|
|
|
|
instance := new(Instance)
|
|
|
|
|
|
|
|
|
|
for _, v := range option {
|
|
|
|
|
v(instance)
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
2021-12-03 11:32:26 +08:00
|
|
|
|
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 {
|
2021-09-28 11:47:19 +08:00
|
|
|
|
return orm
|
|
|
|
|
}
|