use of com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression in project cobar by alibaba.
the class MySQLExprParserTest method testLogical.
public void testLogical() throws Exception {
String sql = "a || b Or c";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("a OR b OR c", output);
Assert.assertEquals(LogicalOrExpression.class, expr.getClass());
LogicalOrExpression or = (LogicalOrExpression) expr;
Assert.assertEquals(3, or.getArity());
Assert.assertEquals("b", ((Identifier) or.getOperand(1)).getIdText());
sql = "a XOR b xOr c";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a XOR b XOR c", output);
Assert.assertEquals(LogicalXORExpression.class, expr.getClass());
LogicalXORExpression xor = (LogicalXORExpression) expr;
Assert.assertEquals(LogicalXORExpression.class, xor.getLeftOprand().getClass());
xor = (LogicalXORExpression) xor.getLeftOprand();
Assert.assertEquals("b", ((Identifier) xor.getRightOprand()).getIdText());
sql = "a XOR( b xOr c)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a XOR (b XOR c)", output);
xor = (LogicalXORExpression) expr;
LogicalXORExpression xor2 = (LogicalXORExpression) xor.getRightOprand();
Assert.assertEquals("a", ((Identifier) xor.getLeftOprand()).getIdText());
Assert.assertEquals("b", ((Identifier) xor2.getLeftOprand()).getIdText());
Assert.assertEquals("c", ((Identifier) xor2.getRightOprand()).getIdText());
sql = "a and b && c";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a AND b AND c", output);
Assert.assertEquals(LogicalAndExpression.class, expr.getClass());
LogicalAndExpression and = (LogicalAndExpression) expr;
Assert.assertEquals(3, or.getArity());
Assert.assertEquals("b", ((Identifier) and.getOperand(1)).getIdText());
sql = "not NOT Not a";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("NOT NOT NOT a", output);
Assert.assertEquals(LogicalNotExpression.class, expr.getClass());
LogicalNotExpression not = (LogicalNotExpression) ((LogicalNotExpression) expr).getOperand();
Assert.assertEquals(LogicalNotExpression.class, not.getClass());
not = (LogicalNotExpression) not.getOperand();
Assert.assertEquals(LogicalNotExpression.class, not.getClass());
Assert.assertEquals("a", ((Identifier) not.getOperand()).getIdText());
}
Aggregations