我希望在from子句中获取所有子查询,但是当我使用
时private bool IsInputDateMatch(InputDate inputDate, DateTime date)
{
if (inputDate.Day != 0 && date.Day != inputDate.Day)
return false;
if (inputDate.Month != 0 && date.Month != inputDate.Month)
return false;
return inputDate.Year == 0 || date.Year == inputDate.Year;
}
它只检索from子句中的第一个子查询,当我使用
时 FromItem fromItems = plainSelect.getFromItem();
它检索from语句中的表而不是Sub查询。
答案 0 :(得分:0)
所以这里有一个小代码剪断。您必须知道,第一个表/子选择存储在PlainSelect的fromItem中以及连接中的以下内容中。你必须处理两者。这是由于JSqlParser使用的解析架构。
在JSqlParser中,您有两种类型遍历返回的对象层次结构:1。访问者模式,2。直接对象层次结构访问。我在这个小例子中实现了两个。
编辑:为了找到JSqlParser标识的分层所有子查询,我包含了类型3,这是一种黑客行为,因为我不知何故错过了deparser。
public class SimpleSqlParser10 {
public static void main(String args[]) throws JSQLParserException {
String sql = "SELECT * FROM myTable, (select * from myTable2) as data1, (select b from myTable3) as data2";
Select select = (Select)CCJSqlParserUtil.parse(sql);
System.out.println(select.toString());
System.out.println("Type 1: Visitor processing");
select.getSelectBody().accept(new SelectVisitorAdapter(){
@Override
public void visit(PlainSelect plainSelect) {
plainSelect.getFromItem().accept(fromVisitor);
if (plainSelect.getJoins()!=null)
plainSelect.getJoins().forEach(join -> join.getRightItem().accept(fromVisitor));
}
});
System.out.println("Type 2: simple method calls");
processFromItem(((PlainSelect)select.getSelectBody()).getFromItem());
if (((PlainSelect)select.getSelectBody()).getJoins()!=null)
((PlainSelect)select.getSelectBody()).getJoins().forEach(join -> processFromItem(join.getRightItem()));
System.out.println("Type 3: hierarchically process all subselects");
select.getSelectBody().accept(new SelectDeParser() {
@Override
public void visit(SubSelect subSelect) {
System.out.println(" found subselect=" + subSelect.toString());
super.visit(subSelect); }
});
}
private final static FromItemVisitorAdapter fromVisitor = new FromItemVisitorAdapter() {
@Override
public void visit(SubSelect subSelect) {
System.out.println("subselect=" + subSelect);
}
@Override
public void visit(Table table) {
System.out.println("table=" + table);
}
} ;
private static void processFromItem(FromItem fromItem) {
System.out.println("fromItem=" + fromItem);
}
}
这个输出
SELECT * FROM myTable, (SELECT * FROM myTable2) AS data1, (SELECT b FROM myTable3) AS data2
Type 1: Visitor processing
table=myTable
subselect=(SELECT * FROM myTable2) AS data1
subselect=(SELECT b FROM myTable3) AS data2
Type 2: simple method calls
fromItem=myTable
fromItem=(SELECT * FROM myTable2) AS data1
fromItem=(SELECT b FROM myTable3) AS data2
Type 3: hierarchically process all subselects
found subselect=(SELECT * FROM myTable2) AS data1
found subselect=(SELECT b FROM myTable3) AS data2