nl.topicus.jdbc.shaded.net.sf.jsqlparser.util.TablesNamesFinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spanner-jdbc Show documentation
Show all versions of spanner-jdbc Show documentation
JDBC Driver for Google Cloud Spanner
/*
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2013 JSQLParser
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package nl.topicus.jdbc.shaded.net.sf.jsqlparser.util;
import java.util.ArrayList;
import java.util.List;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.AllComparisonExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.AnalyticExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.AnyComparisonExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.BinaryExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.CaseExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.CastExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.DateValue;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.DoubleValue;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.Expression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.ExpressionVisitor;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.ExtractExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.Function;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.HexValue;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.IntervalExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.JdbcNamedParameter;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.JdbcParameter;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.JsonExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.KeepExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.LongValue;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.MySQLGroupConcat;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.NotExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.NullValue;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.NumericBind;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.OracleHierarchicalExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.OracleHint;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.Parenthesis;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.RowConstructor;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.SignedExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.StringValue;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.TimeKeyExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.TimeValue;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.TimestampValue;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.UserVariable;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.WhenClause;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.WithinGroupExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.arithmetic.Addition;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.arithmetic.BitwiseAnd;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.arithmetic.BitwiseOr;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.arithmetic.BitwiseXor;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.arithmetic.Concat;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.arithmetic.Division;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.arithmetic.Modulo;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.arithmetic.Multiplication;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.arithmetic.Subtraction;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.Between;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.ExistsExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.GreaterThan;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.InExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.IsNullExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.ItemsListVisitor;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.JsonOperator;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.LikeExpression;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.Matches;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.MinorThan;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.MultiExpressionList;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.operators.relational.RegExpMySQLOperator;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.schema.Column;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.schema.Table;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.Commit;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.SetStatement;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.Statement;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.StatementVisitor;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.Statements;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.alter.Alter;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.create.index.CreateIndex;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.create.table.CreateTable;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.create.view.AlterView;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.create.view.CreateView;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.delete.Delete;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.drop.Drop;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.execute.Execute;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.insert.Insert;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.merge.Merge;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.replace.Replace;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.AllColumns;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.AllTableColumns;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.FromItemVisitor;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.Join;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.LateralSubSelect;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.PlainSelect;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.Select;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.SelectBody;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.SelectExpressionItem;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.SelectItem;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.SelectItemVisitor;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.SelectVisitor;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.SetOperationList;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.SubJoin;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.SubSelect;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.TableFunction;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.ValuesList;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.select.WithItem;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.truncate.Truncate;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.update.Update;
import nl.topicus.jdbc.shaded.net.sf.jsqlparser.statement.upsert.Upsert;
/**
* Find all used tables within an select statement.
*/
public class TablesNamesFinder implements SelectVisitor, FromItemVisitor, ExpressionVisitor, ItemsListVisitor, SelectItemVisitor, StatementVisitor {
private static final String NOT_SUPPORTED_YET = "Not supported yet.";
private List tables;
/**
* There are special names, that are not table names but are parsed as tables. These names are
* collected here and are not included in the tables - names anymore.
*/
private List otherItemNames;
/**
* Main entry for this Tool class. A list of found tables is returned.
*
* @param delete
* @return
*/
public List getTableList(Statement statement) {
init();
statement.accept(this);
return tables;
}
@Override
public void visit(Select select) {
if (select.getWithItemsList() != null) {
for (WithItem withItem : select.getWithItemsList()) {
withItem.accept(this);
}
}
select.getSelectBody().accept(this);
}
/**
* Main entry for this Tool class. A list of found tables is returned.
*
* @param update
* @return
*/
public List getTableList(Expression expr) {
init();
expr.accept(this);
return tables;
}
@Override
public void visit(WithItem withItem) {
otherItemNames.add(withItem.getName().toLowerCase());
withItem.getSelectBody().accept(this);
}
@Override
public void visit(PlainSelect plainSelect) {
if (plainSelect.getSelectItems() != null) {
for (SelectItem item : plainSelect.getSelectItems()) {
item.accept(this);
}
}
if (plainSelect.getFromItem() != null) {
plainSelect.getFromItem().accept(this);
}
if (plainSelect.getJoins() != null) {
for (Join join : plainSelect.getJoins()) {
join.getRightItem().accept(this);
}
}
if (plainSelect.getWhere() != null) {
plainSelect.getWhere().accept(this);
}
if (plainSelect.getOracleHierarchical() != null) {
plainSelect.getOracleHierarchical().accept(this);
}
}
@Override
public void visit(Table tableName) {
String tableWholeName = tableName.getFullyQualifiedName();
if (!otherItemNames.contains(tableWholeName.toLowerCase())
&& !tables.contains(tableWholeName)) {
tables.add(tableWholeName);
}
}
@Override
public void visit(SubSelect subSelect) {
if (subSelect.getWithItemsList() != null) {
for (WithItem withItem : subSelect.getWithItemsList()) {
withItem.accept(this);
}
}
subSelect.getSelectBody().accept(this);
}
@Override
public void visit(Addition addition) {
visitBinaryExpression(addition);
}
@Override
public void visit(AndExpression andExpression) {
visitBinaryExpression(andExpression);
}
@Override
public void visit(Between between) {
between.getLeftExpression().accept(this);
between.getBetweenExpressionStart().accept(this);
between.getBetweenExpressionEnd().accept(this);
}
@Override
public void visit(Column tableColumn) {
}
@Override
public void visit(Division division) {
visitBinaryExpression(division);
}
@Override
public void visit(DoubleValue doubleValue) {
}
@Override
public void visit(EqualsTo equalsTo) {
visitBinaryExpression(equalsTo);
}
@Override
public void visit(Function function) {
ExpressionList exprList = function.getParameters();
if (exprList != null) {
visit(exprList);
}
}
@Override
public void visit(GreaterThan greaterThan) {
visitBinaryExpression(greaterThan);
}
@Override
public void visit(GreaterThanEquals greaterThanEquals) {
visitBinaryExpression(greaterThanEquals);
}
@Override
public void visit(InExpression inExpression) {
if (inExpression.getLeftExpression() != null) {
inExpression.getLeftExpression().accept(this);
} else if (inExpression.getLeftItemsList() != null) {
inExpression.getLeftItemsList().accept(this);
}
inExpression.getRightItemsList().accept(this);
}
@Override
public void visit(SignedExpression signedExpression) {
signedExpression.getExpression().accept(this);
}
@Override
public void visit(IsNullExpression isNullExpression) {
}
@Override
public void visit(JdbcParameter jdbcParameter) {
}
@Override
public void visit(LikeExpression likeExpression) {
visitBinaryExpression(likeExpression);
}
@Override
public void visit(ExistsExpression existsExpression) {
existsExpression.getRightExpression().accept(this);
}
@Override
public void visit(LongValue longValue) {
}
@Override
public void visit(MinorThan minorThan) {
visitBinaryExpression(minorThan);
}
@Override
public void visit(MinorThanEquals minorThanEquals) {
visitBinaryExpression(minorThanEquals);
}
@Override
public void visit(Multiplication multiplication) {
visitBinaryExpression(multiplication);
}
@Override
public void visit(NotEqualsTo notEqualsTo) {
visitBinaryExpression(notEqualsTo);
}
@Override
public void visit(NullValue nullValue) {
}
@Override
public void visit(OrExpression orExpression) {
visitBinaryExpression(orExpression);
}
@Override
public void visit(Parenthesis parenthesis) {
parenthesis.getExpression().accept(this);
}
@Override
public void visit(StringValue stringValue) {
}
@Override
public void visit(Subtraction subtraction) {
visitBinaryExpression(subtraction);
}
@Override
public void visit(NotExpression notExpr) {
notExpr.getExpression().accept(this);
}
public void visitBinaryExpression(BinaryExpression binaryExpression) {
binaryExpression.getLeftExpression().accept(this);
binaryExpression.getRightExpression().accept(this);
}
@Override
public void visit(ExpressionList expressionList) {
for (Expression expression : expressionList.getExpressions()) {
expression.accept(this);
}
}
@Override
public void visit(DateValue dateValue) {
}
@Override
public void visit(TimestampValue timestampValue) {
}
@Override
public void visit(TimeValue timeValue) {
}
/*
* (non-Javadoc)
*
* @see nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.ExpressionVisitor#visit(nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.CaseExpression)
*/
@Override
public void visit(CaseExpression caseExpression) {
}
/*
* (non-Javadoc)
*
* @see nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.ExpressionVisitor#visit(nl.topicus.jdbc.shaded.net.sf.jsqlparser.expression.WhenClause)
*/
@Override
public void visit(WhenClause whenClause) {
}
@Override
public void visit(AllComparisonExpression allComparisonExpression) {
allComparisonExpression.getSubSelect().getSelectBody().accept(this);
}
@Override
public void visit(AnyComparisonExpression anyComparisonExpression) {
anyComparisonExpression.getSubSelect().getSelectBody().accept(this);
}
@Override
public void visit(SubJoin subjoin) {
subjoin.getLeft().accept(this);
subjoin.getJoin().getRightItem().accept(this);
}
@Override
public void visit(Concat concat) {
visitBinaryExpression(concat);
}
@Override
public void visit(Matches matches) {
visitBinaryExpression(matches);
}
@Override
public void visit(BitwiseAnd bitwiseAnd) {
visitBinaryExpression(bitwiseAnd);
}
@Override
public void visit(BitwiseOr bitwiseOr) {
visitBinaryExpression(bitwiseOr);
}
@Override
public void visit(BitwiseXor bitwiseXor) {
visitBinaryExpression(bitwiseXor);
}
@Override
public void visit(CastExpression cast) {
cast.getLeftExpression().accept(this);
}
@Override
public void visit(Modulo modulo) {
visitBinaryExpression(modulo);
}
@Override
public void visit(AnalyticExpression analytic) {
}
@Override
public void visit(SetOperationList list) {
for (SelectBody plainSelect : list.getSelects()) {
plainSelect.accept(this);
}
}
@Override
public void visit(ExtractExpression eexpr) {
}
@Override
public void visit(LateralSubSelect lateralSubSelect) {
lateralSubSelect.getSubSelect().getSelectBody().accept(this);
}
@Override
public void visit(MultiExpressionList multiExprList) {
for (ExpressionList exprList : multiExprList.getExprList()) {
exprList.accept(this);
}
}
@Override
public void visit(ValuesList valuesList) {
}
/**
* Initializes table names collector.
*/
protected void init() {
otherItemNames = new ArrayList();
tables = new ArrayList();
}
@Override
public void visit(IntervalExpression iexpr) {
}
@Override
public void visit(JdbcNamedParameter jdbcNamedParameter) {
}
@Override
public void visit(OracleHierarchicalExpression oexpr) {
if (oexpr.getStartExpression() != null) {
oexpr.getStartExpression().accept(this);
}
if (oexpr.getConnectExpression() != null) {
oexpr.getConnectExpression().accept(this);
}
}
@Override
public void visit(RegExpMatchOperator rexpr) {
visitBinaryExpression(rexpr);
}
@Override
public void visit(RegExpMySQLOperator rexpr) {
visitBinaryExpression(rexpr);
}
@Override
public void visit(JsonExpression jsonExpr) {
}
@Override
public void visit(JsonOperator jsonExpr) {
}
@Override
public void visit(AllColumns allColumns) {
}
@Override
public void visit(AllTableColumns allTableColumns) {
}
@Override
public void visit(SelectExpressionItem item) {
item.getExpression().accept(this);
}
@Override
public void visit(WithinGroupExpression wgexpr) {
}
@Override
public void visit(UserVariable var) {
}
@Override
public void visit(NumericBind bind) {
}
@Override
public void visit(KeepExpression aexpr) {
}
@Override
public void visit(MySQLGroupConcat groupConcat) {
}
@Override
public void visit(Delete delete) {
tables.add(delete.getTable().getName());
if (delete.getWhere() != null) {
delete.getWhere().accept(this);
}
}
@Override
public void visit(Update update) {
for (Table table : update.getTables()) {
tables.add(table.getName());
}
if (update.getExpressions() != null) {
for (Expression expression : update.getExpressions()) {
expression.accept(this);
}
}
if (update.getFromItem() != null) {
update.getFromItem().accept(this);
}
if (update.getJoins() != null) {
for (Join join : update.getJoins()) {
join.getRightItem().accept(this);
}
}
if (update.getWhere() != null) {
update.getWhere().accept(this);
}
}
@Override
public void visit(Insert insert) {
tables.add(insert.getTable().getName());
if (insert.getItemsList() != null) {
insert.getItemsList().accept(this);
}
if (insert.getSelect() != null) {
visit(insert.getSelect());
}
}
@Override
public void visit(Replace replace) {
tables.add(replace.getTable().getName());
if (replace.getExpressions() != null) {
for (Expression expression : replace.getExpressions()) {
expression.accept(this);
}
}
if (replace.getItemsList() != null) {
replace.getItemsList().accept(this);
}
}
@Override
public void visit(Drop drop) {
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
@Override
public void visit(Truncate truncate) {
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
@Override
public void visit(CreateIndex createIndex) {
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
@Override
public void visit(CreateTable create) {
tables.add(create.getTable().getFullyQualifiedName());
if (create.getSelect() != null) {
create.getSelect().accept(this);
}
}
@Override
public void visit(CreateView createView) {
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
@Override
public void visit(Alter alter) {
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
@Override
public void visit(Statements stmts) {
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
@Override
public void visit(Execute execute) {
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
@Override
public void visit(SetStatement set) {
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
@Override
public void visit(RowConstructor rowConstructor) {
for (Expression expr : rowConstructor.getExprList().getExpressions()) {
expr.accept(this);
}
}
@Override
public void visit(HexValue hexValue) {
}
@Override
public void visit(Merge merge) {
tables.add(merge.getTable().getName());
if (merge.getUsingTable() != null) {
merge.getUsingTable().accept(this);
} else if (merge.getUsingSelect() != null) {
merge.getUsingSelect().accept((FromItemVisitor) this);
}
}
@Override
public void visit(OracleHint hint) {
}
@Override
public void visit(TableFunction valuesList) {
}
@Override
public void visit(AlterView alterView) {
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}
@Override
public void visit(TimeKeyExpression timeKeyExpression) {
}
@Override
public void visit(DateTimeLiteralExpression literal) {
}
@Override
public void visit(Commit nl.topicus.jdbc.shaded.com.it) {
}
@Override
public void visit(Upsert upsert) {
tables.add(upsert.getTable().getName());
if (upsert.getItemsList() != null) {
upsert.getItemsList().accept(this);
}
if (upsert.getSelect() != null) {
visit(upsert.getSelect());
}
}
}