-
Notifications
You must be signed in to change notification settings - Fork 2
/
kind.go
188 lines (168 loc) · 4.06 KB
/
kind.go
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package sqlparser
import (
"strings"
"unicode"
)
const (
KindUnknown = "unknown"
KindSelect = "select"
KindLoad = "load"
KindInsert = "insert"
KindUpdate = "update"
KindMerge = "merge"
KindDelete = "delete"
KindRegisterType = "register type"
KindRegisterSet = "register set"
KindCreateTable = "create table"
KindDropTable = "drop table"
KindCreateIndex = "create index"
KindDropIndex = "drop index"
KindTruncateTable = "truncate table"
)
// Kind represents the type of SQL statement.
type Kind string
// IsUnknown returns true if the Kind is KindUnknown.
func (k Kind) IsUnknown() bool {
return k == KindUnknown
}
// IsInsert returns true if the Kind is KindInsert.
func (k Kind) IsInsert() bool {
return k == KindInsert
}
// IsSelect returns true if the Kind is KindSelect.
func (k Kind) IsSelect() bool {
return k == KindSelect
}
// IsUpdate returns true if the Kind is KindUpdate.
func (k Kind) IsUpdate() bool {
return k == KindUpdate
}
// IsLoad returns true if the Kind is KindLoad.
func (k Kind) IsLoad() bool {
return k == KindLoad
}
// IsMerge returns true if the Kind is KindMerge.
func (k Kind) IsMerge() bool {
return k == KindMerge
}
// IsDelete returns true if the Kind is KindDelete.
func (k Kind) IsDelete() bool {
return k == KindDelete
}
// IsRegisterType returns true if the Kind is KindRegisterType.
func (k Kind) IsRegisterType() bool {
return k == KindRegisterType
}
// IsRegisterSet returns true if the Kind is KindRegisterSet.
func (k Kind) IsRegisterSet() bool {
return k == KindRegisterSet
}
// IsCreateTable returns true if the Kind is KindCreateTable.
func (k Kind) IsCreateTable() bool {
return k == KindCreateTable
}
// IsDropTable returns true if the Kind is KindDropTable.
func (k Kind) IsDropTable() bool {
return k == KindDropTable
}
func ParseKind(SQL string) Kind {
SQL = removeSQLComments(SQL)
normalizedSQL := strings.TrimSpace(SQL)
if len(normalizedSQL) < 2 {
return KindUnknown
}
firstToken := strings.ToLower(normalizedSQL[0:2])
secondToken := ""
secondPart := ""
if index := strings.Index(normalizedSQL, " "); index != -1 {
for i := index; i < len(normalizedSQL); i++ {
if unicode.IsLetter(rune(normalizedSQL[i])) {
secondToken = strings.ToLower(normalizedSQL[i : i+1])
if i+1 < len(normalizedSQL) {
secondPart = normalizedSQL[i+1:]
}
break
}
}
}
thirdToken := ""
if index := strings.Index(secondPart, " "); index != -1 {
for i := index; i < len(secondPart); i++ {
if unicode.IsLetter(rune(secondPart[i])) {
thirdToken = strings.ToLower(secondPart[i : i+1])
break
}
}
}
strings.ToLower(strings.TrimSpace(SQL)[0:1])
switch firstToken[0] {
case 's', 'w': //select, with
return KindSelect
case 'i': //insert
return KindInsert
case 'u': //update
return KindUpdate
case 'l': //load
return KindLoad
case 'm': //merge
return KindMerge
case 'd': //delete or drop
switch firstToken[1] {
case 'e': //delete
return KindDelete
case 'r': //drop
if len(secondToken) == 0 {
return KindUnknown
}
switch secondToken[0] {
case 't': //drop table
return KindDropTable
case 'i':
return KindDropIndex
}
}
case 't':
if len(secondToken) == 0 {
return KindUnknown
}
switch secondToken[0] {
case 't': //truncate
return KindTruncateTable
}
case 'r': //register
if len(secondToken) == 0 {
return KindUnknown
}
switch secondToken[0] {
case 't': //register type
return KindRegisterType
case 's': //register set
return KindRegisterSet
case 'g': //g global
if len(thirdToken) == 0 {
return KindUnknown
}
switch thirdToken[0] {
case 't': //register type (global type)
return KindRegisterType
case 's': //register set (global set)
return KindRegisterSet
}
}
case 'c': //create
if len(secondToken) == 0 {
return KindUnknown
}
switch secondToken[0] {
case 't': //create table
return KindCreateTable
case 'i':
return KindCreateIndex
}
switch thirdToken[0] {
case 'i':
return KindCreateIndex
}
}
return KindUnknown
}