2021-09-28 11:47:19 +08:00
|
|
|
|
package orm
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"SciencesServer/serve/orm/logic"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm/logger"
|
|
|
|
|
"gorm.io/gorm/schema"
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
orm *gorm.DB
|
|
|
|
|
)
|
|
|
|
|
|
2021-12-03 11:32:26 +08:00
|
|
|
|
type Instance struct {
|
2021-12-06 14:55:41 +08:00
|
|
|
|
Engine *gorm.DB
|
|
|
|
|
|
2021-12-03 15:22:23 +08:00
|
|
|
|
debug bool
|
|
|
|
|
dbMode string
|
|
|
|
|
tablePrefix string
|
|
|
|
|
singularTable bool
|
|
|
|
|
maxIdleConns, maxOpenConns, maxLifetime int
|
2021-12-06 14:55:41 +08:00
|
|
|
|
mysql *logic.Mysql
|
|
|
|
|
sqlite *logic.Sqlite
|
2021-12-03 11:32:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-06 14:55:41 +08:00
|
|
|
|
func WithMysqlOption(option *logic.Mysql) Option {
|
2021-12-03 15:22:23 +08:00
|
|
|
|
return func(instance *Instance) {
|
2021-12-06 14:55:41 +08:00
|
|
|
|
instance.mysql = option
|
2021-12-03 15:22:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-03 11:32:26 +08:00
|
|
|
|
|
2021-12-06 14:55:41 +08:00
|
|
|
|
func WithSqliteOption(option *logic.Sqlite) Option {
|
2021-12-03 15:22:23 +08:00
|
|
|
|
return func(instance *Instance) {
|
2021-12-06 14:55:41 +08:00
|
|
|
|
instance.sqlite = option
|
2021-12-03 15:22:23 +08:00
|
|
|
|
}
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 14:55:41 +08:00
|
|
|
|
func (this *Instance) Init() *Instance {
|
|
|
|
|
var engine logic.IEngine
|
2021-09-28 11:47:19 +08:00
|
|
|
|
|
2021-12-06 14:55:41 +08:00
|
|
|
|
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))
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
option := &gorm.Config{
|
|
|
|
|
DisableForeignKeyConstraintWhenMigrating: true,
|
|
|
|
|
NamingStrategy: schema.NamingStrategy{
|
2021-12-06 14:55:41 +08:00
|
|
|
|
TablePrefix: this.tablePrefix,
|
|
|
|
|
SingularTable: this.singularTable,
|
2021-09-28 11:47:19 +08:00
|
|
|
|
},
|
|
|
|
|
}
|
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,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
2021-12-06 14:55:41 +08:00
|
|
|
|
db, err := gorm.Open(engine.DSN(), option)
|
2021-09-28 11:47:19 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-12-06 14:55:41 +08:00
|
|
|
|
panic("Orm Init Error:" + err.Error())
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
_db, _ := db.DB()
|
2021-12-06 14:55:41 +08:00
|
|
|
|
_db.SetMaxIdleConns(this.maxIdleConns)
|
|
|
|
|
_db.SetMaxOpenConns(this.maxOpenConns)
|
|
|
|
|
_db.SetConnMaxLifetime(time.Duration(this.maxLifetime) * time.Second)
|
|
|
|
|
|
|
|
|
|
this.Engine = db
|
|
|
|
|
return this
|
|
|
|
|
}
|
2021-09-28 11:47:19 +08:00
|
|
|
|
|
2021-12-06 14:55:41 +08:00
|
|
|
|
func (this *Instance) Local() *Instance {
|
|
|
|
|
orm = this.Engine
|
|
|
|
|
return this
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
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 GetDB() *gorm.DB {
|
2021-09-28 11:47:19 +08:00
|
|
|
|
return orm
|
|
|
|
|
}
|