Skip to content

Commit 4369929

Browse files
committed
- problem with TablesNamesFinder: finds with - alias instead of tablenames
1 parent d9fe37a commit 4369929

File tree

3 files changed

+257
-18
lines changed

3 files changed

+257
-18
lines changed

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,32 @@
5959
import net.sf.jsqlparser.statement.select.SetOperationList;
6060
import net.sf.jsqlparser.statement.select.SubJoin;
6161
import net.sf.jsqlparser.statement.select.SubSelect;
62+
import net.sf.jsqlparser.statement.select.WithItem;
6263

6364
/**
64-
* Find all used tables within an select statement.
65+
* Find all used tables within an select statement.
6566
*/
6667
public class TablesNamesFinder implements SelectVisitor, FromItemVisitor, ExpressionVisitor, ItemsListVisitor {
67-
private List tables;
6868

69-
public List getTableList(Select select) {
70-
tables = new ArrayList();
69+
private List<String> tables;
70+
/**
71+
* There are special names, that are not table names but are parsed as
72+
* tables. These names are collected here and are not included in the tables
73+
* - names anymore.
74+
*/
75+
private List<String> otherItemNames;
76+
77+
public List<String> getTableList(Select select) {
78+
otherItemNames = new ArrayList<String>();
79+
tables = new ArrayList<String>();
80+
if (select.getWithItemsList() != null) {
81+
for (WithItem withItem : select.getWithItemsList()) {
82+
otherItemNames.add(withItem.getName().toLowerCase());
83+
withItem.getSelectBody().accept(this);
84+
}
85+
}
7186
select.getSelectBody().accept(this);
87+
7288
return tables;
7389
}
7490

@@ -91,8 +107,10 @@ public void visit(PlainSelect plainSelect) {
91107
@Override
92108
public void visit(Table tableName) {
93109
String tableWholeName = tableName.getWholeTableName();
94-
tables.add(tableWholeName);
95-
}
110+
if (!otherItemNames.contains(tableWholeName.toLowerCase())) {
111+
tables.add(tableWholeName);
112+
}
113+
}
96114

97115
@Override
98116
public void visit(SubSelect subSelect) {
@@ -258,7 +276,6 @@ public void visit(TimeValue timeValue) {
258276
@Override
259277
public void visit(CaseExpression caseExpression) {
260278
// TODO Auto-generated method stub
261-
262279
}
263280

264281
/*
@@ -269,7 +286,6 @@ public void visit(CaseExpression caseExpression) {
269286
@Override
270287
public void visit(WhenClause whenClause) {
271288
// TODO Auto-generated method stub
272-
273289
}
274290

275291
@Override
@@ -317,15 +333,14 @@ public void visit(BitwiseXor bitwiseXor) {
317333
public void visit(CastExpression cast) {
318334
cast.getLeftExpression().accept(this);
319335
}
320-
336+
321337
@Override
322338
public void visit(Modulo modulo) {
323339
visitBinaryExpression(modulo);
324340
}
325-
341+
326342
@Override
327343
public void visit(AnalyticExpression analytic) {
328-
329344
}
330345

331346
@Override
@@ -338,6 +353,5 @@ public void visit(SetOperationList list) {
338353

339354
@Override
340355
public void visit(ExtractExpression eexpr) {
341-
342356
}
343357
}

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Iterator;
1111
import java.util.List;
1212
import java.util.StringTokenizer;
13+
import static junit.framework.Assert.assertEquals;
1314

1415
import junit.framework.TestCase;
1516
import net.sf.jsqlparser.parser.CCJSqlParserManager;
@@ -19,15 +20,23 @@
1920
import net.sf.jsqlparser.test.simpleparsing.CCJSqlParserManagerTest;
2021

2122
public class TablesNamesFinderTest extends TestCase {
23+
2224
CCJSqlParserManager pm = new CCJSqlParserManager();
2325

2426
public TablesNamesFinderTest(String arg0) {
2527
super(arg0);
2628
}
2729

2830
public void testRUBiSTableList() throws Exception {
31+
runTestOnResource("/RUBiS-select-requests.txt");
32+
}
33+
34+
public void testMoreComplexExamples() throws Exception {
35+
runTestOnResource("complex-select-requests.txt");
36+
}
2937

30-
BufferedReader in = new BufferedReader( new InputStreamReader( CreateTableTest.class.getResourceAsStream( "/RUBiS-select-requests.txt" ) ) );
38+
private void runTestOnResource(String resPath) throws Exception {
39+
BufferedReader in = new BufferedReader(new InputStreamReader(TablesNamesFinderTest.class.getResourceAsStream(resPath)));
3140
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
3241

3342
try {
@@ -75,18 +84,16 @@ public void testRUBiSTableList() throws Exception {
7584

7685
String[] tablesArray = (String[]) tablesList.toArray(new String[tablesList.size()]);
7786

78-
List tableListRetr = tablesNamesFinder.getTableList(select);
87+
List<String> tableListRetr = tablesNamesFinder.getTableList(select);
7988
assertEquals("stm num:" + numSt, tablesArray.length, tableListRetr.size());
8089

8190
for (int i = 0; i < tablesArray.length; i++) {
8291
assertEquals("stm num:" + numSt, tablesArray[i], tableListRetr.get(i));
83-
8492
}
8593
} catch (Exception e) {
8694
throw new TestException("error at stm num: " + numSt, e);
8795
}
8896
numSt++;
89-
9097
}
9198
} finally {
9299
if (in != null) {
@@ -108,7 +115,7 @@ public void testGetTableList() throws Exception {
108115
if (statement instanceof Select) {
109116
Select selectStatement = (Select) statement;
110117
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
111-
List tableList = tablesNamesFinder.getTableList(selectStatement);
118+
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
112119
assertEquals(6, tableList.size());
113120
int i = 1;
114121
for (Iterator iter = tableList.iterator(); iter.hasNext(); i++) {
@@ -119,8 +126,29 @@ public void testGetTableList() throws Exception {
119126

120127
}
121128

129+
public void testGetTableListWithAlias() throws Exception {
130+
String sql = "SELECT * FROM MY_TABLE1 as ALIAS_TABLE1";
131+
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
132+
133+
Select selectStatement = (Select) statement;
134+
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
135+
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
136+
assertEquals(1, tableList.size());
137+
assertEquals("MY_TABLE1", (String) tableList.get(0));
138+
}
139+
140+
public void testGetTableListWithStmt() throws Exception {
141+
String sql = "WITH TESTSTMT as (SELECT * FROM MY_TABLE1 as ALIAS_TABLE1) SELECT * FROM TESTSTMT";
142+
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
143+
144+
Select selectStatement = (Select) statement;
145+
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
146+
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
147+
assertEquals(1, tableList.size());
148+
assertEquals("MY_TABLE1", (String) tableList.get(0));
149+
}
150+
122151
private String getLine(BufferedReader in) throws Exception {
123152
return CCJSqlParserManagerTest.getLine(in);
124153
}
125-
126154
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
// -----------------------------------------------------------------------------------------------------------------------
2+
// SELECT requests test
3+
//
4+
// The requests must have the following format:
5+
// #begin
6+
// <SELECT 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+
WITH TEST_YANIT_DATA AS
26+
(SELECT
27+
IK.KURS.F_EGITIM_KURUM_NO,
28+
IK.KURS.F_EGITIM_YER_NO,
29+
IK.KURS.F_EGITMEN_NO,
30+
IK.KURS.F_EGITIM_KOD,
31+
IK.KURS.NO AS F_KURS_NO,
32+
IK.KURS_KATILIM.NO AS F_KURS_KATILIM_NO,
33+
IK.TEST_SORU_YANIT.F_TEST_BOLUM_KOD,
34+
IK.TEST_YANIT.NO AS TEST_YANIT_NO,
35+
IK.TEST_SORU_SECENEK.PUAN
36+
37+
FROM
38+
IK.TEST_YANIT
39+
LEFT OUTER JOIN
40+
IK.TEST_SORU_YANIT
41+
ON IK.TEST_YANIT.NO=IK.TEST_SORU_YANIT.F_TEST_YANIT_NO
42+
43+
AND IK.TEST_YANIT.F_FIRMA_KOD=IK.TEST_SORU_YANIT.F_FIRMA_KOD
44+
AND IK.TEST_YANIT.F_TEST_NO=IK.TEST_SORU_YANIT.F_TEST_NO
45+
46+
LEFT OUTER JOIN
47+
IK.TEST_SORU_SECENEK
48+
ON IK.TEST_SORU_YANIT.F_YANITLANAN_SECENEK_NO= IK.TEST_SORU_SECENEK.NO
49+
50+
LEFT OUTER JOIN
51+
IK.KURS
52+
ON IK.TEST_YANIT.F_KURS_NO=IK.KURS.NO
53+
54+
AND IK.TEST_YANIT.F_FIRMA_KOD=IK.KURS.F_FIRMA_KOD
55+
56+
LEFT OUTER JOIN
57+
IK.KURS_KATILIM
58+
ON IK.TEST_YANIT.F_KURS_KATILIM_NO=IK.KURS_KATILIM.NO
59+
60+
AND IK.TEST_YANIT.F_FIRMA_KOD=IK.KURS_KATILIM.F_FIRMA_KOD
61+
62+
WHERE
63+
IK.KURS.F_EGITIM_KURUM_NO IS NOT NULL
64+
65+
AND IK.KURS.IPTAL='0'
66+
AND IK.KURS.ONAY='1'
67+
AND IK.KURS_KATILIM.ONAY='1'
68+
),
69+
70+
EGITIM_KURUM_DATA AS
71+
(SELECT
72+
F_EGITIM_KURUM_NO,
73+
TEST_YANIT_NO,
74+
SUM(PUAN) AS YANIT_PUAN
75+
76+
FROM
77+
TEST_YANIT_DATA
78+
WHERE
79+
F_EGITIM_KURUM_NO IS NOT NULL
80+
81+
GROUP BY
82+
F_EGITIM_KURUM_NO,
83+
TEST_YANIT_NO
84+
),
85+
86+
EGITIM_YER_DATA AS
87+
(SELECT
88+
F_EGITIM_YER_NO,
89+
TEST_YANIT_NO,
90+
SUM(PUAN) AS YANIT_PUAN
91+
92+
FROM
93+
TEST_YANIT_DATA
94+
WHERE
95+
F_EGITIM_YER_NO IS NOT NULL
96+
AND F_TEST_BOLUM_KOD='d'
97+
98+
GROUP BY
99+
F_EGITIM_YER_NO,
100+
TEST_YANIT_NO
101+
),
102+
103+
EGITMEN_DATA AS
104+
(SELECT
105+
F_EGITMEN_NO,
106+
TEST_YANIT_NO,
107+
SUM(PUAN) AS YANIT_PUAN
108+
109+
FROM
110+
TEST_YANIT_DATA
111+
WHERE
112+
F_EGITMEN_NO IS NOT NULL
113+
AND F_TEST_BOLUM_KOD='b'
114+
115+
GROUP BY
116+
F_EGITMEN_NO,
117+
TEST_YANIT_NO
118+
),
119+
120+
EGITIM_DATA AS
121+
(SELECT
122+
F_EGITIM_KOD,
123+
TEST_YANIT_NO,
124+
SUM(PUAN) AS YANIT_PUAN
125+
126+
FROM
127+
TEST_YANIT_DATA
128+
WHERE
129+
F_EGITIM_KOD IS NOT NULL
130+
131+
GROUP BY
132+
F_EGITIM_KOD,
133+
TEST_YANIT_NO
134+
),
135+
136+
EGITIM_KURUM_EGITIM_DATA AS
137+
(SELECT
138+
F_EGITIM_KOD,
139+
F_EGITIM_KURUM_NO,
140+
TEST_YANIT_NO,
141+
SUM(PUAN) AS YANIT_PUAN
142+
FROM
143+
TEST_YANIT_DATA
144+
145+
WHERE
146+
F_EGITIM_KOD IS NOT NULL
147+
AND F_EGITIM_KURUM_NO IS NOT NULL
148+
149+
GROUP BY
150+
F_EGITIM_KOD,
151+
F_EGITIM_KURUM_NO,
152+
TEST_YANIT_NO
153+
),
154+
155+
KURS_DATA AS
156+
(SELECT
157+
F_KURS_NO,
158+
TEST_YANIT_NO,
159+
SUM(PUAN) AS YANIT_PUAN
160+
FROM
161+
TEST_YANIT_DATA
162+
WHERE
163+
F_KURS_NO IS NOT NULL
164+
GROUP BY
165+
F_KURS_NO,
166+
TEST_YANIT_NO
167+
),
168+
169+
KURS_KATILIM_DATA AS
170+
(SELECT
171+
F_KURS_KATILIM_NO,
172+
TEST_YANIT_NO,
173+
SUM(PUAN) AS YANIT_PUAN
174+
FROM
175+
TEST_YANIT_DATA
176+
WHERE
177+
F_KURS_KATILIM_NO IS NOT NULL
178+
GROUP BY
179+
F_KURS_KATILIM_NO,
180+
TEST_YANIT_NO
181+
)
182+
183+
SELECT
184+
F_KURS_NO,
185+
COUNT(TEST_YANIT_NO) AS SAYAC,
186+
SUM(YANIT_PUAN) AS TOPLAM_PUAN,
187+
DOUBLE(SUM(YANIT_PUAN))/COUNT(TEST_YANIT_NO) AS ORTALAMA
188+
FROM
189+
KURS_DATA
190+
GROUP BY
191+
F_KURS_NO
192+
#end
193+
true
194+
?
195+
IK.TEST_YANIT IK.TEST_SORU_YANIT IK.TEST_SORU_SECENEK IK.KURS IK.KURS_KATILIM
196+
?
197+
?

0 commit comments

Comments
 (0)