Here I found this code Table Finder which allow you to find the Tables in the select statement can we modify this code to be Attribute finder
Asked
Active
Viewed 344 times
0
-
Hello again :). What do you mean by Attribute? Give an example. Do you mean columns? – wumpz Aug 26 '17 at 18:26
-
Yes, Columns :) Actually I made one that traversing the select statements recursively but I am just asking because I found this is much easier and neat implementation than recursive calls. – Amr Azzam Aug 27 '17 at 09:39
2 Answers
1
Here is one way that you can start from and modify it.
It should work on JSQLParser version 1.1
static public void columnFinder(String selectQuery) {
TablesNamesFinder finder = new TablesNamesFinder() {
private Set<String> columns;
@Override
protected void init() {
columns = new HashSet<>();
super.init();
}
@Override
public void visit(Column tableColumn) {
String colName = tableColumn.getColumnName().toLowerCase();
columns.add(colName);
}
@Override
public void visit(CaseExpression caseExpression) {
caseExpression.getElseExpression().accept(this);
caseExpression.getWhenClauses().forEach(w -> w.accept(this));
}
@Override
public void visit(WhenClause whenClause) {
whenClause.getWhenExpression().accept(this);
whenClause.getThenExpression().accept(this);
}
@Override
public void visit(ExtractExpression eexpr) {
eexpr.getExpression().accept(this);
}
@Override
public List<String> getTableList(Statement statement) {
init();
statement.accept(this);
return new ArrayList<>(columns);
}
};
Statement statement = null;
try {
statement = CCJSqlParserUtil.parse(selectQuery);
} catch (JSQLParserException e) {
e.printStackTrace();
}
finder.getTableList(statement).forEach(System.out::println);
}

yakout
- 782
- 3
- 9
- 24
-
1I corrected TableNamesFinder at least for case when processing. Actual version JSqlParser 1.2-SNAPSHOT (jar still undeployed) can handle this now. Please file an issue if you find problems like this. – wumpz Aug 28 '17 at 08:25
-
@wumpz Cool. btw why there is no **ColumnsNamesFinder**? I think this will be very useful I can make pull request for that :-) – yakout Aug 28 '17 at 09:49
-
The time the fork emerged, even the TableNamesFinder was only an example. My intention was to create a complete visitor and build the finder on that but never had enough time. PRs are welcome. – wumpz Aug 28 '17 at 10:18
0
Theoretically you could override the visit (Column ) method of TableNameFinder to get the columns. I am not sure if this is enough due to its specialisation for tablenames. It could be that the processing us skipped somewhere or not complete for columns.
Another approach is the AST node processing.
The visitor pattern I a hierarchy Traversing pattern and so a similar to a tree traversal, like a tree recursion.

wumpz
- 8,257
- 3
- 30
- 25