Skip to content

Commit 2ad1330

Browse files
committed
Merge branch 'support-more-tablelists' of github.com:rotty3000/JSqlParser into support-more-tablelists
2 parents f27659d + 1d09b0c commit 2ad1330

File tree

2 files changed

+134
-2
lines changed

2 files changed

+134
-2
lines changed

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

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
7474
import net.sf.jsqlparser.schema.Column;
7575
import net.sf.jsqlparser.schema.Table;
76+
import net.sf.jsqlparser.statement.delete.Delete;
77+
import net.sf.jsqlparser.statement.insert.Insert;
78+
import net.sf.jsqlparser.statement.replace.Replace;
7679
import net.sf.jsqlparser.statement.select.FromItemVisitor;
7780
import net.sf.jsqlparser.statement.select.Join;
7881
import net.sf.jsqlparser.statement.select.LateralSubSelect;
@@ -84,6 +87,7 @@
8487
import net.sf.jsqlparser.statement.select.SubSelect;
8588
import net.sf.jsqlparser.statement.select.ValuesList;
8689
import net.sf.jsqlparser.statement.select.WithItem;
90+
import net.sf.jsqlparser.statement.update.Update;
8791

8892
/**
8993
* Find all used tables within an select statement.
@@ -98,15 +102,65 @@ public class TablesNamesFinder implements SelectVisitor, FromItemVisitor, Expres
98102
*/
99103
private List<String> otherItemNames;
100104

