简体   繁体   English

如何使用JSQLPARSE从SQl检索表和列名称

[英]How to retrieve table and column names from SQl using JSQLPARSE

I am using JSQLPARSER for the first time. 我第一次使用JSQLPARSER。 I have some SQL files which come dynamically, i need to read table and column names from that SQL. 我有一些动态生成的SQL文件,我需要从该SQL中读取表名和列名。 After lot of googling I tried with JSQLPARSER. 经过大量的搜寻之后,我尝试使用JSQLPARSER。 I am trying to read column names from the file but I am unable to read column names due to expression, please can any one correct my code where I went wrong. 我正在尝试从文件中读取列名,但由于表达式原因我无法读取列名,请任何人都可以纠正我在哪里出错的代码。 I am getting CLASSCASTEXCEPTION code: 我正在获取CLASSCASTEXCEPTION代码:

public static void main(String[] args) throws JSQLParserException
    {
        // TODO Auto-generated method stub
         String statement="SELECT LOCATION_D.REGION_NAME, LOCATION_D.AREA_NAME, COUNT(DISTINCT INCIDENT_FACT.TICKET_ID) FROM LOCATION_D, INCIDENT_FACT WHERE ( LOCATION_D.LOCATION_SK=INCIDENT_FACT.LOCATION_SK ) GROUP BY LOCATION_D.REGION_NAME, LOCATION_D.AREA_NAME"; 
         CCJSqlParserManager parserManager = new CCJSqlParserManager();
         Select select=(Select) parserManager.parse(new StringReader(statement));

         PlainSelect plain=(PlainSelect)select.getSelectBody();     
         List selectitems=plain.getSelectItems();
         System.out.println(selectitems.size());
         for(int i=0;i<selectitems.size();i++)
         {
            Expression expression=((SelectExpressionItem) selectitems.get(i)).getExpression();  
            System.out.println("Expression:-"+expression);
            Column col=(Column)expression;
            System.out.println(col.getTable()+","+col.getColumnName());      
         }
    }
public class TablesNamesFinder 
    implements SelectVisitor, FromItemVisitor, ExpressionVisitor, 
    ItemsListVisitor
{
    private List tables;

    public List getTableList(Select select) {
        tables = new ArrayList();
        select.getSelectBody().accept(this);
        return tables;
    }

    public void visit(PlainSelect plainSelect) {
        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(Union union) {
        for (Iterator iter = union.getPlainSelects().iterator(); iter.hasNext();) {
            PlainSelect plainSelect = (PlainSelect) iter.next();
            visit(plainSelect);
        }
    }

    public void visit(Table tableName) {
        String tableWholeName = tableName.getWholeTableName();
        tables.add(tableWholeName);
    }

    public void visit(SubSelect subSelect) {
        subSelect.getSelectBody().accept(this);
    }
//and other visit implement ....

}

The third value you request with your select is not a column but a function. 您选择的第三个值不是列而是函数。 Therefore JSqlParser delivers an expression that you cannot cast to Column. 因此,JSqlParser提供了无法转换为Column的表达式。

As wumpz pointed out the issue, function in the list cannot be casted to a column. 正如wumpz指出的那样,列表中的函数无法转换为列。 Same is the case with my requirement and this code helped in solving it. 我的要求也是如此,此代码有助于解决它。 Hope this helps. 希望这可以帮助。 Expression can be verified if it is a column or a function and can be used accordingly. 可以验证表达式是列还是函数,并可以相应地使用它。

        for(int i=0;i<selectitems.size();i++)
        {
           Expression expression=((SelectExpressionItem) selectitems.get(i)).getExpression();  
           System.out.println("Expression:-"+expression);
           if( expression instanceof Column)
           {
               Column col=(Column)expression;
               System.out.println(col.getTable()+","+col.getColumnName());      

           }
           else if (expression instanceof Function)
           {
               Function function = (Function) expression;
               System.out.println(function.getAttribute()+","+function.getName()+""+function.getParameters());      

           }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM