修正运算符优先级别处理不当的问题

git-svn-id: http://code.alibabatech.com/svn/druid/trunk@156 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-05-20 06:18:32 +00:00
parent ce32206d97
commit 107b62f003
7 changed files with 274 additions and 53 deletions
@@ -15,51 +15,66 @@
*/
package com.alibaba.druid.sql.ast.expr;
/**
*
* 二元操作符
* @author wenshao 2011-5-20 下午12:32:02
* @formatter:off
*/
public enum SQLBinaryOperator {
Add("+", 70), Subtract("-", 70), Multiply("*", 60), Divide("/", 60), Concat("||", 140), BitwiseAnd("&", 90), BitwiseNot("!", 130), BitwiseOr("|", 100),
BitwiseXor("^", 50), BooleanAnd("AND", 140), BooleanOr("OR", 160), BooleanXor("XOR", 150), Equality("=", 170), Assignment(":=", 169),
GreaterThan(">", 110), GreaterThanOrEqual(">=", 110), Is("IS", 110), LessThan("<", 110), LessThanOrEqual("<=", 110), LessThanOrEqualOrGreaterThan("<=>",
110),
LessThanOrGreater("<>", 110), LeftShift("<<", 80), Like("LIKE", 110), RightShift(">>", 80), Modulus("%", 60), NotEqual("!=", 110), NotLessThan("!<", 110),
NotGreaterThan("!>", 110), Union("UNION", 0), NotLike("NOT LIKE", 110), IsNot("IS NOT", 110), Escape("ESCAPE", 110), RegExp("REGEXP", 110),
NotRegExp("NOT REGEXP", 110), COLLATE("COLLATE", 20);
Union("UNION", 0),
COLLATE("COLLATE", 20),
BitwiseXor("^", 50),
Multiply("*", 60),
Divide("/", 60),
Modulus("%", 60),
Add("+", 70),
Subtract("-", 70),
LeftShift("<<", 80),
RightShift(">>", 80),
BitwiseAnd("&", 90),
BitwiseOr("|", 100),
GreaterThan(">", 110),
GreaterThanOrEqual(">=", 110),
Is("IS", 110),
LessThan("<", 110),
LessThanOrEqual("<=", 110),
LessThanOrEqualOrGreaterThan("<=>",110),
LessThanOrGreater("<>", 110),
Like("LIKE", 110),
NotEqual("!=", 110),
NotLessThan("!<", 110),
NotGreaterThan("!>", 110),
NotLike("NOT LIKE", 110),
IsNot("IS NOT", 110),
Escape("ESCAPE", 110),
RegExp("REGEXP", 110),
NotRegExp("NOT REGEXP", 110),
Equality("=", 110),
BitwiseNot("!", 130),
Concat("||", 140),
BooleanAnd("AND", 140),
BooleanXor("XOR", 150),
BooleanOr("OR", 160),
Assignment(":=", 169),
;
public static int getPriority(SQLBinaryOperator operator) {
return 0;
}
// public static final int Add = 0;
// public static final int Subtract = 26;
// public static final int Multiply = 22;
// public static final int Divide = 9;
// public static final int Concat = 42;
// public static final int Assign = 2;
// public static final int BitwiseAnd = 3;
// public static final int BitwiseNot = 4;
// public static final int BitwiseOr = 5;
// public static final int BitwiseXor = 6;
// public static final int BooleanAnd = 7;
// public static final int BooleanOr = 8;
// public static final int Equality = 10;
// public static final int GreaterThan = 11;
// public static final int GreaterThanOrEqual = 12;
// public static final int Is = 13;
// public static final int LessThan = 14;
// public static final int LessThanOrEqual = 15;
// public static final int LessThanOrGreater = 16;
// public static final int LeftShift = 17;
// public static final int Like = 18;
// public static final int RightShift = 19;
// public static final int Modulus = 21;
// public static final int NotEqual = 23;
// public static final int NotLessThan = 24;
// public static final int NotGreaterThan = 25;
// public static final int Union = 27;
// public static final int NotLike = 40;
// public static final int IsNot = 41;
// public static final int Escape = 43;
//
public final String name;
public final int priority;
@@ -0,0 +1,109 @@
package com.alibaba.druid.sql.dialect.mysql.visitor;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLInListExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor {
public MySqlParameterizedOutputVisitor(Appendable appender){
super(appender);
}
public void println() {
print(' ');
}
public boolean visit(SQLInListExpr x) {
x.getExpr().accept(this);
if (x.isNot()) {
print(" NOT IN (##)");
} else {
print(" IN (##)");
}
return false;
}
public boolean visit(SQLBinaryOpExpr x) {
if (x.getLeft() instanceof SQLLiteralExpr && x.getRight() instanceof SQLLiteralExpr) {
print(x.getLeft().toString());
print(' ');
print(x.getOperator().name);
print(' ');
print(x.getRight().toString());
return false;
}
return super.visit(x);
}
public boolean visit(SQLIntegerExpr x) {
print('?');
return false;
}
@Override
public boolean visit(MySqlInsertStatement x) {
print("INSERT ");
if (x.isLowPriority()) {
print("LOW_PRIORITY ");
}
if (x.isDelayed()) {
print("DELAYED ");
}
if (x.isHighPriority()) {
print("HIGH_PRIORITY ");
}
if (x.isIgnore()) {
print("IGNORE ");
}
print("INTO ");
x.getTableName().accept(this);
if (x.getColumns().size() > 0) {
print(" (");
for (int i = 0, size = x.getColumns().size(); i < size; ++i) {
if (i != 0) {
print(", ");
}
x.getColumns().get(i).accept(this);
}
print(")");
}
if (x.getValuesList().size() != 0) {
print(" VALUES ");
int size = x.getValuesList().size();
if (size == 0) {
print("()");
} else {
for (int i = 0; i < 1; ++i) {
if (i != 0) {
print(", ");
}
x.getValuesList().get(i).accept(this);
}
}
}
if (x.getQuery() != null) {
print(" ");
x.getQuery().accept(this);
}
if (x.getDuplicateKeyUpdate().size() != 0) {
print(" ON DUPLICATE KEY UPDATE ");
printAndAccept(x.getDuplicateKeyUpdate(), ", ");
}
return false;
}
}
@@ -650,14 +650,14 @@ public class SQLExprParser extends SQLParser {
SQLExpr rightExp;
if (lexer.token() == Token.EQ) {
lexer.nextToken();
rightExp = or();
rightExp = relational();
rightExp = equalityRest(rightExp);
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp);
} else if (lexer.token() == Token.BANGEQ) {
lexer.nextToken();
rightExp = or();
rightExp = relational();
rightExp = equalityRest(rightExp);
@@ -731,13 +731,13 @@ public class SQLExprParser extends SQLParser {
public SQLExpr shiftRest(SQLExpr expr) throws ParserException {
if (lexer.token() == Token.LTLT) {
lexer.nextToken();
SQLExpr rightExp = multiplicative();
SQLExpr rightExp = additive();
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LeftShift, rightExp);
expr = shiftRest(expr);
} else if (lexer.token() == Token.GTGT) {
lexer.nextToken();
SQLExpr rightExp = multiplicative();
SQLExpr rightExp = additive();
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.RightShift, rightExp);
expr = shiftRest(expr);
@@ -747,42 +747,47 @@ public class SQLExprParser extends SQLParser {
}
public final SQLExpr and() throws ParserException {
SQLExpr expr = equality();
SQLExpr expr = relational();
return andRest(expr);
}
public final SQLExpr andRest(SQLExpr expr) throws ParserException {
while (lexer.token() == Token.AND || lexer.token() == Token.AMPAMP) {
if (lexer.token() == Token.AND || lexer.token() == Token.AMPAMP) {
lexer.nextToken();
SQLExpr rightExp = equality();
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanAnd, rightExp);
expr = andRest(expr);
}
return expr;
}
public final SQLExpr xor() throws ParserException {
SQLExpr expr = and();
SQLExpr expr = relational();
return xorRest(expr);
}
public final SQLExpr xorRest(SQLExpr expr) throws ParserException {
while (lexer.token() == Token.XOR) {
if (lexer.token() == Token.XOR) {
lexer.nextToken();
SQLExpr rightExp = and();
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanXor, rightExp);
SQLExpr rightExp = equality();
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanAnd, rightExp);
expr = xorRest(expr);
}
return expr;
}
public final SQLExpr or() throws ParserException {
SQLExpr expr = xor();
SQLExpr expr = and();
return orRest(expr);
}
public final SQLExpr orRest(SQLExpr expr) throws ParserException {
while (lexer.token() == Token.OR) {
lexer.nextToken();
SQLExpr rightExp = xor();
SQLExpr rightExp = and();
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.BooleanOr, rightExp);
}
return expr;
@@ -796,6 +801,7 @@ public class SQLExprParser extends SQLParser {
public SQLExpr relationalRest(SQLExpr expr) throws ParserException {
SQLExpr rightExp;
if (lexer.token() == Token.LT) {
lexer.nextToken();
rightExp = bitOr();