Java 列出已解析的SQL SELECT语句的所有表/列

Java 列出已解析的SQL SELECT语句的所有表/列,java,jsqlparser,Java,Jsqlparser,我试图使用JSqlParser从SQL SELECT语句中提取表/列列表。选择包含计算和嵌套选择: SELECT col1 AS a, (col2 - col21) AS b, col3 AS c FROM table1 where col1 in (select col4 from table4) 这应返回: col1 table1 col2 table1 col21 table1 col3 table1 col4 table4 如果我用CCJSqlParserUtil解析

我试图使用JSqlParser从SQL SELECT语句中提取表/列列表。选择包含计算和嵌套选择:

SELECT col1 AS a, (col2 - col21) AS b, col3 AS c FROM table1 
       where col1 in (select col4 from table4)
这应返回:

col1 table1
col2 table1
col21 table1
col3 table1
col4 table4
如果我用
CCJSqlParserUtil
解析它,我会得到不同的结果:

    Select stmt = (Select) CCJSqlParserUtil.parse("SELECT col1 AS a, col2 AS b, col3 AS c "
            + "FROM table1 WHERE where col1 in (select col4 from table4)");
    Map<String, Expression> map = new HashMap<>();        
    for (SelectItem selectItem : ((PlainSelect)stmt.getSelectBody()).getSelectItems()) {
        selectItem.accept(new SelectItemVisitorAdapter() {
            @Override
            public void visit(SelectExpressionItem item) {
                map.put(item.getAlias().getName(), item.getExpression());
            }
        });
    }

    System.out.println("map " + map); 
这不是我想要的。SELECT语句甚至可能更复杂,包括嵌套和子嵌套SELECT;有没有办法获取所有列/表的列表?

要提取所有表名(模拟列名),您可以使用TableNamesFinder

Statement statement = CCJSqlParserUtil.parse("SELECT * FROM MY_TABLE1");
Select selectStatement = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
Statement statement = CCJSqlParserUtil.parse("SELECT * FROM MY_TABLE1");
Select selectStatement = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
select a from tab1, tab2
select a, (select a from tab2) b from tab1
...