-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqllexer_utils.go
349 lines (306 loc) · 7.86 KB
/
sqllexer_utils.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package sqllexer
import (
"strings"
"unicode"
)
type DBMSType string
const (
// DBMSSQLServer is a MS SQL
DBMSSQLServer DBMSType = "mssql"
// DBMSPostgres is a PostgreSQL Server
DBMSPostgres DBMSType = "postgresql"
// DBMSMySQL is a MySQL Server
DBMSMySQL DBMSType = "mysql"
// DBMSOracle is a Oracle Server
DBMSOracle DBMSType = "oracle"
// DBMSSnowflake is a Snowflake Server
DBMSSnowflake DBMSType = "snowflake"
)
func PrecomputeCaseInsensitiveKeys[T any](input map[string]T) map[string]T {
result := make(map[string]T, len(input)*3)
for key, value := range input {
result[key] = value
result[strings.ToLower(key)] = value
result[strings.ToUpper(key)] = value
}
return result
}
var commands = map[string]bool{
"Select": true,
"Insert": true,
"Update": true,
"Delete": true,
"Create": true,
"Alter": true,
"Drop": true,
"Join": true,
"Grant": true,
"Revoke": true,
"Commit": true,
"Begin": true,
"Truncate": true,
"Merge": true,
"Execute": true,
"Exec": true,
"Explain": true,
"Straight_Join": true,
"Use": true,
"Clone": true,
}
var commandsMap = PrecomputeCaseInsensitiveKeys(commands)
var tableIndicators = map[string]bool{
"From": true,
"Join": true,
"Into": true,
"Update": true,
"Table": true,
"Exists": true, // Drop Table If Exists
"Straight_Join": true, // MySQL
"Clone": true, // Snowflake
"Only": true, // PostgreSQL
}
var tableIndicatorsMap = PrecomputeCaseInsensitiveKeys(tableIndicators)
var keywords = map[string]bool{
"Select": true,
"Insert": true,
"Update": true,
"Delete": true,
"Create": true,
"Alter": true,
"Drop": true,
"Grant": true,
"Revoke": true,
"Add": true,
"All": true,
"And": true,
"Any": true,
"As": true,
"Asc": true,
"Begin": true,
"Between": true,
"By": true,
"Case": true,
"Check": true,
"Column": true,
"Commit": true,
"Constraint": true,
"Database": true,
"Declare": true,
"Default": true,
"Desc": true,
"Distinct": true,
"Else": true,
"End": true,
"Exec": true,
"Exists": true,
"Foreign": true,
"From": true,
"Group": true,
"Having": true,
"In": true,
"Index": true,
"Inner": true,
"Into": true,
"Is": true,
"Join": true,
"Key": true,
"Left": true,
"Like": true,
"Limit": true,
"Not": true,
"On": true,
"Or": true,
"Order": true,
"Outer": true,
"Primary": true,
"Procedure": true,
"Replace": true,
"Returns": true,
"Right": true,
"Rollback": true,
"Rownum": true,
"Set": true,
"Some": true,
"Table": true,
"Top": true,
"Truncate": true,
"Union": true,
"Unique": true,
"Use": true,
"Values": true,
"View": true,
"Where": true,
"Cube": true,
"Rollup": true,
"Literal": true,
"Window": true,
"Vaccum": true,
"Analyze": true,
"Ilike": true,
"Using": true,
"Assertion": true,
"Domain": true,
"Cluster": true,
"Copy": true,
"Explain": true,
"Plpgsql": true,
"Trigger": true,
"Temporary": true,
"Unlogged": true,
"Recursive": true,
"Returning": true,
"Offset": true,
"Of": true,
"Skip": true,
"If": true,
"Only": true,
}
var keywordsMap = PrecomputeCaseInsensitiveKeys(keywords)
var jsonOperators = map[string]bool{
"->": true,
"->>": true,
"#>": true,
"#>>": true,
"@?": true,
"@@": true,
"?|": true,
"?&": true,
"@>": true,
"<@": true,
"#-": true,
}
func isWhitespace(ch rune) bool {
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
}
func isDigit(ch rune) bool {
return '0' <= ch && ch <= '9'
}
func isExpontent(ch rune) bool {
return ch == 'e' || ch == 'E'
}
func isLeadingSign(ch rune) bool {
return ch == '+' || ch == '-'
}
func isLetter(ch rune) bool {
// Fast path: ASCII letters and underscore
if ch <= 127 {
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || ch == '_'
}
// Fallback to Unicode
return unicode.IsLetter(ch)
}
func isAlphaNumeric(ch rune) bool {
// Check if it's a digit first, then letter (faster for numbers)
return isDigit(ch) || isLetter(ch)
}
func isDoubleQuote(ch rune) bool {
return ch == '"'
}
func isSingleQuote(ch rune) bool {
return ch == '\''
}
func isOperator(ch rune) bool {
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=' || ch == '<' || ch == '>' || ch == '!' || ch == '&' || ch == '|' || ch == '^' || ch == '%' || ch == '~' || ch == '?' || ch == '@' || ch == ':' || ch == '#'
}
func isWildcard(ch rune) bool {
return ch == '*'
}
func isSingleLineComment(ch rune, nextCh rune) bool {
return ch == '-' && nextCh == '-'
}
func isMultiLineComment(ch rune, nextCh rune) bool {
return ch == '/' && nextCh == '*'
}
func isPunctuation(ch rune) bool {
return ch == '(' || ch == ')' || ch == ',' || ch == ';' || ch == '.' || ch == ':' || ch == '[' || ch == ']' || ch == '{' || ch == '}'
}
func isEOF(ch rune) bool {
return ch == 0
}
func isCommand(ident string) bool {
_, ok := commandsMap[ident]
return ok
}
func isTableIndicator(ident string) bool {
_, ok := tableIndicatorsMap[ident]
return ok
}
func isSQLKeyword(ident string) bool {
_, ok := keywordsMap[ident]
return ok
}
func isProcedure(token *Token) bool {
if token.Type != IDENT {
return false
}
return token.Value == "PROCEDURE" || token.Value == "procedure" || token.Value == "Procedure" || token.Value == "PROC" || token.Value == "proc" || token.Value == "Proc"
}
func isBoolean(ident string) bool {
// allocation free fast path for common cases
return ident == "true" || ident == "false" || ident == "TRUE" || ident == "FALSE" || ident == "True" || ident == "False"
}
func isNull(ident string) bool {
// allocation free fast path for common cases
return ident == "null" || ident == "NULL" || ident == "Null"
}
func isWith(ident string) bool {
return ident == "WITH" || ident == "with" || ident == "With"
}
func isAs(ident string) bool {
return ident == "AS" || ident == "as" || ident == "As"
}
func isJsonOperator(token *Token) bool {
if token.Type != OPERATOR {
return false
}
_, ok := jsonOperators[token.Value]
return ok
}
func replaceDigits(input string, placeholder string) string {
var builder strings.Builder
n := len(input)
i := 0
for i < n {
// Skip over non-digit characters
start := i
for i < n && !isDigit(rune(input[i])) {
i++
}
// Write non-digit substring (if any)
if start < i {
builder.WriteString(input[start:i])
}
// Replace consecutive digits with the placeholder
if i < n && isDigit(rune(input[i])) {
builder.WriteString(placeholder)
// Skip over all consecutive digits
for i < n && isDigit(rune(input[i])) {
i++
}
}
}
return builder.String()
}
var (
doubleQuotesReplacer = strings.NewReplacer("\"", "")
backQuotesReplacer = strings.NewReplacer("`", "")
bracketQuotesReplacer = strings.NewReplacer("[", "", "]", "")
)
func trimQuotes(input string, delim string, closingDelim string) string {
var replacer *strings.Replacer
switch {
// common quote types get an already allocated replacer
case delim == closingDelim && delim == "\"":
replacer = doubleQuotesReplacer
case delim == closingDelim && delim == "`":
replacer = backQuotesReplacer
case delim == "[" && closingDelim == "]":
replacer = bracketQuotesReplacer
// common case of `delim` and `closingDelim` being the same, gets a simpler replacer
case delim == closingDelim:
replacer = strings.NewReplacer(delim, "")
default:
replacer = strings.NewReplacer(delim, "", closingDelim, "")
}
return replacer.Replace(input)
}