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

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