-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer_sql_type_test.go
More file actions
163 lines (155 loc) · 4.68 KB
/
analyzer_sql_type_test.go
File metadata and controls
163 lines (155 loc) · 4.68 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Copyright ApeCloud, Inc.
Licensed under the Apache v2(found in the LICENSE file in the root directory).
*/
package sqlparser
import (
"testing"
)
func TestIsSelectForUpdateStatement(t *testing.T) {
testcases := []struct {
sql string
want bool
}{
{"select 1 from t for update", true},
{"select /* for update */ 1 from t for update", true},
{"select * from t lock in share mode", true},
{"select /* for update */ 1 from t", false},
{"select /* for update */ 1 from t union all select * from t2 for update", true},
{"select /* for update */ 1 from t union all select * from t2 lock in share mode", true},
{"select /* for update, lock in share mode */ 1 from t union all select * from t2", false},
{"/* select 1 from t for update */", false},
{"create database d1", false},
}
for _, tcase := range testcases {
tree, err := Parse(tcase.sql)
if err != nil {
t.Error(err)
continue
}
if got := IsSelectForUpdateStatement(tree); got != tcase.want {
t.Errorf("IsSelectForUpdateStatement(%s): %v, want %v", tcase.sql, got, tcase.want)
}
}
}
func TestIsDDLStatement(t *testing.T) {
testcases := []struct {
sql string
want bool
}{
{"create database d1", true},
{"drop database d1", true},
{"create table t1 (c1 int)", true},
{"drop table t1", true},
{"alter table t1 add column c2 int", true},
{"alter table t1 drop column c2", true},
{"/* alter table t1 drop column c2 */", false},
{"CREATE VIEW my_view AS SELECT id, col FROM user", true},
}
for _, tcase := range testcases {
tree, err := Parse(tcase.sql)
if err != nil {
t.Error(err)
continue
}
if got := IsDDLStatement(tree); got != tcase.want {
t.Errorf("IsDDLStatement(%s): %v, want %v", tcase.sql, got, tcase.want)
}
}
}
func TestIsPureSelectStatement(t *testing.T) {
testcases := []struct {
sql string
want bool
}{
{"select 1 from t for update", false},
{"select /* for update */ 1 from t for update", false},
{"select * from t lock in share mode", false},
{"select /* for update */ 1 from t", true},
{"select /* for update */ 1 from t union all select * from t2 for update", false},
{"select /* for update */ 1 from t union all select * from t2 lock in share mode", false},
{"select /* for update, lock in share mode */ 1 from t union all select * from t2", true},
{"/* select 1 from t for update */", false},
{"create database d1", false},
}
for _, tcase := range testcases {
tree, err := Parse(tcase.sql)
if err != nil {
t.Error(err)
continue
}
if got := IsPureSelectStatement(tree); got != tcase.want {
t.Errorf("IsPureSelectStatement(%s): %v, want %v", tcase.sql, got, tcase.want)
}
}
}
func TestIsSelectLastInsertIDStatement(t *testing.T) {
testcases := []struct {
sql string
want bool
}{
{"select last_insert_id()", true},
{"select last_insert_id() union all select last_insert_id()", true},
{"select 1 union all select 1 union all select last_insert_id()", true},
{"select 1 union all select last_insert_id() union all select 1", true},
{"select last_insert_id() union all select 1 union all select 1", true},
{"select 1 union all select 1 union all select last_insert_id() union all select 1 union all select 1", true},
{"select /* select last_insert_id() */ 1", false},
{"select database()", false},
}
for _, tcase := range testcases {
tree, err := Parse(tcase.sql)
if err != nil {
t.Error(err)
continue
}
if got := ContainsLastInsertIDStatement(tree); got != tcase.want {
t.Errorf("ContainsLastInsertIDStatement(%s): %v, want %v", tcase.sql, got, tcase.want)
}
}
}
func _TestIsKillStatement(t *testing.T) {
testcases := []struct {
sql string
want bool
}{
{"kill 1", true},
{"kill connection 1", true},
{"kill query 1", true},
}
for _, tcase := range testcases {
tree, err := Parse(tcase.sql)
if err != nil {
t.Error(err)
continue
}
if got := IsKillStatement(tree); got != tcase.want {
t.Errorf("IsKillStatement(%s): %v, want %v", tcase.sql, got, tcase.want)
}
}
}
func TestContainsLockStatement(t *testing.T) {
testcases := []struct {
sql string
want bool
}{
{"select get_lock('aaa', 1000)", true},
{"select get_lock('aaa', 1000) union all select get_lock('bbb', 1000) ", true},
{"select release_lock('aaa')", true},
{"select IS_USED_LOCK('aaa')", true},
{"select IS_FREE_LOCK('aaa')", true},
{"select RELEASE_ALL_LOCKS()", true},
{"select /* get_lock('aaa', 1000) */ 1", false},
{"select database()", false},
}
for _, tcase := range testcases {
tree, err := Parse(tcase.sql)
if err != nil {
t.Error(err)
continue
}
if got := ContainsLockStatement(tree); got != tcase.want {
t.Errorf("ContainsLockStatement(%s): %v, want %v", tcase.sql, got, tcase.want)
}
}
}