Browse Source

refactor addTime

pull/90/head
yedf2 5 years ago
parent
commit
afa6c4070f
  1. 2
      conf.sample.yml
  2. 5
      dtmcli/dtmimp/db_special.go
  3. 2
      dtmcli/dtmimp/db_special_test.go
  4. 7
      dtmsvr/storage/sql.go

2
conf.sample.yml

@ -35,7 +35,7 @@ Store: # specify which engine to store trans status
# TimeoutToFail: 35 # timeout for XA, TCC to fail. saga's timeout default to infinite, which can be overwritten in saga options
# RetryInterval: 10 # the subtrans branch will be retried after this interval
### db config of examples
### dtm can run examples, and examples will use following config to connect db
ExamplesDB:
Driver: 'mysql'
Host: 'localhost'

5
dtmcli/dtmimp/db_special.go

@ -13,7 +13,6 @@ import (
// DBSpecial db specific operations
type DBSpecial interface {
TimestampAdd(second int) string
GetPlaceHoldSQL(sql string) string
GetInsertIgnoreTemplate(tableAndValues string, pgConstraint string) string
GetXaSQL(command string, xid string) string
@ -24,10 +23,6 @@ var currentDBType = DBTypeMysql
type mysqlDBSpecial struct{}
func (*mysqlDBSpecial) TimestampAdd(second int) string {
return fmt.Sprintf("date_add(now(), interval %d second)", second)
}
func (*mysqlDBSpecial) GetPlaceHoldSQL(sql string) string {
return sql
}

2
dtmcli/dtmimp/db_special_test.go

@ -22,13 +22,11 @@ func TestDBSpecial(t *testing.T) {
assert.Equal(t, "? ?", sp.GetPlaceHoldSQL("? ?"))
assert.Equal(t, "xa start 'xa1'", sp.GetXaSQL("start", "xa1"))
assert.Equal(t, "date_add(now(), interval 1000 second)", sp.TimestampAdd(1000))
assert.Equal(t, "insert ignore into a(f) values(?)", sp.GetInsertIgnoreTemplate("a(f) values(?)", "c"))
SetCurrentDBType(DBTypePostgres)
sp = GetDBSpecial()
assert.Equal(t, "$1 $2", sp.GetPlaceHoldSQL("? ?"))
assert.Equal(t, "begin", sp.GetXaSQL("start", "xa1"))
assert.Equal(t, "current_timestamp + interval '1000 second'", sp.TimestampAdd(1000))
assert.Equal(t, "insert into a(f) values(?) on conflict ON CONSTRAINT c do nothing", sp.GetInsertIgnoreTemplate("a(f) values(?)", "c"))
SetCurrentDBType(old)
}

7
dtmsvr/storage/sql.go

@ -112,7 +112,12 @@ func (s *SqlStore) TouchCronTime(global *TransGlobalStore, nextCronInterval int6
func (s *SqlStore) LockOneGlobalTrans(expireIn time.Duration) *TransGlobalStore {
db := dbGet()
getTime := dtmimp.GetDBSpecial().TimestampAdd
getTime := func(second int) string {
return map[string]string{
"mysql": fmt.Sprintf("date_add(now(), interval %d second)", second),
"postgres": fmt.Sprintf("current_timestamp + interval '%d second'", second),
}[config.Store.Driver]
}
expire := int(expireIn / time.Second)
whereTime := fmt.Sprintf("next_cron_time < %s", getTime(expire))
owner := uuid.NewString()

Loading…
Cancel
Save