use of com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression 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.BitAndExpression in project cobar by alibaba.
the class MySQLExprParserTest method testAssignment.
public void testAssignment() throws Exception {
String sql = "a /*dd*/:=b:=c";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("a := b := c", output);
Assert.assertEquals(AssignmentExpression.class, expr.getClass());
AssignmentExpression ass = (AssignmentExpression) expr;
Assert.assertEquals(AssignmentExpression.class, ass.getRightOprand().getClass());
ass = (AssignmentExpression) ass.getRightOprand();
Assert.assertEquals("b", ((Identifier) ass.getLeftOprand()).getIdText());
sql = "c=@var:=1";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("c = (@var := 1)", output);
Assert.assertEquals(ComparisionEqualsExpression.class, expr.getClass());
ass = (AssignmentExpression) ((BinaryOperatorExpression) expr).getRightOprand();
UsrDefVarPrimary usr = (UsrDefVarPrimary) ass.getLeftOprand();
Assert.assertEquals("@var", usr.getVarText());
sql = "a:=b or c &&d :=0b1101 or b'01'&0xabc";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a := b OR c AND d := b'1101' OR b'01' & x'abc'", output);
Assert.assertEquals(AssignmentExpression.class, expr.getClass());
ass = (AssignmentExpression) expr;
Assert.assertEquals(AssignmentExpression.class, ass.getRightOprand().getClass());
ass = (AssignmentExpression) ass.getRightOprand();
Assert.assertEquals(LogicalOrExpression.class, ass.getLeftOprand().getClass());
Assert.assertEquals(LogicalOrExpression.class, ass.getRightOprand().getClass());
LogicalOrExpression lor = (LogicalOrExpression) ass.getLeftOprand();
Assert.assertEquals(LogicalAndExpression.class, lor.getOperand(1).getClass());
lor = (LogicalOrExpression) ass.getRightOprand();
Assert.assertEquals(LiteralBitField.class, lor.getOperand(0).getClass());
Assert.assertEquals(BitAndExpression.class, lor.getOperand(1).getClass());
sql = "a:=((b or (c &&d)) :=((0b1101 or (b'01'&0xabc))))";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a := b OR c AND d := b'1101' OR b'01' & x'abc'", output);
Assert.assertEquals(AssignmentExpression.class, expr.getClass());
ass = (AssignmentExpression) expr;
Assert.assertEquals(AssignmentExpression.class, ass.getRightOprand().getClass());
ass = (AssignmentExpression) ass.getRightOprand();
Assert.assertEquals(LogicalOrExpression.class, ass.getLeftOprand().getClass());
Assert.assertEquals(LogicalOrExpression.class, ass.getRightOprand().getClass());
lor = (LogicalOrExpression) ass.getLeftOprand();
Assert.assertEquals(LogicalAndExpression.class, lor.getOperand(1).getClass());
lor = (LogicalOrExpression) ass.getRightOprand();
Assert.assertEquals(LiteralBitField.class, lor.getOperand(0).getClass());
Assert.assertEquals(BitAndExpression.class, lor.getOperand(1).getClass());
sql = "(a:=b) or c &&(d :=0b1101 or b'01')&0xabc ^null";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("(a := b) OR c AND (d := b'1101' OR b'01') & x'abc' ^ NULL", output);
Assert.assertEquals(LogicalOrExpression.class, expr.getClass());
lor = (LogicalOrExpression) expr;
Assert.assertEquals(AssignmentExpression.class, lor.getOperand(0).getClass());
Assert.assertEquals(LogicalAndExpression.class, lor.getOperand(1).getClass());
LogicalAndExpression land = (LogicalAndExpression) lor.getOperand(1);
Assert.assertEquals(Identifier.class, land.getOperand(0).getClass());
Assert.assertEquals(BitAndExpression.class, land.getOperand(1).getClass());
BitAndExpression band = (BitAndExpression) land.getOperand(1);
Assert.assertEquals(AssignmentExpression.class, band.getLeftOprand().getClass());
Assert.assertEquals(BitXORExpression.class, band.getRightOprand().getClass());
ass = (AssignmentExpression) band.getLeftOprand();
Assert.assertEquals(LogicalOrExpression.class, ass.getRightOprand().getClass());
BitXORExpression bxor = (BitXORExpression) band.getRightOprand();
Assert.assertEquals(LiteralHexadecimal.class, bxor.getLeftOprand().getClass());
Assert.assertEquals(LiteralNull.class, bxor.getRightOprand().getClass());
}
use of com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression 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.BitAndExpression 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