/
StatementParserTest.java
741 lines (689 loc) · 30.6 KB
/
StatementParserTest.java
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
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.spanner.connection;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.connection.ClientSideStatementImpl.CompileException;
import com.google.cloud.spanner.connection.StatementParser.ParsedStatement;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
public class StatementParserTest {
public static final String COPYRIGHT_PATTERN =
"\\/\\*\n"
+ " \\* Copyright \\d{4} Google LLC\n"
+ " \\*\n"
+ " \\* Licensed under the Apache License, Version 2.0 \\(the \"License\"\\);\n"
+ " \\* you may not use this file except in compliance with the License.\n"
+ " \\* You may obtain a copy of the License at\n"
+ " \\*\n"
+ " \\* http://www.apache.org/licenses/LICENSE-2.0\n"
+ " \\*\n"
+ " \\* Unless required by applicable law or agreed to in writing, software\n"
+ " \\* distributed under the License is distributed on an \"AS IS\" BASIS,\n"
+ " \\* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
+ " \\* See the License for the specific language governing permissions and\n"
+ " \\* limitations under the License.\n"
+ " \\*\\/\n";
private final StatementParser parser = StatementParser.INSTANCE;
private static final Pattern EXPECT_PATTERN = Pattern.compile("(?is)\\s*(?:@EXPECT)\\s+'(.*)'");
@Test
public void testRemoveComments() {
List<String> statements = readStatementsFromFile("CommentsTest.sql");
String currentlyExpected = "";
for (String statement : statements) {
String sql = statement.trim();
if (sql.startsWith("@EXPECT")) {
Matcher matcher = EXPECT_PATTERN.matcher(sql);
if (matcher.matches()) {
currentlyExpected = matcher.group(1);
} else {
throw new IllegalArgumentException("Unknown @EXPECT statement: " + sql);
}
} else {
assertThat(
StatementParser.removeCommentsAndTrim(statement), is(equalTo(currentlyExpected)));
}
}
assertThat(StatementParser.removeCommentsAndTrim(""), is(equalTo("")));
assertThat(
StatementParser.removeCommentsAndTrim("SELECT * FROM FOO"),
is(equalTo("SELECT * FROM FOO")));
assertThat(
StatementParser.removeCommentsAndTrim("-- This is a one line comment\nSELECT * FROM FOO"),
is(equalTo("SELECT * FROM FOO")));
assertThat(
StatementParser.removeCommentsAndTrim(
"/* This is a simple multi line comment */\nSELECT * FROM FOO"),
is(equalTo("SELECT * FROM FOO")));
assertThat(
StatementParser.removeCommentsAndTrim(
"/* This is a \nmulti line comment */\nSELECT * FROM FOO"),
is(equalTo("SELECT * FROM FOO")));
assertThat(
StatementParser.removeCommentsAndTrim(
"/* This\nis\na\nmulti\nline\ncomment */\nSELECT * FROM FOO"),
is(equalTo("SELECT * FROM FOO")));
}
@Test
public void testStatementWithCommentContainingSlash() {
String sql =
"/*\n"
+ " * Script for testing invalid/unrecognized statements\n"
+ " */\n"
+ "\n"
+ "-- MERGE into test comment MERGE -- \n"
+ "@EXPECT EXCEPTION INVALID_ARGUMENT 'INVALID_ARGUMENT: Unknown statement'\n"
+ "MERGE INTO Singers s\n"
+ "/*** test ****/"
+ "USING (VALUES (1, 'John', 'Doe')) v\n"
+ "ON v.column1 = s.SingerId\n"
+ "WHEN NOT MATCHED \n"
+ " INSERT VALUES (v.column1, v.column2, v.column3)\n"
+ "WHEN MATCHED\n"
+ " UPDATE SET FirstName = v.column2,\n"
+ " LastName = v.column3;";
String sqlWithoutComments =
"@EXPECT EXCEPTION INVALID_ARGUMENT 'INVALID_ARGUMENT: Unknown statement'\n"
+ "MERGE INTO Singers s\n"
+ "USING (VALUES (1, 'John', 'Doe')) v\n"
+ "ON v.column1 = s.SingerId\n"
+ "WHEN NOT MATCHED \n"
+ " INSERT VALUES (v.column1, v.column2, v.column3)\n"
+ "WHEN MATCHED\n"
+ " UPDATE SET FirstName = v.column2,\n"
+ " LastName = v.column3";
ParsedStatement statement = parser.parse(Statement.of(sql));
assertThat(statement.getSqlWithoutComments(), is(equalTo(sqlWithoutComments)));
}
@Test
public void testStatementWithCommentContainingSlashAndNoAsteriskOnNewLine() {
String sql =
"/*\n"
+ " * Script for testing invalid/unrecognized statements\n"
+ " foo bar baz"
+ " */\n"
+ "\n"
+ "-- MERGE INTO test comment MERGE\n"
+ "@EXPECT EXCEPTION INVALID_ARGUMENT 'INVALID_ARGUMENT: Unknown statement'\n"
+ "MERGE INTO Singers s\n"
+ "USING (VALUES (1, 'John', 'Doe')) v\n"
+ "ON v.column1 = s.SingerId\n"
+ "-- test again --\n"
+ "WHEN NOT MATCHED \n"
+ " INSERT VALUES (v.column1, v.column2, v.column3)\n"
+ "WHEN MATCHED\n"
+ " UPDATE SET FirstName = v.column2,\n"
+ " LastName = v.column3;";
String sqlWithoutComments =
"@EXPECT EXCEPTION INVALID_ARGUMENT 'INVALID_ARGUMENT: Unknown statement'\n"
+ "MERGE INTO Singers s\n"
+ "USING (VALUES (1, 'John', 'Doe')) v\n"
+ "ON v.column1 = s.SingerId\n"
+ "\nWHEN NOT MATCHED \n"
+ " INSERT VALUES (v.column1, v.column2, v.column3)\n"
+ "WHEN MATCHED\n"
+ " UPDATE SET FirstName = v.column2,\n"
+ " LastName = v.column3";
ParsedStatement statement = parser.parse(Statement.of(sql));
assertThat(statement.getSqlWithoutComments(), is(equalTo(sqlWithoutComments)));
}
@Test
public void testStatementWithHashTagSingleLineComment() {
assertThat(
parser
.parse(Statement.of("# this is a comment\nselect * from foo"))
.getSqlWithoutComments(),
is(equalTo("select * from foo")));
assertThat(
parser.parse(Statement.of("select * from foo\n#this is a comment")).getSqlWithoutComments(),
is(equalTo("select * from foo")));
assertThat(
parser
.parse(Statement.of("select *\nfrom foo # this is a comment\nwhere bar=1"))
.getSqlWithoutComments(),
is(equalTo("select *\nfrom foo \nwhere bar=1")));
}
@Test
public void testIsDdlStatement() {
assertFalse(parser.isDdlStatement(""));
assertFalse(parser.isDdlStatement("random text"));
assertFalse(parser.isDdlStatement("CREATETABLE"));
assertFalse(parser.isDdlStatement("CCREATE TABLE"));
assertFalse(parser.isDdlStatement("SELECT 1"));
assertFalse(parser.isDdlStatement("SELECT FOO FROM BAR"));
assertFalse(parser.isDdlStatement("INSERT INTO FOO (ID, NAME) VALUES (1, 'NAME')"));
assertFalse(parser.isDdlStatement("UPDATE FOO SET NAME='NAME' WHERE ID=1"));
assertFalse(parser.isDdlStatement("DELETE FROM FOO"));
assertTrue(
parser.isDdlStatement("CREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"));
assertTrue(parser.isDdlStatement("alter table foo add Description string(100)"));
assertTrue(parser.isDdlStatement("drop table foo"));
assertTrue(parser.isDdlStatement("Create index BAR on foo (name)"));
assertTrue(
parser
.parse(
Statement.of(
"\t\tCREATE\n\t TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"))
.isDdl());
assertTrue(
parser
.parse(
Statement.of(
"\n\n\nCREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"))
.isDdl());
assertTrue(
parser
.parse(
Statement.of(
"-- this is a comment\nCREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"))
.isDdl());
assertTrue(
parser
.parse(
Statement.of(
"/* multi line comment\n* with more information on the next line\n*/\nCREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"))
.isDdl());
assertTrue(
parser
.parse(
Statement.of(
"/** java doc comment\n* with more information on the next line\n*/\nCREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"))
.isDdl());
assertTrue(
parser
.parse(
Statement.of(
"-- SELECT in a single line comment \nCREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"))
.isDdl());
assertTrue(
parser
.parse(
Statement.of(
"/* SELECT in a multi line comment\n* with more information on the next line\n*/\nCREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"))
.isDdl());
assertTrue(
parser
.parse(
Statement.of(
"/** SELECT in a java doc comment\n* with more information on the next line\n*/\nCREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"))
.isDdl());
}
@Test
public void testIsQuery() {
assertFalse(parser.isQuery(""));
assertFalse(parser.isQuery("random text"));
assertFalse(parser.isQuery("SELECT1"));
assertFalse(parser.isQuery("SSELECT 1"));
assertTrue(parser.isQuery("SELECT 1"));
assertTrue(parser.isQuery("select 1"));
assertTrue(parser.isQuery("SELECT foo FROM bar WHERE id=@id"));
assertFalse(parser.isQuery("INSERT INTO FOO (ID, NAME) VALUES (1, 'NAME')"));
assertFalse(parser.isQuery("UPDATE FOO SET NAME='NAME' WHERE ID=1"));
assertFalse(parser.isQuery("DELETE FROM FOO"));
assertFalse(parser.isQuery("CREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"));
assertFalse(parser.isQuery("alter table foo add Description string(100)"));
assertFalse(parser.isQuery("drop table foo"));
assertFalse(parser.isQuery("Create index BAR on foo (name)"));
assertTrue(parser.isQuery("select * from foo"));
assertFalse(parser.isQuery("INSERT INTO FOO (ID, NAME) SELECT ID+1, NAME FROM FOO"));
assertTrue(
parser.isQuery(
"WITH subQ1 AS (SELECT SchoolID FROM Roster),\n"
+ " subQ2 AS (SELECT OpponentID FROM PlayerStats)\n"
+ "SELECT * FROM subQ1\n"
+ "UNION ALL\n"
+ "SELECT * FROM subQ2"));
assertTrue(
parser.isQuery(
"with subQ1 AS (SELECT SchoolID FROM Roster),\n"
+ " subQ2 AS (SELECT OpponentID FROM PlayerStats)\n"
+ "select * FROM subQ1\n"
+ "UNION ALL\n"
+ "SELECT * FROM subQ2"));
assertTrue(
parser
.parse(
Statement.of(
"-- this is a comment\nwith foo as (select * from bar)\nselect * from foo"))
.isQuery());
assertTrue(parser.parse(Statement.of("-- this is a comment\nselect * from foo")).isQuery());
assertTrue(
parser
.parse(
Statement.of(
"/* multi line comment\n* with more information on the next line\n*/\nSELECT ID, NAME\nFROM\tTEST\n\tWHERE ID=1"))
.isQuery());
assertTrue(
parser
.parse(
Statement.of(
"/** java doc comment\n* with more information on the next line\n*/\nselect max(id) from test"))
.isQuery());
assertTrue(
parser.parse(Statement.of("-- INSERT in a single line comment \n select 1")).isQuery());
assertTrue(
parser
.parse(
Statement.of(
"/* UPDATE in a multi line comment\n* with more information on the next line\n*/\nSELECT 1"))
.isQuery());
assertTrue(
parser
.parse(
Statement.of(
"/** DELETE in a java doc comment\n* with more information on the next line\n*/\n\n\n\n -- UPDATE test\nSELECT 1"))
.isQuery());
}
@Test
public void testQueryHints() {
// Valid query hints.
assertTrue(parser.isQuery("@{JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable"));
assertTrue(parser.isQuery("@ {JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable"));
assertTrue(parser.isQuery("@{ JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable"));
assertTrue(parser.isQuery("@{JOIN_METHOD=HASH_JOIN } SELECT * FROM PersonsTable"));
assertTrue(parser.isQuery("@{JOIN_METHOD=HASH_JOIN}\nSELECT * FROM PersonsTable"));
assertTrue(parser.isQuery("@{\nJOIN_METHOD = HASH_JOIN \t}\n\t SELECT * FROM PersonsTable"));
assertTrue(
parser.isQuery(
"@{JOIN_METHOD=HASH_JOIN}\n -- Single line comment\nSELECT * FROM PersonsTable"));
assertTrue(
parser.isQuery(
"@{JOIN_METHOD=HASH_JOIN}\n /* Multi line comment\n with more comments\n */SELECT * FROM PersonsTable"));
assertTrue(
parser.isQuery(
"@{JOIN_METHOD=HASH_JOIN} WITH subQ1 AS (SELECT SchoolID FROM Roster),\n"
+ " subQ2 AS (SELECT OpponentID FROM PlayerStats)\n"
+ "SELECT * FROM subQ1\n"
+ "UNION ALL\n"
+ "SELECT * FROM subQ2"));
// Multiple query hints.
assertTrue(
StatementParser.INSTANCE.isQuery(
"@{FORCE_INDEX=index_name} @{JOIN_METHOD=HASH_JOIN} SELECT * FROM tbl"));
assertTrue(
StatementParser.INSTANCE.isQuery(
"@{FORCE_INDEX=index_name} @{JOIN_METHOD=HASH_JOIN} Select * FROM tbl"));
assertTrue(
StatementParser.INSTANCE.isQuery(
"@{FORCE_INDEX=index_name}\n@{JOIN_METHOD=HASH_JOIN}\nWITH subQ1 AS (SELECT SchoolID FROM Roster),\n"
+ " subQ2 AS (SELECT OpponentID FROM PlayerStats)\n"
+ "SELECT * FROM subQ1\n"
+ "UNION ALL\n"
+ "SELECT * FROM subQ2"));
// Invalid query hints.
assertFalse(parser.isQuery("@{JOIN_METHOD=HASH_JOIN SELECT * FROM PersonsTable"));
assertFalse(parser.isQuery("@JOIN_METHOD=HASH_JOIN} SELECT * FROM PersonsTable"));
assertFalse(parser.isQuery("@JOIN_METHOD=HASH_JOIN SELECT * FROM PersonsTable"));
assertFalse(
StatementParser.INSTANCE.isQuery(
"@{FORCE_INDEX=index_name} @{JOIN_METHOD=HASH_JOIN} UPDATE tbl set FOO=1 WHERE ID=2"));
}
@Test
public void testIsUpdate_InsertStatements() {
assertFalse(parser.isUpdateStatement(""));
assertFalse(parser.isUpdateStatement("random text"));
assertFalse(parser.isUpdateStatement("INSERTINTO FOO (ID) VALUES (1)"));
assertFalse(parser.isUpdateStatement("IINSERT INTO FOO (ID) VALUES (1)"));
assertTrue(parser.isUpdateStatement("INSERT INTO FOO (ID) VALUES (1)"));
assertTrue(parser.isUpdateStatement("insert into foo (id) values (1)"));
assertTrue(parser.isUpdateStatement("INSERT into Foo (id)\nSELECT id FROM bar WHERE id=@id"));
assertFalse(parser.isUpdateStatement("SELECT 1"));
assertFalse(parser.isUpdateStatement("SELECT NAME FROM FOO WHERE ID=1"));
assertFalse(
parser.isUpdateStatement("CREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"));
assertFalse(parser.isUpdateStatement("alter table foo add Description string(100)"));
assertFalse(parser.isUpdateStatement("drop table foo"));
assertFalse(parser.isUpdateStatement("Create index BAR on foo (name)"));
assertFalse(parser.isUpdateStatement("select * from foo"));
assertTrue(parser.isUpdateStatement("INSERT INTO FOO (ID, NAME) SELECT ID+1, NAME FROM FOO"));
assertTrue(
parser
.parse(Statement.of("-- this is a comment\ninsert into foo (id) values (1)"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"/* multi line comment\n* with more information on the next line\n*/\nINSERT INTO FOO\n(ID)\tVALUES\n\t(1)"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"/** java doc comment\n* with more information on the next line\n*/\nInsert intO foo (id) select 1"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"-- SELECT in a single line comment \n insert into foo (id) values (1)"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"/* CREATE in a multi line comment\n* with more information on the next line\n*/\nINSERT INTO FOO (ID) VALUES (1)"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"/** DROP in a java doc comment\n* with more information on the next line\n*/\n\n\n\n -- SELECT test\ninsert into foo (id) values (1)"))
.isUpdate());
}
@Test
public void testIsUpdate_UpdateStatements() {
assertFalse(parser.isUpdateStatement(""));
assertFalse(parser.isUpdateStatement("random text"));
assertFalse(parser.isUpdateStatement("UPDATEFOO SET NAME='foo' WHERE ID=1"));
assertFalse(parser.isUpdateStatement("UUPDATE FOO SET NAME='foo' WHERE ID=1"));
assertTrue(parser.isUpdateStatement("UPDATE FOO SET NAME='foo' WHERE ID=1"));
assertTrue(parser.isUpdateStatement("update foo set name='foo' where id=1"));
assertTrue(
parser.isUpdateStatement("update foo set name=\n(SELECT name FROM bar WHERE id=@id)"));
assertFalse(parser.isUpdateStatement("SELECT 1"));
assertFalse(parser.isUpdateStatement("SELECT NAME FROM FOO WHERE ID=1"));
assertFalse(
parser.isUpdateStatement("CREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"));
assertFalse(parser.isUpdateStatement("alter table foo add Description string(100)"));
assertFalse(parser.isUpdateStatement("drop table foo"));
assertFalse(parser.isUpdateStatement("Create index BAR on foo (name)"));
assertFalse(parser.isUpdateStatement("select * from foo"));
assertTrue(
parser.isUpdateStatement(
"UPDATE FOO SET NAME=(SELECT NAME FROM FOO) WHERE ID=(SELECT ID+1 FROM FOO)"));
assertTrue(
parser
.parse(Statement.of("-- this is a comment\nupdate foo set name='foo' where id=@id"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"/* multi line comment\n* with more information on the next line\n*/\nUPDATE FOO\nSET NAME=\t'foo'\n\tWHERE ID=1"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"/** java doc comment\n* with more information on the next line\n*/\nUPDATE FOO SET NAME=(select 'bar')"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of("-- SELECT in a single line comment \n update foo set name='bar'"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"/* CREATE in a multi line comment\n* with more information on the next line\n*/\nUPDATE FOO SET NAME='BAR'"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"/** DROP in a java doc comment\n* with more information on the next line\n*/\n\n\n\n -- SELECT test\nupdate foo set bar='foo'"))
.isUpdate());
}
@Test
public void testIsUpdate_DeleteStatements() {
assertFalse(parser.isUpdateStatement(""));
assertFalse(parser.isUpdateStatement("random text"));
assertFalse(parser.isUpdateStatement("DELETEFROM FOO WHERE ID=1"));
assertFalse(parser.isUpdateStatement("DDELETE FROM FOO WHERE ID=1"));
assertTrue(parser.isUpdateStatement("DELETE FROM FOO WHERE ID=1"));
assertTrue(parser.isUpdateStatement("delete from foo where id=1"));
assertTrue(
parser.isUpdateStatement(
"delete from foo where name=\n(SELECT name FROM bar WHERE id=@id)"));
assertFalse(parser.isUpdateStatement("SELECT 1"));
assertFalse(parser.isUpdateStatement("SELECT NAME FROM FOO WHERE ID=1"));
assertFalse(
parser.isUpdateStatement("CREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"));
assertFalse(parser.isUpdateStatement("alter table foo add Description string(100)"));
assertFalse(parser.isUpdateStatement("drop table foo"));
assertFalse(parser.isUpdateStatement("Create index BAR on foo (name)"));
assertFalse(parser.isUpdateStatement("select * from foo"));
assertTrue(
parser.isUpdateStatement(
"UPDATE FOO SET NAME=(SELECT NAME FROM FOO) WHERE ID=(SELECT ID+1 FROM FOO)"));
assertTrue(
parser
.parse(Statement.of("-- this is a comment\ndelete from foo where id=@id"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"/* multi line comment\n* with more information on the next line\n*/\nDELETE FROM FOO\n\n\tWHERE ID=1"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"/** java doc comment\n* with more information on the next line\n*/\nDELETE FROM FOO WHERE NAME=(select 'bar')"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"-- SELECT in a single line comment \n delete from foo where name='bar'"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"/* CREATE in a multi line comment\n* with more information on the next line\n*/\nDELETE FROM FOO WHERE NAME='BAR'"))
.isUpdate());
assertTrue(
parser
.parse(
Statement.of(
"/** DROP in a java doc comment\n* with more information on the next line\n*/\n\n\n\n -- SELECT test\ndelete from foo where bar='foo'"))
.isUpdate());
}
@Test
public void testParseStatementsWithNoParameters() throws CompileException {
for (ClientSideStatementImpl statement : getAllStatements()) {
if (statement.getSetStatement() == null) {
for (String testStatement : statement.getExampleStatements()) {
testParseStatement(testStatement, statement.getClass());
}
}
}
}
@Test
public void testParseStatementsWithOneParameterAtTheEnd() throws CompileException {
for (ClientSideStatementImpl statement : getAllStatements()) {
if (statement.getSetStatement() != null) {
for (String testStatement : statement.getExampleStatements()) {
testParseStatementWithOneParameterAtTheEnd(testStatement, statement.getClass());
}
}
}
}
private Set<ClientSideStatementImpl> getAllStatements() throws CompileException {
return ClientSideStatements.INSTANCE.getCompiledStatements();
}
private <T extends ClientSideStatementImpl> void assertParsing(
String value, Class<T> statementClass) {
assertThat(this.<T>parse(value), is(equalTo(statementClass)));
}
private <T extends ClientSideStatementImpl> void testParseStatement(
String statement, Class<T> statementClass) {
assertThat(
"\"" + statement + "\" should be " + statementClass.getName(),
this.<T>parse(statement),
is(equalTo(statementClass)));
assertParsing(upper(statement), statementClass);
assertParsing(lower(statement), statementClass);
assertParsing(withSpaces(statement), statementClass);
assertParsing(withTabs(statement), statementClass);
assertParsing(withLinefeeds(statement), statementClass);
assertParsing(withLeadingSpaces(statement), statementClass);
assertParsing(withLeadingTabs(statement), statementClass);
assertParsing(withLeadingLinefeeds(statement), statementClass);
assertParsing(withTrailingSpaces(statement), statementClass);
assertParsing(withTrailingTabs(statement), statementClass);
assertParsing(withTrailingLinefeeds(statement), statementClass);
assertThat(parse(withInvalidPrefix(statement)), is(nullValue()));
assertThat(parse(withInvalidSuffix(statement)), is(nullValue()));
assertNull(parse(withPrefix("%", statement)));
assertNull(parse(withPrefix("_", statement)));
assertNull(parse(withPrefix("&", statement)));
assertNull(parse(withPrefix("$", statement)));
assertNull(parse(withPrefix("@", statement)));
assertNull(parse(withPrefix("!", statement)));
assertNull(parse(withPrefix("*", statement)));
assertNull(parse(withPrefix("(", statement)));
assertNull(parse(withPrefix(")", statement)));
assertThat(
withSuffix("%", statement) + " is not a valid statement",
parse(withSuffix("%", statement)),
is(nullValue()));
assertNull(parse(withSuffix("_", statement)));
assertNull(parse(withSuffix("&", statement)));
assertNull(parse(withSuffix("$", statement)));
assertNull(parse(withSuffix("@", statement)));
assertNull(parse(withSuffix("!", statement)));
assertNull(parse(withSuffix("*", statement)));
assertNull(parse(withSuffix("(", statement)));
assertNull(parse(withSuffix(")", statement)));
}
private <T extends ClientSideStatementImpl> void testParseStatementWithOneParameterAtTheEnd(
String statement, Class<T> statementClass) {
assertThat(
"\"" + statement + "\" should be " + statementClass.getName(),
this.<T>parse(statement),
is(equalTo(statementClass)));
assertParsing(upper(statement), statementClass);
assertParsing(lower(statement), statementClass);
assertParsing(withSpaces(statement), statementClass);
assertParsing(withTabs(statement), statementClass);
assertParsing(withLinefeeds(statement), statementClass);
assertParsing(withLeadingSpaces(statement), statementClass);
assertParsing(withLeadingTabs(statement), statementClass);
assertParsing(withLeadingLinefeeds(statement), statementClass);
assertParsing(withTrailingSpaces(statement), statementClass);
assertParsing(withTrailingTabs(statement), statementClass);
assertParsing(withTrailingLinefeeds(statement), statementClass);
assertNull(parse(withInvalidPrefix(statement)));
assertParsing(withInvalidSuffix(statement), statementClass);
assertNull(parse(withPrefix("%", statement)));
assertNull(parse(withPrefix("_", statement)));
assertNull(parse(withPrefix("&", statement)));
assertNull(parse(withPrefix("$", statement)));
assertNull(parse(withPrefix("@", statement)));
assertNull(parse(withPrefix("!", statement)));
assertNull(parse(withPrefix("*", statement)));
assertNull(parse(withPrefix("(", statement)));
assertNull(parse(withPrefix(")", statement)));
assertParsing(withSuffix("%", statement), statementClass);
assertParsing(withSuffix("_", statement), statementClass);
assertParsing(withSuffix("&", statement), statementClass);
assertParsing(withSuffix("$", statement), statementClass);
assertParsing(withSuffix("@", statement), statementClass);
assertParsing(withSuffix("!", statement), statementClass);
assertParsing(withSuffix("*", statement), statementClass);
assertParsing(withSuffix("(", statement), statementClass);
assertParsing(withSuffix(")", statement), statementClass);
}
@SuppressWarnings("unchecked")
private <T extends ClientSideStatementImpl> Class<T> parse(String statement) {
ClientSideStatementImpl optional = parser.parseClientSideStatement(statement);
return optional != null ? (Class<T>) optional.getClass() : null;
}
private String upper(String statement) {
return statement.toUpperCase();
}
private String lower(String statement) {
return statement.toLowerCase();
}
private String withLeadingSpaces(String statement) {
return " " + statement;
}
private String withLeadingTabs(String statement) {
return "\t\t\t" + statement;
}
private String withLeadingLinefeeds(String statement) {
return "\n\n\n" + statement;
}
private String withTrailingSpaces(String statement) {
return statement + " ";
}
private String withTrailingTabs(String statement) {
return statement + "\t\t";
}
private String withTrailingLinefeeds(String statement) {
return statement + "\n\n";
}
private String withSpaces(String statement) {
return statement.replaceAll(" ", " ");
}
private String withTabs(String statement) {
return statement.replaceAll(" ", "\t");
}
private String withLinefeeds(String statement) {
return statement.replaceAll(" ", "\n");
}
private String withInvalidPrefix(String statement) {
return "foo " + statement;
}
private String withInvalidSuffix(String statement) {
return statement + " bar";
}
private String withPrefix(String prefix, String statement) {
return prefix + statement;
}
private String withSuffix(String suffix, String statement) {
return statement + suffix;
}
private List<String> readStatementsFromFile(String filename) {
File file = new File(getClass().getResource(filename).getFile());
StringBuilder builder = new StringBuilder();
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
builder.append(line).append("\n");
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
String script = builder.toString().replaceAll(COPYRIGHT_PATTERN, "");
String[] array = script.split(";");
List<String> res = new ArrayList<>(array.length);
for (String statement : array) {
if (statement != null && statement.trim().length() > 0) {
res.add(statement);
}
}
return res;
}
}