This commit is contained in:
bryanqiu 2024-07-01 13:39:31 +08:00
parent 52d8b4bb88
commit 1151bf1156

View File

@ -1,10 +1,10 @@
package model
import (
"strconv"
"strings"
"time"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"qoobing.com/gomod/database"
@ -34,7 +34,7 @@ var (
defaultDsn string
defaultDbDebug bool
defaultGormDB *gorm.DB
defaultRds redis.Config
defaultRds *redis.Config
defaultRedis *redis.Pool
defaultRedisDebug bool
defaultOptions []Option
@ -62,12 +62,12 @@ func NewModelDefaultDatabase() *Model {
func NewModelWithOption(options ...Option) *Model {
n := 0
m := Model{}
log.Debugf("Start NewModelWithOption...")
//log.Debugf("Start NewModelWithOption...")
for _, option := range options {
n++
option(&m)
}
log.Debugf("Finish NewModelWithOption(with %d options)", n)
//log.Debugf("Finish NewModelWithOption(with %d options)", n)
return &m
}
@ -113,15 +113,50 @@ type Option func(*Model)
// OptOpenDefaultDatabase option function for open default database
func OptOpenDefaultDatabase(m *Model) {
var err error
if defaultDb == nil {
panic("defaultDb not init")
}
if defaultGormDB != nil {
m.DB = defaultGormDB
} else if db, err := gorm.Open(
postgres.New(postgres.Config{
//TODO: check cocurrent
m.DB = m.DB.Session(&gorm.Session{QueryFields: true})
//log.Debugf("Opt for open default database done")
return
}
var dialector gorm.Dialector
var gormconfig = &gorm.Config{
NowFunc: func() time.Time {
return time.Now().UTC()
},
}
switch defaultDb.Type {
case "mysql":
dialector = mysql.New(
mysql.Config{
DSN: defaultDsn,
DefaultStringSize: 512,
},
)
case "pgsql":
dialector = postgres.New(
postgres.Config{
DSN: defaultDsn,
PreferSimpleProtocol: true,
}), &gorm.Config{}); err != nil {
},
)
default:
panic("UNKNOWN DATABASE TYPE:" + defaultDb.Type)
}
var db *gorm.DB
db, err = gorm.Open(dialector, gormconfig)
if err != nil {
panic("DATABASE_OPEN_ERROR")
} else {
}
if defaultDbDebug {
m.DB = db.Debug()
} else {
@ -135,10 +170,10 @@ func OptOpenDefaultDatabase(m *Model) {
sqlDB.SetConnMaxLifetime(time.Minute)
}
defaultGormDB = m.DB
}
//TODO: check cocurrent
m.DB = m.DB.Session(&gorm.Session{QueryFields: true})
log.Debugf("Opt for open default database done")
//log.Debugf("Opt for open default database done")
return
}
@ -147,7 +182,7 @@ func OptOpenDefaultRedis(m *Model) {
if defaultRedis != nil {
m.Redis = defaultRedis.Get()
m.RedisPool = defaultRedis
} else if pool := sentinel.NewPool(defaultRds); pool == nil {
} else if pool := sentinel.NewPool(*defaultRds); pool == nil {
panic("REDIS_ERROR")
} else {
defaultRedis = pool
@ -155,32 +190,27 @@ func OptOpenDefaultRedis(m *Model) {
m.RedisPool = defaultRedis
}
log.Debugf("Opt for open default redis done")
//log.Debugf("Opt for open default redis done")
return
}
// Init init default database config & redis config
func Init(defaultDb *database.Config, defaultRds *redis.Config) {
func Init(_defaultDb *database.Config, _defaultRds *redis.Config) {
defaultDb = _defaultDb
defaultRds = _defaultRds
defaultOptions = []Option{}
if defaultDb != nil{
if defaultDb != nil {
// database init
arrConfStr := []string{
"host=" + defaultDb.Host,
"port=" + strconv.Itoa(defaultDb.Port),
"user=" + defaultDb.Username,
"password=" + defaultDb.Password,
"dbname=" + defaultDb.Dbname,
defaultDb.ExtraParameters,
}
defaultDsn = strings.Join(arrConfStr, " ")
defaultDsn = database.GetDsn(defaultDb)
defaultDbDebug = defaultDb.Debug
// database debug
if defaultDbDebug && len(defaultDb.Password) > 5 {
p := defaultDb.Password
l := len(p)
secDefaultDsn := strings.Replace(defaultDsn, p[2:l-3], "*****", 1)
log.Debugf("defaultDsn='%s'", secDefaultDsn)
log.Infof("defaultDsn='%s'", secDefaultDsn)
}
defaultOptions = append(defaultOptions, OptOpenDefaultDatabase)
@ -190,11 +220,12 @@ func Init(defaultDb *database.Config, defaultRds *redis.Config) {
// redis init
defaultRds = defaultRds
defaultRedisDebug = defaultRds.Debug
// redis debug
if defaultRedisDebug && len(defaultRds.Password) > 5 {
p := defaultRds.Password
l := len(p)
secDefaultRds := defaultRds
secDefaultRds := *defaultRds
secDefaultRds.Password = strings.Replace(p, p[2:l-3], "*****", 1)
log.PrintPretty("defaultRds=", secDefaultRds)
}