diff --git a/go.mod b/go.mod index 4a64b72..cb031da 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/model.go b/model.go index 3f674b3..454098f 100644 --- a/model.go +++ b/model.go @@ -7,43 +7,44 @@ 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 ( - defaultDb *database.Config + defaultDb *database.Config defaultDsn string defaultDbDebug bool defaultGormDB *gorm.DB 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 @@ -158,45 +159,45 @@ func OptOpenDefaultRedis(m *Model) { return } -// Init init default database config & redis config -func Init(defaultDb *database.Config, defaultRds *redis.Config){ - defaultOptions = []Options{} +// Init init default database config & redis config +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) + } }