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.

parser_factory.go 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 parser
  18. import (
  19. tparser "github.com/pingcap/tidb/parser"
  20. "github.com/pingcap/tidb/parser/ast"
  21. _ "github.com/pingcap/tidb/types/parser_driver"
  22. "github.com/seata/seata-go/pkg/datasource/sql/types"
  23. )
  24. // ExecutorType
  25. //go:generate stringer -type=ExecutorType
  26. type ExecutorType int32
  27. const (
  28. _ ExecutorType = iota
  29. UnsupportExecutor
  30. InsertExecutor
  31. UpdateExecutor
  32. DeleteExecutor
  33. ReplaceIntoExecutor
  34. InsertOnDuplicateExecutor
  35. )
  36. type ParseContext struct {
  37. // SQLType
  38. SQLType types.SQLType
  39. // ExecutorType
  40. ExecutorType ExecutorType
  41. // InsertStmt
  42. InsertStmt *ast.InsertStmt
  43. // UpdateStmt
  44. UpdateStmt *ast.UpdateStmt
  45. // DeleteStmt
  46. DeleteStmt *ast.DeleteStmt
  47. }
  48. func DoParser(query string) (*ParseContext, error) {
  49. p := tparser.New()
  50. stmtNode, err := p.ParseOneStmt(query, "", "")
  51. if err != nil {
  52. return nil, err
  53. }
  54. parserCtx := new(ParseContext)
  55. switch stmt := stmtNode.(type) {
  56. case *ast.InsertStmt:
  57. parserCtx.SQLType = types.SQLTypeInsert
  58. parserCtx.InsertStmt = stmt
  59. parserCtx.ExecutorType = InsertExecutor
  60. if stmt.IsReplace {
  61. parserCtx.ExecutorType = ReplaceIntoExecutor
  62. }
  63. if len(stmt.OnDuplicate) != 0 {
  64. parserCtx.ExecutorType = InsertOnDuplicateExecutor
  65. }
  66. case *ast.UpdateStmt:
  67. parserCtx.SQLType = types.SQLTypeUpdate
  68. parserCtx.UpdateStmt = stmt
  69. parserCtx.ExecutorType = UpdateExecutor
  70. case *ast.DeleteStmt:
  71. parserCtx.SQLType = types.SQLTypeDelete
  72. parserCtx.DeleteStmt = stmt
  73. parserCtx.ExecutorType = DeleteExecutor
  74. }
  75. return parserCtx, nil
  76. }