MyDb change DB
This commit is contained in:
parent
c6e6adbad5
commit
cb2988cd6c
@ -17,20 +17,20 @@ type ModelBase struct {
|
||||
UpdateTime *time.Time `gorm:"autoUpdateTime"`
|
||||
}
|
||||
|
||||
var dbs = map[string]*MyDb{}
|
||||
var dbs = map[string]*DB{}
|
||||
|
||||
type MyDb struct {
|
||||
type DB struct {
|
||||
*gorm.DB
|
||||
}
|
||||
|
||||
func (m *MyDb) Must() *MyDb {
|
||||
func (m *DB) Must() *DB {
|
||||
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)
|
||||
return &MyDb{DB: db}
|
||||
return &DB{DB: db}
|
||||
}
|
||||
|
||||
type tracePlugin struct{}
|
||||
@ -86,7 +86,7 @@ func ReplaceDsnPassword(dsn string) string {
|
||||
return reg.ReplaceAllString(dsn, ":****@")
|
||||
}
|
||||
|
||||
func DbGet(conf map[string]string) *MyDb {
|
||||
func DbGet(conf map[string]string) *DB {
|
||||
dsn := GetDsn(conf)
|
||||
if dbs[dsn] == nil {
|
||||
logrus.Printf("connecting %s", ReplaceDsnPassword(dsn))
|
||||
@ -95,7 +95,7 @@ func DbGet(conf map[string]string) *MyDb {
|
||||
})
|
||||
E2P(err)
|
||||
db1.Use(&tracePlugin{})
|
||||
dbs[dsn] = &MyDb{DB: db1}
|
||||
dbs[dsn] = &DB{DB: db1}
|
||||
}
|
||||
return dbs[dsn]
|
||||
}
|
||||
@ -110,7 +110,7 @@ func (conn *MyConn) Close() {
|
||||
conn.Conn.Close()
|
||||
}
|
||||
|
||||
func DbAlone(conf map[string]string) (*MyDb, *MyConn) {
|
||||
func DbAlone(conf map[string]string) (*DB, *MyConn) {
|
||||
dsn := GetDsn(conf)
|
||||
logrus.Printf("opening alone mysql: %s", ReplaceDsnPassword(dsn))
|
||||
mdb, err := sql.Open("mysql", dsn)
|
||||
@ -120,5 +120,5 @@ func DbAlone(conf map[string]string) (*MyDb, *MyConn) {
|
||||
}), &gorm.Config{})
|
||||
E2P(err)
|
||||
gormDB.Use(&tracePlugin{})
|
||||
return &MyDb{DB: gormDB}, &MyConn{Conn: mdb, Dsn: dsn}
|
||||
return &DB{DB: gormDB}, &MyConn{Conn: mdb, Dsn: dsn}
|
||||
}
|
||||
|
||||
@ -30,16 +30,16 @@ func (*TransGlobal) TableName() string {
|
||||
|
||||
type TransProcessor interface {
|
||||
GenBranches() []TransBranch
|
||||
ProcessOnce(db *common.MyDb, branches []TransBranch)
|
||||
ExecBranch(db *common.MyDb, branch *TransBranch)
|
||||
ProcessOnce(db *common.DB, branches []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", "", "", "")
|
||||
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, "", "")
|
||||
updates := M{
|
||||
"status": status,
|
||||
@ -71,7 +71,7 @@ func (*TransBranch) TableName() string {
|
||||
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, "")
|
||||
dbr := db.Must().Model(t).Where("status=?", t.Status).Updates(M{
|
||||
"status": status,
|
||||
@ -100,7 +100,7 @@ func (trans *TransGlobal) getProcessor() TransProcessor {
|
||||
return processorFac[trans.TransType](trans)
|
||||
}
|
||||
|
||||
func (t *TransGlobal) MayQueryPrepared(db *common.MyDb) {
|
||||
func (t *TransGlobal) MayQueryPrepared(db *common.DB) {
|
||||
if t.Status != "prepared" {
|
||||
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 func() {
|
||||
if TransProcessedTestChan != nil {
|
||||
@ -133,9 +133,9 @@ func (trans *TransGlobal) Process(db *common.MyDb) {
|
||||
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 {
|
||||
db := &common.MyDb{DB: db1}
|
||||
db := &common.DB{DB: db1}
|
||||
|
||||
writeTransLog(t.Gid, "create trans", t.Status, "", t.Data)
|
||||
dbr := db.Must().Clauses(clause.OnConflict{
|
||||
@ -174,7 +174,7 @@ func TransFromContext(c *gin.Context) *TransGlobal {
|
||||
return &m
|
||||
}
|
||||
|
||||
func TransFromDb(db *common.MyDb, gid string) *TransGlobal {
|
||||
func TransFromDb(db *common.DB, gid string) *TransGlobal {
|
||||
m := TransGlobal{}
|
||||
dbr := db.Must().Model(&m).Where("gid=?", gid).First(&m)
|
||||
e2p(dbr.Error)
|
||||
|
||||
@ -32,7 +32,7 @@ func (t *TransMsgProcessor) GenBranches() []TransBranch {
|
||||
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)
|
||||
e2p(err)
|
||||
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)
|
||||
if t.Status != "committed" {
|
||||
return
|
||||
|
||||
@ -34,7 +34,7 @@ func (t *TransSagaProcessor) GenBranches() []TransBranch {
|
||||
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)
|
||||
e2p(err)
|
||||
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)
|
||||
if t.Status != "committed" {
|
||||
return
|
||||
|
||||
@ -34,7 +34,7 @@ func (t *TransTccProcessor) GenBranches() []TransBranch {
|
||||
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)
|
||||
e2p(err)
|
||||
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)
|
||||
if t.Status != "committed" {
|
||||
return
|
||||
|
||||
@ -18,7 +18,7 @@ func init() {
|
||||
func (t *TransXaProcessor) GenBranches() []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{
|
||||
"branch": branch.Branch,
|
||||
"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" {
|
||||
return
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ type M = map[string]interface{}
|
||||
var p2e = common.P2E
|
||||
var e2p = common.E2P
|
||||
|
||||
func dbGet() *common.MyDb {
|
||||
func dbGet() *common.DB {
|
||||
return common.DbGet(config.Mysql)
|
||||
}
|
||||
func writeTransLog(gid string, action string, status string, branch string, detail string) {
|
||||
|
||||
@ -27,7 +27,7 @@ type UserAccount struct {
|
||||
|
||||
func (u *UserAccount) TableName() string { return "user_account" }
|
||||
|
||||
func dbGet() *common.MyDb {
|
||||
func dbGet() *common.DB {
|
||||
return common.DbGet(Config.Mysql)
|
||||
}
|
||||
|
||||
@ -75,7 +75,7 @@ func XaAddRoute(app *gin.Engine) {
|
||||
}
|
||||
|
||||
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)
|
||||
if req.TransInResult != "SUCCESS" {
|
||||
return fmt.Errorf("tranIn failed")
|
||||
@ -89,7 +89,7 @@ func xaTransIn(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)
|
||||
if req.TransOutResult != "SUCCESS" {
|
||||
return fmt.Errorf("tranOut failed")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user