This commit is contained in:
bryanqiu 2024-06-28 16:17:01 +08:00
parent 5952c1103b
commit cb1e36d005
2 changed files with 115 additions and 91 deletions

23
go.mod
View File

@ -1,3 +1,26 @@
module qoobing.com/gomod/model module qoobing.com/gomod/model
go 1.19.2 go 1.19.2
require (
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.10
qoobing.com/gomod/database v0.0.0-20240627111018-316f516e9b69
qoobing.com/gomod/log v1.2.8
qoobing.com/gomod/redis v1.3.4
)
require (
github.com/gomodule/redigo v1.9.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/tylerb/gls v0.0.0-20150407001822-e606233f194d // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.14.0 // indirect
qoobing.com/gomod/str v1.0.5 // indirect
)

183
model.go
View File

@ -7,43 +7,44 @@ import (
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
"qoobing.com/gomod/database"
"qoobing.com/gomod/log" "qoobing.com/gomod/log"
"qoobing.com/gomod/redis" "qoobing.com/gomod/redis"
"qoobing.com/gomod/database" "qoobing.com/gomod/redis/sentinel"
) )
type Model struct { type Model struct {
DB *gorm.DB DB *gorm.DB
DbTxStatus DbTxStatus DbTxStatus DbTxStatus
Redis redis.Conn Redis redis.Conn
RedisPool *redis.Pool RedisPool *redis.Pool
} }
type DbTxStatus int type DbTxStatus int
const ( const (
DBTX_STATUS_TX_NONE DbTxStatus = 0 DBTX_STATUS_TX_NONE DbTxStatus = 0
DBTX_STATUS_TX_DOING DbTxStatus = 1 DBTX_STATUS_TX_DOING DbTxStatus = 1
DBTX_STATUS_TX_SUCCESS DbTxStatus = 2 DBTX_STATUS_TX_SUCCESS DbTxStatus = 2
DBTX_STATUS_TX_FAILED DbTxStatus = 3 DBTX_STATUS_TX_FAILED DbTxStatus = 3
) )
var ( var (
defaultDb *database.Config defaultDb *database.Config
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
) )
func NewModel() *Model { func NewModel() *Model {
if len(defaultOptions) == 0 { if len(defaultOptions) == 0 {
panic("No defualt Database&Redis been configed") panic("No defualt Database&Redis been configed")
} }
return NewModelWithOption(defaultOptions...) return NewModelWithOption(defaultOptions...)
} }
func NewModelDefault() *Model { func NewModelDefault() *Model {
@ -59,52 +60,52 @@ 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
} }
func (m *Model) Close() { func (m *Model) Close() {
if m.Redis != nil { if m.Redis != nil {
m.Redis.Close() m.Redis.Close()
} }
if m.DB == nil { if m.DB == nil {
m.DbTxStatus = DBTX_STATUS_TX_NONE m.DbTxStatus = DBTX_STATUS_TX_NONE
} else if m.DbTxStatus == DBTX_STATUS_TX_DOING { } else if m.DbTxStatus == DBTX_STATUS_TX_DOING {
m.DB.Rollback() m.DB.Rollback()
m.DbTxStatus = DBTX_STATUS_TX_FAILED m.DbTxStatus = DBTX_STATUS_TX_FAILED
} }
} }
func (m *Model) Begin() { func (m *Model) Begin() {
if m.DB == nil { if m.DB == nil {
panic("unreachable code, m.DB is uninitialized") panic("unreachable code, m.DB is uninitialized")
} else if m.DbTxStatus != DBTX_STATUS_TX_NONE { } else if m.DbTxStatus != DBTX_STATUS_TX_NONE {
panic("unreachable code, begin transaction towice???") panic("unreachable code, begin transaction towice???")
} else { } else {
m.DbTxStatus = DBTX_STATUS_TX_DOING m.DbTxStatus = DBTX_STATUS_TX_DOING
m.DB = m.DB.Begin() m.DB = m.DB.Begin()
} }
} }
func (m *Model) Commit() error { func (m *Model) Commit() error {
if m.DB == nil { if m.DB == nil {
panic("unreachable code, m.DB is uninitialized") panic("unreachable code, m.DB is uninitialized")
} else if m.DbTxStatus != DBTX_STATUS_TX_DOING { } else if m.DbTxStatus != DBTX_STATUS_TX_DOING {
return nil return nil
} else if err := m.DB.Commit().Error; err != nil { } else if err := m.DB.Commit().Error; err != nil {
m.DbTxStatus = DBTX_STATUS_TX_FAILED m.DbTxStatus = DBTX_STATUS_TX_FAILED
return err return err
} else { } else {
m.DbTxStatus = DBTX_STATUS_TX_SUCCESS m.DbTxStatus = DBTX_STATUS_TX_SUCCESS
return nil return nil
} }
} }
// Option is model's option // Option is model's option
@ -158,45 +159,45 @@ func OptOpenDefaultRedis(m *Model) {
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) {
defaultOptions = []Options{} defaultOptions = []Options{}
if defaultDb { if defaultDb {
// database init // database init
arrConfStr := []string{ arrConfStr := []string{
"host=" + defaultDb.Host, "host=" + defaultDb.Host,
"port=" + strconv.Itoa(defaultDb.Port), "port=" + strconv.Itoa(defaultDb.Port),
"user=" + defaultDb.Username, "user=" + defaultDb.Username,
"password=" + defaultDb.Password, "password=" + defaultDb.Password,
"dbname=" + defaultDb.Dbname, "dbname=" + defaultDb.Dbname,
defaultDb.ExtraParameters, defaultDb.ExtraParameters,
} }
defaultDsn = strings.Join(arrConfStr, " ") 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.Debugf("defaultDsn='%s'", secDefaultDsn)
} }
defaultOptions = append(defaultOptions, OptOpenDefaultDatabase) defaultOptions = append(defaultOptions, OptOpenDefaultDatabase)
} }
if defaultRds != nil { if defaultRds != nil {
// redis init // redis init
defaultRds = coreRedis defaultRds = coreRedis
defaultRedisDebug = coreRedis.Debug defaultRedisDebug = coreRedis.Debug
// redis debug // redis debug
if defaultRedisDebug && len(coreRedis.Password) > 5 { if defaultRedisDebug && len(coreRedis.Password) > 5 {
p := coreRedis.Password p := coreRedis.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)
} }
defaultOptions = append(defaultOptions, OptOpenDefaultRedis) defaultOptions = append(defaultOptions, OptOpenDefaultRedis)
} }
} }