JSqlParser - Getting table name from a column

I just started to learn JSqlparser. In my opinion, I modified TablesNamesFinder to retrieve columns and tables and its work, but a very small problem.

@Override public void visit(Column col) { Column c = col; String cname = c.getFullyQualifiedName(); Table t = c.getTable(); System.out.println(t.getName()); } 

This table does not print the table, because in most cases it prints zero, and in very few cases it prints the alias of the table, but not the table. Did I forget something?

Other visits

 @Override public void visit(SelectExpressionItem exp){ exp.getExpression().accept(this); } @Override public void visit(Table tableName) { // System.out.println(tableName.getFullyQualifiedName()); } @Override public void visit(Select select) { select.getSelectBody().accept(this); } 
+7
java jsqlparser
source share
1 answer

First of all, your source code is correct. You should keep in mind:

JSqlParser is a parser only . So if you do something like

 select col1 from table1 

The Column object created by the parser does not know its name. This will only be the case if you write it in full:

 select table1.col1 from table1 

Similar behavior occurs with aliases. JSqlParser does not extend alias definitions .

Why? If you look at this example, which is the correct SQL:

 select col1 from table1, table2 

it becomes clear that to calculate the table to which the column belongs, the database schema itself is needed.

+6
source share

All Articles