-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathTablesNamesFinderTest.java
More file actions
655 lines (563 loc) · 28.4 KB
/
TablesNamesFinderTest.java
File metadata and controls
655 lines (563 loc) · 28.4 KB
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
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.util;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.OracleHint;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.DescribeStatement;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.comment.Comment;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.merge.MergeInsert;
import net.sf.jsqlparser.statement.replace.Replace;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.simpleparsing.CCJSqlParserManagerTest;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.upsert.Upsert;
import net.sf.jsqlparser.test.TestException;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class TablesNamesFinderTest {
private static CCJSqlParserManager pm = new CCJSqlParserManager();
@Test
public void testRUBiSTableList() throws Exception {
runTestOnResource("/RUBiS-select-requests.txt");
}
@Test
public void testMoreComplexExamples() throws Exception {
runTestOnResource("complex-select-requests.txt");
}
@Test
public void testComplexMergeExamples() throws Exception {
runTestOnResource("complex-merge-requests.txt");
}
private void runTestOnResource(String resPath) throws Exception {
BufferedReader in = new BufferedReader(
new InputStreamReader(TablesNamesFinderTest.class.getResourceAsStream(resPath)));
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
try {
int numSt = 1;
while (true) {
String line = getLine(in);
if (line == null) {
break;
}
if (line.length() == 0) {
continue;
}
if (!"#begin".equals(line)) {
break;
}
line = getLine(in);
StringBuilder buf = new StringBuilder(line);
while (true) {
line = getLine(in);
if ("#end".equals(line)) {
break;
}
buf.append("\n");
buf.append(line);
}
String query = buf.toString();
if (!getLine(in).equals("true")) {
continue;
}
String cols = getLine(in);
String tables = getLine(in);
String whereCols = getLine(in);
String type = getLine(in);
try {
Statement statement = pm.parse(new StringReader(query));
String[] tablesArray = tables.split("\\s+");
List<String> tableListRetr = tablesNamesFinder.getTableList(statement);
assertEquals("stm num:" + numSt, tablesArray.length, tableListRetr.size());
for (String element : tablesArray) {
assertTrue("stm num:" + numSt, tableListRetr.contains(element));
}
} catch (Exception e) {
throw new TestException("error at stm num: " + numSt + " in file " + resPath, e);
}
numSt++;
}
} finally {
if (in != null) {
in.close();
}
}
}
@Test
public void testGetTableList() throws Exception {
String sql = "SELECT * FROM MY_TABLE1, MY_TABLE2, (SELECT * FROM MY_TABLE3) LEFT OUTER JOIN MY_TABLE4 "
+ " WHERE ID = (SELECT MAX(ID) FROM MY_TABLE5) AND ID2 IN (SELECT * FROM MY_TABLE6)";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
// now you should use a class that implements StatementVisitor to decide what to
// do
// based on the kind of the statement, that is SELECT or INSERT etc. but here we
// are only
// interested in SELECTS
if (statement instanceof Select) {
Select selectStatement = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
assertEquals(6, tableList.size());
int i = 1;
for (Iterator iter = tableList.iterator(); iter.hasNext(); i++) {
String tableName = (String) iter.next();
assertEquals("MY_TABLE" + i, tableName);
}
}
}
@Test
public void testGetTableListWithAlias() throws Exception {
String sql = "SELECT * FROM MY_TABLE1 as ALIAS_TABLE1";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Select selectStatement = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
assertEquals(1, tableList.size());
assertEquals("MY_TABLE1", tableList.get(0));
}
@Test
public void testGetTableListWithStmt() throws Exception {
String sql = "WITH TESTSTMT as (SELECT * FROM MY_TABLE1 as ALIAS_TABLE1) SELECT * FROM TESTSTMT";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Select selectStatement = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
assertEquals(1, tableList.size());
assertEquals("MY_TABLE1", tableList.get(0));
}
@Test
public void testGetTableListWithLateral() throws Exception {
String sql = "SELECT * FROM MY_TABLE1, LATERAL(select a from MY_TABLE2) as AL";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Select selectStatement = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("MY_TABLE1"));
assertTrue(tableList.contains("MY_TABLE2"));
}
@Test
public void testGetTableListFromDelete() throws Exception {
String sql = "DELETE FROM MY_TABLE1 as AL WHERE a = (SELECT a from MY_TABLE2)";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Delete deleteStatement = (Delete) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(deleteStatement);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("MY_TABLE1"));
assertTrue(tableList.contains("MY_TABLE2"));
}
@Test
public void testGetTableListFromDelete2() throws Exception {
String sql = "DELETE FROM MY_TABLE1";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Delete deleteStatement = (Delete) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(deleteStatement);
assertEquals(1, tableList.size());
assertTrue(tableList.contains("MY_TABLE1"));
}
@Test
public void testGetTableListFromTruncate() throws Exception {
String sql = "TRUNCATE TABLE MY_TABLE1";
List<String> tables = new TablesNamesFinder().getTableList(pm.parse(new StringReader(sql)));
assertEquals(1, tables.size());
assertTrue(tables.contains("MY_TABLE1"));
}
@Test
public void testGetTableListFromDeleteWithJoin() throws Exception {
String sql = "DELETE t1, t2 FROM MY_TABLE1 t1 JOIN MY_TABLE2 t2 ON t1.id = t2.id";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Delete deleteStatement = (Delete) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(deleteStatement);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("MY_TABLE1"));
assertTrue(tableList.contains("MY_TABLE2"));
}
@Test
public void testGetTableListFromInsert() throws Exception {
String sql = "INSERT INTO MY_TABLE1 (a) VALUES ((SELECT a from MY_TABLE2 WHERE a = 1))";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Insert insertStatement = (Insert) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(insertStatement);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("MY_TABLE1"));
assertTrue(tableList.contains("MY_TABLE2"));
}
@Test
public void testGetTableListFromInsertValues() throws Exception {
String sql = "INSERT INTO MY_TABLE1 (a) VALUES (5)";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Insert insertStatement = (Insert) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(insertStatement);
assertEquals(1, tableList.size());
assertTrue(tableList.contains("MY_TABLE1"));
}
@Test
public void testGetTableListFromReplace() throws Exception {
String sql = "REPLACE INTO MY_TABLE1 (a) VALUES ((SELECT a from MY_TABLE2 WHERE a = 1))";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Replace replaceStatement = (Replace) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(replaceStatement);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("MY_TABLE1"));
assertTrue(tableList.contains("MY_TABLE2"));
}
@Test
public void testGetTableListFromUpdate() throws Exception {
String sql = "UPDATE MY_TABLE1 SET a = (SELECT a from MY_TABLE2 WHERE a = 1)";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Update updateStatement = (Update) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(updateStatement);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("MY_TABLE1"));
assertTrue(tableList.contains("MY_TABLE2"));
}
@Test
public void testGetTableListFromUpdate2() throws Exception {
String sql = "UPDATE MY_TABLE1 SET a = 5 WHERE 0 < (SELECT COUNT(b) FROM MY_TABLE3)";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Update updateStatement = (Update) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(updateStatement);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("MY_TABLE1"));
assertTrue(tableList.contains("MY_TABLE3"));
}
@Test
public void testGetTableListFromUpdate3() throws Exception {
String sql = "UPDATE MY_TABLE1 SET a = 5 FROM MY_TABLE1 INNER JOIN MY_TABLE2 on MY_TABLE1.C = MY_TABLE2.D WHERE 0 < (SELECT COUNT(b) FROM MY_TABLE3)";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Update updateStatement = (Update) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(updateStatement);
assertEquals(3, tableList.size());
assertTrue(tableList.contains("MY_TABLE1"));
assertTrue(tableList.contains("MY_TABLE2"));
assertTrue(tableList.contains("MY_TABLE3"));
}
@Test
public void testCmplxSelectProblem() throws Exception {
String sql = "SELECT cid, (SELECT name FROM tbl0 WHERE tbl0.id = cid) AS name, original_id AS bc_id FROM tbl WHERE crid = ? AND user_id is null START WITH ID = (SELECT original_id FROM tbl2 WHERE USER_ID = ?) CONNECT BY prior parent_id = id AND rownum = 1";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Select selectStatement = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
assertEquals(3, tableList.size());
assertTrue(tableList.contains("tbl0"));
assertTrue(tableList.contains("tbl"));
assertTrue(tableList.contains("tbl2"));
}
@Test
public void testInsertSelect() throws Exception {
String sql = "INSERT INTO mytable (mycolumn) SELECT mycolumn FROM mytable2";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Insert insertStatement = (Insert) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(insertStatement);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("mytable"));
assertTrue(tableList.contains("mytable2"));
}
@Test
public void testCreateSelect() throws Exception {
String sql = "CREATE TABLE mytable AS SELECT mycolumn FROM mytable2";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
CreateTable createTable = (CreateTable) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(createTable);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("mytable"));
assertTrue(tableList.contains("mytable2"));
}
@Test
public void testInsertSubSelect() throws JSQLParserException {
String sql = "INSERT INTO Customers (CustomerName, Country) SELECT SupplierName, Country FROM Suppliers WHERE Country='Germany'";
Insert insert = (Insert) pm.parse(new StringReader(sql));
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(insert);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("Customers"));
assertTrue(tableList.contains("Suppliers"));
}
@Test
public void testExpr() throws JSQLParserException {
String sql = "mycol in (select col2 from mytable)";
Expression expr = CCJSqlParserUtil.parseCondExpression(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(expr);
assertEquals(1, tableList.size());
assertTrue(tableList.contains("mytable"));
}
private String getLine(BufferedReader in) throws Exception {
return CCJSqlParserManagerTest.getLine(in);
}
@Test
public void testOracleHint() throws JSQLParserException {
String sql = "select --+ HINT\ncol2 from mytable";
Select select = (Select) CCJSqlParserUtil.parse(sql);
final OracleHint[] holder = new OracleHint[1];
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder() {
@Override
public void visit(OracleHint hint) {
super.visit(hint);
holder[0] = hint;
}
};
tablesNamesFinder.getTableList(select);
assertNull(holder[0]);
}
@Test
public void testGetTableListIssue194() throws Exception {
String sql = "SELECT 1";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Select selectStatement = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
assertEquals(0, tableList.size());
}
@Test
public void testGetTableListIssue284() throws Exception {
String sql = "SELECT NVL( (SELECT 1 FROM DUAL), 1) AS A FROM TEST1";
Select selectStatement = (Select) CCJSqlParserUtil.parse(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("DUAL"));
assertTrue(tableList.contains("TEST1"));
}
@Test
public void testUpdateGetTableListIssue295() throws JSQLParserException {
Update statement = (Update) CCJSqlParserUtil.parse(
"UPDATE component SET col = 0 WHERE (component_id,ver_num) IN (SELECT component_id,ver_num FROM component_temp)");
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(statement);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("component"));
assertTrue(tableList.contains("component_temp"));
}
@Test
public void testGetTableListForMerge() throws Exception {
String sql = "MERGE INTO employees e USING hr_records h ON (e.id = h.emp_id) WHEN MATCHED THEN UPDATE SET e.address = h.address WHEN NOT MATCHED THEN INSERT (id, address) VALUES (h.emp_id, h.address);";
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
Merge parsed = (Merge) CCJSqlParserUtil.parse(sql);
List<String> tableList = tablesNamesFinder.getTableList(parsed);
assertEquals(2, tableList.size());
assertEquals("employees", tableList.get(0));
assertEquals("hr_records", tableList.get(1));
Merge created = new Merge()
.withMergeInsert(new MergeInsert().addColumns(new Column("id"), new Column("address")));
// TestUtils.assertEqualsObjectTree(parsed, created);
}
@Test
public void testGetTableListForMergeUsingQuery() throws Exception {
String sql = "MERGE INTO employees e USING (SELECT * FROM hr_records WHERE start_date > ADD_MONTHS(SYSDATE, -1)) h ON (e.id = h.emp_id) WHEN MATCHED THEN UPDATE SET e.address = h.address WHEN NOT MATCHED THEN INSERT (id, address) VALUES (h.emp_id, h.address)";
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(CCJSqlParserUtil.parse(sql));
assertEquals(2, tableList.size());
assertEquals("employees", tableList.get(0));
assertEquals("hr_records", tableList.get(1));
}
@Test
public void testUpsertValues() throws Exception {
String sql = "UPSERT INTO MY_TABLE1 (a) VALUES (5)";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Upsert insertStatement = (Upsert) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(insertStatement);
assertEquals(1, tableList.size());
assertTrue(tableList.contains("MY_TABLE1"));
}
@Test
public void testUpsertSelect() throws Exception {
String sql = "UPSERT INTO mytable (mycolumn) SELECT mycolumn FROM mytable2";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Upsert insertStatement = (Upsert) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(insertStatement);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("mytable"));
assertTrue(tableList.contains("mytable2"));
}
@Test
public void testCaseWhenSubSelect() throws JSQLParserException {
String sql = "select case (select count(*) from mytable2) when 1 then 0 else -1 end";
Statement stmt = CCJSqlParserUtil.parse(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(stmt);
assertEquals(1, tableList.size());
assertTrue(tableList.contains("mytable2"));
}
@Test
public void testCaseWhenSubSelect2() throws JSQLParserException {
String sql = "select case when (select count(*) from mytable2) = 1 then 0 else -1 end";
Statement stmt = CCJSqlParserUtil.parse(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(stmt);
assertEquals(1, tableList.size());
assertTrue(tableList.contains("mytable2"));
}
@Test
public void testCaseWhenSubSelect3() throws JSQLParserException {
String sql = "select case when 1 = 2 then 0 else (select count(*) from mytable2) end";
Statement stmt = CCJSqlParserUtil.parse(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(stmt);
assertEquals(1, tableList.size());
assertTrue(tableList.contains("mytable2"));
}
@Test
public void testExpressionIssue515() throws JSQLParserException {
TablesNamesFinder finder = new TablesNamesFinder();
List<String> tableList = finder.getTableList(CCJSqlParserUtil.parseCondExpression("SOME_TABLE.COLUMN = 'A'"));
assertEquals(1, tableList.size());
assertTrue(tableList.contains("SOME_TABLE"));
}
@Test
public void testSelectHavingSubquery() throws Exception {
String sql = "SELECT * FROM TABLE1 GROUP BY COL1 HAVING SUM(COL2) > (SELECT COUNT(*) FROM TABLE2)";
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
Select selectStmt = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStmt);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("TABLE1"));
assertTrue(tableList.contains("TABLE2"));
}
@Test
public void testMySQLValueListExpression() throws JSQLParserException {
String sql = "SELECT * FROM TABLE1 WHERE (a, b) = (c, d)";
TablesNamesFinder finder = new TablesNamesFinder();
List<String> tableList = finder.getTableList(CCJSqlParserUtil.parse(sql));
assertEquals(1, tableList.size());
assertTrue(tableList.contains("TABLE1"));
}
@Test
public void testSkippedSchemaIssue600() throws JSQLParserException {
String sql = "delete from schema.table where id = 1";
TablesNamesFinder finder = new TablesNamesFinder();
List<String> tableList = finder.getTableList(CCJSqlParserUtil.parse(sql));
assertEquals(1, tableList.size());
assertTrue(tableList.contains("schema.table"));
}
@Test
public void testCommentTable() throws JSQLParserException {
String sql = "comment on table schema.table is 'comment1'";
TablesNamesFinder finder = new TablesNamesFinder();
List<String> tableList = finder.getTableList(CCJSqlParserUtil.parse(sql));
assertEquals(1, tableList.size());
assertTrue(tableList.contains("schema.table"));
}
@Test
public void testCommentColumn() throws JSQLParserException {
String sql = "comment on column schema.table.column1 is 'comment1'";
TablesNamesFinder finder = new TablesNamesFinder();
List<String> tableList = finder.getTableList(CCJSqlParserUtil.parse(sql));
assertEquals(1, tableList.size());
assertTrue(tableList.contains("schema.table"));
}
@Test
public void testCommentColumn2() throws JSQLParserException {
Comment comment = new Comment();
comment.setColumn(new Column());
TablesNamesFinder finder = new TablesNamesFinder();
List<String> tableList = finder.getTableList(comment);
assertEquals(0, tableList.size());
}
@Test
public void testDescribe() throws JSQLParserException {
DescribeStatement describe = new DescribeStatement(new Table("foo", "product"));
TablesNamesFinder finder = new TablesNamesFinder();
List<String> tableList = finder.getTableList(describe);
assertEquals(1, tableList.size());
assertEquals("foo.product", tableList.get(0));
}
@Test
public void testBetween() throws JSQLParserException {
String sql = "mycol BETWEEN (select col2 from mytable) AND (select col3 from mytable2)";
Expression expr = CCJSqlParserUtil.parseCondExpression(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(expr);
assertEquals(2, tableList.size());
assertTrue(tableList.contains("mytable"));
assertTrue(tableList.contains("mytable2"));
}
@Test
public void testRemoteLink() throws JSQLParserException {
String sql = "select * from table1@remote";
Statement stmt = CCJSqlParserUtil.parse(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(stmt);
assertEquals(1, tableList.size());
assertTrue(tableList.contains("table1@remote"));
}
@Test
public void testCreateSequence_throwsException() throws JSQLParserException {
String sql = "CREATE SEQUENCE my_seq";
Statement stmt = CCJSqlParserUtil.parse(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
assertThatThrownBy(() -> tablesNamesFinder.getTableList(stmt)).isInstanceOf(UnsupportedOperationException.class)
.hasMessage("Finding tables from CreateSequence is not supported");
}
@Test
public void testAlterSequence_throwsException() throws JSQLParserException {
String sql = "ALTER SEQUENCE my_seq";
Statement stmt = CCJSqlParserUtil.parse(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
assertThatThrownBy(() -> tablesNamesFinder.getTableList(stmt)).isInstanceOf(UnsupportedOperationException.class)
.hasMessage("Finding tables from AlterSequence is not supported");
}
@Test
public void testCreateSynonym_throwsException() throws JSQLParserException {
String sql = "CREATE SYNONYM foo FOR bar";
Statement stmt = CCJSqlParserUtil.parse(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
assertThatThrownBy(() -> tablesNamesFinder.getTableList(stmt)).isInstanceOf(UnsupportedOperationException.class)
.hasMessage("Finding tables from CreateSynonym is not supported");
}
@Test
public void testNPEIssue1009() throws JSQLParserException {
Statement stmt = CCJSqlParserUtil.parse(" SELECT * FROM (SELECT * FROM biz_fund_info WHERE tenant_code = ? AND ((ta_code, manager_code) IN ((?, ?)) OR department_type IN (?)))");
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
assertThat(tablesNamesFinder.getTableList(stmt)).containsExactly("biz_fund_info");
}
@Test
public void testAtTimeZoneExpression() throws JSQLParserException {
String sql = "SELECT DATE(date1 AT TIME ZONE 'UTC' AT TIME ZONE 'australia/sydney') AS another_date FROM mytbl";
Statement stmt = CCJSqlParserUtil.parse(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(stmt);
assertEquals(1, tableList.size());
assertTrue(tableList.contains("mytbl"));
}
}