105+
/**
106+
* Main entry for this Tool class. A list of found tables is returned.
107+
*
108+
* @param delete
109+
* @return
110+
*/
111+
public List<String> getTableList(Delete delete) {
112+
init();
113+
tables.add(delete.getTable().getName());
114+
delete.getWhere().accept(this);
115+
116+
return tables;
117+
}
118+
119+
/**
120+
* Main entry for this Tool class. A list of found tables is returned.
121+
*
122+
* @param insert
123+
* @return
124+
*/
125+
public List<String> getTableList(Insert insert) {
126+
init();
127+
tables.add(insert.getTable().getName());
128+
if (insert.getItemsList() != null) {
129+
insert.getItemsList().accept(this);
130+
}
131+
132+
return tables;
133+
}
134+
135+
/**
136+
* Main entry for this Tool class. A list of found tables is returned.
137+
*
138+
* @param replace
139+
* @return
140+
*/
141+
public List<String> getTableList(Replace replace) {
142+
init();
143+
tables.add(replace.getTable().getName());
144+
if (replace.getExpressions() != null) {
145+
for (Expression expression : replace.getExpressions()) {
146+
expression.accept(this);
147+
}
148+
}
149+
if (replace.getItemsList() != null) {
150+
replace.getItemsList().accept(this);
151+
}
152+
153+
return tables;
154+
}
155+
101156
/**
102157
* Main entry for this Tool class. A list of found tables is returned.
103158
*
104159
* @param select
105160
* @return
106161
*/
107162
public List<String> getTableList(Select select) {
108-
otherItemNames = new ArrayList<String>();
109-
tables = new ArrayList<String>();
163+
init();
110164
if (select.getWithItemsList() != null) {
111165
for (WithItem withItem : select.getWithItemsList()) {
112166
withItem.accept(this);
@@ -117,6 +171,27 @@ public List<String> getTableList(Select select) {
117171
return tables;
118172
}
119173

174+
/**
175+
* Main entry for this Tool class. A list of found tables is returned.
176+
*
177+
* @param update
178+
* @return
179+
*/
180+
public List<String> getTableList(Update update) {
181+
init();
182+
tables.add(update.getTable().getName());
183+
if (update.getExpressions() != null) {
184+
for (Expression expression : update.getExpressions()) {
185+
expression.accept(this);
186+
}
187+
}
188+
if (update.getWhere() != null) {
189+
update.getWhere().accept(this);
190+
}
191+
192+
return tables;
193+
}
194+
120195
@Override
121196
public void visit(WithItem withItem) {
122197
otherItemNames.add(withItem.getName().toLowerCase());
@@ -400,4 +475,9 @@ public void visit(MultiExpressionList multiExprList) {
400475
@Override
401476
public void visit(ValuesList valuesList) {
402477
}
478+
479+
private void init() {
480+
otherItemNames = new ArrayList<String>();
481+
tables = new ArrayList<String>();
482+
}
403483
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

1616
import junit.framework.TestCase;
1717
import net.sf.jsqlparser.parser.CCJSqlParserManager;
18+
import net.sf.jsqlparser.statement.delete.Delete;
19+
import net.sf.jsqlparser.statement.insert.Insert;
20+
import net.sf.jsqlparser.statement.replace.Replace;
1821
import net.sf.jsqlparser.statement.select.Select;
22+
import net.sf.jsqlparser.statement.update.Update;
1923
import net.sf.jsqlparser.test.TestException;
2024
import net.sf.jsqlparser.test.create.CreateTableTest;
2125
import net.sf.jsqlparser.test.simpleparsing.CCJSqlParserManagerTest;
@@ -161,6 +165,54 @@ public void testGetTableListWithLateral() throws Exception {
161165
assertTrue(tableList.contains("MY_TABLE2"));
162166
}
163167

168+
public void testGetTableListFromDelete() throws Exception {
169+
String sql = "DELETE FROM MY_TABLE1 as AL WHERE a = (SELECT a from MY_TABLE2)";
170+
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
171+
172+
Delete deleteStatement = (Delete) statement;
173+
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
174+
List<String> tableList = tablesNamesFinder.getTableList(deleteStatement);
175+
assertEquals(2, tableList.size());
176+
assertTrue(tableList.contains("MY_TABLE1"));
177+
assertTrue(tableList.contains("MY_TABLE2"));
178+
}
179+
180+
public void testGetTableListFromInsert() throws Exception {
181+
String sql = "INSERT INTO MY_TABLE1 (a) VALUES ((SELECT a from MY_TABLE2 WHERE a = 1))";
182+
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
183+
184+
Insert insertStatement = (Insert) statement;
185+
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
186+
List<String> tableList = tablesNamesFinder.getTableList(insertStatement);
187+
assertEquals(2, tableList.size());
188+
assertTrue(tableList.contains("MY_TABLE1"));
189+
assertTrue(tableList.contains("MY_TABLE2"));
190+
}
191+
192+
public void testGetTableListFromReplace() throws Exception {
193+
String sql = "REPLACE INTO MY_TABLE1 (a) VALUES ((SELECT a from MY_TABLE2 WHERE a = 1))";
194+
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
195+
196+
Replace replaceStatement = (Replace) statement;
197+
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
198+
List<String> tableList = tablesNamesFinder.getTableList(replaceStatement);
199+
assertEquals(2, tableList.size());
200+
assertTrue(tableList.contains("MY_TABLE1"));
201+
assertTrue(tableList.contains("MY_TABLE2"));
202+
}
203+
204+
public void testGetTableListFromUpdate() throws Exception {
205+
String sql = "UPDATE MY_TABLE1 SET a = (SELECT a from MY_TABLE2 WHERE a = 1)";
206+
net.sf.jsqlparser.statement.Statement statement = pm.parse(new StringReader(sql));
207+
208+
Update updateStatement = (Update) statement;
209+
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
210+
List<String> tableList = tablesNamesFinder.getTableList(updateStatement);
211+
assertEquals(2, tableList.size());
212+
assertTrue(tableList.contains("MY_TABLE1"));
213+
assertTrue(tableList.contains("MY_TABLE2"));
214+
}
215+
164216
private String getLine(BufferedReader in) throws Exception {
165217
return CCJSqlParserManagerTest.getLine(in);
166218
}

0 commit comments

Comments
 (0)