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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/common/log"
  22. "github.com/seata/seata-go/pkg/datasource/sql/parser"
  23. "github.com/seata/seata-go/pkg/datasource/sql/types"
  24. )
  25. // executorSolts
  26. var executorSolts = make(map[types.DBType]map[parser.ExecutorType]func() SQLExecutor)
  27. func RegisterExecutor(dt types.DBType, et parser.ExecutorType, builder func() SQLExecutor) {
  28. if _, ok := executorSolts[dt]; !ok {
  29. executorSolts[dt] = make(map[parser.ExecutorType]func() SQLExecutor)
  30. }
  31. val := executorSolts[dt]
  32. val[et] = func() SQLExecutor {
  33. return &BaseExecutor{ex: builder()}
  34. }
  35. }
  36. type (
  37. CallbackWithNamedValue func(ctx context.Context, query string, args []driver.NamedValue) (types.ExecResult, error)
  38. CallbackWithValue func(ctx context.Context, query string, args []driver.Value) (types.ExecResult, error)
  39. SQLExecutor interface {
  40. // Interceptors
  41. interceptors(interceptors []SQLInterceptor)
  42. // Exec
  43. ExecWithNamedValue(ctx context.Context, execCtx *ExecContext, f CallbackWithNamedValue) (types.ExecResult, error)
  44. // Exec
  45. ExecWithValue(ctx context.Context, execCtx *ExecContext, f CallbackWithValue) (types.ExecResult, error)
  46. }
  47. )
  48. // buildExecutor
  49. func BuildExecutor(dbType types.DBType, query string) (SQLExecutor, error) {
  50. parseCtx, err := parser.DoParser(query)
  51. if err != nil {
  52. return nil, err
  53. }
  54. hooks := make([]SQLInterceptor, 0, 4)
  55. hooks = append(hooks, commonHook...)
  56. hooks = append(hooks, hookSolts[parseCtx.SQLType]...)
  57. factories, ok := executorSolts[dbType]
  58. if !ok {
  59. log.Debugf("%s not found executor factories, return default Executor", dbType.String())
  60. e := &BaseExecutor{}
  61. e.interceptors(hooks)
  62. return e, nil
  63. }
  64. supplier, ok := factories[parseCtx.ExecutorType]
  65. if !ok {
  66. log.Debugf("%s not found executor for %s, return default Executor",
  67. dbType.String(), parseCtx.ExecutorType.String())
  68. e := &BaseExecutor{}
  69. e.interceptors(hooks)
  70. return e, nil
  71. }
  72. executor := supplier()
  73. executor.interceptors(hooks)
  74. return executor, nil
  75. }
  76. type BaseExecutor struct {
  77. is []SQLInterceptor
  78. ex SQLExecutor
  79. }
  80. // Interceptors
  81. func (e *BaseExecutor) interceptors(interceptors []SQLInterceptor) {
  82. e.is = interceptors
  83. }
  84. // Exec
  85. func (e *BaseExecutor) ExecWithNamedValue(ctx context.Context, execCtx *ExecContext, f CallbackWithNamedValue) (types.ExecResult, error) {
  86. for i := range e.is {
  87. e.is[i].Before(ctx, execCtx)
  88. }
  89. defer func() {
  90. for i := range e.is {
  91. e.is[i].After(ctx, execCtx)
  92. }
  93. }()
  94. if e.ex != nil {
  95. return e.ex.ExecWithNamedValue(ctx, execCtx, f)
  96. }
  97. return f(ctx, execCtx.Query, execCtx.NamedValues)
  98. }
  99. // Exec
  100. func (e *BaseExecutor) ExecWithValue(ctx context.Context, execCtx *ExecContext, f CallbackWithValue) (types.ExecResult, error) {
  101. for i := range e.is {
  102. e.is[i].Before(ctx, execCtx)
  103. }
  104. defer func() {
  105. for i := range e.is {
  106. e.is[i].After(ctx, execCtx)
  107. }
  108. }()
  109. if e.ex != nil {
  110. return e.ex.ExecWithValue(ctx, execCtx, f)
  111. }
  112. return f(ctx, execCtx.Query, execCtx.Values)
  113. }