Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSqlParser - Getting table name from Column

I have just started to explore JSqlparser. According to my understanding, I have modified the TablesNamesFinder to extract columns and tables and its working fine 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 wont print table, for most of the cases it prints null and for very few cases it prints alias of the table but not the table. Is there anything I am forgetting?

Rest of the 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);
}
like image 283
Waleed Avatar asked May 16 '16 09:05

Waleed


1 Answers

First of all your sourcecode is correct. You have to keep in mind:

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

select col1 from table1

The parser generated object Column does not know its tablename. This is only the case if you write it fully qualified:

select table1.col1 from table1

Similar behaviour occurs with aliases. JSqlParser does not expand alias definitions.

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

select col1 from table1, table2

it becomes clear, that calculating the table the column belongs to needs the database schema itself.

like image 193
wumpz Avatar answered Oct 13 '22 02:10

wumpz