use of com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression in project cobar by alibaba.
the class MySQLExprParserTest method testUnary.
public void testUnary() throws Exception {
String sql = "!-~ binary a collate latin1_danish_ci";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("! - ~ BINARY a COLLATE latin1_danish_ci", output);
NegativeValueExpression neg = (NegativeValueExpression) expr;
MinusExpression mi = (MinusExpression) neg.getOperand();
BitInvertExpression bi = (BitInvertExpression) mi.getOperand();
CastBinaryExpression bin = (CastBinaryExpression) bi.getOperand();
CollateExpression col = (CollateExpression) bin.getOperand();
Assert.assertEquals("a", ((Identifier) col.getString()).getIdText());
}
use of com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression 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.MinusExpression in project cobar by alibaba.
the class MySQLExprParser method unaryOpExpression.
/**
* <code>('+'|'-'|'~'|'!'|'BINARY')* higherExpr</code><br/>
* '!' has higher precedence
*/
private Expression unaryOpExpression(String consumed, String consumedUp) throws SQLSyntaxErrorException {
if (consumed == null) {
Expression expr;
switch(lexer.token()) {
case OP_EXCLAMATION:
lexer.nextToken();
expr = unaryOpExpression(null, null);
return new NegativeValueExpression(expr).setCacheEvalRst(cacheEvalRst);
case OP_PLUS:
lexer.nextToken();
return unaryOpExpression(null, null);
case OP_MINUS:
lexer.nextToken();
expr = unaryOpExpression(null, null);
return new MinusExpression(expr).setCacheEvalRst(cacheEvalRst);
case OP_TILDE:
lexer.nextToken();
expr = unaryOpExpression(null, null);
return new BitInvertExpression(expr).setCacheEvalRst(cacheEvalRst);
case KW_BINARY:
lexer.nextToken();
expr = unaryOpExpression(null, null);
return new CastBinaryExpression(expr).setCacheEvalRst(cacheEvalRst);
}
}
return collateExpression(consumed, consumedUp);
}
Aggregations