Skip to content

Commit

Permalink
refactor: TablesNamesFinder
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
manticore-projects committed Aug 20, 2023
1 parent f0dd3dc commit 3cae390
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 362 deletions.
72 changes: 63 additions & 9 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package net.sf.jsqlparser.util;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.AllValue;
import net.sf.jsqlparser.expression.AnalyticExpression;
import net.sf.jsqlparser.expression.AnyComparisonExpression;
Expand Down Expand Up @@ -97,6 +98,7 @@
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
import net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator;
import net.sf.jsqlparser.expression.operators.relational.SimilarToExpression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Block;
Expand Down Expand Up @@ -162,8 +164,10 @@
import net.sf.jsqlparser.statement.upsert.Upsert;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

private static final String NOT_SUPPORTED_YET = "Not supported yet.";
private List<String> tables;
private Set<String> tables;
private boolean allowColumnProcessing = false;

private List<String> otherItemNames;

@Deprecated
public List<String> getTableList(Statement statement) {
return new ArrayList<String>(getTables(statement));
}

public Set<String> getTables(Statement statement) {
init(false);
statement.accept(this);
return tables;
}

public static Set<String> findTables(String sqlStr) throws JSQLParserException {
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
return tablesNamesFinder.getTables(CCJSqlParserUtil.parse(sqlStr));
}

@Override
public void visit(Select select) {
List<WithItem> withItemsList = select.getWithItemsList();
Expand Down Expand Up @@ -222,12 +236,22 @@ public void visit(RangeExpression rangeExpression) {
/**
* Main entry for this Tool class. A list of found tables is returned.
*/
@Deprecated
public List<String> getTableList(Expression expr) {
return new ArrayList<String>(getTables(expr));
}

public Set<String> getTables(Expression expr) {
init(true);
expr.accept(this);
return tables;
}

public static Set<String> findTablesInExpression(String exprStr) throws JSQLParserException {
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
return tablesNamesFinder.getTables(CCJSqlParserUtil.parseExpression(exprStr));
}

@Override
public void visit(WithItem withItem) {
otherItemNames.add(withItem.getAlias().getName().toLowerCase());
Expand All @@ -254,7 +278,7 @@ public void visit(PlainSelect plainSelect) {
}
}
if (plainSelect.getSelectItems() != null) {
for (SelectItem item : plainSelect.getSelectItems()) {
for (SelectItem<?> item : plainSelect.getSelectItems()) {
item.accept(this);
}
}
Expand All @@ -266,6 +290,10 @@ public void visit(PlainSelect plainSelect) {
if (plainSelect.getJoins() != null) {
for (Join join : plainSelect.getJoins()) {
join.getFromItem().accept(this);
join.getRightItem().accept(this);
for (Expression expression : join.getOnExpressions()) {
expression.accept(this);
}
}
}
if (plainSelect.getWhere() != null) {
Expand Down Expand Up @@ -493,7 +521,7 @@ public void visitBinaryExpression(BinaryExpression binaryExpression) {

@Override
public void visit(ExpressionList<?> expressionList) {
for (Expression expression : expressionList.getExpressions()) {
for (Expression expression : expressionList) {
expression.accept(this);
}
}
Expand Down Expand Up @@ -628,7 +656,7 @@ public void visit(LateralSubSelect lateralSubSelect) {
*/
protected void init(boolean allowColumnProcessing) {
otherItemNames = new ArrayList<String>();
tables = new ArrayList<String>();
tables = new HashSet<>();
this.allowColumnProcessing = allowColumnProcessing;
}

Expand Down Expand Up @@ -727,6 +755,10 @@ public void visit(Delete delete) {
if (delete.getJoins() != null) {
for (Join join : delete.getJoins()) {
join.getFromItem().accept(this);
join.getRightItem().accept(this);
for (Expression expression : join.getOnExpressions()) {
expression.accept(this);
}
}
}

Expand All @@ -738,9 +770,15 @@ public void visit(Delete delete) {
@Override
public void visit(Update update) {
visit(update.getTable());
if (update.getWithItemsList() != null) {
for (WithItem withItem : update.getWithItemsList()) {
withItem.accept((SelectVisitor) this);
}
}

if (update.getStartJoins() != null) {
for (Join join : update.getStartJoins()) {
join.getFromItem().accept(this);
join.getRightItem().accept(this);
}
}
if (update.getExpressions() != null) {
Expand All @@ -755,7 +793,10 @@ public void visit(Update update) {

if (update.getJoins() != null) {
for (Join join : update.getJoins()) {
join.getFromItem().accept(this);
join.getRightItem().accept(this);
for (Expression expression : join.getOnExpressions()) {
expression.accept(this);
}
}
}

Expand All @@ -767,6 +808,11 @@ public void visit(Update update) {
@Override
public void visit(Insert insert) {
visit(insert.getTable());
if (insert.getWithItemsList() != null) {
for (WithItem withItem : insert.getWithItemsList()) {
withItem.accept((SelectVisitor) this);
}
}
if (insert.getSelect() != null) {
visit(insert.getSelect());
}
Expand Down Expand Up @@ -865,7 +911,15 @@ public void visit(HexValue hexValue) {
@Override
public void visit(Merge merge) {
visit(merge.getTable());
merge.getFromItem().accept((FromItemVisitor) this);
if (merge.getWithItemsList() != null) {
for (WithItem withItem : merge.getWithItemsList()) {
withItem.accept((SelectVisitor) this);
}
}

if (merge.getFromItem() != null) {
merge.getFromItem().accept(this);
}
}

@Override
Expand All @@ -874,8 +928,8 @@ public void visit(OracleHint hint) {
}

@Override
public void visit(TableFunction valuesList) {

public void visit(TableFunction tableFunction) {
visit(tableFunction.getFunction());
}

@Override
Expand Down
Loading

0 comments on commit 3cae390

Please sign in to comment.