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 3.5 kB

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