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
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
)

179
model.go
View File

@ -7,25 +7,26 @@ import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
"qoobing.com/gomod/database"
"qoobing.com/gomod/log"
"qoobing.com/gomod/redis"
"qoobing.com/gomod/database"
"qoobing.com/gomod/redis/sentinel"
)
type Model struct {
DB *gorm.DB
DbTxStatus DbTxStatus
Redis redis.Conn
RedisPool *redis.Pool
DB *gorm.DB
DbTxStatus DbTxStatus
Redis redis.Conn
RedisPool *redis.Pool
}
type DbTxStatus int
const (
DBTX_STATUS_TX_NONE DbTxStatus = 0
DBTX_STATUS_TX_DOING DbTxStatus = 1
DBTX_STATUS_TX_SUCCESS DbTxStatus = 2
DBTX_STATUS_TX_FAILED DbTxStatus = 3
DBTX_STATUS_TX_NONE DbTxStatus = 0
DBTX_STATUS_TX_DOING DbTxStatus = 1
DBTX_STATUS_TX_SUCCESS DbTxStatus = 2
DBTX_STATUS_TX_FAILED DbTxStatus = 3
)
var (
@ -36,14 +37,14 @@ var (
defaultRds redis.Config
defaultRedis *redis.Pool
defaultRedisDebug bool
defaultOptions []Option
defaultOptions []Option
)
func NewModel() *Model {
if len(defaultOptions) == 0 {
panic("No defualt Database&Redis been configed")
}
return NewModelWithOption(defaultOptions...)
if len(defaultOptions) == 0 {
panic("No defualt Database&Redis been configed")
}
return NewModelWithOption(defaultOptions...)
}
func NewModelDefault() *Model {
@ -59,52 +60,52 @@ func NewModelDefaultDatabase() *Model {
}
func NewModelWithOption(options ...Option) *Model {
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
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
}
func (m *Model) Close() {
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
}
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
}
}
func (m *Model) Begin() {
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()
}
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()
}
}
func (m *Model) Commit() error {
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
}
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
}
}
// Option is model's option
@ -159,44 +160,44 @@ func OptOpenDefaultRedis(m *Model) {
}
// Init init default database config & redis config
func Init(defaultDb *database.Config, defaultRds *redis.Config){
defaultOptions = []Options{}
func Init(defaultDb *database.Config, defaultRds *redis.Config) {
defaultOptions = []Options{}
if defaultDb {
// 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)
}
if defaultDb {
// 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)
}
defaultOptions = append(defaultOptions, OptOpenDefaultDatabase)
}
if defaultRds != nil {
// redis init
defaultRds = coreRedis
defaultRedisDebug = coreRedis.Debug
// redis debug
if defaultRedisDebug && len(coreRedis.Password) > 5 {
p := coreRedis.Password
l := len(p)
secDefaultRds := defaultRds
secDefaultRds.Password = strings.Replace(p, p[2:l-3], "*****", 1)
log.PrintPretty("defaultRds=", secDefaultRds)
}
defaultOptions = append(defaultOptions, OptOpenDefaultRedis)
}
if defaultRds != nil {
// redis init
defaultRds = coreRedis
defaultRedisDebug = coreRedis.Debug
// redis debug
if defaultRedisDebug && len(coreRedis.Password) > 5 {
p := coreRedis.Password
l := len(p)
secDefaultRds := defaultRds
secDefaultRds.Password = strings.Replace(p, p[2:l-3], "*****", 1)
log.PrintPretty("defaultRds=", secDefaultRds)
}
defaultOptions = append(defaultOptions, OptOpenDefaultRedis)
}
}