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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 DoParser(query string) (*ParseContext, error) {
  48. p := aparser.New()
  49. stmtNode, err := p.ParseOneStmt(query, "", "")
  50. if err != nil {
  51. return nil, err
  52. }
  53. parserCtx := new(ParseContext)
  54. switch stmt := stmtNode.(type) {
  55. case *ast.InsertStmt:
  56. parserCtx.SQLType = types.SQLTypeInsert
  57. parserCtx.InsertStmt = stmt
  58. parserCtx.ExecutorType = InsertExecutor
  59. if stmt.IsReplace {
  60. parserCtx.ExecutorType = ReplaceIntoExecutor
  61. }
  62. if len(stmt.OnDuplicate) != 0 {
  63. parserCtx.ExecutorType = InsertOnDuplicateExecutor
  64. }
  65. case *ast.UpdateStmt:
  66. parserCtx.SQLType = types.SQLTypeUpdate
  67. parserCtx.UpdateStmt = stmt
  68. parserCtx.ExecutorType = UpdateExecutor
  69. case *ast.DeleteStmt:
  70. parserCtx.SQLType = types.SQLTypeDelete
  71. parserCtx.DeleteStmt = stmt
  72. parserCtx.ExecutorType = DeleteExecutor
  73. }
  74. return parserCtx, nil
  75. }