一、单条件等于查询
public void testParseWhere(){
String sql = "select *from A as a left join B on a.bid = B.id left join C on A.cid = C.id left join D on B.did = D.id where a.id = 23";
try {
Select select = (Select)CCJSqlParserUtil.parse(sql);
SelectBody selectBody = select.getSelectBody();
PlainSelect plainSelect = (PlainSelect)selectBody;
Expression where = plainSelect.getWhere();
ExpressionDeParser expressionDeParser = new ExpressionDeParser();
plainSelect.getWhere().accept(expressionDeParser);
// 此处根据where实际情况强转 最外层
EqualsTo equalsTo = (EqualsTo)where;
System.out.println("Table:"+((Column)equalsTo.getLeftExpression()).getTable());
System.out.println("Field:"+((Column)equalsTo.getLeftExpression()).getColumnName());
System.out.println("equal:"+equalsTo.getRightExpression());
} catch (JSQLParserException e) {
e.printStackTrace();
}
}
由于单条件等于,返回为EqualsTo,根据EqualsTo获得表名,列名,及对应单条件值.
二、两个条件or连接
public void testParseWhere(){
String sql = "select *from A as a left join B on a.bid = B.id left join C on A.cid = C.id left join D on B.did = D.id where a.id = 23 or b.id = 34";
try {
Select select = (Select)CCJSqlParserUtil.parse(sql);
SelectBody selectBody = select.getSelectBody();
PlainSelect plainSelect = (PlainSelect)selectBody;
Expression where = plainSelect.getWhere();
ExpressionDeParser expressionDeParser = new ExpressionDeParser();
plainSelect.getWhere().accept(expressionDeParser);
// 此处根据where实际情况强转 最外层
OrExpression orExpression = (OrExpression)where;
EqualsTo equalsTo = (EqualsTo)orExpression.getLeftExpression();
System.out.println("Table:"+((Column)equalsTo.getLeftExpression()).getTable());
System.out.println("Field:"+((Column)equalsTo.getLeftExpression()).getColumnName());
System.out.println("equal:"+equalsTo.getRightExpression());
System.out.println("-----------------");
equalsTo = (EqualsTo)orExpression.getRightExpression();
System.out.println("Table:"+((Column)equalsTo.getLeftExpression()).getTable());
System.out.println("Field:"+((Column)equalsTo.getLeftExpression()).getColumnName());
System.out.println("equal:"+equalsTo.getRightExpression());
} catch (JSQLParserException e) {
e.printStackTrace();
}
}
代码中有两个条件or连接,可回忆转成OrExpression,里面还是两个EqualsTo。
三、三个条件or连接
public void testParseWhere(){
String sql = "select *from A as a left join B on a.bid = B.id left join C on A.cid = C.id left join D on B.did = D.id where a.id = 23 or b.id = 34 or c.id = 54";
try {
Select select = (Select)CCJSqlParserUtil.parse(sql);
SelectBody selectBody = select.getSelectBody();
PlainSelect plainSelect = (PlainSelect)selectBody;
Expression where = plainSelect.getWhere();
ExpressionDeParser expressionDeParser = new ExpressionDeParser();
plainSelect.getWhere().accept(expressionDeParser);
// 此处根据where实际情况强转 最外层
OrExpression orExpression = (OrExpression)where;
OrExpression leftOrExpression = (OrExpression)orExpression.getLeftExpression();
EqualsTo equalsTo = (EqualsTo)leftOrExpression.getLeftExpression();
System.out.println("Table:"+((Column)equalsTo.getLeftExpression()).getTable());
System.out.println("Field:"+((Column)equalsTo.getLeftExpression()).getColumnName());
System.out.println("equal:"+equalsTo.getRightExpression());
System.out.println("-----------------");
equalsTo = (EqualsTo)leftOrExpression.getRightExpression();
System.out.println("Table:"+((Column)equalsTo.getLeftExpression()).getTable());
System.out.println("Field:"+((Column)equalsTo.getLeftExpression()).getColumnName());
System.out.println("equal:"+equalsTo.getRightExpression());
System.out.println("-----------------");
equalsTo = (EqualsTo)orExpression.getRightExpression();
System.out.println("Table:"+((Column)equalsTo.getLeftExpression()).getTable());
System.out.println("Field:"+((Column)equalsTo.getLeftExpression()).getColumnName());
System.out.println("equal:"+equalsTo.getRightExpression());
} catch (JSQLParserException e) {
e.printStackTrace();
}
}
得到的第一层的leftExpression还是一个orExpression,rightExpression是一个EqualsTo