MyDb change DB

This commit is contained in:
yedongfu 2021-06-10 10:34:39 +08:00
parent c6e6adbad5
commit cb2988cd6c
9 changed files with 33 additions and 33 deletions

View File

@ -17,20 +17,20 @@ type ModelBase struct {
UpdateTime *time.Time `gorm:"autoUpdateTime"` UpdateTime *time.Time `gorm:"autoUpdateTime"`
} }
var dbs = map[string]*MyDb{} var dbs = map[string]*DB{}
type MyDb struct { type DB struct {
*gorm.DB *gorm.DB
} }
func (m *MyDb) Must() *MyDb { func (m *DB) Must() *DB {
db := m.InstanceSet("ivy.must", true) db := m.InstanceSet("ivy.must", true)
return &MyDb{DB: db} return &DB{DB: db}
} }
func (m *MyDb) NoMust() *MyDb { func (m *DB) NoMust() *DB {
db := m.InstanceSet("ivy.must", false) db := m.InstanceSet("ivy.must", false)
return &MyDb{DB: db} return &DB{DB: db}
} }
type tracePlugin struct{} type tracePlugin struct{}
@ -86,7 +86,7 @@ func ReplaceDsnPassword(dsn string) string {
return reg.ReplaceAllString(dsn, ":****@") return reg.ReplaceAllString(dsn, ":****@")
} }
func DbGet(conf map[string]string) *MyDb { func DbGet(conf map[string]string) *DB {
dsn := GetDsn(conf) dsn := GetDsn(conf)
if dbs[dsn] == nil { if dbs[dsn] == nil {
logrus.Printf("connecting %s", ReplaceDsnPassword(dsn)) logrus.Printf("connecting %s", ReplaceDsnPassword(dsn))
@ -95,7 +95,7 @@ func DbGet(conf map[string]string) *MyDb {
}) })
E2P(err) E2P(err)
db1.Use(&tracePlugin{}) db1.Use(&tracePlugin{})
dbs[dsn] = &MyDb{DB: db1} dbs[dsn] = &DB{DB: db1}
} }
return dbs[dsn] return dbs[dsn]
} }
@ -110,7 +110,7 @@ func (conn *MyConn) Close() {
conn.Conn.Close() conn.Conn.Close()
} }
func DbAlone(conf map[string]string) (*MyDb, *MyConn) { func DbAlone(conf map[string]string) (*DB, *MyConn) {
dsn := GetDsn(conf) dsn := GetDsn(conf)
logrus.Printf("opening alone mysql: %s", ReplaceDsnPassword(dsn)) logrus.Printf("opening alone mysql: %s", ReplaceDsnPassword(dsn))
mdb, err := sql.Open("mysql", dsn) mdb, err := sql.Open("mysql", dsn)
@ -120,5 +120,5 @@ func DbAlone(conf map[string]string) (*MyDb, *MyConn) {
}), &gorm.Config{}) }), &gorm.Config{})
E2P(err) E2P(err)
gormDB.Use(&tracePlugin{}) gormDB.Use(&tracePlugin{})
return &MyDb{DB: gormDB}, &MyConn{Conn: mdb, Dsn: dsn} return &DB{DB: gormDB}, &MyConn{Conn: mdb, Dsn: dsn}
} }

View File

