列出已解析的 SQL SELECT 语句的所有 tables/columns
Listing all the tables/columns of parsed SQL SELECT statement
我正在尝试使用 JSqlParser 从 SQL SELECT 语句中提取 tables/columns 的列表。 SELECT 包含一个计算和一个嵌套的 SELECT:
SELECT col1 AS a, (col2 - col21) AS b, col3 AS c FROM table1
where col1 in (select col4 from table4)
这应该return:
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);
打印:
map {a=col1, b=col2, c=col3}
这不是我要找的。 SELECT 语句甚至可以更复杂,有嵌套和子嵌套的 SELECT;有没有办法获得所有 columns/tables 的列表?
要提取所有表名(模拟列名),您可以使用 TableNamesFinder:
Statement statement = CCJSqlParserUtil.parse("SELECT * FROM MY_TABLE1");
Select selectStatement = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
您使用的代码(我假设来自 JSQLParsers wiki)用于:
The task here is to build a map, where the alias is the key and the
expression to this alias the value of one Map entry.
因此完全不同。
另外你应该检查:。
请记住,JSqlParser 只是一个解析器。要将列映射到表,您必须检查数据库模式并在 sql 中引入某种块或可见性检查。以下是一些有问题的星座:
select a from tab1, tab2
select a, (select a from tab2) b from tab1
...
我正在尝试使用 JSqlParser 从 SQL SELECT 语句中提取 tables/columns 的列表。 SELECT 包含一个计算和一个嵌套的 SELECT:
SELECT col1 AS a, (col2 - col21) AS b, col3 AS c FROM table1
where col1 in (select col4 from table4)
这应该return:
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);
打印:
map {a=col1, b=col2, c=col3}
这不是我要找的。 SELECT 语句甚至可以更复杂,有嵌套和子嵌套的 SELECT;有没有办法获得所有 columns/tables 的列表?
要提取所有表名(模拟列名),您可以使用 TableNamesFinder:
Statement statement = CCJSqlParserUtil.parse("SELECT * FROM MY_TABLE1");
Select selectStatement = (Select) statement;
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(selectStatement);
您使用的代码(我假设来自 JSQLParsers wiki)用于:
The task here is to build a map, where the alias is the key and the expression to this alias the value of one Map entry.
因此完全不同。
另外你应该检查:
请记住,JSqlParser 只是一个解析器。要将列映射到表,您必须检查数据库模式并在 sql 中引入某种块或可见性检查。以下是一些有问题的星座:
select a from tab1, tab2
select a, (select a from tab2) b from tab1
...