Skip to content

Commit 1b1655c

Browse files
authored
Merge pull request #321 from iceflying/tables_names_finder_oracle_bugs
fix bug of TablesNamesFinder when SubSelect has withItemsList
2 parents 37f0610 + 276ddc8 commit 1b1655c

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ public void visit(Table tableName) {
141141

142142
@Override
143143
public void visit(SubSelect subSelect) {
144+
if (subSelect.getWithItemsList() != null) {
145+
for (WithItem withItem : subSelect.getWithItemsList()) {
146+
withItem.accept(this);
147+
}
148+
}
144149
subSelect.getSelectBody().accept(this);
145150
}
146151

src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import net.sf.jsqlparser.parser.CCJSqlParserManager;
1515
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
16+
import net.sf.jsqlparser.statement.Statement;
1617
import net.sf.jsqlparser.statement.create.table.CreateTable;
1718
import net.sf.jsqlparser.statement.delete.Delete;
1819
import net.sf.jsqlparser.statement.insert.Insert;
@@ -39,6 +40,11 @@ public void testMoreComplexExamples() throws Exception {
3940
runTestOnResource("complex-select-requests.txt");
4041
}
4142

43+
@Test
44+
public void testComplexMergeExamples() throws Exception {
45+
runTestOnResource("complex-merge-requests.txt");
46+
}
47+
4248
private void runTestOnResource(String resPath) throws Exception {
4349
BufferedReader in = new BufferedReader(new InputStreamReader(TablesNamesFinderTest.class.getResourceAsStream(resPath)));
4450
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
@@ -79,7 +85,7 @@ private void runTestOnResource(String resPath) throws Exception {
7985
String whereCols = getLine(in);
8086
String type = getLine(in);
8187
try {
82-
Select select = (Select) pm.parse(new StringReader(query));
88+
Statement statement = pm.parse(new StringReader(query));
8389
StringTokenizer tokenizer = new StringTokenizer(tables, " ");
8490
List tablesList = new ArrayList();
8591
while (tokenizer.hasMoreTokens()) {
@@ -88,7 +94,7 @@ private void runTestOnResource(String resPath) throws Exception {
8894

8995
String[] tablesArray = (String[]) tablesList.toArray(new String[tablesList.size()]);
9096

91-
List<String> tableListRetr = tablesNamesFinder.getTableList(select);
97+
List<String> tableListRetr = tablesNamesFinder.getTableList(statement);
9298
assertEquals("stm num:" + numSt, tablesArray.length, tableListRetr.size());
9399

94100
for (int i = 0; i < tablesArray.length; i++) {
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// -----------------------------------------------------------------------------------------------------------------------
2+
// MERGE requests test
3+
//
4+
// The requests must have the following format:
5+
// #begin
6+
// <MERGE request>
7+
// #end
8+
// <isValid flag>: true if this request is valid
9+
//
10+
// If the request is valid:
11+
// <selected columns concerning by the select clause prefixed by the table>: for example: my_table.id
12+
// <selected table concerning by the from clause, eventually suffixed by an alias>: for example: my_table, my_table.my_alias, my_table
13+
// <selected columns concerning by the where clause prefixed by the table>: for example: my_table.id
14+
// <request type>: either CACHEABLE, UNCACHEABLE or UNIQUE_CACHEABLE
15+
//
16+
// If the request is not valid:
17+
// <error message>
18+
//
19+
// do not add empty line between the lines defining a test
20+
// line beginning by a // are ignored except in a test
21+
// -----------------------------------------------------------------------------------------------------------------------
22+
23+
//1
24+
#begin
25+
MERGE INTO TEST_TABLE1 A
26+
USING
27+
(WITH
28+
ALL_SID AS (
29+
SELECT
30+
TS.SID
31+
32+
FROM
33+
TEST_TABLE2 TG
34+
LEFT JOIN
35+
TEST_TABLE3 TS
36+
ON TG.GID=TS.GID
37+
38+
WHERE
39+
TG.GID IN (
40+
SELECT
41+
DISTINCT GID
42+
43+
FROM
44+
TEST_TABLE4
45+
46+
WHERE
47+
LOGDAY > TRUNC(SYSDATE-90)
48+
)
49+
),
50+
51+
VIRTUAL_SPLIT AS (
52+
SELECT
53+
VIRTUAL_SID,
54+
MEMBER_SID
55+
56+
FROM
57+
ALL_SID,
58+
TEST_TABLE5
59+
60+
WHERE
61+
ALL_SID.SID=TEST_TABLE5.VIRTUAL_SID
62+
),
63+
64+
ALL_CFS AS (
65+
SELECT
66+
DISTINCT SID AS SID
67+
68+
FROM (
69+
SELECT
70+
SID
71+
FROM
72+
ALL_SID
73+
WHERE
74+
SID NOT IN (
75+
SELECT
76+
VIRTUAL_SID
77+
FROM
78+
VIRTUAL_SPLIT
79+
) UNION ALL
80+
SELECT
81+
MEMBER_SID AS SID
82+
FROM
83+
VIRTUAL_SPLIT
84+
)
85+
)
86+
87+
SELECT
88+
INV.SID,
89+
90+
CASE
91+
WHEN
92+
CF.SID IS NULL
93+
THEN
94+
0
95+
ELSE
96+
1
97+
END AS FOCUS_MARK
98+
99+
FROM
100+
TEST_TABLE1 INV
101+
LEFT JOIN
102+
ALL_CFS CF
103+
ON INV.SID=CF.SID
104+
) B
105+
ON (
106+
A.SID=B.SID
107+
)
108+
109+
WHEN
110+
MATCHED
111+
112+
THEN
113+
UPDATE
114+
SET
115+
A.FOCUS_REMARK = B.FOCUS_MARK,
116+
A.UPDATE_TIME = SYSDATE
117+
#end
118+
true
119+
?
120+
TEST_TABLE1 TEST_TABLE2 TEST_TABLE3 TEST_TABLE4 TEST_TABLE5
121+
?
122+
?

0 commit comments

Comments
 (0)