-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtidb_parser.go
More file actions
61 lines (53 loc) · 1.37 KB
/
tidb_parser.go
File metadata and controls
61 lines (53 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/ast"
_ "github.com/pingcap/tidb/pkg/parser/test_driver"
)
var p *parser.Parser
func init() {
p = parser.New()
}
func isQueryStmt(stmt ast.StmtNode) bool {
switch stmt.(type) {
// DML
case *ast.InsertStmt, *ast.UpdateStmt, *ast.DeleteStmt:
return false
// DDL
case *ast.CreateTableStmt, *ast.AlterTableStmt, *ast.DropTableStmt, *ast.GrantStmt,
*ast.RevokeStmt, *ast.TruncateTableStmt, *ast.RenameTableStmt, *ast.CreateIndexStmt,
*ast.CreateDatabaseStmt, *ast.DropDatabaseStmt, *ast.FlashBackDatabaseStmt, *ast.AlterDatabaseStmt:
return false
// Txn
case *ast.BeginStmt, *ast.CommitStmt, *ast.RollbackStmt:
return false
case *ast.UseStmt, *ast.SetStmt:
return false
default:
return true
}
}
func isQuery(stmt string) (bool, error) {
stmtNodes, _, err := p.Parse(stmt, "", "")
if err != nil {
return false, err
}
for _, stmt := range stmtNodes {
if !isQueryStmt(stmt) {
return false, nil
}
}
return true, nil
}
// splitSQLStatements parses the input SQL and returns individual statements
func splitSQLStatements(sql string) ([]string, error) {
stmtNodes, _, err := p.Parse(sql, "", "")
if err != nil {
return nil, err
}
var statements []string
for _, stmt := range stmtNodes {
statements = append(statements, stmt.Text())
}
return statements, nil
}