@ -30,16 +30,16 @@ func (*TransGlobal) TableName() string {
type TransProcessor interface { type TransProcessor interface {
GenBranches() []TransBranch GenBranches() []TransBranch
ProcessOnce(db *common.MyDb, branches []TransBranch) ProcessOnce(db *common.DB, branches []TransBranch)
ExecBranch(db *common.MyDb, branch *TransBranch) ExecBranch(db *common.DB, branch *TransBranch)
} }
func (t *TransGlobal) touch(db *common.MyDb) *gorm.DB { func (t *TransGlobal) touch(db *common.DB) *gorm.DB {
writeTransLog(t.Gid, "touch trans", "", "", "") writeTransLog(t.Gid, "touch trans", "", "", "")
return db.Model(&TransGlobal{}).Where("gid=?", t.Gid).Update("gid", t.Gid) // 更新update_time避免被定时任务再次 return db.Model(&TransGlobal{}).Where("gid=?", t.Gid).Update("gid", t.Gid) // 更新update_time避免被定时任务再次
} }
func (t *TransGlobal) changeStatus(db *common.MyDb, status string) *gorm.DB { func (t *TransGlobal) changeStatus(db *common.DB, status string) *gorm.DB {
writeTransLog(t.Gid, "change status", status, "", "") writeTransLog(t.Gid, "change status", status, "", "")
updates := M{ updates := M{
"status": status, "status": status,
@ -71,7 +71,7 @@ func (*TransBranch) TableName() string {
return "trans_branch" return "trans_branch"
} }
func (t *TransBranch) changeStatus(db *common.MyDb, status string) *gorm.DB { func (t *TransBranch) changeStatus(db *common.DB, status string) *gorm.DB {
writeTransLog(t.Gid, "branch change", status, t.Branch, "") writeTransLog(t.Gid, "branch change", status, t.Branch, "")
dbr := db.Must().Model(t).Where("status=?", t.Status).Updates(M{ dbr := db.Must().Model(t).Where("status=?", t.Status).Updates(M{
"status": status, "status": status,
@ -100,7 +100,7 @@ func (trans *TransGlobal) getProcessor() TransProcessor {
return processorFac[trans.TransType](trans) return processorFac[trans.TransType](trans)
} }
func (t *TransGlobal) MayQueryPrepared(db *common.MyDb) { func (t *TransGlobal) MayQueryPrepared(db *common.DB) {
if t.Status != "prepared" { if t.Status != "prepared" {
return return
} }
@ -121,7 +121,7 @@ func (t *TransGlobal) MayQueryPrepared(db *common.MyDb) {
} }
} }
func (trans *TransGlobal) Process(db *common.MyDb) { func (trans *TransGlobal) Process(db *common.DB) {
defer handlePanic() defer handlePanic()
defer func() { defer func() {
if TransProcessedTestChan != nil { if TransProcessedTestChan != nil {
@ -133,9 +133,9 @@ func (trans *TransGlobal) Process(db *common.MyDb) {
trans.getProcessor().ProcessOnce(db, branches) trans.getProcessor().ProcessOnce(db, branches)
} }
func (t *TransGlobal) SaveNew(db *common.MyDb) { func (t *TransGlobal) SaveNew(db *common.DB) {
err := db.Transaction(func(db1 *gorm.DB) error { err := db.Transaction(func(db1 *gorm.DB) error {
db := &common.MyDb{DB: db1} db := &common.DB{DB: db1}
writeTransLog(t.Gid, "create trans", t.Status, "", t.Data) writeTransLog(t.Gid, "create trans", t.Status, "", t.Data)
dbr := db.Must().Clauses(clause.OnConflict{ dbr := db.Must().Clauses(clause.OnConflict{
@ -174,7 +174,7 @@ func TransFromContext(c *gin.Context) *TransGlobal {
return &m return &m
} }
func TransFromDb(db *common.MyDb, gid string) *TransGlobal { func TransFromDb(db *common.DB, gid string) *TransGlobal {
m := TransGlobal{} m := TransGlobal{}
dbr := db.Must().Model(&m).Where("gid=?", gid).First(&m) dbr := db.Must().Model(&m).Where("gid=?", gid).First(&m)
e2p(dbr.Error) e2p(dbr.Error)

View File

@ -32,7 +32,7 @@ func (t *TransMsgProcessor) GenBranches() []TransBranch {
return branches return branches
} }
func (t *TransMsgProcessor) ExecBranch(db *common.MyDb, branch *TransBranch) { func (t *TransMsgProcessor) ExecBranch(db *common.DB, branch *TransBranch) {
resp, err := common.RestyClient.R().SetBody(branch.Data).SetQueryParam("gid", branch.Gid).Post(branch.Url) resp, err := common.RestyClient.R().SetBody(branch.Data).SetQueryParam("gid", branch.Gid).Post(branch.Url)
e2p(err) e2p(err)
body := resp.String() body := resp.String()
@ -44,7 +44,7 @@ func (t *TransMsgProcessor) ExecBranch(db *common.MyDb, branch *TransBranch) {
} }
} }
func (t *TransMsgProcessor) ProcessOnce(db *common.MyDb, branches []TransBranch) { func (t *TransMsgProcessor) ProcessOnce(db *common.DB, branches []TransBranch) {
t.MayQueryPrepared(db) t.MayQueryPrepared(db)
if t.Status != "committed" { if t.Status != "committed" {
return return

View File

@ -34,7 +34,7 @@ func (t *TransSagaProcessor) GenBranches() []TransBranch {
return branches return branches
} }
func (t *TransSagaProcessor) ExecBranch(db *common.MyDb, branch *TransBranch) { func (t *TransSagaProcessor) ExecBranch(db *common.DB, branch *TransBranch) {
resp, err := common.RestyClient.R().SetBody(branch.Data).SetQueryParam("gid", branch.Gid).Post(branch.Url) resp, err := common.RestyClient.R().SetBody(branch.Data).SetQueryParam("gid", branch.Gid).Post(branch.Url)
e2p(err) e2p(err)
body := resp.String() body := resp.String()
@ -48,7 +48,7 @@ func (t *TransSagaProcessor) ExecBranch(db *common.MyDb, branch *TransBranch) {
} }
} }
func (t *TransSagaProcessor) ProcessOnce(db *common.MyDb, branches []TransBranch) { func (t *TransSagaProcessor) ProcessOnce(db *common.DB, branches []TransBranch) {
t.MayQueryPrepared(db) t.MayQueryPrepared(db)
if t.Status != "committed" { if t.Status != "committed" {
return return

View File

@ -34,7 +34,7 @@ func (t *TransTccProcessor) GenBranches() []TransBranch {
return branches return branches
} }
func (t *TransTccProcessor) ExecBranch(db *common.MyDb, branch *TransBranch) { func (t *TransTccProcessor) ExecBranch(db *common.DB, branch *TransBranch) {
resp, err := common.RestyClient.R().SetBody(branch.Data).SetQueryParam("gid", branch.Gid).Post(branch.Url) resp, err := common.RestyClient.R().SetBody(branch.Data).SetQueryParam("gid", branch.Gid).Post(branch.Url)
e2p(err) e2p(err)
body := resp.String() body := resp.String()
@ -48,7 +48,7 @@ func (t *TransTccProcessor) ExecBranch(db *common.MyDb, branch *TransBranch) {
} }
} }
func (t *TransTccProcessor) ProcessOnce(db *common.MyDb, branches []TransBranch) { func (t *TransTccProcessor) ProcessOnce(db *common.DB, branches []TransBranch) {
t.MayQueryPrepared(db) t.MayQueryPrepared(db)
if t.Status != "committed" { if t.Status != "committed" {
return return

View File

@ -18,7 +18,7 @@ func init() {
func (t *TransXaProcessor) GenBranches() []TransBranch { func (t *TransXaProcessor) GenBranches() []TransBranch {
return []TransBranch{} return []TransBranch{}
} }
func (t *TransXaProcessor) ExecBranch(db *common.MyDb, branch *TransBranch) { func (t *TransXaProcessor) ExecBranch(db *common.DB, branch *TransBranch) {
resp, err := common.RestyClient.R().SetBody(M{ resp, err := common.RestyClient.R().SetBody(M{
"branch": branch.Branch, "branch": branch.Branch,
"action": common.If(t.Status == "prepared", "rollback", "commit"), "action": common.If(t.Status == "prepared", "rollback", "commit"),
@ -34,7 +34,7 @@ func (t *TransXaProcessor) ExecBranch(db *common.MyDb, branch *TransBranch) {
} }
} }
func (t *TransXaProcessor) ProcessOnce(db *common.MyDb, branches []TransBranch) { func (t *TransXaProcessor) ProcessOnce(db *common.DB, branches []TransBranch) {
if t.Status == "succeed" { if t.Status == "succeed" {
return return
} }

View File

@ -10,7 +10,7 @@ type M = map[string]interface{}
var p2e = common.P2E var p2e = common.P2E
var e2p = common.E2P var e2p = common.E2P
func dbGet() *common.MyDb { func dbGet() *common.DB {
return common.DbGet(config.Mysql) return common.DbGet(config.Mysql)
} }
func writeTransLog(gid string, action string, status string, branch string, detail string) { func writeTransLog(gid string, action string, status string, branch string, detail string) {

View File

@ -27,7 +27,7 @@ type UserAccount struct {
func (u *UserAccount) TableName() string { return "user_account" } func (u *UserAccount) TableName() string { return "user_account" }
func dbGet() *common.MyDb { func dbGet() *common.DB {
return common.DbGet(Config.Mysql) return common.DbGet(Config.Mysql)
} }
@ -75,7 +75,7 @@ func XaAddRoute(app *gin.Engine) {
} }
func xaTransIn(c *gin.Context) (interface{}, error) { func xaTransIn(c *gin.Context) (interface{}, error) {
err := XaClient.XaLocalTransaction(c.Query("gid"), func(db *common.MyDb) (rerr error) { err := XaClient.XaLocalTransaction(c.Query("gid"), func(db *common.DB) (rerr error) {
req := transReqFromContext(c) req := transReqFromContext(c)
if req.TransInResult != "SUCCESS" { if req.TransInResult != "SUCCESS" {
return fmt.Errorf("tranIn failed") return fmt.Errorf("tranIn failed")
@ -89,7 +89,7 @@ func xaTransIn(c *gin.Context) (interface{}, error) {
} }
func xaTransOut(c *gin.Context) (interface{}, error) { func xaTransOut(c *gin.Context) (interface{}, error) {
err := XaClient.XaLocalTransaction(c.Query("gid"), func(db *common.MyDb) (rerr error) { err := XaClient.XaLocalTransaction(c.Query("gid"), func(db *common.DB) (rerr error) {
req := transReqFromContext(c) req := transReqFromContext(c)
if req.TransOutResult != "SUCCESS" { if req.TransOutResult != "SUCCESS" {
return fmt.Errorf("tranOut failed") return fmt.Errorf("tranOut failed")

2
xa.go
View File

@ -15,7 +15,7 @@ var e2p = common.E2P
type XaGlobalFunc func() error type XaGlobalFunc func() error
type XaLocalFunc func(db *common.MyDb) error type XaLocalFunc func(db *common.DB) error
type XaClient struct { type XaClient struct {
Server string Server string