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

时间:2013-05-27 07:31:35

标签: java sql sql-parser jsqlparser

我第一次使用JSQLPARSER。我有一些动态的SQL文件,我需要从该SQL读取表和列名称。经过大量谷歌搜索后,我尝试使用JSQLPARSER。我试图从文件中读取列名,但由于表达式,我无法读取列名,请问任何人在我出错的地方纠正我的代码。我正在获得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());      
         }
    }

3 个答案:

答案 0 :(得分:2)

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 ....

}

答案 1 :(得分:2)

您使用select请求的第三个值不是列而是函数。因此,JSqlParser提供了一个无法转换为Column的表达式。

答案 2 :(得分:1)

正如wumpz指出的那样,列表中的函数无法转换为列。 我的要求也是如此,这段代码有助于解决它。希望这可以帮助。如果表达式是列或函数,则可以验证表达式,并且可以相应地使用它。

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

           }

        }