use of com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression 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.ArithmeticDivideExpression in project cobar by alibaba.
the class MySQLExprParser method arithmeticFactorOperatorExpression.
/**
* <code>higherExpr ( ('*'|'/'|'%'|'DIV'|'MOD') higherExpr)+</code>
*/
private Expression arithmeticFactorOperatorExpression(String consumed, String consumedUp) throws SQLSyntaxErrorException {
Expression temp;
for (Expression expr = bitXORExpression(consumed, consumedUp); ; ) {
switch(lexer.token()) {
case OP_ASTERISK:
lexer.nextToken();
temp = bitXORExpression(null, null);
expr = new ArithmeticMultiplyExpression(expr, temp).setCacheEvalRst(cacheEvalRst);
break;
case OP_SLASH:
lexer.nextToken();
temp = bitXORExpression(null, null);
expr = new ArithmeticDivideExpression(expr, temp).setCacheEvalRst(cacheEvalRst);
break;
case KW_DIV:
lexer.nextToken();
temp = bitXORExpression(null, null);
expr = new ArithmeticIntegerDivideExpression(expr, temp).setCacheEvalRst(cacheEvalRst);
break;
case OP_PERCENT:
case KW_MOD:
lexer.nextToken();
temp = bitXORExpression(null, null);
expr = new ArithmeticModExpression(expr, temp).setCacheEvalRst(cacheEvalRst);
break;
default:
return expr;
}
}
}
Aggregations