JSqlParser - 从Column获取表名

时间:2022-09-08 08:02:59

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.

我刚刚开始探索JSqlparser。根据我的理解,我已经修改了TablesNamesFinder以提取列和表,并且它的工作正常但是一个非常小的问题。

@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?

这个不打印的表,对于大多数情况,它打印为null,并且在极少数情况下它打印表的别名但不打印表。有什么我忘了吗?

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);
}

1 个解决方案

#1


6  

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

首先,您的源代码是正确的。你必须记住:

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

JSqlParser只是一个解析器。所以,如果你做了类似的事情

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:

解析器生成的对象Column不知道其tablename。只有在你完全限定的情况下才会这样:

select table1.col1 from table1

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

别名会发生类似的行为。 JSqlParser不会扩展别名定义。

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

为什么?如果你看一下这个例子,这是一个正确的SQL:

select col1 from table1, table2

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

很明显,计算列所属的表需要数据库模式本身。

#1


6  

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

首先,您的源代码是正确的。你必须记住:

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

JSqlParser只是一个解析器。所以,如果你做了类似的事情

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:

解析器生成的对象Column不知道其tablename。只有在你完全限定的情况下才会这样:

select table1.col1 from table1

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

别名会发生类似的行为。 JSqlParser不会扩展别名定义。

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

为什么?如果你看一下这个例子,这是一个正确的SQL:

select col1 from table1, table2

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

很明显,计算列所属的表需要数据库模式本身。