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.

at_executor.go 2.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 at
  18. import (
  19. "context"
  20. "seata.apache.org/seata-go/pkg/datasource/sql/exec"
  21. "seata.apache.org/seata-go/pkg/datasource/sql/parser"
  22. "seata.apache.org/seata-go/pkg/datasource/sql/types"
  23. "seata.apache.org/seata-go/pkg/datasource/sql/util"
  24. "seata.apache.org/seata-go/pkg/tm"
  25. )
  26. func Init() {
  27. exec.RegisterATExecutor(types.DBTypeMySQL, func() exec.SQLExecutor { return &ATExecutor{} })
  28. }
  29. type executor interface {
  30. ExecContext(ctx context.Context, f exec.CallbackWithNamedValue) (types.ExecResult, error)
  31. }
  32. type ATExecutor struct {
  33. hooks []exec.SQLHook
  34. }
  35. func (e *ATExecutor) Interceptors(hooks []exec.SQLHook) {
  36. e.hooks = hooks
  37. }
  38. // ExecWithNamedValue find the executor by sql type
  39. func (e *ATExecutor) ExecWithNamedValue(ctx context.Context, execCtx *types.ExecContext, f exec.CallbackWithNamedValue) (types.ExecResult, error) {
  40. queryParser, err := parser.DoParser(execCtx.Query)
  41. if err != nil {
  42. return nil, err
  43. }
  44. var executor executor
  45. if !tm.IsGlobalTx(ctx) {
  46. executor = NewPlainExecutor(queryParser, execCtx)
  47. } else {
  48. switch queryParser.SQLType {
  49. case types.SQLTypeInsert:
  50. executor = NewInsertExecutor(queryParser, execCtx, e.hooks)
  51. case types.SQLTypeUpdate:
  52. executor = NewUpdateExecutor(queryParser, execCtx, e.hooks)
  53. case types.SQLTypeDelete:
  54. executor = NewDeleteExecutor(queryParser, execCtx, e.hooks)
  55. case types.SQLTypeSelectForUpdate:
  56. executor = NewSelectForUpdateExecutor(queryParser, execCtx, e.hooks)
  57. case types.SQLTypeInsertOnDuplicateUpdate:
  58. executor = NewInsertOnUpdateExecutor(queryParser, execCtx, e.hooks)
  59. case types.SQLTypeMulti:
  60. executor = NewMultiExecutor(queryParser, execCtx, e.hooks)
  61. default:
  62. executor = NewPlainExecutor(queryParser, execCtx)
  63. }
  64. }
  65. return executor.ExecContext(ctx, f)
  66. }
  67. // ExecWithValue transfer value to nameValue execute
  68. func (e *ATExecutor) ExecWithValue(ctx context.Context, execCtx *types.ExecContext, f exec.CallbackWithNamedValue) (types.ExecResult, error) {
  69. execCtx.NamedValues = util.ValueToNamedValue(execCtx.Values)
  70. return e.ExecWithNamedValue(ctx, execCtx, f)
  71. }