jsqlParser例⼦(⼀)where
⼀、单条件等于查询
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 = SelectBody();
PlainSelect plainSelect = (PlainSelect)selectBody;
Expression where = Where();
equals不等于ExpressionDeParser expressionDeParser = new ExpressionDeParser();
// 此处根据where实际情况强转最外层
EqualsTo equalsTo = (EqualsTo)where;
System.out.println("Table:"+((LeftExpression()).getTable());
System.out.println("Field:"+((LeftExpression()).getColumnName());
System.out.println("equal:"+RightExpression());
} 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 = SelectBody();
PlainSelect plainSelect = (PlainSelect)selectBody;
Expression where = Where();
ExpressionDeParser expressionDeParser = new ExpressionDeParser();
// 此处根据where实际情况强转最外层
OrExpression orExpression = (OrExpression)where;
EqualsTo equalsTo = (LeftExpression();
System.out.println("Table:"+((LeftExpression()).getTable());
System.out.println("Field:"+((LeftExpression()).getColumnName());
System.out.println("equal:"+RightExpression());
System.out.println("-----------------");
equalsTo = (RightExpression();
System.out.println("Table:"+((LeftExpression()).getTable());
System.out.println("Field:"+((LeftExpression()).getColumnName());
System.out.println("equal:"+RightExpression());
} 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 = SelectBody();
PlainSelect plainSelect = (PlainSelect)selectBody;
Expression where = Where();
ExpressionDeParser expressionDeParser = new ExpressionDeParser();
// 此处根据where实际情况强转最外层
OrExpression orExpression = (OrExpression)where;
OrExpression leftOrExpression = (LeftExpression();
EqualsTo equalsTo = (LeftExpression();
System.out.println("Table:"+((LeftExpression()).getTable());
System.out.println("Field:"+((LeftExpression()).getColumnName());
System.out.println("equal:"+RightExpression());
System.out.println("-----------------");
equalsTo = (RightExpression();
System.out.println("Table:"+((LeftExpression()).getTable());
System.out.println("Field:"+((LeftExpression()).getColumnName());
System.out.println("equal:"+RightExpression());
System.out.println("-----------------");
equalsTo = (RightExpression();
System.out.println("Table:"+((LeftExpression()).getTable());
System.out.println("Field:"+((LeftExpression()).getColumnName());
System.out.println("equal:"+RightExpression());
} catch (JSQLParserException e) {
e.printStackTrace();
}
}
得到的第⼀层的leftExpression还是⼀个orExpression,rightExpression是⼀个EqualsTo

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。