dtm/dtmsvr/types.go
yedongfu ff6b6e79b4 e2p
2021-05-27 20:54:00 +08:00

80 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dtmsvr
import (
"fmt"
"time"
"github.com/yedf/dtm/common"
"gorm.io/gorm"
)
type M = map[string]interface{}
var p2e = common.P2E
var e2p = common.E2P
type TransGlobalModel struct {
common.ModelBase
Gid string `json:"gid"`
TransType string `json:"trans_type"`
Data string `json:"data"`
Status string `json:"status"`
QueryPrepared string `json:"query_prepared"`
CommitTime *time.Time
FinishTime *time.Time
RollbackTime *time.Time
}
func (*TransGlobalModel) TableName() string {
return "trans_global"
}
func (t *TransGlobalModel) touch(db *common.MyDb) *gorm.DB {
writeTransLog(t.Gid, "touch trans", "", "", "")
return db.Model(&TransGlobalModel{}).Where("gid=?", t.Gid).Update("gid", t.Gid) // 更新update_time避免被定时任务再次
}
func (t *TransGlobalModel) saveStatus(db *common.MyDb, status string) *gorm.DB {
writeTransLog(t.Gid, "step change", status, "", "")
dbr := db.Must().Model(t).Where("status=?", t.Status).Updates(M{
"status": status,
"finish_time": time.Now(),
})
checkAffected(dbr)
t.Status = status
return dbr
}
type TransBranchModel struct {
common.ModelBase
Gid string
Url string
Data string
Branch string
BranchType string
Status string
FinishTime *time.Time
RollbackTime *time.Time
}
func (*TransBranchModel) TableName() string {
return "trans_branch"
}
func (t *TransBranchModel) saveStatus(db *common.MyDb, status string) *gorm.DB {
writeTransLog(t.Gid, "step change", status, t.Branch, "")
dbr := db.Must().Model(t).Where("status=?", t.Status).Updates(M{
"status": status,
"finish_time": time.Now(),
})
checkAffected(dbr)
t.Status = status
return dbr
}
func checkAffected(db1 *gorm.DB) {
if db1.RowsAffected == 0 {
panic(fmt.Errorf("duplicate updating"))
}
}