Skip to content

Commit 3cae390

Browse files
refactor: TablesNamesFinder
- use `Set` instead of `List` - implement 2 static Tool Functions takes SQL or Expression Strings - simplify the existing tests - add support for `JOIN` `ON` Expressions - fixes #1838 Signed-off-by: Andreas Reichel <[email protected]>
1 parent f0dd3dc commit 3cae390

File tree

2 files changed

+215
-362
lines changed

2 files changed

+215
-362
lines changed

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

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package net.sf.jsqlparser.util;
1111

12+
import net.sf.jsqlparser.JSQLParserException;
1213
import net.sf.jsqlparser.expression.AllValue;
1314
import net.sf.jsqlparser.expression.AnalyticExpression;
1415
import net.sf.jsqlparser.expression.AnyComparisonExpression;
@@ -97,6 +98,7 @@
9798
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
9899
import net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator;
99100
import net.sf.jsqlparser.expression.operators.relational.SimilarToExpression;
101+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
100102
import net.sf.jsqlparser.schema.Column;
101103
import net.sf.jsqlparser.schema.Table;
102104
import net.sf.jsqlparser.statement.Block;
@@ -162,8 +164,10 @@
162164
import net.sf.jsqlparser.statement.upsert.Upsert;
163165

164166
import java.util.ArrayList;
167+
import java.util.HashSet;
165168
import java.util.List;
166169
import java.util.Map;
170+
import java.util.Set;
167171

168172
/**
169173
* Find all used tables within an select statement.
@@ -176,17 +180,27 @@ public class TablesNamesFinder implements SelectVisitor, FromItemVisitor, Expres
176180
SelectItemVisitor, StatementVisitor {
177181

178182
private static final String NOT_SUPPORTED_YET = "Not supported yet.";
179-
private List<String> tables;
183+
private Set<String> tables;
180184
private boolean allowColumnProcessing = false;
181185

182186
private List<String> otherItemNames;
183187

188+
@Deprecated
184189
public List<String> getTableList(Statement statement) {
190+
return new ArrayList<String>(getTables(statement));
191+
}
192+
193+
public Set<String> getTables(Statement statement) {
185194
init(false);
186195
statement.accept(this);
187196
return tables;
188197
}
189198

199+
public static Set<String> findTables(String sqlStr) throws JSQLParserException {
200+
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
201+
return tablesNamesFinder.getTables(CCJSqlParserUtil.parse(sqlStr));
202+
}
203+
190204
@Override
191205
public void visit(Select select) {
192206
List<WithItem> withItemsList = select.getWithItemsList();
@@ -222,12 +236,22 @@ public void visit(RangeExpression rangeExpression) {
222236
/**
223237
* Main entry for this Tool class. A list of found tables is returned.
224238
*/
239+
@Deprecated
225240
public List<String> getTableList(Expression expr) {
241+
return new ArrayList<String>(getTables(expr));
242+
}
243+
244+
public Set<String> getTables(Expression expr) {
226245
init(true);
227246
expr.accept(this);
228247
return tables;
229248
}
230249

