forked from marianogappa/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_test.go
465 lines (458 loc) · 13.5 KB
/
sql_test.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package sqlparser
import (
"fmt"
"io/ioutil"
"log"
"os"
"testing"
"text/template"
"github.com/marianogappa/sqlparser/query"
"github.com/stretchr/testify/require"
)
type testCase struct {
Name string
SQL string
Expected query.Query
Err error
}
type output struct {
NoErrorExamples []testCase
ErrorExamples []testCase
Types []string
Operators []string
}
func TestSQL(t *testing.T) {
ts := []testCase{
{
Name: "empty query fails",
SQL: "",
Expected: query.Query{},
Err: fmt.Errorf("query type cannot be empty"),
},
{
Name: "SELECT without FROM fails",
SQL: "SELECT",
Expected: query.Query{Type: query.Select},
Err: fmt.Errorf("table name cannot be empty"),
},
{
Name: "SELECT without fields fails",
SQL: "SELECT FROM 'a'",
Expected: query.Query{Type: query.Select},
Err: fmt.Errorf("at SELECT: expected field to SELECT"),
},
{
Name: "SELECT with comma and empty field fails",
SQL: "SELECT b, FROM 'a'",
Expected: query.Query{Type: query.Select},
Err: fmt.Errorf("at SELECT: expected field to SELECT"),
},
{
Name: "SELECT works",
SQL: "SELECT a FROM 'b'",
Expected: query.Query{Type: query.Select, TableName: "b", Fields: []string{"a"}},
Err: nil,
},
{
Name: "SELECT works with lowercase",
SQL: "select a fRoM 'b'",
Expected: query.Query{Type: query.Select, TableName: "b", Fields: []string{"a"}},
Err: nil,
},
{
Name: "SELECT many fields works",
SQL: "SELECT a, c, d FROM 'b'",
Expected: query.Query{Type: query.Select, TableName: "b", Fields: []string{"a", "c", "d"}},
Err: nil,
},
{
Name: "SELECT with empty WHERE fails",
SQL: "SELECT a, c, d FROM 'b' WHERE",
Expected: query.Query{Type: query.Select, TableName: "b", Fields: []string{"a", "c", "d"}},
Err: fmt.Errorf("at WHERE: empty WHERE clause"),
},
{
Name: "SELECT with WHERE with only operand fails",
SQL: "SELECT a, c, d FROM 'b' WHERE a",
Expected: query.Query{Type: query.Select, TableName: "b", Fields: []string{"a", "c", "d"}},
Err: fmt.Errorf("at WHERE: condition without operator"),
},
{
Name: "SELECT with WHERE with = works",
SQL: "SELECT a, c, d FROM 'b' WHERE a = ''",
Expected: query.Query{
Type: query.Select,
TableName: "b",
Fields: []string{"a", "c", "d"},
Conditions: []query.Condition{
{Operand1: "a", Operand1IsField: true, Operator: query.Eq, Operand2: "", Operand2IsField: false},
},
},
Err: nil,
},
{
Name: "SELECT with WHERE with < works",
SQL: "SELECT a, c, d FROM 'b' WHERE a < '1'",
Expected: query.Query{
Type: query.Select,
TableName: "b",
Fields: []string{"a", "c", "d"},
Conditions: []query.Condition{
{Operand1: "a", Operand1IsField: true, Operator: query.Lt, Operand2: "1", Operand2IsField: false},
},
},
Err: nil,
},
{
Name: "SELECT with WHERE with <= works",
SQL: "SELECT a, c, d FROM 'b' WHERE a <= '1'",
Expected: query.Query{
Type: query.Select,
TableName: "b",
Fields: []string{"a", "c", "d"},
Conditions: []query.Condition{
{Operand1: "a", Operand1IsField: true, Operator: query.Lte, Operand2: "1", Operand2IsField: false},
},
},
Err: nil,
},
{
Name: "SELECT with WHERE with > works",
SQL: "SELECT a, c, d FROM 'b' WHERE a > '1'",
Expected: query.Query{
Type: query.Select,
TableName: "b",
Fields: []string{"a", "c", "d"},
Conditions: []query.Condition{
{Operand1: "a", Operand1IsField: true, Operator: query.Gt, Operand2: "1", Operand2IsField: false},
},
},
Err: nil,
},
{
Name: "SELECT with WHERE with >= works",
SQL: "SELECT a, c, d FROM 'b' WHERE a >= '1'",
Expected: query.Query{
Type: query.Select,
TableName: "b",
Fields: []string{"a", "c", "d"},
Conditions: []query.Condition{
{Operand1: "a", Operand1IsField: true, Operator: query.Gte, Operand2: "1", Operand2IsField: false},
},
},
Err: nil,
},
{
Name: "SELECT with WHERE with != works",
SQL: "SELECT a, c, d FROM 'b' WHERE a != '1'",
Expected: query.Query{
Type: query.Select,
TableName: "b",
Fields: []string{"a", "c", "d"},
Conditions: []query.Condition{
{Operand1: "a", Operand1IsField: true, Operator: query.Ne, Operand2: "1", Operand2IsField: false},
},
},
Err: nil,
},
{
Name: "SELECT with WHERE with != works (comparing field against another field)",
SQL: "SELECT a, c, d FROM 'b' WHERE a != b",
Expected: query.Query{
Type: query.Select,
TableName: "b",
Fields: []string{"a", "c", "d"},
Conditions: []query.Condition{
{Operand1: "a", Operand1IsField: true, Operator: query.Ne, Operand2: "b", Operand2IsField: true},
},
},
Err: nil,
},
{
Name: "SELECT * works",
SQL: "SELECT * FROM 'b'",
Expected: query.Query{
Type: query.Select,
TableName: "b",
Fields: []string{"*"},
Conditions: nil,
},
Err: nil,
},
{
Name: "SELECT a, * works",
SQL: "SELECT a, * FROM 'b'",
Expected: query.Query{
Type: query.Select,
TableName: "b",
Fields: []string{"a", "*"},
Conditions: nil,
},
Err: nil,
},
{
Name: "SELECT with WHERE with two conditions using AND works",
SQL: "SELECT a, c, d FROM 'b' WHERE a != '1' AND b = '2'",
Expected: query.Query{
Type: query.Select,
TableName: "b",
Fields: []string{"a", "c", "d"},
Conditions: []query.Condition{
{Operand1: "a", Operand1IsField: true, Operator: query.Ne, Operand2: "1", Operand2IsField: false},
{Operand1: "b", Operand1IsField: true, Operator: query.Eq, Operand2: "2", Operand2IsField: false},
},
},
Err: nil,
},
{
Name: "Empty UPDATE fails",
SQL: "UPDATE",
Expected: query.Query{},
Err: fmt.Errorf("table name cannot be empty"),
},
{
Name: "Incomplete UPDATE with table name fails",
SQL: "UPDATE 'a'",
Expected: query.Query{},
Err: fmt.Errorf("at WHERE: WHERE clause is mandatory for UPDATE & DELETE"),
},
{
Name: "Incomplete UPDATE with table name and SET fails",
SQL: "UPDATE 'a' SET",
Expected: query.Query{},
Err: fmt.Errorf("at WHERE: WHERE clause is mandatory for UPDATE & DELETE"),
},
{
Name: "Incomplete UPDATE with table name, SET with a field but no value and WHERE fails",
SQL: "UPDATE 'a' SET b WHERE",
Expected: query.Query{},
Err: fmt.Errorf("at UPDATE: expected '='"),
},
{
Name: "Incomplete UPDATE with table name, SET with a field and = but no value and WHERE fails",
SQL: "UPDATE 'a' SET b = WHERE",
Expected: query.Query{},
Err: fmt.Errorf("at UPDATE: expected quoted value"),
},
{
Name: "Incomplete UPDATE due to no WHERE clause fails",
SQL: "UPDATE 'a' SET b = 'hello' WHERE",
Expected: query.Query{},
Err: fmt.Errorf("at WHERE: empty WHERE clause"),
},
{
Name: "Incomplete UPDATE due incomplete WHERE clause fails",
SQL: "UPDATE 'a' SET b = 'hello' WHERE a",
Expected: query.Query{},
Err: fmt.Errorf("at WHERE: condition without operator"),
},
{
Name: "UPDATE works",
SQL: "UPDATE 'a' SET b = 'hello' WHERE a = '1'",
Expected: query.Query{
Type: query.Update,
TableName: "a",
Updates: map[string]string{"b": "hello"},
Conditions: []query.Condition{
{Operand1: "a", Operand1IsField: true, Operator: query.Eq, Operand2: "1", Operand2IsField: false},
},
},
Err: nil,
},
{
Name: "UPDATE works with simple quote inside",
SQL: "UPDATE 'a' SET b = 'hello\\'world' WHERE a = '1'",
Expected: query.Query{
Type: query.Update,
TableName: "a",
Updates: map[string]string{"b": "hello\\'world"},
Conditions: []query.Condition{
{Operand1: "a", Operand1IsField: true, Operator: query.Eq, Operand2: "1", Operand2IsField: false},
},
},
Err: nil,
},
{
Name: "UPDATE with multiple SETs works",
SQL: "UPDATE 'a' SET b = 'hello', c = 'bye' WHERE a = '1'",
Expected: query.Query{
Type: query.Update,
TableName: "a",
Updates: map[string]string{"b": "hello", "c": "bye"},
Conditions: []query.Condition{
{Operand1: "a", Operand1IsField: true, Operator: query.Eq, Operand2: "1", Operand2IsField: false},
},
},
Err: nil,
},
{
Name: "UPDATE with multiple SETs and multiple conditions works",
SQL: "UPDATE 'a' SET b = 'hello', c = 'bye' WHERE a = '1' AND b = '789'",
Expected: query.Query{
Type: query.Update,
TableName: "a",
Updates: map[string]string{"b": "hello", "c": "bye"},
Conditions: []query.Condition{
{Operand1: "a", Operand1IsField: true, Operator: query.Eq, Operand2: "1", Operand2IsField: false},
{Operand1: "b", Operand1IsField: true, Operator: query.Eq, Operand2: "789", Operand2IsField: false},
},
},
Err: nil,
},
{
Name: "Empty DELETE fails",
SQL: "DELETE FROM",
Expected: query.Query{},
Err: fmt.Errorf("table name cannot be empty"),
},
{
Name: "DELETE without WHERE fails",
SQL: "DELETE FROM 'a'",
Expected: query.Query{},
Err: fmt.Errorf("at WHERE: WHERE clause is mandatory for UPDATE & DELETE"),
},
{
Name: "DELETE with empty WHERE fails",
SQL: "DELETE FROM 'a' WHERE",
Expected: query.Query{},
Err: fmt.Errorf("at WHERE: empty WHERE clause"),
},
{
Name: "DELETE with WHERE with field but no operator fails",
SQL: "DELETE FROM 'a' WHERE b",
Expected: query.Query{},
Err: fmt.Errorf("at WHERE: condition without operator"),
},
{
Name: "DELETE with WHERE works",
SQL: "DELETE FROM 'a' WHERE b = '1'",
Expected: query.Query{
Type: query.Delete,
TableName: "a",
Conditions: []query.Condition{
{Operand1: "b", Operand1IsField: true, Operator: query.Eq, Operand2: "1", Operand2IsField: false},
},
},
Err: nil,
},
{
Name: "Empty INSERT fails",
SQL: "INSERT INTO",
Expected: query.Query{},
Err: fmt.Errorf("table name cannot be empty"),
},
{
Name: "INSERT with no rows to insert fails",
SQL: "INSERT INTO 'a'",
Expected: query.Query{},
Err: fmt.Errorf("at INSERT INTO: need at least one row to insert"),
},
{
Name: "INSERT with incomplete value section fails",
SQL: "INSERT INTO 'a' (",
Expected: query.Query{},
Err: fmt.Errorf("at INSERT INTO: need at least one row to insert"),
},
{
Name: "INSERT with incomplete value section fails #2",
SQL: "INSERT INTO 'a' (b",
Expected: query.Query{},
Err: fmt.Errorf("at INSERT INTO: need at least one row to insert"),
},
{
Name: "INSERT with incomplete value section fails #3",
SQL: "INSERT INTO 'a' (b)",
Expected: query.Query{},
Err: fmt.Errorf("at INSERT INTO: need at least one row to insert"),
},
{
Name: "INSERT with incomplete value section fails #4",
SQL: "INSERT INTO 'a' (b) VALUES",
Expected: query.Query{},
Err: fmt.Errorf("at INSERT INTO: need at least one row to insert"),
},
{
Name: "INSERT with incomplete row fails",
SQL: "INSERT INTO 'a' (b) VALUES (",
Expected: query.Query{},
Err: fmt.Errorf("at INSERT INTO: value count doesn't match field count"),
},
{
Name: "INSERT works",
SQL: "INSERT INTO 'a' (b) VALUES ('1')",
Expected: query.Query{
Type: query.Insert,
TableName: "a",
Fields: []string{"b"},
Inserts: [][]string{{"1"}},
},
Err: nil,
},
{
Name: "INSERT * fails",
SQL: "INSERT INTO 'a' (*) VALUES ('1')",
Expected: query.Query{},
Err: fmt.Errorf("at INSERT INTO: expected at least one field to insert"),
},
{
Name: "INSERT with multiple fields works",
SQL: "INSERT INTO 'a' (b,c, d) VALUES ('1','2' , '3' )",
Expected: query.Query{
Type: query.Insert,
TableName: "a",
Fields: []string{"b", "c", "d"},
Inserts: [][]string{{"1", "2", "3"}},
},
Err: nil,
},
{
Name: "INSERT with multiple fields and multiple values works",
SQL: "INSERT INTO 'a' (b,c, d) VALUES ('1','2' , '3' ),('4','5' ,'6' )",
Expected: query.Query{
Type: query.Insert,
TableName: "a",
Fields: []string{"b", "c", "d"},
Inserts: [][]string{{"1", "2", "3"}, {"4", "5", "6"}},
},
Err: nil,
},
}
output := output{Types: query.TypeString, Operators: query.OperatorString}
for _, tc := range ts {
t.Run(tc.Name, func(t *testing.T) {
actual, err := ParseMany([]string{tc.SQL})
if tc.Err != nil && err == nil {
t.Errorf("Error should have been %v", tc.Err)
}
if tc.Err == nil && err != nil {
t.Errorf("Error should have been nil but was %v", err)
}
if tc.Err != nil && err != nil {
require.Equal(t, tc.Err, err, "Unexpected error")
}
if len(actual) > 0 {
require.Equal(t, tc.Expected, actual[0], "Query didn't match expectation")
}
if tc.Err != nil {
output.ErrorExamples = append(output.ErrorExamples, tc)
} else {
output.NoErrorExamples = append(output.NoErrorExamples, tc)
}
})
}
createReadme(output)
}
func createReadme(out output) {
content, err := ioutil.ReadFile("README.template")
if err != nil {
log.Fatal(err)
}
t := template.Must(template.New("").Parse(string(content)))
f, err := os.Create("README.md")
if err != nil {
log.Fatal(err)
}
if err := t.Execute(f, out); err != nil {
log.Fatal(err)
}
}