Search in sources :

Example 6 with LogicalOrExpression

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());
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) RegexpExpression(com.alibaba.cobar.parser.ast.expression.string.RegexpExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) MinusExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression) ArithmeticMultiplyExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression) ComparisionLessThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanOrEqualsExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) TernaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression) ArithmeticSubtractExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression) BitAndExpression(com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) NegativeValueExpression(com.alibaba.cobar.parser.ast.expression.logical.NegativeValueExpression) SoundsLikeExpression(com.alibaba.cobar.parser.ast.expression.string.SoundsLikeExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) CastBinaryExpression(com.alibaba.cobar.parser.ast.expression.type.CastBinaryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) ArithmeticIntegerDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) BitInvertExpression(com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression) BitXORExpression(com.alibaba.cobar.parser.ast.expression.bit.BitXORExpression) LogicalNotExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalNotExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) BitShiftExpression(com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression) AssignmentExpression(com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) BitOrExpression(com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression) ComparisionGreaterThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanOrEqualsExpression) LogicalXORExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalXORExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) ArithmeticAddExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) ComparisionNotEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNotEqualsExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ComparisionLessThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanExpression) ArithmeticModExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression) ComparisionGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanExpression) ArithmeticDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression) ComparisionLessOrGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessOrGreaterThanExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) LogicalXORExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalXORExpression) LogicalNotExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalNotExpression) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)

Aggregations

Expression (com.alibaba.cobar.parser.ast.expression.Expression)6 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)6 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)6 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)6 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)6 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)6 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)6 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)6 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)6 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)6 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)6 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)6 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)6 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)6 ArithmeticAddExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression)5 ArithmeticDivideExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression)5 ArithmeticIntegerDivideExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression)5 ArithmeticModExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression)5 ArithmeticMultiplyExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression)5 ArithmeticSubtractExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression)5