mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved mysql & pg parser. for issue #2483
This commit is contained in:
@@ -180,7 +180,7 @@ public class PGExprParser extends SQLExprParser {
|
||||
if (expr.getClass() == SQLIdentifierExpr.class) {
|
||||
String ident = ((SQLIdentifierExpr)expr).getName();
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
if (lexer.token() == Token.COMMA || lexer.token() == Token.RPAREN) {
|
||||
return super.primaryRest(expr);
|
||||
}
|
||||
|
||||
|
||||
@@ -91,6 +91,7 @@ public class SQLExprParser extends SQLParser {
|
||||
} else if (token == Token.EQ) {
|
||||
expr = relationalRest(expr);
|
||||
expr = andRest(expr);
|
||||
expr = xorRest(expr);
|
||||
expr = orRest(expr);
|
||||
return expr;
|
||||
} else {
|
||||
@@ -109,6 +110,7 @@ public class SQLExprParser extends SQLParser {
|
||||
expr = relationalRest(expr);
|
||||
// expr = equalityRest(expr);
|
||||
expr = andRest(expr);
|
||||
expr = xorRest(expr);
|
||||
expr = orRest(expr);
|
||||
|
||||
return expr;
|
||||
@@ -1914,9 +1916,33 @@ public class SQLExprParser extends SQLParser {
|
||||
return expr;
|
||||
}
|
||||
|
||||
public SQLExpr or() {
|
||||
|
||||
public SQLExpr xor() {
|
||||
SQLExpr expr = and();
|
||||
if (lexer.token == Token.OR || lexer.token == Token.BARBAR || lexer.token == Token.XOR) {
|
||||
if (lexer.token == Token.XOR) {
|
||||
expr = xorRest(expr);
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
|
||||
public SQLExpr xorRest(SQLExpr expr) {
|
||||
for (;;) {
|
||||
if (lexer.token == Token.XOR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = and();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanXor, rightExp, dbType);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
public SQLExpr or() {
|
||||
SQLExpr expr = xor();
|
||||
if (lexer.token == Token.OR || lexer.token == Token.BARBAR) {
|
||||
expr = orRest(expr);
|
||||
}
|
||||
return expr;
|
||||
@@ -1926,7 +1952,7 @@ public class SQLExprParser extends SQLParser {
|
||||
for (;;) {
|
||||
if (lexer.token == Token.OR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = and();
|
||||
SQLExpr rightExp = xor();
|
||||
|
||||
if (lexer.token == Token.OR
|
||||
&& lexer.isEnabled(SQLParserFeature.EnableSQLBinaryOpExprGroup)) {
|
||||
@@ -1941,7 +1967,7 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
for (;;) {
|
||||
lexer.nextToken();
|
||||
SQLExpr more = and();
|
||||
SQLExpr more = xor();
|
||||
group.add(more);
|
||||
if (lexer.token == Token.OR) {
|
||||
if (lexer.isKeepComments() && lexer.hasComment()) {
|
||||
@@ -1959,18 +1985,13 @@ public class SQLExprParser extends SQLParser {
|
||||
}
|
||||
} else if (lexer.token == Token.BARBAR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = and();
|
||||
SQLExpr rightExp = xor();
|
||||
|
||||
SQLBinaryOperator op = JdbcConstants.MYSQL.equals(dbType) && !isEnabled(SQLParserFeature.PipesAsConcat)
|
||||
? SQLBinaryOperator.BooleanOr
|
||||
: SQLBinaryOperator.Concat;
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, op, rightExp, dbType);
|
||||
} else if (lexer.token == Token.XOR) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = and();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanXor, rightExp, dbType);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -450,6 +450,7 @@ public class SQLSelectParser extends SQLParser {
|
||||
}
|
||||
|
||||
where = this.exprParser.andRest(where);
|
||||
where = this.exprParser.xorRest(where);
|
||||
where = this.exprParser.orRest(where);
|
||||
} else {
|
||||
identExpr = this.exprParser.primaryRest(identExpr);
|
||||
|
||||
@@ -458,7 +458,35 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
// ////////////////////
|
||||
|
||||
public boolean visit(SQLBetweenExpr x) {
|
||||
printExpr(x.getTestExpr());
|
||||
final SQLExpr testExpr = x.getTestExpr();
|
||||
final SQLExpr beginExpr = x.getBeginExpr();
|
||||
final SQLExpr endExpr = x.getEndExpr();
|
||||
|
||||
boolean quote = false;
|
||||
if (testExpr instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOperator operator = ((SQLBinaryOpExpr) testExpr).getOperator();
|
||||
switch (operator) {
|
||||
case BooleanAnd:
|
||||
case BooleanOr:
|
||||
case BooleanXor:
|
||||
case Assignment:
|
||||
quote = true;
|
||||
break;
|
||||
default:
|
||||
quote = ((SQLBinaryOpExpr) testExpr).isBracket();
|
||||
break;
|
||||
}
|
||||
} else if (testExpr instanceof SQLNotExpr){
|
||||
quote = true;
|
||||
}
|
||||
|
||||
if (quote) {
|
||||
print('(');
|
||||
printExpr(testExpr);
|
||||
print(')');
|
||||
} else {
|
||||
printExpr(testExpr);
|
||||
}
|
||||
|
||||
if (x.isNot()) {
|
||||
print0(ucase ? " NOT BETWEEN " : " not between ");
|
||||
@@ -466,9 +494,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
print0(ucase ? " BETWEEN " : " between ");
|
||||
}
|
||||
|
||||
printExpr(x.getBeginExpr());
|
||||
printExpr(beginExpr);
|
||||
print0(ucase ? " AND " : " and ");
|
||||
printExpr(x.getEndExpr());
|
||||
|
||||
printExpr(endExpr);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user