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