250+
public static Set<String> findTablesInExpression(String exprStr) throws JSQLParserException {
251+
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
252+
return tablesNamesFinder.getTables(CCJSqlParserUtil.parseExpression(exprStr));
253+
}
254+
231255
@Override
232256
public void visit(WithItem withItem) {
233257
otherItemNames.add(withItem.getAlias().getName().toLowerCase());
@@ -254,7 +278,7 @@ public void visit(PlainSelect plainSelect) {
254278
}
255279
}
256280
if (plainSelect.getSelectItems() != null) {
257-
for (SelectItem item : plainSelect.getSelectItems()) {
281+
for (SelectItem<?> item : plainSelect.getSelectItems()) {
258282
item.accept(this);
259283
}
260284
}
@@ -266,6 +290,10 @@ public void visit(PlainSelect plainSelect) {
266290
if (plainSelect.getJoins() != null) {
267291
for (Join join : plainSelect.getJoins()) {
268292
join.getFromItem().accept(this);
293+
join.getRightItem().accept(this);
294+
for (Expression expression : join.getOnExpressions()) {
295+
expression.accept(this);
296+
}
269297
}
270298
}
271299
if (plainSelect.getWhere() != null) {
@@ -493,7 +521,7 @@ public void visitBinaryExpression(BinaryExpression binaryExpression) {
493521

494522
@Override
495523
public void visit(ExpressionList<?> expressionList) {
496-
for (Expression expression : expressionList.getExpressions()) {
524+
for (Expression expression : expressionList) {
497525
expression.accept(this);
498526
}
499527
}
@@ -628,7 +656,7 @@ public void visit(LateralSubSelect lateralSubSelect) {
628656
*/
629657
protected void init(boolean allowColumnProcessing) {
630658
otherItemNames = new ArrayList<String>();
631-
tables = new ArrayList<String>();
659+
tables = new HashSet<>();
632660
this.allowColumnProcessing = allowColumnProcessing;
633661
}
634662

@@ -727,6 +755,10 @@ public void visit(Delete delete) {
727755
if (delete.getJoins() != null) {
728756
for (Join join : delete.getJoins()) {
729757
join.getFromItem().accept(this);
758+
join.getRightItem().accept(this);
759+
for (Expression expression : join.getOnExpressions()) {
760+
expression.accept(this);
761+
}
730762
}
731763
}
732764

@@ -738,9 +770,15 @@ public void visit(Delete delete) {
738770
@Override
739771
public void visit(Update update) {
740772
visit(update.getTable());
773+
if (update.getWithItemsList() != null) {
774+
for (WithItem withItem : update.getWithItemsList()) {
775+
withItem.accept((SelectVisitor) this);
776+
}
777+
}
778+
741779
if (update.getStartJoins() != null) {
742780
for (Join join : update.getStartJoins()) {
743-
join.getFromItem().accept(this);
781+
join.getRightItem().accept(this);
744782
}
745783
}
746784
if (update.getExpressions() != null) {
@@ -755,7 +793,10 @@ public void visit(Update update) {
755793

756794
if (update.getJoins() != null) {
757795
for (Join join : update.getJoins()) {
758-
join.getFromItem().accept(this);
796+
join.getRightItem().accept(this);
797+
for (Expression expression : join.getOnExpressions()) {
798+
expression.accept(this);
799+
}
759800
}
760801
}
761802

@@ -767,6 +808,11 @@ public void visit(Update update) {
767808
@Override
768809
public void visit(Insert insert) {
769810
visit(insert.getTable());
811+
if (insert.getWithItemsList() != null) {
812+
for (WithItem withItem : insert.getWithItemsList()) {
813+
withItem.accept((SelectVisitor) this);
814+
}
815+
}
770816
if (insert.getSelect() != null) {
771817
visit(insert.getSelect());
772818
}
@@ -865,7 +911,15 @@ public void visit(HexValue hexValue) {
865911
@Override
866912
public void visit(Merge merge) {
867913
visit(merge.getTable());
868-
merge.getFromItem().accept((FromItemVisitor) this);
914+
if (merge.getWithItemsList() != null) {
915+
for (WithItem withItem : merge.getWithItemsList()) {
916+
withItem.accept((SelectVisitor) this);
917+
}
918+
}
919+
920+
if (merge.getFromItem() != null) {
921+
merge.getFromItem().accept(this);
922+
}
869923
}
870924

871925
@Override
@@ -874,8 +928,8 @@ public void visit(OracleHint hint) {
874928
}
875929

876930
@Override
877-
public void visit(TableFunction valuesList) {
878-
931+
public void visit(TableFunction tableFunction) {
932+
visit(tableFunction.getFunction());
879933
}
880934

881935
@Override

0 commit comments

Comments
 (0)