-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer_sql_type.go
More file actions
124 lines (110 loc) · 2.91 KB
/
analyzer_sql_type.go
File metadata and controls
124 lines (110 loc) · 2.91 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Copyright ApeCloud, Inc.
Licensed under the Apache v2(found in the LICENSE file in the root directory).
*/
package sqlparser
// IsPureSelectStatement returns true if the query is a Select or Union statement without any Lock.
func IsPureSelectStatement(stmt Statement) bool {
switch stmt := stmt.(type) {
case *Select:
if stmt.Lock == NoLock {
return true
}
case *Union:
if stmt.Lock == NoLock {
return true
}
}
return false
}
// ContainsLockStatement returns true if the query contains a Get Lock statement.
func ContainsLockStatement(stmt Statement) bool {
switch stmt := stmt.(type) {
case *Select:
return isLockStatement(stmt)
case *Union:
return isLockStatement(stmt.Left) || isLockStatement(stmt.Right)
}
return false
}
// isLockStatement returns true if the query is a Get Lock statement.
func isLockStatement(stmt Statement) bool {
s, ok := stmt.(*Select)
if !ok {
return false
}
foundLockingFunc := false
err := Walk(func(node SQLNode) (kontinue bool, err error) {
switch node.(type) {
case *LockingFunc:
foundLockingFunc = true
return false, nil
}
return true, nil
}, s)
if err != nil {
return false
}
return foundLockingFunc
}
func hasFuncInStatement(funcs []string, stmt Statement) bool {
//return false if stmt is not a Select statement
s, ok := stmt.(*Select)
if !ok {
return false
}
//visit the select statement and check if it is a Select Last Insert ID statement
foundFunc := false
err := Walk(func(node SQLNode) (kontinue bool, err error) {
switch node := node.(type) {
case *FuncExpr:
for _, f := range funcs {
if node.Name.Lowered() == f {
foundFunc = true
return false, nil
}
}
}
return true, nil
}, s)
if err != nil {
return false
}
return foundFunc
}
// ContainsLastInsertIDStatement returns true if the query is a Select Last Insert ID statement.
func ContainsLastInsertIDStatement(stmt Statement) bool {
switch stmt := stmt.(type) {
case *Select:
return isSelectLastInsertIDStatement(stmt)
case *Union:
return ContainsLastInsertIDStatement(stmt.Left) || ContainsLastInsertIDStatement(stmt.Right)
}
return false
}
// IsSelectLastInsertIDStatement returns true if the query is a Select Last Insert ID statement.
func isSelectLastInsertIDStatement(stmt Statement) bool {
return hasFuncInStatement([]string{"last_insert_id"}, stmt)
}
// IsDDLStatement returns true if the query is an DDL statement.
func IsDDLStatement(stmt Statement) bool {
return ASTToStatementType(stmt) == StmtDDL
}
// IsSelectForUpdateStatement returns true if the query is a Select For Update statement.
func IsSelectForUpdateStatement(stmt Statement) bool {
switch stmt := stmt.(type) {
case *Select:
if stmt.Lock != NoLock {
return true
}
case *Union:
if stmt.Lock != NoLock {
return true
}
}
return false
}
// IsKillStatement returns true if the query is a Kill statement.
func IsKillStatement(_ Statement) bool {
panic("implement me")
}