scouter.server.core.sqltable.TableFinder Maven / Gradle / Ivy
/*
* Copyright 2015 the original author or authors.
* @https://github.com/scouter-project/scouter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package scouter.server.core.sqltable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import net.sf.jsqlparser.expression.AllComparisonExpression;
import net.sf.jsqlparser.expression.AnalyticExpression;
import net.sf.jsqlparser.expression.AnyComparisonExpression;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.CaseExpression;
import net.sf.jsqlparser.expression.CastExpression;
import net.sf.jsqlparser.expression.DateValue;
import net.sf.jsqlparser.expression.DoubleValue;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.expression.ExtractExpression;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.expression.HexValue;
import net.sf.jsqlparser.expression.IntervalExpression;
import net.sf.jsqlparser.expression.JdbcNamedParameter;
import net.sf.jsqlparser.expression.JdbcParameter;
import net.sf.jsqlparser.expression.JsonExpression;
import net.sf.jsqlparser.expression.KeepExpression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.MySQLGroupConcat;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.NumericBind;
import net.sf.jsqlparser.expression.OracleHierarchicalExpression;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.RowConstructor;
import net.sf.jsqlparser.expression.SignedExpression;
import net.sf.jsqlparser.expression.TimeValue;
import net.sf.jsqlparser.expression.TimestampValue;
import net.sf.jsqlparser.expression.UserVariable;
import net.sf.jsqlparser.expression.WhenClause;
import net.sf.jsqlparser.expression.WithinGroupExpression;
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseAnd;
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseOr;
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseXor;
import net.sf.jsqlparser.expression.operators.arithmetic.Concat;
import net.sf.jsqlparser.expression.operators.arithmetic.Division;
import net.sf.jsqlparser.expression.operators.arithmetic.Modulo;
import net.sf.jsqlparser.expression.operators.arithmetic.Multiplication;
import net.sf.jsqlparser.expression.operators.arithmetic.Subtraction;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.Between;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExistsExpression;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.expression.operators.relational.IsNullExpression;
import net.sf.jsqlparser.expression.operators.relational.ItemsListVisitor;
import net.sf.jsqlparser.expression.operators.relational.LikeExpression;
import net.sf.jsqlparser.expression.operators.relational.Matches;
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList;
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
import net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator;
import net.sf.jsqlparser.expression.operators.relational.RegExpMySQLOperator;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.AllColumns;
import net.sf.jsqlparser.statement.select.AllTableColumns;
import net.sf.jsqlparser.statement.select.FromItemVisitor;
import net.sf.jsqlparser.statement.select.IntoTableVisitor;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.LateralSubSelect;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.statement.select.SelectItemVisitor;
import net.sf.jsqlparser.statement.select.SelectVisitor;
import net.sf.jsqlparser.statement.select.SetOperationList;
import net.sf.jsqlparser.statement.select.SubJoin;
import net.sf.jsqlparser.statement.select.SubSelect;
import net.sf.jsqlparser.statement.select.ValuesList;
import net.sf.jsqlparser.statement.select.WithItem;
public class TableFinder implements IntoTableVisitor, SelectVisitor, SelectItemVisitor, FromItemVisitor, ExpressionVisitor,
ItemsListVisitor {
final private Set tables=new TreeSet();
public Set getTableList() {
return tables;
}
public void visit(PlainSelect plainSelect) {
List cols = plainSelect.getSelectItems();
for(SelectItem col : cols){
col.accept(this);
}
if(plainSelect.getFromItem()!=null){
plainSelect.getFromItem().accept(this);
}
if (plainSelect.getJoins() != null) {
for (Iterator joinsIt = plainSelect.getJoins().iterator(); joinsIt.hasNext();) {
Join join = (Join) joinsIt.next();
join.getRightItem().accept(this);
}
}
if (plainSelect.getWhere() != null)
plainSelect.getWhere().accept(this);
}
public void visit(Table tableName) {
String tableWholeName = tableName.getFullyQualifiedName();
tables.add(tableWholeName);
}
public void visit(SubSelect subSelect) {
subSelect.getSelectBody().accept(this);
}
public void visit(Addition addition) {
visitBinaryExpression(addition);
}
public void visit(AndExpression andExpression) {
visitBinaryExpression(andExpression);
}
public void visit(Between between) {
between.getLeftExpression().accept(this);
between.getBetweenExpressionStart().accept(this);
between.getBetweenExpressionEnd().accept(this);
}
public void visit(Column tableColumn) {
}
public void visit(Division division) {
visitBinaryExpression(division);
}
public void visit(DoubleValue doubleValue) {
}
public void visit(EqualsTo equalsTo) {
visitBinaryExpression(equalsTo);
}
public void visit(Function function) {
}
public void visit(GreaterThan greaterThan) {
visitBinaryExpression(greaterThan);
}
public void visit(GreaterThanEquals greaterThanEquals) {
visitBinaryExpression(greaterThanEquals);
}
public void visit(InExpression inExpression) {
Expression e = inExpression.getLeftExpression();
if(e!=null){
e.accept(this);
}
inExpression.getRightItemsList().accept(this);
}
public void visit(IsNullExpression isNullExpression) {
}
public void visit(JdbcParameter jdbcParameter) {
}
public void visit(LikeExpression likeExpression) {
visitBinaryExpression(likeExpression);
}
public void visit(ExistsExpression existsExpression) {
existsExpression.getRightExpression().accept(this);
}
public void visit(LongValue longValue) {
}
public void visit(MinorThan minorThan) {
visitBinaryExpression(minorThan);
}
public void visit(MinorThanEquals minorThanEquals) {
visitBinaryExpression(minorThanEquals);
}
public void visit(Multiplication multiplication) {
visitBinaryExpression(multiplication);
}
public void visit(NotEqualsTo notEqualsTo) {
visitBinaryExpression(notEqualsTo);
}
public void visit(NullValue nullValue) {
}
public void visit(OrExpression orExpression) {
visitBinaryExpression(orExpression);
}
public void visit(Parenthesis parenthesis) {
parenthesis.getExpression().accept(this);
}
public void visit(Subtraction subtraction) {
visitBinaryExpression(subtraction);
}
public void visitBinaryExpression(BinaryExpression binaryExpression) {
binaryExpression.getLeftExpression().accept(this);
binaryExpression.getRightExpression().accept(this);
}
public void visit(ExpressionList expressionList) {
for (Iterator iter = expressionList.getExpressions().iterator(); iter.hasNext();) {
Expression expression = (Expression) iter.next();
expression.accept(this);
}
}
public void visit(DateValue dateValue) {
}
public void visit(TimestampValue timestampValue) {
}
public void visit(TimeValue timeValue) {
}
public void visit(CaseExpression caseExpression) {
}
public void visit(WhenClause whenClause) {
}
public void visit(SubJoin subjoin) {
subjoin.getLeft().accept(this);
subjoin.getJoin().getRightItem().accept(this);
}
public void visit(net.sf.jsqlparser.expression.StringValue stringValue) {
}
public void visit(Concat concat) {
}
public void visit(Matches matches) {
}
public void visit(BitwiseAnd bitwiseAnd) {
}
public void visit(BitwiseOr bitwiseOr) {
}
public void visit(BitwiseXor bitwiseXor) {
}
public void visit(MultiExpressionList multiExprList) {
}
public void visit(SignedExpression signedExpression) {
}
public void visit(JdbcNamedParameter jdbcNamedParameter) {
}
public void visit(AllComparisonExpression allComparisonExpression) {
}
public void visit(AnyComparisonExpression anyComparisonExpression) {
}
public void visit(CastExpression cast) {
}
public void visit(Modulo modulo) {
}
public void visit(AnalyticExpression aexpr) {
}
public void visit(ExtractExpression eexpr) {
}
public void visit(IntervalExpression iexpr) {
}
public void visit(OracleHierarchicalExpression oexpr) {
}
public void visit(RegExpMatchOperator rexpr) {
}
public void visit(JsonExpression jsonExpr) {
}
public void visit(RegExpMySQLOperator regExpMySQLOperator) {
}
public void visit(LateralSubSelect lateralSubSelect) {
}
public void visit(ValuesList valuesList) {
}
public void visit(SetOperationList setOpList) {
}
public void visit(WithItem withItem) {
}
public void visit(WithinGroupExpression wgexpr) {
}
public void visit(AllColumns allColumns) {
}
public void visit(AllTableColumns allTableColumns) {
}
public void visit(SelectExpressionItem selectExpressionItem) {
selectExpressionItem.getExpression().accept( this);
}
public void visit(UserVariable var) {
// TODO Auto-generated method stub
}
public void visit(NumericBind bind) {
// TODO Auto-generated method stub
}
public void visit(HexValue arg0) {
// TODO Auto-generated method stub
}
public void visit(KeepExpression arg0) {
// TODO Auto-generated method stub
}
public void visit(MySQLGroupConcat arg0) {
// TODO Auto-generated method stub
}
public void visit(RowConstructor arg0) {
// TODO Auto-generated method stub
}
}