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.

executor.go 4.1 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 exec
  18. import (
  19. "context"
  20. "database/sql/driver"
  21. "github.com/seata/seata-go/pkg/datasource/sql/parser"
  22. "github.com/seata/seata-go/pkg/datasource/sql/types"
  23. "github.com/seata/seata-go/pkg/datasource/sql/undo"
  24. "github.com/seata/seata-go/pkg/datasource/sql/undo/builder"
  25. )
  26. func init() {
  27. undo.RegisterUndoLogBuilder(types.MultiExecutor, builder.GetMySQLMultiUndoLogBuilder)
  28. }
  29. var (
  30. atExecutors = make(map[types.DBType]func() SQLExecutor)
  31. xaExecutors = make(map[types.DBType]func() SQLExecutor)
  32. )
  33. // RegisterATExecutor AT executor
  34. func RegisterATExecutor(dt types.DBType, builder func() SQLExecutor) {
  35. atExecutors[dt] = builder
  36. }
  37. // RegisterXAExecutor XA executor
  38. func RegisterXAExecutor(dt types.DBType, builder func() SQLExecutor) {
  39. xaExecutors[dt] = func() SQLExecutor {
  40. return builder()
  41. }
  42. }
  43. type (
  44. CallbackWithNamedValue func(ctx context.Context, query string, args []driver.NamedValue) (types.ExecResult, error)
  45. CallbackWithValue func(ctx context.Context, query string, args []driver.Value) (types.ExecResult, error)
  46. SQLExecutor interface {
  47. Interceptors(interceptors []SQLHook)
  48. ExecWithNamedValue(ctx context.Context, execCtx *types.ExecContext, f CallbackWithNamedValue) (types.ExecResult, error)
  49. ExecWithValue(ctx context.Context, execCtx *types.ExecContext, f CallbackWithNamedValue) (types.ExecResult, error)
  50. }
  51. )
  52. // BuildExecutor use db type and transaction type to build an executor. the executor can
  53. // add custom hook, and intercept the user's business sql to generate the undo log.
  54. func BuildExecutor(dbType types.DBType, transactionMode types.TransactionMode, query string) (SQLExecutor, error) {
  55. parseContext, err := parser.DoParser(query)
  56. if err != nil {
  57. return nil, err
  58. }
  59. hooks := make([]SQLHook, 0, 4)
  60. hooks = append(hooks, commonHook...)
  61. hooks = append(hooks, hookSolts[parseContext.SQLType]...)
  62. if transactionMode == types.XAMode {
  63. e := xaExecutors[dbType]()
  64. e.Interceptors(hooks)
  65. return e, nil
  66. }
  67. e := atExecutors[dbType]()
  68. e.Interceptors(hooks)
  69. return e, nil
  70. }
  71. type BaseExecutor struct {
  72. hooks []SQLHook
  73. ex SQLExecutor
  74. }
  75. // Interceptors
  76. func (e *BaseExecutor) Interceptors(interceptors []SQLHook) {
  77. e.hooks = interceptors
  78. }
  79. // ExecWithNamedValue
  80. func (e *BaseExecutor) ExecWithNamedValue(ctx context.Context, execCtx *types.ExecContext, f CallbackWithNamedValue) (types.ExecResult, error) {
  81. for i := range e.hooks {
  82. e.hooks[i].Before(ctx, execCtx)
  83. }
  84. defer func() {
  85. for i := range e.hooks {
  86. e.hooks[i].After(ctx, execCtx)
  87. }
  88. }()
  89. if e.ex != nil {
  90. return e.ex.ExecWithNamedValue(ctx, execCtx, f)
  91. }
  92. return f(ctx, execCtx.Query, execCtx.NamedValues)
  93. }
  94. // ExecWithValue
  95. func (e *BaseExecutor) ExecWithValue(ctx context.Context, execCtx *types.ExecContext, f CallbackWithNamedValue) (types.ExecResult, error) {
  96. for i := range e.hooks {
  97. e.hooks[i].Before(ctx, execCtx)
  98. }
  99. defer func() {
  100. for i := range e.hooks {
  101. e.hooks[i].After(ctx, execCtx)
  102. }
  103. }()
  104. if e.ex != nil {
  105. return e.ex.ExecWithValue(ctx, execCtx, f)
  106. }
  107. nvargs := make([]driver.NamedValue, len(execCtx.Values))
  108. for i, value := range execCtx.Values {
  109. nvargs = append(nvargs, driver.NamedValue{
  110. Value: value,
  111. Ordinal: i,
  112. })
  113. }
  114. return f(ctx, execCtx.Query, nvargs)
  115. }