use of com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression in project cobar by alibaba.
the class MySQLExprParser method comparisionExpression.
/**
* <code>BETWEEN ... AND</code> has lower precedence than other comparison
* operator
*/
private Expression comparisionExpression() throws SQLSyntaxErrorException {
Expression temp;
for (Expression fst = bitOrExpression(null, null); ; ) {
switch(lexer.token()) {
case KW_NOT:
lexer.nextToken();
switch(lexer.token()) {
case KW_BETWEEN:
lexer.nextToken();
Expression snd = comparisionExpression();
match(KW_AND);
Expression trd = comparisionExpression();
return new BetweenAndExpression(true, fst, snd, trd).setCacheEvalRst(cacheEvalRst);
case KW_RLIKE:
case KW_REGEXP:
lexer.nextToken();
temp = bitOrExpression(null, null);
fst = new RegexpExpression(true, fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case KW_LIKE:
lexer.nextToken();
temp = bitOrExpression(null, null);
Expression escape = null;
if (equalsIdentifier("ESCAPE") >= 0) {
lexer.nextToken();
escape = bitOrExpression(null, null);
}
fst = new LikeExpression(true, fst, temp, escape).setCacheEvalRst(cacheEvalRst);
continue;
case KW_IN:
if (lexer.nextToken() != PUNC_LEFT_PAREN) {
lexer.addCacheToke(KW_IN);
return fst;
}
Expression in = rightOprandOfIn();
fst = new InExpression(true, fst, in).setCacheEvalRst(cacheEvalRst);
continue;
default:
throw err("unexpect token after NOT: " + lexer.token());
}
case KW_BETWEEN:
lexer.nextToken();
Expression snd = comparisionExpression();
match(KW_AND);
Expression trd = comparisionExpression();
return new BetweenAndExpression(false, fst, snd, trd).setCacheEvalRst(cacheEvalRst);
case KW_RLIKE:
case KW_REGEXP:
lexer.nextToken();
temp = bitOrExpression(null, null);
fst = new RegexpExpression(false, fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case KW_LIKE:
lexer.nextToken();
temp = bitOrExpression(null, null);
Expression escape = null;
if (equalsIdentifier("ESCAPE") >= 0) {
lexer.nextToken();
escape = bitOrExpression(null, null);
}
fst = new LikeExpression(false, fst, temp, escape).setCacheEvalRst(cacheEvalRst);
continue;
case KW_IN:
if (lexer.nextToken() != PUNC_LEFT_PAREN) {
lexer.addCacheToke(KW_IN);
return fst;
}
temp = rightOprandOfIn();
fst = new InExpression(false, fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case KW_IS:
switch(lexer.nextToken()) {
case KW_NOT:
switch(lexer.nextToken()) {
case LITERAL_NULL:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NOT_NULL).setCacheEvalRst(cacheEvalRst);
continue;
case LITERAL_BOOL_FALSE:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NOT_FALSE).setCacheEvalRst(cacheEvalRst);
continue;
case LITERAL_BOOL_TRUE:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NOT_TRUE).setCacheEvalRst(cacheEvalRst);
continue;
default:
matchIdentifier("UNKNOWN");
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NOT_UNKNOWN).setCacheEvalRst(cacheEvalRst);
continue;
}
case LITERAL_NULL:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NULL).setCacheEvalRst(cacheEvalRst);
continue;
case LITERAL_BOOL_FALSE:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_FALSE).setCacheEvalRst(cacheEvalRst);
continue;
case LITERAL_BOOL_TRUE:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_TRUE).setCacheEvalRst(cacheEvalRst);
continue;
default:
matchIdentifier("UNKNOWN");
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_UNKNOWN).setCacheEvalRst(cacheEvalRst);
continue;
}
case OP_EQUALS:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_NULL_SAFE_EQUALS:
lexer.nextToken();
temp = bitOrExpression(null, null);
fst = new ComparisionNullSafeEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_GREATER_OR_EQUALS:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionGreaterThanOrEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_GREATER_THAN:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionGreaterThanExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_LESS_OR_EQUALS:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionLessThanOrEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_LESS_THAN:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionLessThanExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_LESS_OR_GREATER:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionLessOrGreaterThanExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_NOT_EQUALS:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionNotEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
default:
if (equalsIdentifier("SOUNDS") >= 0) {
lexer.nextToken();
match(KW_LIKE);
temp = bitOrExpression(null, null);
fst = new SoundsLikeExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
}
return fst;
}
}
}
use of com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression in project cobar by alibaba.
the class MySQLExprParser method bitOrExpression.
/**
* @param consumed not null means that a token that has been pre-consumed
* stands for next token
*/
private Expression bitOrExpression(String consumed, String consumedUp) throws SQLSyntaxErrorException {
for (Expression expr = bitAndExpression(consumed, consumedUp); ; ) {
switch(lexer.token()) {
case OP_VERTICAL_BAR:
lexer.nextToken();
Expression newExpr = bitAndExpression(null, null);
expr = new BitOrExpression(expr, newExpr).setCacheEvalRst(cacheEvalRst);
break;
default:
return expr;
}
}
}
use of com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression in project cobar by alibaba.
the class MySQLExprParserTest method testBit.
public void testBit() throws Exception {
String sql = "0b01001001 | 3 & 1.2 <<d >> 0x0f";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("b'01001001' | 3 & 1.2 << d >> x'0f'", output);
BitOrExpression or = (BitOrExpression) expr;
BitAndExpression and = (BitAndExpression) or.getRightOprand();
BitShiftExpression rs = (BitShiftExpression) and.getRightOprand();
BitShiftExpression ls = (BitShiftExpression) rs.getLeftOprand();
Assert.assertEquals("d", ((Identifier) ls.getRightOprand()).getIdText());
Assert.assertTrue(rs.isRightShift());
Assert.assertFalse(ls.isRightShift());
sql = "true + b & false ^ d - null ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("TRUE + b & FALSE ^ d - NULL", output);
and = (BitAndExpression) expr;
ArithmeticAddExpression add = (ArithmeticAddExpression) and.getLeftOprand();
ArithmeticSubtractExpression sub = (ArithmeticSubtractExpression) and.getRightOprand();
BitXORExpression xor = (BitXORExpression) sub.getLeftOprand();
Assert.assertEquals("d", ((Identifier) xor.getRightOprand()).getIdText());
Assert.assertEquals("b", ((Identifier) add.getRightOprand()).getIdText());
}
Aggregations