mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -89,7 +89,7 @@ public class SQLExprParser extends SQLParser {
|
||||
if (token == Token.COMMA) {
|
||||
return expr;
|
||||
} else if (token == Token.EQ) {
|
||||
expr = equalityRest2(expr);
|
||||
expr = relationalRest(expr);
|
||||
expr = andRest(expr);
|
||||
expr = orRest(expr);
|
||||
return expr;
|
||||
@@ -107,7 +107,7 @@ public class SQLExprParser extends SQLParser {
|
||||
expr = bitOrRest(expr);
|
||||
expr = inRest(expr);
|
||||
expr = relationalRest(expr);
|
||||
expr = equalityRest(expr);
|
||||
// expr = equalityRest(expr);
|
||||
expr = andRest(expr);
|
||||
expr = orRest(expr);
|
||||
|
||||
@@ -228,20 +228,20 @@ public class SQLExprParser extends SQLParser {
|
||||
SQLExpr rightExp = bitXor();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.DIV, rightExp, getDbType());
|
||||
expr = multiplicativeRest(expr);
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.MOD)) {
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.MOD) || lexer.token == Token.MOD) {
|
||||
Lexer.SavePoint savePoint = lexer.mark();
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token == Token.COMMA) {
|
||||
if (lexer.token == Token.COMMA || lexer.token == Token.EOF) {
|
||||
lexer.reset(savePoint);
|
||||
return expr;
|
||||
}
|
||||
|
||||
SQLExpr rightExp = primary();
|
||||
SQLExpr rightExp = bitXor();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Modulus, rightExp, dbType);
|
||||
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.Modulus, rightExp, JdbcConstants.MYSQL);
|
||||
expr = multiplicativeRest(expr);
|
||||
}
|
||||
return expr;
|
||||
}
|
||||
@@ -768,6 +768,9 @@ public class SQLExprParser extends SQLParser {
|
||||
sqlExpr = new SQLBinaryExpr(strVal);
|
||||
break;
|
||||
}
|
||||
case CONTAINS:
|
||||
sqlExpr = inRest(null);
|
||||
break;
|
||||
case SET: {
|
||||
Lexer.SavePoint savePoint = lexer.mark();
|
||||
lexer.nextToken();
|
||||
@@ -1670,100 +1673,6 @@ public class SQLExprParser extends SQLParser {
|
||||
return expr;
|
||||
}
|
||||
|
||||
public final SQLExpr equality() {
|
||||
SQLExpr expr = bitOr();
|
||||
return equalityRest(expr);
|
||||
}
|
||||
|
||||
private SQLExpr equalityRest2(SQLExpr expr) {
|
||||
SQLExpr rightExp;
|
||||
lexer.nextToken();
|
||||
try {
|
||||
if (lexer.token == Token.LITERAL_INT) {
|
||||
rightExp = new SQLIntegerExpr(lexer.integerValue());
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token == Token.AND
|
||||
|| lexer.token == Token.RPAREN
|
||||
|| lexer.token == Token.EOF
|
||||
|| lexer.token == Token.OR) {
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp, dbType);
|
||||
}
|
||||
|
||||
rightExp = primaryRest(rightExp);
|
||||
rightExp = bitXorRest(rightExp);
|
||||
rightExp = multiplicativeRest(rightExp);
|
||||
rightExp = additiveRest(rightExp);
|
||||
rightExp = shiftRest(rightExp);
|
||||
rightExp = bitAndRest(rightExp);
|
||||
rightExp = bitOrRest(rightExp);
|
||||
rightExp = inRest(rightExp);
|
||||
rightExp = relationalRest(rightExp);
|
||||
} else {
|
||||
rightExp = bitOr();
|
||||
}
|
||||
} catch (EOFParserException e) {
|
||||
throw new ParserException("EOF, " + expr + "=", e);
|
||||
}
|
||||
|
||||
if (lexer.token == Token.COLONEQ) {
|
||||
lexer.nextToken();
|
||||
SQLExpr colonExpr = expr();
|
||||
rightExp = new SQLBinaryOpExpr(rightExp, SQLBinaryOperator.Assignment, colonExpr, dbType);
|
||||
}
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp, dbType);
|
||||
|
||||
if (lexer.token == Token.IS || lexer.token == Token.BETWEEN) {
|
||||
expr = relationalRest(expr);
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
|
||||
public SQLExpr equalityRest(SQLExpr expr) {
|
||||
SQLExpr rightExp;
|
||||
|
||||
final Token token = lexer.token;
|
||||
if (token == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
try {
|
||||
rightExp = bitOr();
|
||||
} catch (EOFParserException e) {
|
||||
throw new ParserException("EOF, " + expr + "=", e);
|
||||
}
|
||||
|
||||
if (lexer.token == Token.COLONEQ) {
|
||||
lexer.nextToken();
|
||||
SQLExpr colonExpr = expr();
|
||||
rightExp = new SQLBinaryOpExpr(rightExp, SQLBinaryOperator.Assignment, colonExpr, dbType);
|
||||
}
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp, dbType);
|
||||
} else if (lexer.token() == Token.EQGT) {
|
||||
lexer.nextToken();
|
||||
rightExp = expr();
|
||||
String argumentName = ((SQLIdentifierExpr) expr).getName();
|
||||
return new OracleArgumentExpr(argumentName, rightExp);
|
||||
} else if (token == Token.BANGEQ || token == Token.CARETEQ) {
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
rightExp = equalityRest(rightExp);
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotEqual, rightExp, dbType);
|
||||
} else if (token == Token.COLONEQ) {
|
||||
lexer.nextToken();
|
||||
rightExp = expr();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Assignment, rightExp, dbType);
|
||||
}
|
||||
|
||||
if (lexer.token == Token.IS || lexer.token == Token.BETWEEN) {
|
||||
expr = relationalRest(expr);
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
public final SQLExpr inRest(SQLExpr expr) {
|
||||
if (lexer.token == Token.IN) {
|
||||
lexer.nextTokenLParen();
|
||||
@@ -1821,6 +1730,52 @@ public class SQLExprParser extends SQLParser {
|
||||
expr = inSubQueryExpr;
|
||||
}
|
||||
}
|
||||
} else if (lexer.token == Token.CONTAINS) {
|
||||
lexer.nextTokenLParen();
|
||||
|
||||
SQLContainsExpr containsExpr = new SQLContainsExpr(expr);
|
||||
List<SQLExpr> targetList = containsExpr.getTargetList();
|
||||
if (lexer.token == Token.LPAREN) {
|
||||
lexer.nextTokenValue();
|
||||
|
||||
if (lexer.token == Token.WITH) {
|
||||
SQLSelect select = this.createSelectParser().select();
|
||||
SQLInSubQueryExpr queryExpr = new SQLInSubQueryExpr(select);
|
||||
queryExpr.setExpr(expr);
|
||||
accept(Token.RPAREN);
|
||||
return queryExpr;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
SQLExpr item;
|
||||
if (lexer.token == Token.LITERAL_INT) {
|
||||
item = new SQLIntegerExpr(lexer.integerValue());
|
||||
lexer.nextToken();
|
||||
if (lexer.token != Token.COMMA && lexer.token != Token.RPAREN) {
|
||||
item = this.primaryRest(item);
|
||||
item = this.exprRest(item);
|
||||
}
|
||||
} else {
|
||||
item = this.expr();
|
||||
}
|
||||
|
||||
item.setParent(containsExpr);
|
||||
targetList.add(item);
|
||||
if (lexer.token == Token.COMMA) {
|
||||
lexer.nextTokenValue();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
} else {
|
||||
SQLExpr itemExpr = primary();
|
||||
itemExpr.setParent(containsExpr);
|
||||
targetList.add(itemExpr);
|
||||
}
|
||||
|
||||
expr = containsExpr;
|
||||
}
|
||||
|
||||
return expr;
|
||||
@@ -2007,7 +1962,7 @@ public class SQLExprParser extends SQLParser {
|
||||
}
|
||||
|
||||
public SQLExpr relational() {
|
||||
SQLExpr expr = equality();
|
||||
SQLExpr expr = bitOr();
|
||||
|
||||
return relationalRest(expr);
|
||||
}
|
||||
@@ -2016,289 +1971,414 @@ public class SQLExprParser extends SQLParser {
|
||||
SQLExpr rightExp;
|
||||
|
||||
Token token = lexer.token;
|
||||
if (token == Token.LT) {
|
||||
SQLBinaryOperator op = SQLBinaryOperator.LessThan;
|
||||
|
||||
lexer.nextToken();
|
||||
if (lexer.token == Token.EQ) {
|
||||
switch (token) {
|
||||
case EQ:{
|
||||
lexer.nextToken();
|
||||
op = SQLBinaryOperator.LessThanOrEqual;
|
||||
try {
|
||||
rightExp = bitOr();
|
||||
} catch (EOFParserException e) {
|
||||
throw new ParserException("EOF, " + expr + "=", e);
|
||||
}
|
||||
|
||||
if (lexer.token == Token.COLONEQ) {
|
||||
lexer.nextToken();
|
||||
SQLExpr colonExpr = expr();
|
||||
rightExp = new SQLBinaryOpExpr(rightExp, SQLBinaryOperator.Assignment, colonExpr, dbType);
|
||||
}
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp, dbType);
|
||||
}
|
||||
|
||||
rightExp = bitOr();
|
||||
expr = new SQLBinaryOpExpr(expr, op, rightExp, getDbType());
|
||||
// expr = relationalRest(expr);
|
||||
} else if (token == Token.LTEQ) {
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
// rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LessThanOrEqual, rightExp, getDbType());
|
||||
} else if (token == Token.LTEQGT) {
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LessThanOrEqualOrGreaterThan, rightExp, getDbType());
|
||||
} else if (token == Token.GT) {
|
||||
SQLBinaryOperator op = SQLBinaryOperator.GreaterThan;
|
||||
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
op = SQLBinaryOperator.GreaterThanOrEqual;
|
||||
}
|
||||
|
||||
rightExp = bitOr();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, op, rightExp, getDbType());
|
||||
} else if (token == Token.GTEQ) {
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
// rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.GreaterThanOrEqual, rightExp, getDbType());
|
||||
} else if (token == Token.BANGLT) {
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
// rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotLessThan, rightExp, getDbType());
|
||||
} else if (token == Token.BANGGT) {
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotGreaterThan, rightExp, getDbType());
|
||||
} else if (token == Token.LTGT) {
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
// rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LessThanOrGreater, rightExp, getDbType());
|
||||
} else if (token == Token.LIKE) {
|
||||
lexer.nextTokenValue();
|
||||
rightExp = bitOr();
|
||||
|
||||
// rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Like, rightExp, getDbType());
|
||||
|
||||
if (lexer.token == Token.ESCAPE) {
|
||||
lexer.nextToken();
|
||||
rightExp = primary();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Escape, rightExp, getDbType());
|
||||
}
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.RLIKE)) {
|
||||
lexer.nextToken();
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.RLike, rightExp, getDbType());
|
||||
} else if (token == Token.ILIKE) {
|
||||
lexer.nextToken();
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.ILike, rightExp, getDbType());
|
||||
} else if (token == Token.MONKEYS_AT_AT) {
|
||||
lexer.nextToken();
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.AT_AT, rightExp, getDbType());
|
||||
} else if (token == Token.MONKEYS_AT_GT) {
|
||||
lexer.nextToken();
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Array_Contains, rightExp, getDbType());
|
||||
} else if (token == Token.LT_MONKEYS_AT) {
|
||||
lexer.nextToken();
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Array_ContainedBy, rightExp, getDbType());
|
||||
} else if (token == (Token.NOT)) {
|
||||
lexer.nextToken();
|
||||
expr = notRationalRest(expr);
|
||||
} else if (token == Token.BETWEEN) {
|
||||
lexer.nextToken();
|
||||
SQLExpr beginExpr = bitOr();
|
||||
accept(Token.AND);
|
||||
SQLExpr endExpr = bitOr();
|
||||
expr = new SQLBetweenExpr(expr, beginExpr, endExpr);
|
||||
} else if (token == (Token.IS)) {
|
||||
lexer.nextTokenNotOrNull();
|
||||
|
||||
SQLBinaryOperator op;
|
||||
if (lexer.token == Token.NOT) {
|
||||
op = SQLBinaryOperator.IsNot;
|
||||
break;
|
||||
case IS: {
|
||||
lexer.nextTokenNotOrNull();
|
||||
} else {
|
||||
op = SQLBinaryOperator.Is;
|
||||
|
||||
SQLBinaryOperator op;
|
||||
if (lexer.token == Token.NOT) {
|
||||
op = SQLBinaryOperator.IsNot;
|
||||
lexer.nextTokenNotOrNull();
|
||||
} else {
|
||||
op = SQLBinaryOperator.Is;
|
||||
}
|
||||
rightExp = primary();
|
||||
expr = new SQLBinaryOpExpr(expr, op, rightExp, dbType);
|
||||
}
|
||||
rightExp = primary();
|
||||
expr = new SQLBinaryOpExpr(expr, op, rightExp, dbType);
|
||||
} else if (token == Token.IN) {
|
||||
expr = inRest(expr);
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.REGEXP)) {
|
||||
lexer.nextToken();
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.RegExp, rightExp, JdbcConstants.MYSQL);
|
||||
|
||||
} else if (JdbcConstants.POSTGRESQL.equals(lexer.dbType)) {
|
||||
if (lexer.identifierEquals(FnvHash.Constants.SIMILAR)) {
|
||||
break;
|
||||
case EQGT: {
|
||||
lexer.nextToken();
|
||||
accept(Token.TO);
|
||||
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.SIMILAR_TO, rightExp, getDbType());
|
||||
} else if (token == Token.TILDE) {
|
||||
lexer.nextToken();
|
||||
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.POSIX_Regular_Match, rightExp, getDbType());
|
||||
} else if (token == Token.TILDE_STAR) {
|
||||
lexer.nextToken();
|
||||
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.POSIX_Regular_Match_Insensitive, rightExp, getDbType());
|
||||
} else if (token == Token.BANG_TILDE) {
|
||||
lexer.nextToken();
|
||||
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.POSIX_Regular_Not_Match, rightExp, getDbType());
|
||||
} else if (token == Token.BANG_TILDE_STAR) {
|
||||
lexer.nextToken();
|
||||
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.POSIX_Regular_Not_Match_POSIX_Regular_Match_Insensitive, rightExp, getDbType());
|
||||
} else if (token == Token.TILDE_EQ) {
|
||||
lexer.nextToken();
|
||||
|
||||
rightExp = equality();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.SAME_AS, rightExp, getDbType());
|
||||
rightExp = expr();
|
||||
String argumentName = ((SQLIdentifierExpr) expr).getName();
|
||||
expr = new OracleArgumentExpr(argumentName, rightExp);
|
||||
}
|
||||
break;
|
||||
case BANGEQ:
|
||||
case CARETEQ: {
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotEqual, rightExp, dbType);
|
||||
}
|
||||
break;
|
||||
case COLONEQ:{
|
||||
lexer.nextToken();
|
||||
rightExp = expr();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Assignment, rightExp, dbType);
|
||||
}
|
||||
break;
|
||||
case LT:{
|
||||
SQLBinaryOperator op = SQLBinaryOperator.LessThan;
|
||||
|
||||
lexer.nextToken();
|
||||
if (lexer.token == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
op = SQLBinaryOperator.LessThanOrEqual;
|
||||
}
|
||||
|
||||
rightExp = bitOr();
|
||||
expr = new SQLBinaryOpExpr(expr, op, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
case LTEQ: {
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
// rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LessThanOrEqual, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
case LTEQGT: {
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LessThanOrEqualOrGreaterThan, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
case GT: {
|
||||
SQLBinaryOperator op = SQLBinaryOperator.GreaterThan;
|
||||
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
op = SQLBinaryOperator.GreaterThanOrEqual;
|
||||
}
|
||||
|
||||
rightExp = bitOr();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, op, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
case GTEQ:{
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.GreaterThanOrEqual, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
case BANGLT:{
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotLessThan, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
case BANGGT:
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotGreaterThan, rightExp, getDbType());
|
||||
break;
|
||||
case LTGT:
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.LessThanOrGreater, rightExp, getDbType());
|
||||
break;
|
||||
case LIKE:
|
||||
lexer.nextTokenValue();
|
||||
rightExp = bitOr();
|
||||
|
||||
if (rightExp.getClass() == SQLIdentifierExpr.class) {
|
||||
String name = ((SQLIdentifierExpr) rightExp).getName();
|
||||
int length = name.length();
|
||||
if(length > 1 && name.charAt(0) == name.charAt(length -1 )) {
|
||||
rightExp = new SQLCharExpr(name.substring(1, length - 1));
|
||||
}
|
||||
}
|
||||
|
||||
// rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Like, rightExp, getDbType());
|
||||
|
||||
if (lexer.token == Token.ESCAPE) {
|
||||
lexer.nextToken();
|
||||
rightExp = primary();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Escape, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
case ILIKE:
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.ILike, rightExp, getDbType());
|
||||
break;
|
||||
case MONKEYS_AT_AT:
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.AT_AT, rightExp, getDbType());
|
||||
break;
|
||||
case MONKEYS_AT_GT:
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Array_Contains, rightExp, getDbType());
|
||||
break;
|
||||
case LT_MONKEYS_AT:
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Array_ContainedBy, rightExp, getDbType());
|
||||
break;
|
||||
case NOT:
|
||||
lexer.nextToken();
|
||||
expr = notRationalRest(expr);
|
||||
break;
|
||||
case BETWEEN:
|
||||
lexer.nextToken();
|
||||
SQLExpr beginExpr = relational();
|
||||
accept(Token.AND);
|
||||
SQLExpr endExpr = relational();
|
||||
expr = new SQLBetweenExpr(expr, beginExpr, endExpr);
|
||||
break;
|
||||
case IN:
|
||||
case CONTAINS:
|
||||
expr = inRest(expr);
|
||||
break;
|
||||
case EQEQ:
|
||||
if (JdbcConstants.ODPS.equals(dbType)) {
|
||||
lexer.nextToken();
|
||||
try {
|
||||
rightExp = bitOr();
|
||||
} catch (EOFParserException e) {
|
||||
throw new ParserException("EOF, " + expr + "=", e);
|
||||
}
|
||||
|
||||
if (lexer.token == Token.COLONEQ) {
|
||||
lexer.nextToken();
|
||||
SQLExpr colonExpr = expr();
|
||||
rightExp = new SQLBinaryOpExpr(rightExp, SQLBinaryOperator.Assignment, colonExpr, dbType);
|
||||
}
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Equality, rightExp, dbType);
|
||||
}
|
||||
break;
|
||||
case TILDE:
|
||||
if (JdbcConstants.POSTGRESQL.equals(lexer.dbType)) {
|
||||
lexer.nextToken();
|
||||
|
||||
rightExp = relational();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.POSIX_Regular_Match, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
case TILDE_STAR:
|
||||
if (JdbcConstants.POSTGRESQL.equals(lexer.dbType)) {
|
||||
lexer.nextToken();
|
||||
rightExp = relational();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.POSIX_Regular_Match_Insensitive, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
case BANG_TILDE:
|
||||
if (JdbcConstants.POSTGRESQL.equals(lexer.dbType)) {
|
||||
lexer.nextToken();
|
||||
rightExp = relational();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.POSIX_Regular_Not_Match, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
case BANG_TILDE_STAR:
|
||||
if (JdbcConstants.POSTGRESQL.equals(lexer.dbType)) {
|
||||
lexer.nextToken();
|
||||
rightExp = relational();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.POSIX_Regular_Not_Match_POSIX_Regular_Match_Insensitive, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
case TILDE_EQ:
|
||||
if (JdbcConstants.POSTGRESQL.equals(lexer.dbType)) {
|
||||
lexer.nextToken();
|
||||
rightExp = relational();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.SAME_AS, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
case RLIKE:
|
||||
lexer.nextToken();
|
||||
rightExp = relational();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.RLike, rightExp, getDbType());
|
||||
break;
|
||||
case IDENTIFIER:
|
||||
long hash = lexer.hash_lower;
|
||||
if (hash == FnvHash.Constants.SOUNDS) {
|
||||
lexer.nextToken();
|
||||
accept(Token.LIKE);
|
||||
|
||||
rightExp = relational();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.SoudsLike, rightExp, getDbType());
|
||||
} else if (hash == FnvHash.Constants.REGEXP) {
|
||||
lexer.nextToken();
|
||||
rightExp = relational();
|
||||
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.RegExp, rightExp, JdbcConstants.MYSQL);
|
||||
|
||||
} else if (hash == FnvHash.Constants.SIMILAR && JdbcConstants.POSTGRESQL.equals(lexer.dbType)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.TO);
|
||||
|
||||
rightExp = relational();
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.SIMILAR_TO, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (lexer.token == Token.IS
|
||||
|| lexer.token == Token.BETWEEN) {
|
||||
expr = relationalRest(expr);
|
||||
switch (lexer.token) {
|
||||
case BETWEEN:
|
||||
case IS:
|
||||
case EQ:
|
||||
case IN:
|
||||
case CONTAINS:
|
||||
case BANG_TILDE_STAR:
|
||||
case TILDE_EQ:
|
||||
case LT:
|
||||
case LTEQ:
|
||||
case LTEQGT:
|
||||
case GT:
|
||||
case GTEQ:
|
||||
case LTGT:
|
||||
case BANGEQ:
|
||||
case LIKE:
|
||||
case NOT:
|
||||
expr = relationalRest(expr);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
public SQLExpr notRationalRest(SQLExpr expr) {
|
||||
if (lexer.token == (Token.LIKE)) {
|
||||
lexer.nextTokenValue();
|
||||
SQLExpr rightExp = equality();
|
||||
switch (lexer.token) {
|
||||
case LIKE:
|
||||
lexer.nextTokenValue();
|
||||
SQLExpr rightExp = bitOr();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotLike, rightExp, getDbType());
|
||||
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotLike, rightExp, getDbType());
|
||||
|
||||
if (lexer.token == Token.ESCAPE) {
|
||||
lexer.nextToken();
|
||||
rightExp = expr();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Escape, rightExp, getDbType());
|
||||
}
|
||||
} else if (lexer.token == Token.IN) {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLInListExpr inListExpr = new SQLInListExpr(expr, true);
|
||||
if (lexer.token == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
|
||||
exprList(inListExpr.getTargetList(), inListExpr);
|
||||
expr = inListExpr;
|
||||
|
||||
accept(Token.RPAREN);
|
||||
} else {
|
||||
SQLExpr valueExpr = this.primary();
|
||||
valueExpr.setParent(inListExpr);
|
||||
inListExpr.getTargetList().add(valueExpr);
|
||||
expr = inListExpr;
|
||||
}
|
||||
|
||||
if (inListExpr.getTargetList().size() == 1) {
|
||||
SQLExpr targetExpr = inListExpr.getTargetList().get(0);
|
||||
if (targetExpr instanceof SQLQueryExpr) {
|
||||
SQLInSubQueryExpr inSubQueryExpr = new SQLInSubQueryExpr();
|
||||
inSubQueryExpr.setNot(true);
|
||||
inSubQueryExpr.setExpr(inListExpr.getExpr());
|
||||
inSubQueryExpr.setSubQuery(((SQLQueryExpr) targetExpr).getSubQuery());
|
||||
expr = inSubQueryExpr;
|
||||
if (lexer.token == Token.ESCAPE) {
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Escape, rightExp, getDbType());
|
||||
}
|
||||
}
|
||||
break;
|
||||
case IN:
|
||||
lexer.nextToken();
|
||||
|
||||
expr = relationalRest(expr);
|
||||
return expr;
|
||||
} else if (lexer.token == (Token.BETWEEN)) {
|
||||
lexer.nextToken();
|
||||
SQLExpr beginExpr = bitOr();
|
||||
accept(Token.AND);
|
||||
SQLExpr endExpr = bitOr();
|
||||
SQLInListExpr inListExpr = new SQLInListExpr(expr, true);
|
||||
if (lexer.token == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
|
||||
expr = new SQLBetweenExpr(expr, true, beginExpr, endExpr);
|
||||
exprList(inListExpr.getTargetList(), inListExpr);
|
||||
expr = inListExpr;
|
||||
|
||||
return expr;
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.RLIKE)) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = primary();
|
||||
accept(Token.RPAREN);
|
||||
} else {
|
||||
SQLExpr valueExpr = this.primary();
|
||||
valueExpr.setParent(inListExpr);
|
||||
inListExpr.getTargetList().add(valueExpr);
|
||||
expr = inListExpr;
|
||||
}
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
if (inListExpr.getTargetList().size() == 1) {
|
||||
SQLExpr targetExpr = inListExpr.getTargetList().get(0);
|
||||
if (targetExpr instanceof SQLQueryExpr) {
|
||||
SQLInSubQueryExpr inSubQueryExpr = new SQLInSubQueryExpr();
|
||||
inSubQueryExpr.setNot(true);
|
||||
inSubQueryExpr.setExpr(inListExpr.getExpr());
|
||||
inSubQueryExpr.setSubQuery(((SQLQueryExpr) targetExpr).getSubQuery());
|
||||
expr = inSubQueryExpr;
|
||||
}
|
||||
}
|
||||
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotRLike, rightExp, getDbType());
|
||||
} else if (lexer.token == Token.ILIKE) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = primary();
|
||||
break;
|
||||
case CONTAINS:
|
||||
lexer.nextToken();
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
SQLContainsExpr containsExpr = new SQLContainsExpr(expr, true);
|
||||
if (lexer.token == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotILike, rightExp, getDbType());
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.REGEXP)) {
|
||||
lexer.nextToken();
|
||||
SQLExpr rightExp = primary();
|
||||
exprList(containsExpr.getTargetList(), containsExpr);
|
||||
expr = containsExpr;
|
||||
|
||||
rightExp = relationalRest(rightExp);
|
||||
accept(Token.RPAREN);
|
||||
} else {
|
||||
SQLExpr valueExpr = this.primary();
|
||||
valueExpr.setParent(containsExpr);
|
||||
containsExpr.getTargetList().add(valueExpr);
|
||||
expr = containsExpr;
|
||||
}
|
||||
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotRegExp, rightExp, JdbcConstants.MYSQL);
|
||||
} else {
|
||||
throw new ParserException("TODO " + lexer.info());
|
||||
if (containsExpr.getTargetList().size() == 1) {
|
||||
SQLExpr targetExpr = containsExpr.getTargetList().get(0);
|
||||
if (targetExpr instanceof SQLQueryExpr) {
|
||||
SQLInSubQueryExpr inSubQueryExpr = new SQLInSubQueryExpr();
|
||||
inSubQueryExpr.setNot(true);
|
||||
inSubQueryExpr.setExpr(containsExpr.getExpr());
|
||||
inSubQueryExpr.setSubQuery(((SQLQueryExpr) targetExpr).getSubQuery());
|
||||
expr = inSubQueryExpr;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case BETWEEN:
|
||||
lexer.nextToken();
|
||||
SQLExpr beginExpr = relational();
|
||||
accept(Token.AND);
|
||||
SQLExpr endExpr = relational();
|
||||
|
||||
expr = new SQLBetweenExpr(expr, true, beginExpr, endExpr);
|
||||
break;
|
||||
case ILIKE:
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
|
||||
return new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotILike, rightExp, getDbType());
|
||||
case LPAREN:
|
||||
expr = this.primary();
|
||||
break;
|
||||
case RLIKE:
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotRLike, rightExp, getDbType());
|
||||
break;
|
||||
case IDENTIFIER:
|
||||
long hash = lexer.hash_lower;
|
||||
if (hash == FnvHash.Constants.REGEXP) {
|
||||
lexer.nextToken();
|
||||
rightExp = bitOr();
|
||||
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.NotRegExp, rightExp, getDbType());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new ParserException("TODO " + lexer.info());
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
|
||||
@@ -391,7 +391,24 @@ public class SQLSelectParser extends SQLParser {
|
||||
long hash_lower = lexer.hash_lower();
|
||||
lexer.nextTokenEq();
|
||||
|
||||
SQLExpr identExpr = new SQLIdentifierExpr(ident, hash_lower);
|
||||
SQLExpr identExpr;
|
||||
if (lexer.token == Token.LITERAL_CHARS) {
|
||||
String literal = lexer.stringVal;
|
||||
if (hash_lower == FnvHash.Constants.TIMESTAMP) {
|
||||
identExpr = new SQLTimestampExpr(literal);
|
||||
lexer.nextToken();
|
||||
} else if (hash_lower == FnvHash.Constants.DATE) {
|
||||
identExpr = new SQLDateExpr(literal);
|
||||
lexer.nextToken();
|
||||
} else if (hash_lower == FnvHash.Constants.REAL) {
|
||||
identExpr = new SQLRealExpr(Float.parseFloat(literal));
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
identExpr = new SQLIdentifierExpr(ident, hash_lower);
|
||||
}
|
||||
} else {
|
||||
identExpr = new SQLIdentifierExpr(ident, hash_lower);
|
||||
}
|
||||
|
||||
if (lexer.token == Token.DOT) {
|
||||
identExpr = this.exprParser.primaryRest(identExpr);
|
||||
@@ -401,6 +418,7 @@ public class SQLSelectParser extends SQLParser {
|
||||
SQLExpr rightExp;
|
||||
|
||||
lexer.nextToken();
|
||||
|
||||
try {
|
||||
rightExp = this.exprParser.bitOr();
|
||||
} catch (EOFParserException e) {
|
||||
@@ -408,6 +426,29 @@ public class SQLSelectParser extends SQLParser {
|
||||
}
|
||||
|
||||
where = new SQLBinaryOpExpr(identExpr, SQLBinaryOperator.Equality, rightExp, dbType);
|
||||
switch (lexer.token) {
|
||||
case BETWEEN:
|
||||
case IS:
|
||||
case EQ:
|
||||
case IN:
|
||||
case CONTAINS:
|
||||
case BANG_TILDE_STAR:
|
||||
case TILDE_EQ:
|
||||
case LT:
|
||||
case LTEQ:
|
||||
case LTEQGT:
|
||||
case GT:
|
||||
case GTEQ:
|
||||
case LTGT:
|
||||
case BANGEQ:
|
||||
case LIKE:
|
||||
case NOT:
|
||||
where = this.exprParser.relationalRest(where);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
where = this.exprParser.andRest(where);
|
||||
where = this.exprParser.orRest(where);
|
||||
} else {
|
||||
@@ -418,17 +459,17 @@ public class SQLSelectParser extends SQLParser {
|
||||
where = this.exprParser.expr();
|
||||
}
|
||||
// where = this.exprParser.expr();
|
||||
|
||||
|
||||
if (beforeComments != null) {
|
||||
where.addBeforeComment(beforeComments);
|
||||
}
|
||||
|
||||
|
||||
if (lexer.hasComment() && lexer.isKeepComments() //
|
||||
&& lexer.token != Token.INSERT // odps multi-insert
|
||||
) {
|
||||
where.addAfterComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
|
||||
queryBlock.setWhere(where);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,6 +100,8 @@ public enum Token {
|
||||
END("END"),
|
||||
EXISTS("EXISTS"),
|
||||
IN("IN"),
|
||||
CONTAINS("CONTAINS"),
|
||||
RLIKE("RLIKE"),
|
||||
|
||||
NEW("NEW"),
|
||||
ASC("ASC"),
|
||||
|
||||
Reference in New Issue
Block a user