use of com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression in project cobar by alibaba.
the class MySQLExprParser method bitAndExpression.
private Expression bitAndExpression(String consumed, String consumedUp) throws SQLSyntaxErrorException {
for (Expression expr = bitShiftExpression(consumed, consumedUp); ; ) {
switch(lexer.token()) {
case OP_AMPERSAND:
lexer.nextToken();
Expression newExpr = bitShiftExpression(null, null);
expr = new BitAndExpression(expr, newExpr).setCacheEvalRst(cacheEvalRst);
break;
default:
return expr;
}
}
}
use of com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression in project cobar by alibaba.
the class MySQLExprParser method bitShiftExpression.
/**
* <code>higherExpr ( ('<<'|'>>') higherExpr)+</code>
*/
private Expression bitShiftExpression(String consumed, String consumedUp) throws SQLSyntaxErrorException {
Expression temp;
for (Expression expr = arithmeticTermOperatorExpression(consumed, consumedUp); ; ) {
switch(lexer.token()) {
case OP_LEFT_SHIFT:
lexer.nextToken();
temp = arithmeticTermOperatorExpression(null, null);
expr = new BitShiftExpression(false, expr, temp).setCacheEvalRst(cacheEvalRst);
break;
case OP_RIGHT_SHIFT:
lexer.nextToken();
temp = arithmeticTermOperatorExpression(null, null);
expr = new BitShiftExpression(true, expr, temp).setCacheEvalRst(cacheEvalRst);
break;
default:
return expr;
}
}
}
use of com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression 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