forked from longbridgeapp/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
978 lines (943 loc) · 36.1 KB
/
parser_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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
package sqlparser_test
import (
"strings"
"testing"
"github.com/go-test/deep"
"github.com/longbridgeapp/sqlparser"
)
func TestParser_ParseStatement(t *testing.T) {
t.Run("ErrNoStatement", func(t *testing.T) {
AssertParseStatementError(t, `123`, `1:1: expected statement, found 123`)
})
t.Run("Select", func(t *testing.T) {
AssertParseStatement(t, `SELECT * FROM tbl`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
})
AssertParseStatement(t, `SELECT /* hint */ * FROM tbl`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
Hint: &sqlparser.Hint{
Value: "hint",
},
})
AssertParseStatement(t, `SELECT DISTINCT * FROM tbl`, &sqlparser.SelectStatement{
Distinct: true,
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
})
AssertParseStatement(t, `SELECT ALL * FROM tbl`, &sqlparser.SelectStatement{
All: true,
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
})
AssertParseStatement(t, `SELECT foo AS FOO, bar baz, tbl.* FROM tbl`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{
&sqlparser.ResultColumn{
Expr: &sqlparser.Ident{Name: "foo"},
Alias: &sqlparser.Ident{Name: "FOO"},
},
&sqlparser.ResultColumn{
Expr: &sqlparser.Ident{Name: "bar"},
Alias: &sqlparser.Ident{Name: "baz"},
},
&sqlparser.ResultColumn{
Expr: &sqlparser.QualifiedRef{
Table: &sqlparser.Ident{Name: "tbl"},
Star: true,
},
},
},
FromItems: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
})
AssertParseStatement(t, `SELECT * FROM tbl tbl2`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
Alias: &sqlparser.Ident{Name: "tbl2"},
},
})
AssertParseStatement(t, `SELECT * FROM tbl AS tbl2`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
Alias: &sqlparser.Ident{Name: "tbl2"},
},
})
AssertParseStatement(t, `SELECT * FROM (SELECT *) AS tbl`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.ParenSource{
X: &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
},
Alias: &sqlparser.Ident{Name: "tbl"},
},
})
AssertParseStatement(t, `SELECT * FROM foo, bar`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.JoinClause{
X: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "foo"},
},
Operator: &sqlparser.JoinOperator{Comma: true},
Y: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "bar"},
},
},
})
AssertParseStatement(t, `SELECT * FROM foo JOIN bar`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.JoinClause{
X: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "foo"},
},
Operator: &sqlparser.JoinOperator{},
Y: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "bar"},
},
},
})
AssertParseStatement(t, `SELECT * FROM foo NATURAL JOIN bar`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.JoinClause{
X: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "foo"},
},
Operator: &sqlparser.JoinOperator{Natural: true},
Y: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "bar"},
},
},
})
AssertParseStatement(t, `SELECT * FROM foo INNER JOIN bar ON true`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.JoinClause{
X: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "foo"},
},
Operator: &sqlparser.JoinOperator{Inner: true},
Y: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "bar"},
},
Constraint: &sqlparser.OnConstraint{
X: &sqlparser.BoolLit{Value: true},
},
},
})
AssertParseStatement(t, `SELECT * FROM foo LEFT JOIN bar USING (x, y)`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.JoinClause{
X: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "foo"},
},
Operator: &sqlparser.JoinOperator{Left: true},
Y: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "bar"},
},
Constraint: &sqlparser.UsingConstraint{
Columns: []*sqlparser.Ident{
{Name: "x"},
{Name: "y"},
},
},
},
})
AssertParseStatement(t, `SELECT * FROM X INNER JOIN Y ON true INNER JOIN Z ON false`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.JoinClause{
X: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "X"},
},
Operator: &sqlparser.JoinOperator{Inner: true},
Y: &sqlparser.JoinClause{
X: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "Y"},
},
Operator: &sqlparser.JoinOperator{Inner: true},
Y: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "Z"},
},
Constraint: &sqlparser.OnConstraint{
X: &sqlparser.BoolLit{Value: false},
},
},
Constraint: &sqlparser.OnConstraint{
X: &sqlparser.BoolLit{Value: true},
},
},
})
AssertParseStatement(t, `SELECT * FROM foo LEFT OUTER JOIN bar`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.JoinClause{
X: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "foo"},
},
Operator: &sqlparser.JoinOperator{Left: true, Outer: true},
Y: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "bar"},
},
},
})
AssertParseStatement(t, `SELECT * FROM foo CROSS JOIN bar`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
FromItems: &sqlparser.JoinClause{
X: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "foo"},
},
Operator: &sqlparser.JoinOperator{Cross: true},
Y: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "bar"},
},
},
})
AssertParseStatement(t, `SELECT * WHERE true`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{Star: true}},
Condition: &sqlparser.BoolLit{Value: true},
})
AssertParseStatement(t, `SELECT * GROUP BY foo, bar`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{Star: true}},
GroupingElements: []sqlparser.Expr{
&sqlparser.Ident{Name: "foo"},
&sqlparser.Ident{Name: "bar"},
},
})
AssertParseStatement(t, `SELECT * GROUP BY foo HAVING true`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{Star: true}},
GroupingElements: []sqlparser.Expr{
&sqlparser.Ident{Name: "foo"},
},
HavingCondition: &sqlparser.BoolLit{Value: true},
})
AssertParseStatement(t, `SELECT * ORDER BY foo ASC, bar DESC`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
OrderBy: []*sqlparser.OrderingTerm{
{X: &sqlparser.Ident{Name: "foo"}, Asc: true},
{X: &sqlparser.Ident{Name: "bar"}, Desc: true},
},
})
AssertParseStatement(t, `SELECT * LIMIT 1`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
Limit: &sqlparser.NumberLit{Value: "1"},
})
AssertParseStatement(t, `SELECT * LIMIT 1 OFFSET 2`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
Limit: &sqlparser.NumberLit{Value: "1"},
Offset: &sqlparser.NumberLit{Value: "2"},
})
AssertParseStatement(t, `SELECT * LIMIT 1, 2`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
Limit: &sqlparser.NumberLit{Value: "1"},
Offset: &sqlparser.NumberLit{Value: "2"},
})
AssertParseStatement(t, `SELECT * UNION SELECT * ORDER BY foo`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
Union: true,
Compound: &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
},
OrderBy: []*sqlparser.OrderingTerm{
{X: &sqlparser.Ident{Name: "foo"}},
},
})
AssertParseStatement(t, `SELECT * UNION ALL SELECT *`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
Union: true,
UnionAll: true,
Compound: &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
},
})
AssertParseStatement(t, `SELECT * INTERSECT SELECT *`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
Intersect: true,
Compound: &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
},
})
AssertParseStatement(t, `SELECT * EXCEPT SELECT *`, &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
Except: true,
Compound: &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
},
})
AssertParseStatementError(t, `SELECT `, `1:7: expected expression, found 'EOF'`)
AssertParseStatementError(t, `SELECT 1+`, `1:9: expected expression, found 'EOF'`)
AssertParseStatementError(t, `SELECT foo,`, `1:11: expected expression, found 'EOF'`)
AssertParseStatementError(t, `SELECT foo AS`, `1:13: expected column alias, found 'EOF'`)
AssertParseStatementError(t, `SELECT foo.* AS`, `1:14: expected semicolon or EOF, found 'AS'`)
AssertParseStatementError(t, `SELECT foo FROM`, `1:15: expected table name or left paren, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM foo INNER`, `1:23: expected JOIN, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM foo CROSS`, `1:23: expected JOIN, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM foo NATURAL`, `1:25: expected JOIN, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM foo LEFT`, `1:22: expected JOIN, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM foo LEFT OUTER`, `1:28: expected JOIN, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM foo,`, `1:18: expected table name or left paren, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM foo JOIN bar ON`, `1:29: expected expression, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM foo JOIN bar USING`, `1:32: expected left paren, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM foo JOIN bar USING (`, `1:34: expected column name, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM foo JOIN bar USING (x`, `1:35: expected comma or right paren, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM foo JOIN bar USING (x,`, `1:36: expected column name, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM (`, `1:15: expected table name or left paren, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM ((`, `1:16: expected table name or left paren, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM (SELECT`, `1:21: expected expression, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM (tbl`, `1:18: expected right paren, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM (SELECT *) AS`, `1:27: expected table alias, found 'EOF'`)
AssertParseStatementError(t, `SELECT * FROM foo AS`, `1:20: expected table alias, found 'EOF'`)
AssertParseStatementError(t, `SELECT foo WHERE`, `1:16: expected expression, found 'EOF'`)
AssertParseStatementError(t, `SELECT * GROUP`, `1:14: expected BY, found 'EOF'`)
AssertParseStatementError(t, `SELECT * GROUP BY`, `1:17: expected expression, found 'EOF'`)
AssertParseStatementError(t, `SELECT * GROUP BY foo bar`, `1:23: expected semicolon or EOF, found bar`)
AssertParseStatementError(t, `SELECT * GROUP BY foo HAVING`, `1:28: expected expression, found 'EOF'`)
AssertParseStatementError(t, `SELECT * ORDER`, `1:14: expected BY, found 'EOF'`)
AssertParseStatementError(t, `SELECT * ORDER BY`, `1:17: expected expression, found 'EOF'`)
AssertParseStatementError(t, `SELECT * ORDER BY 1,`, `1:20: expected expression, found 'EOF'`)
AssertParseStatementError(t, `SELECT * LIMIT`, `1:14: expected expression, found 'EOF'`)
AssertParseStatementError(t, `SELECT * LIMIT 1,`, `1:17: expected expression, found 'EOF'`)
AssertParseStatementError(t, `SELECT * LIMIT 1 OFFSET`, `1:23: expected expression, found 'EOF'`)
AssertParseStatementError(t, `SELECT * UNION`, `1:14: expected SELECT, found 'EOF'`)
})
t.Run("Insert", func(t *testing.T) {
AssertParseStatement(t, `INSERT INTO tbl (x, y) VALUES (1, 2)`, &sqlparser.InsertStatement{
TableName: &sqlparser.TableName{Name: &sqlparser.Ident{Name: "tbl"}},
ColumnNames: []*sqlparser.Ident{
{Name: "x"},
{Name: "y"},
},
Expressions: []*sqlparser.Exprs{{
Exprs: []sqlparser.Expr{
&sqlparser.NumberLit{Value: "1"},
&sqlparser.NumberLit{Value: "2"},
},
}},
})
AssertParseStatement(t, `INSERT INTO tbl (x) SELECT y`, &sqlparser.InsertStatement{
TableName: &sqlparser.TableName{Name: &sqlparser.Ident{Name: "tbl"}},
ColumnNames: []*sqlparser.Ident{{Name: "x"}},
Query: &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Expr: &sqlparser.Ident{Name: "y"},
}},
},
})
AssertParseStatement(t, `INSERT INTO tbl (x) DEFAULT VALUES`, &sqlparser.InsertStatement{
TableName: &sqlparser.TableName{Name: &sqlparser.Ident{Name: "tbl"}},
ColumnNames: []*sqlparser.Ident{{Name: "x"}},
DefaultValues: true,
})
AssertParseStatement(t, `INSERT INTO tbl AS tbl2 (x) DEFAULT VALUES`, &sqlparser.InsertStatement{
TableName: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
Alias: &sqlparser.Ident{Name: "tbl2"},
},
ColumnNames: []*sqlparser.Ident{{Name: "x"}},
DefaultValues: true,
})
AssertParseStatement(t, `INSERT INTO tbl (x) VALUES (1) ON CONFLICT (y ASC, z DESC) DO NOTHING`, &sqlparser.InsertStatement{
TableName: &sqlparser.TableName{Name: &sqlparser.Ident{Name: "tbl"}},
ColumnNames: []*sqlparser.Ident{{Name: "x"}},
Expressions: []*sqlparser.Exprs{{
Exprs: []sqlparser.Expr{
&sqlparser.NumberLit{Value: "1"},
},
}},
UpsertClause: &sqlparser.UpsertClause{
Columns: []*sqlparser.IndexedColumn{
{X: &sqlparser.Ident{Name: "y"}, Asc: true},
{X: &sqlparser.Ident{Name: "z"}, Desc: true},
},
DoNothing: true,
},
})
AssertParseStatement(t, `INSERT INTO tbl (x) VALUES (1) ON CONFLICT (y) WHERE true DO UPDATE SET foo = 1, (bar, baz) = 2 WHERE false`, &sqlparser.InsertStatement{
TableName: &sqlparser.TableName{Name: &sqlparser.Ident{Name: "tbl"}},
ColumnNames: []*sqlparser.Ident{{Name: "x"}},
Expressions: []*sqlparser.Exprs{{
Exprs: []sqlparser.Expr{
&sqlparser.NumberLit{Value: "1"},
},
}},
UpsertClause: &sqlparser.UpsertClause{
Columns: []*sqlparser.IndexedColumn{
{X: &sqlparser.Ident{Name: "y"}},
},
WhereExpr: &sqlparser.BoolLit{Value: true},
DoUpdate: true,
Assignments: []*sqlparser.Assignment{
{
Columns: []*sqlparser.Ident{
{Name: "foo"},
},
Expr: &sqlparser.NumberLit{Value: "1"},
},
{
Columns: []*sqlparser.Ident{
{Name: "bar"},
{Name: "baz"},
},
Expr: &sqlparser.NumberLit{Value: "2"},
},
},
UpdateWhereExpr: &sqlparser.BoolLit{Value: false},
},
})
AssertParseStatementError(t, `INSERT`, `1:6: expected INTO, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO`, `1:11: expected table name, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl AS`, `1:18: expected table alias, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl `, `1:16: expected DEFAULT VALUES, VALUES or SELECT, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (`, `1:17: expected column name, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x`, `1:18: expected comma or right paren, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x)`, `1:19: expected DEFAULT VALUES, VALUES or SELECT, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES`, `1:26: expected left paren, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (`, `1:28: expected expression, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1`, `1:29: expected comma or right paren, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) SELECT`, `1:26: expected expression, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) DEFAULT`, `1:27: expected VALUES, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1) ON`, `1:33: expected CONFLICT, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1) ON CONFLICT (`, `1:44: expected expression, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1) ON CONFLICT (x`, `1:45: expected comma or right paren, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1) ON CONFLICT (x) WHERE`, `1:52: expected expression, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1) ON CONFLICT (x)`, `1:46: expected DO, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1) ON CONFLICT (x) DO`, `1:49: expected NOTHING or UPDATE SET, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1) ON CONFLICT (x) DO UPDATE`, `1:56: expected SET, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1) ON CONFLICT (x) DO UPDATE SET foo`, `1:64: expected =, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1) ON CONFLICT (x) DO UPDATE SET foo =`, `1:66: expected expression, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1) ON CONFLICT (x) DO UPDATE SET foo = 1 WHERE`, `1:74: expected expression, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1) ON CONFLICT (x) DO UPDATE SET (`, `1:62: expected column name, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1) ON CONFLICT (x) DO UPDATE SET (foo`, `1:65: expected comma or right paren, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl (x) VALUES (1) RETURNING`, `1:40: expected expression, found 'EOF'`)
})
t.Run("Update", func(t *testing.T) {
AssertParseStatement(t, `UPDATE tbl SET x = 1, y = 2`, &sqlparser.UpdateStatement{
TableName: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
Assignments: []*sqlparser.Assignment{
{
Columns: []*sqlparser.Ident{{Name: "x"}},
Expr: &sqlparser.NumberLit{Value: "1"},
},
{
Columns: []*sqlparser.Ident{{Name: "y"}},
Expr: &sqlparser.NumberLit{Value: "2"},
},
},
})
AssertParseStatement(t, `UPDATE tbl SET x = 1 WHERE y = 2`, &sqlparser.UpdateStatement{
TableName: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
Assignments: []*sqlparser.Assignment{{
Columns: []*sqlparser.Ident{{Name: "x"}},
Expr: &sqlparser.NumberLit{Value: "1"},
}},
Condition: &sqlparser.BinaryExpr{
X: &sqlparser.Ident{Name: "y"},
Op: sqlparser.EQ,
Y: &sqlparser.NumberLit{Value: "2"},
},
})
AssertParseStatementError(t, `UPDATE`, `1:6: expected table name, found 'EOF'`)
AssertParseStatementError(t, `UPDATE tbl`, `1:10: expected SET, found 'EOF'`)
AssertParseStatementError(t, `UPDATE tbl SET`, `1:14: expected column name or column list, found 'EOF'`)
AssertParseStatementError(t, `UPDATE tbl SET x = `, `1:19: expected expression, found 'EOF'`)
AssertParseStatementError(t, `UPDATE tbl SET x = 1 WHERE`, `1:26: expected expression, found 'EOF'`)
AssertParseStatementError(t, `UPDATE tbl SET x = 1 WHERE y =`, `1:30: expected expression, found 'EOF'`)
})
t.Run("Delete", func(t *testing.T) {
AssertParseStatement(t, `DELETE FROM tbl`, &sqlparser.DeleteStatement{
TableName: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
})
AssertParseStatement(t, `DELETE FROM tbl WHERE x = 1`, &sqlparser.DeleteStatement{
TableName: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
Condition: &sqlparser.BinaryExpr{
X: &sqlparser.Ident{Name: "x"},
Op: sqlparser.EQ,
Y: &sqlparser.NumberLit{Value: "1"},
},
})
AssertParseStatement(t, `DELETE FROM ONLY tbl`, &sqlparser.DeleteStatement{
Only: true,
TableName: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
})
/*
AssertParseStatement(t, `DELETE FROM tbl USING tbl2, tbl3`, &sqlparser.DeleteStatement{
TableName: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
UsingList: []*sqlparser.TableName{
&sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
&sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
},
})*/
AssertParseStatement(t, `DELETE FROM tbl WHERE CURRENT OF c_tasks`, &sqlparser.DeleteStatement{
TableName: &sqlparser.TableName{
Name: &sqlparser.Ident{Name: "tbl"},
},
CursorName: &sqlparser.Ident{Name: "c_tasks"},
})
AssertParseStatementError(t, `DELETE FROM`, `1:11: expected table name, found 'EOF'`)
AssertParseStatementError(t, `DELETE FROM tbl WHERE`, `1:21: expected expression, found 'EOF'`)
})
}
func TestParser_ParseExpr(t *testing.T) {
t.Run("Ident", func(t *testing.T) {
AssertParseExpr(t, `fooBAR_123'`, &sqlparser.Ident{Name: `fooBAR_123`})
})
t.Run("StringLit", func(t *testing.T) {
AssertParseExpr(t, `'foo bar'`, &sqlparser.StringLit{Value: `foo bar`})
})
t.Run("BlobLit", func(t *testing.T) {
AssertParseExpr(t, `x'0123'`, &sqlparser.BlobLit{Value: `0123`})
})
t.Run("Integer", func(t *testing.T) {
AssertParseExpr(t, `123`, &sqlparser.NumberLit{Value: `123`})
})
t.Run("Float", func(t *testing.T) {
AssertParseExpr(t, `123.456`, &sqlparser.NumberLit{Value: `123.456`})
})
t.Run("Null", func(t *testing.T) {
AssertParseExpr(t, `NULL`, &sqlparser.NullLit{})
})
t.Run("Bool", func(t *testing.T) {
AssertParseExpr(t, `true`, &sqlparser.BoolLit{Value: true})
AssertParseExpr(t, `false`, &sqlparser.BoolLit{Value: false})
})
t.Run("Bind", func(t *testing.T) {
AssertParseExpr(t, `$bar`, &sqlparser.BindExpr{Name: "$bar"})
})
t.Run("UnaryExpr", func(t *testing.T) {
AssertParseExpr(t, `-123`, &sqlparser.UnaryExpr{Op: sqlparser.MINUS, X: &sqlparser.NumberLit{Value: `123`}})
AssertParseExprError(t, `-`, `1:1: expected expression, found 'EOF'`)
})
t.Run("QualifiedRef", func(t *testing.T) {
AssertParseExpr(t, `tbl.col`, &sqlparser.QualifiedRef{
Table: &sqlparser.Ident{Name: "tbl"},
Column: &sqlparser.Ident{Name: "col"},
})
AssertParseExpr(t, `"tbl"."col"`, &sqlparser.QualifiedRef{
Table: &sqlparser.Ident{Name: "tbl", Quoted: true, QuoteChar: `"`},
Column: &sqlparser.Ident{Name: "col", Quoted: true, QuoteChar: `"`},
})
AssertParseExpr(t, "`tbl`.`col`", &sqlparser.QualifiedRef{
Table: &sqlparser.Ident{Name: "tbl", Quoted: true, QuoteChar: "`"},
Column: &sqlparser.Ident{Name: "col", Quoted: true, QuoteChar: "`"},
})
AssertParseExprError(t, `tbl.`, `1:4: expected column name, found 'EOF'`)
})
t.Run("Exists", func(t *testing.T) {
AssertParseExpr(t, `EXISTS (SELECT *)`, &sqlparser.Exists{
Select: &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
},
})
AssertParseExpr(t, `NOT EXISTS (SELECT *)`, &sqlparser.Exists{
Not: true,
Select: &sqlparser.SelectStatement{
Columns: &sqlparser.OutputNames{&sqlparser.ResultColumn{
Star: true,
}},
},
})
AssertParseExprError(t, `NOT`, `1:3: expected EXISTS, found 'EOF'`)
AssertParseExprError(t, `EXISTS`, `1:6: expected left paren, found 'EOF'`)
AssertParseExprError(t, `EXISTS (`, `1:8: expected SELECT, found 'EOF'`)
AssertParseExprError(t, `EXISTS (SELECT`, `1:14: expected expression, found 'EOF'`)
AssertParseExprError(t, `EXISTS (SELECT *`, `1:16: expected right paren, found 'EOF'`)
})
t.Run("BinaryExpr", func(t *testing.T) {
AssertParseExpr(t, `1 + 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.PLUS,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 - 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.MINUS,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 * 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.STAR,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 / 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.SLASH,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 % 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.REM,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 || 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.CONCAT,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 << 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.LSHIFT,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 >> 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.RSHIFT,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 & 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.BITAND,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 | 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.BITOR,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 < 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.LT,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 <= 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.LE,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 <> 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.LG,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 > 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.GT,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 >= 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.GE,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 = 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.EQ,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 != 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.NE,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `(1 + 2)'`, &sqlparser.ParenExpr{
X: &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.PLUS,
Y: &sqlparser.NumberLit{Value: "2"},
},
})
AssertParseExprError(t, `(`, `1:1: expected expression, found 'EOF'`)
AssertParseExpr(t, `1 IS 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.IS,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 IS NOT 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.ISNOT,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 LIKE 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.LIKE,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 NOT LIKE 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.NOTLIKE,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 GLOB 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.GLOB,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 NOT GLOB 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.NOTGLOB,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 REGEXP 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.REGEXP,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 NOT REGEXP 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.NOTREGEXP,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 MATCH 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.MATCH,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExpr(t, `1 NOT MATCH 2'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.NOTMATCH,
Y: &sqlparser.NumberLit{Value: "2"},
})
AssertParseExprError(t, `1 NOT TABLE`, `1:7: expected IN, LIKE, GLOB, REGEXP, MATCH, or BETWEEN, found 'TABLE'`)
AssertParseExpr(t, `1 IN (2, 3)'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.IN,
Y: &sqlparser.Exprs{
Exprs: []sqlparser.Expr{
&sqlparser.NumberLit{Value: "2"},
&sqlparser.NumberLit{Value: "3"},
},
},
})
AssertParseExpr(t, `1 NOT IN (2, 3)'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.NOTIN,
Y: &sqlparser.Exprs{
Exprs: []sqlparser.Expr{
&sqlparser.NumberLit{Value: "2"},
&sqlparser.NumberLit{Value: "3"},
},
},
})
AssertParseExprError(t, `1 IN 2`, `1:6: expected left paren, found 2`)
AssertParseExprError(t, `1 IN (`, `1:6: expected expression, found 'EOF'`)
AssertParseExprError(t, `1 IN (2 3`, `1:9: expected comma or right paren, found 3`)
AssertParseExpr(t, `1 BETWEEN 2 AND 3'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.BETWEEN,
Y: &sqlparser.Range{
X: &sqlparser.NumberLit{Value: "2"},
Y: &sqlparser.NumberLit{Value: "3"},
},
})
AssertParseExpr(t, `1 NOT BETWEEN 2 AND 3'`, &sqlparser.BinaryExpr{
X: &sqlparser.NumberLit{Value: "1"},
Op: sqlparser.NOTBETWEEN,
Y: &sqlparser.Range{
X: &sqlparser.NumberLit{Value: "2"},
Y: &sqlparser.NumberLit{Value: "3"},
},
})
AssertParseExprError(t, `1 BETWEEN`, `1:9: expected expression, found 'EOF'`)
AssertParseExprError(t, `1 BETWEEN 2`, `1:11: expected range expression, found 'EOF'`)
AssertParseExprError(t, `1 BETWEEN 2 + 3`, `1:15: expected range expression, found 'EOF'`)
AssertParseExprError(t, `1 + `, `1:4: expected expression, found 'EOF'`)
})
t.Run("Call", func(t *testing.T) {
AssertParseExpr(t, `sum()`, &sqlparser.Call{
Name: &sqlparser.Ident{Name: "sum"},
})
AssertParseExpr(t, `sum(*)`, &sqlparser.Call{
Name: &sqlparser.Ident{Name: "sum"},
Star: true,
})
AssertParseExpr(t, `sum(foo, 123)`, &sqlparser.Call{
Name: &sqlparser.Ident{Name: "sum"},
Args: []sqlparser.Expr{
&sqlparser.Ident{Name: "foo"},
&sqlparser.NumberLit{Value: "123"},
},
})
AssertParseExpr(t, `sum(distinct 'foo')`, &sqlparser.Call{
Name: &sqlparser.Ident{Name: "sum"},
Distinct: true,
Args: []sqlparser.Expr{
&sqlparser.StringLit{Value: "foo"},
},
})
AssertParseExpr(t, `sum() filter (where true)`, &sqlparser.Call{
Name: &sqlparser.Ident{Name: "sum"},
Filter: &sqlparser.FilterClause{
X: &sqlparser.BoolLit{Value: true},
},
})
AssertParseExprError(t, `sum(`, `1:4: expected expression, found 'EOF'`)
AssertParseExprError(t, `sum(*`, `1:5: expected right paren, found 'EOF'`)
AssertParseExprError(t, `sum(foo foo`, `1:9: expected comma or right paren, found foo`)
AssertParseExprError(t, `sum() filter`, `1:12: expected left paren, found 'EOF'`)
AssertParseExprError(t, `sum() filter (`, `1:14: expected WHERE, found 'EOF'`)
AssertParseExprError(t, `sum() filter (where`, `1:19: expected expression, found 'EOF'`)
AssertParseExprError(t, `sum() filter (where true`, `1:24: expected right paren, found 'EOF'`)
})
t.Run("Case", func(t *testing.T) {
AssertParseExpr(t, `CASE 1 WHEN 2 THEN 3 WHEN 4 THEN 5 ELSE 6 END`, &sqlparser.CaseExpr{
Operand: &sqlparser.NumberLit{Value: "1"},
Blocks: []*sqlparser.CaseBlock{
{
Condition: &sqlparser.NumberLit{Value: "2"},
Body: &sqlparser.NumberLit{Value: "3"},
},
{
Condition: &sqlparser.NumberLit{Value: "4"},
Body: &sqlparser.NumberLit{Value: "5"},
},
},
ElseExpr: &sqlparser.NumberLit{Value: "6"},
})
AssertParseExpr(t, `CASE WHEN 1 THEN 2 END`, &sqlparser.CaseExpr{
Blocks: []*sqlparser.CaseBlock{
{
Condition: &sqlparser.NumberLit{Value: "1"},
Body: &sqlparser.NumberLit{Value: "2"},
},
},
})
AssertParseExprError(t, `CASE`, `1:4: expected expression, found 'EOF'`)
AssertParseExprError(t, `CASE 1`, `1:6: expected WHEN, found 'EOF'`)
AssertParseExprError(t, `CASE WHEN`, `1:9: expected expression, found 'EOF'`)
AssertParseExprError(t, `CASE WHEN 1`, `1:11: expected THEN, found 'EOF'`)
AssertParseExprError(t, `CASE WHEN 1 THEN`, `1:16: expected expression, found 'EOF'`)
AssertParseExprError(t, `CASE WHEN 1 THEN 2`, `1:18: expected WHEN, ELSE or END, found 'EOF'`)
AssertParseExprError(t, `CASE WHEN 1 THEN 2 ELSE`, `1:23: expected expression, found 'EOF'`)
AssertParseExprError(t, `CASE WHEN 1 THEN 2 ELSE 3`, `1:25: expected END, found 'EOF'`)
})
}
func TestError_Error(t *testing.T) {
err := &sqlparser.Error{Msg: "test"}
if got, want := err.Error(), `test`; got != want {
t.Fatalf("Error()=%s, want %s", got, want)
}
}
// ParseStatementOrFail parses a statement from s. Fail on error.
func ParseStatementOrFail(tb testing.TB, s string) sqlparser.Statement {
tb.Helper()
stmt, err := sqlparser.NewParser(strings.NewReader(s)).ParseStatement()
if err != nil {
tb.Fatal(err)
}
return stmt
}
// AssertParseStatement asserts the value of the first parse of s.
func AssertParseStatement(tb testing.TB, s string, want sqlparser.Statement) {
tb.Helper()
stmt, err := sqlparser.NewParser(strings.NewReader(s)).ParseStatement()
if err != nil {
tb.Fatal(err)
} else if diff := deep.Equal(stmt, want); diff != nil {
tb.Fatalf("mismatch:\n%s", strings.Join(diff, "\n"))
}
}
// AssertParseStatementError asserts s parses to a given error string.
func AssertParseStatementError(tb testing.TB, s string, want string) {
tb.Helper()
_, err := sqlparser.NewParser(strings.NewReader(s)).ParseStatement()
if err == nil || err.Error() != want {
tb.Fatalf("ParseStatement()=%q, want %q", err, want)
}
}
// ParseExprOrFail parses a expression from s. Fail on error.
func ParseExprOrFail(tb testing.TB, s string) sqlparser.Expr {
tb.Helper()
stmt, err := sqlparser.NewParser(strings.NewReader(s)).ParseExpr()
if err != nil {
tb.Fatal(err)
}
return stmt
}
// AssertParseExpr asserts the value of the first parse of s.
func AssertParseExpr(tb testing.TB, s string, want sqlparser.Expr) {
tb.Helper()
stmt, err := sqlparser.NewParser(strings.NewReader(s)).ParseExpr()
if err != nil {
tb.Fatal(err)
} else if diff := deep.Equal(stmt, want); diff != nil {
tb.Fatalf("mismatch:\n%s", strings.Join(diff, "\n"))
}
}
// AssertParseExprError asserts s parses to a given error string.
func AssertParseExprError(tb testing.TB, s string, want string) {
tb.Helper()
_, err := sqlparser.NewParser(strings.NewReader(s)).ParseExpr()
if err == nil || err.Error() != want {
tb.Fatalf("ParseExpr()=%q, want %q", err, want)
}
}