init
This commit is contained in:
7
serve/orm/logic/engine.go
Normal file
7
serve/orm/logic/engine.go
Normal file
@ -0,0 +1,7 @@
|
||||
package logic
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type IEngine interface {
|
||||
DSN() gorm.Dialector
|
||||
}
|
20
serve/orm/logic/mysql.go
Normal file
20
serve/orm/logic/mysql.go
Normal file
@ -0,0 +1,20 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Mysql struct {
|
||||
User, Password, Host string
|
||||
Port int
|
||||
DBName, Parameters string
|
||||
}
|
||||
|
||||
func (this *Mysql) DSN() gorm.Dialector {
|
||||
return mysql.Open(fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?%s",
|
||||
this.User, this.Password, this.Host, this.Port, this.DBName, this.Parameters,
|
||||
))
|
||||
}
|
19
serve/orm/logic/sqlite.go
Normal file
19
serve/orm/logic/sqlite.go
Normal file
@ -0,0 +1,19 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"github.com/belief428/gorm-engine/tools"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Sqlite struct {
|
||||
Path string
|
||||
Name string
|
||||
}
|
||||
|
||||
func (this *Sqlite) DSN() gorm.Dialector {
|
||||
if isExist, _ := tools.PathExists(this.Path); !isExist {
|
||||
_ = tools.MkdirAll(this.Path)
|
||||
}
|
||||
return sqlite.Open(this.Path + "/" + this.Name)
|
||||
}
|
79
serve/orm/orm.go
Normal file
79
serve/orm/orm.go
Normal file
@ -0,0 +1,79 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"Edu/config"
|
||||
"Edu/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,
|
||||
}
|
||||
)
|
||||
|
||||
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 Init() {
|
||||
handle, has := engines[config.SettingInfo.Engine.DBMode]
|
||||
|
||||
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,
|
||||
},
|
||||
}
|
||||
if config.SettingInfo.Engine.Debug {
|
||||
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
|
||||
}
|
||||
|
||||
func GetDB() *gorm.DB {
|
||||
if _, err := orm.DB(); err != nil {
|
||||
Init()
|
||||
}
|
||||
return orm
|
||||
}
|
Reference in New Issue
Block a user