use of com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression in project cobar by alibaba.
the class MySQLExprParser method arithmeticTermOperatorExpression.
/**
* <code>higherExpr ( ('+'|'-') higherExpr)+</code>
*/
private Expression arithmeticTermOperatorExpression(String consumed, String consumedUp) throws SQLSyntaxErrorException {
Expression temp;
for (Expression expr = arithmeticFactorOperatorExpression(consumed, consumedUp); ; ) {
switch(lexer.token()) {
case OP_PLUS:
lexer.nextToken();
temp = arithmeticFactorOperatorExpression(null, null);
expr = new ArithmeticAddExpression(expr, temp).setCacheEvalRst(cacheEvalRst);
break;
case OP_MINUS:
lexer.nextToken();
temp = arithmeticFactorOperatorExpression(null, null);
expr = new ArithmeticSubtractExpression(expr, temp).setCacheEvalRst(cacheEvalRst);
break;
default:
return expr;
}
}
}
use of com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression in project cobar by alibaba.
the class MySQLExprParserTest method testArithmetic.
public void testArithmetic() throws Exception {
String sql = "? + @usrVar1 * c/@@version- e % -f diV g";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("? + @usrVar1 * c / @@version - e % - f DIV g", output);
ArithmeticSubtractExpression sub = (ArithmeticSubtractExpression) expr;
ArithmeticAddExpression add = (ArithmeticAddExpression) sub.getLeftOprand();
ArithmeticIntegerDivideExpression idiv = (ArithmeticIntegerDivideExpression) sub.getRightOprand();
ArithmeticModExpression mod = (ArithmeticModExpression) idiv.getLeftOprand();
ArithmeticDivideExpression div = (ArithmeticDivideExpression) add.getRightOprand();
ArithmeticMultiplyExpression mt = (ArithmeticMultiplyExpression) div.getLeftOprand();
MinusExpression mi = (MinusExpression) mod.getRightOprand();
Assert.assertEquals("c", ((Identifier) mt.getRightOprand()).getIdText());
Assert.assertEquals("f", ((Identifier) mi.getOperand()).getIdText());
sql = "a+-b";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a + - b", output);
sql = "a+--b";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a + - - b", output);
sql = "a++b";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a + b", output);
sql = "a+++b";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a + b", output);
sql = "a++-b";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a + - b", output);
sql = "a + b mod (-((select id from t1 limit 1)- e) ) ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a + b % - ((SELECT id FROM t1 LIMIT 0, 1) - e)", output);
add = (ArithmeticAddExpression) expr;
mod = (ArithmeticModExpression) add.getRightOprand();
mi = (MinusExpression) mod.getRightOprand();
sub = (ArithmeticSubtractExpression) mi.getOperand();
Assert.assertTrue(QueryExpression.class.isAssignableFrom(sub.getLeftOprand().getClass()));
Assert.assertEquals("e", ((Identifier) sub.getRightOprand()).getIdText());
}
use of com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression 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