model/model.go

204 lines
4.7 KiB
Go
Raw Normal View History

2024-06-27 10:56:52 +00:00
package model
import (
"strconv"
"strings"
"time"
"gorm.io/driver/postgres"
"gorm.io/gorm"
2024-06-28 08:17:01 +00:00
"qoobing.com/gomod/database"
2024-06-27 10:56:52 +00:00
"qoobing.com/gomod/log"
"qoobing.com/gomod/redis"
2024-06-28 08:17:01 +00:00
"qoobing.com/gomod/redis/sentinel"
2024-06-27 10:56:52 +00:00
)
type Model struct {
2024-06-28 08:17:01 +00:00
DB *gorm.DB
DbTxStatus DbTxStatus
Redis redis.Conn
RedisPool *redis.Pool
2024-06-27 10:56:52 +00:00
}
type DbTxStatus int
2024-06-28 08:17:01 +00:00
2024-06-27 10:56:52 +00:00
const (
2024-06-28 08:17:01 +00:00
DBTX_STATUS_TX_NONE DbTxStatus = 0
DBTX_STATUS_TX_DOING DbTxStatus = 1
DBTX_STATUS_TX_SUCCESS DbTxStatus = 2
DBTX_STATUS_TX_FAILED DbTxStatus = 3
2024-06-27 10:56:52 +00:00
)
var (
2024-06-28 08:17:01 +00:00
defaultDb *database.Config
2024-06-27 10:56:52 +00:00
defaultDsn string
defaultDbDebug bool
defaultGormDB *gorm.DB
defaultRds redis.Config
defaultRedis *redis.Pool
defaultRedisDebug bool
2024-06-28 08:17:01 +00:00
defaultOptions []Option
2024-06-27 10:56:52 +00:00
)
func NewModel() *Model {
2024-06-28 08:17:01 +00:00
if len(defaultOptions) == 0 {
panic("No defualt Database&Redis been configed")
}
return NewModelWithOption(defaultOptions...)
2024-06-27 10:56:52 +00:00
}
func NewModelDefault() *Model {
2024-06-28 09:13:34 +00:00
return NewModelWithOption(OptOpenDefaultDatabase, OptOpenDefaultRedis)
2024-06-27 10:56:52 +00:00
}
func NewModelDefaultRedis() *Model {
2024-06-28 09:13:34 +00:00
return NewModelWithOption(OptOpenDefaultRedis)
2024-06-27 10:56:52 +00:00
}
func NewModelDefaultDatabase() *Model {
2024-06-28 09:13:34 +00:00
return NewModelWithOption(OptOpenDefaultDatabase)
2024-06-27 10:56:52 +00:00
}
func NewModelWithOption(options ...Option) *Model {
2024-06-28 08:17:01 +00:00
n := 0
m := Model{}
log.Debugf("Start NewModelWithOption...")
for _, option := range options {
n++
option(&m)
}
log.Debugf("Finish NewModelWithOption(with %d options)", n)
return &m
2024-06-27 10:56:52 +00:00
}
func (m *Model) Close() {
2024-06-28 08:17:01 +00:00
if m.Redis != nil {
m.Redis.Close()
}
if m.DB == nil {
m.DbTxStatus = DBTX_STATUS_TX_NONE
} else if m.DbTxStatus == DBTX_STATUS_TX_DOING {
m.DB.Rollback()
m.DbTxStatus = DBTX_STATUS_TX_FAILED
}
2024-06-27 10:56:52 +00:00
}
func (m *Model) Begin() {
2024-06-28 08:17:01 +00:00
if m.DB == nil {
panic("unreachable code, m.DB is uninitialized")
} else if m.DbTxStatus != DBTX_STATUS_TX_NONE {
panic("unreachable code, begin transaction towice???")
} else {
m.DbTxStatus = DBTX_STATUS_TX_DOING
m.DB = m.DB.Begin()
}
2024-06-27 10:56:52 +00:00
}
func (m *Model) Commit() error {
2024-06-28 08:17:01 +00:00
if m.DB == nil {
panic("unreachable code, m.DB is uninitialized")
} else if m.DbTxStatus != DBTX_STATUS_TX_DOING {
return nil
} else if err := m.DB.Commit().Error; err != nil {
m.DbTxStatus = DBTX_STATUS_TX_FAILED
return err
} else {
m.DbTxStatus = DBTX_STATUS_TX_SUCCESS
return nil
}
2024-06-27 10:56:52 +00:00
}
// Option is model's option
type Option func(*Model)
// OptOpenDefaultDatabase option function for open default database
func OptOpenDefaultDatabase(m *Model) {
if defaultGormDB != nil {
m.DB = defaultGormDB
} else if db, err := gorm.Open(
postgres.New(postgres.Config{
DSN: defaultDsn,
PreferSimpleProtocol: true,
}), &gorm.Config{}); err != nil {
panic("DATABASE_OPEN_ERROR")
} else {
if defaultDbDebug {
m.DB = db.Debug()
} else {
m.DB = db
}
if sqlDB, err := m.DB.DB(); err != nil {
panic("DATABASE_OPEN_ERROR")
} else {
sqlDB.SetMaxIdleConns(3)
sqlDB.SetMaxOpenConns(10)
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")
return
}
// OptOpenDefaultRedisSentinelPool option function for open default redis sentinel
func OptOpenDefaultRedis(m *Model) {
if defaultRedis != nil {
m.Redis = defaultRedis.Get()
m.RedisPool = defaultRedis
} else if pool := sentinel.NewPool(defaultRds); pool == nil {
panic("REDIS_ERROR")
} else {
defaultRedis = pool
m.Redis = defaultRedis.Get()
m.RedisPool = defaultRedis
}
log.Debugf("Opt for open default redis done")
return
}
2024-06-28 08:17:01 +00:00
// Init init default database config & redis config
func Init(defaultDb *database.Config, defaultRds *redis.Config) {
2024-06-28 09:13:34 +00:00
defaultOptions = []Option{}
2024-06-28 08:17:01 +00:00
2024-06-28 09:13:34 +00:00
if defaultDb != nil{
2024-06-28 08:17:01 +00:00
// 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, " ")
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)
}
defaultOptions = append(defaultOptions, OptOpenDefaultDatabase)
}
if defaultRds != nil {
// redis init
2024-06-28 09:13:34 +00:00
defaultRds = defaultRds
defaultRedisDebug = defaultRds.Debug
2024-06-28 08:17:01 +00:00
// redis debug
2024-06-28 09:13:34 +00:00
if defaultRedisDebug && len(defaultRds.Password) > 5 {
p := defaultRds.Password
2024-06-28 08:17:01 +00:00
l := len(p)
secDefaultRds := defaultRds
secDefaultRds.Password = strings.Replace(p, p[2:l-3], "*****", 1)
log.PrintPretty("defaultRds=", secDefaultRds)
}
defaultOptions = append(defaultOptions, OptOpenDefaultRedis)
}
2024-06-27 10:56:52 +00:00
}