You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

undo.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package undo
  18. import (
  19. "context"
  20. "database/sql"
  21. "database/sql/driver"
  22. "errors"
  23. "sync"
  24. "github.com/seata/seata-go/pkg/datasource/sql/exec"
  25. "github.com/seata/seata-go/pkg/datasource/sql/types"
  26. "github.com/seata/seata-go/pkg/datasource/sql/undo/builder"
  27. )
  28. func init() {
  29. RegistrUndoLogBuilder(&builder.MySQLUpdateUndoLogBuilder{})
  30. }
  31. var (
  32. solts = map[types.DBType]*undoLogMgrHolder{}
  33. builders = map[types.SQLType]UndoLogBuilder{}
  34. )
  35. type undoLogMgrHolder struct {
  36. once sync.Once
  37. mgr UndoLogManager
  38. }
  39. func RegisterUndoLogManager(m UndoLogManager) error {
  40. if _, exist := solts[m.DBType()]; exist {
  41. return nil
  42. }
  43. solts[m.DBType()] = &undoLogMgrHolder{
  44. mgr: m,
  45. once: sync.Once{},
  46. }
  47. return nil
  48. }
  49. func RegistrUndoLogBuilder(m UndoLogBuilder) {
  50. if _, ok := builders[m.GetSQLType()]; !ok {
  51. builders[m.GetSQLType()] = m
  52. }
  53. }
  54. func GetUndologBuilder(sqlType types.SQLType) UndoLogBuilder {
  55. return builders[sqlType]
  56. }
  57. // UndoLogManager
  58. type UndoLogManager interface {
  59. Init()
  60. // InsertUndoLog
  61. InsertUndoLog(l []BranchUndoLog, tx driver.Conn) error
  62. // DeleteUndoLog
  63. DeleteUndoLog(ctx context.Context, xid string, branchID int64, conn *sql.Conn) error
  64. // BatchDeleteUndoLog
  65. BatchDeleteUndoLog(xid []string, branchID []int64, conn *sql.Conn) error
  66. // FlushUndoLog
  67. FlushUndoLog(txCtx *types.TransactionContext, tx driver.Conn) error
  68. // RunUndo
  69. RunUndo(xid string, branchID int64, conn *sql.Conn) error
  70. // DBType
  71. DBType() types.DBType
  72. // HasUndoLogTable
  73. HasUndoLogTable(ctx context.Context, conn *sql.Conn) (bool, error)
  74. }
  75. // GetUndoLogManager
  76. func GetUndoLogManager(d types.DBType) (UndoLogManager, error) {
  77. v, ok := solts[d]
  78. if !ok {
  79. return nil, errors.New("not found UndoLogManager")
  80. }
  81. v.once.Do(func() {
  82. v.mgr.Init()
  83. })
  84. return v.mgr, nil
  85. }
  86. // BranchUndoLog
  87. type BranchUndoLog struct {
  88. // Xid
  89. Xid string
  90. // BranchID
  91. BranchID string
  92. // Logs
  93. Logs []SQLUndoLog
  94. }
  95. // Marshal
  96. func (b *BranchUndoLog) Marshal() []byte {
  97. return nil
  98. }
  99. // SQLUndoLog
  100. type SQLUndoLog struct {
  101. SQLType types.SQLType
  102. TableName string
  103. Images types.RoundRecordImage
  104. }
  105. // UndoLogParser
  106. type UndoLogParser interface {
  107. // GetName
  108. GetName() string
  109. // GetDefaultContent
  110. GetDefaultContent() []byte
  111. // Encode
  112. Encode(l BranchUndoLog) []byte
  113. // Decode
  114. Decode(b []byte) BranchUndoLog
  115. }
  116. type UndoLogBuilder interface {
  117. BeforeImage(ctx context.Context, execCtx *exec.ExecContext) (*types.RecordImage, error)
  118. AfterImage(types.RecordImages) (*types.RecordImages, error)
  119. GetSQLType() types.SQLType
  120. }