Search in sources :

Example 1 with CaseWhenOperatorExpression

use of com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression in project cobar by alibaba.

the class MySQLOutputASTVisitor method visit.

@Override
public void visit(CaseWhenOperatorExpression node) {
    appendable.append("CASE");
    Expression comparee = node.getComparee();
    if (comparee != null) {
        appendable.append(' ');
        comparee.accept(this);
    }
    List<Pair<Expression, Expression>> whenList = node.getWhenList();
    for (Pair<Expression, Expression> whenthen : whenList) {
        appendable.append(" WHEN ");
        Expression when = whenthen.getKey();
        when.accept(this);
        appendable.append(" THEN ");
        Expression then = whenthen.getValue();
        then.accept(this);
    }
    Expression elseRst = node.getElseResult();
    if (elseRst != null) {
        appendable.append(" ELSE ");
        elseRst.accept(this);
    }
    appendable.append(" END");
}
Also used : BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) MatchExpression(com.alibaba.cobar.parser.ast.expression.primary.MatchExpression) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) TernaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) Pair(com.alibaba.cobar.parser.util.Pair)

Example 2 with CaseWhenOperatorExpression

use of com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression in project cobar by alibaba.

the class MySQLExprParserTest method testExpr1.

public void testExpr1() throws Exception {
    String sql = "\"abc\" /* */  '\\'s' + id2/ id3, 123-456*(ii moD d)";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLExprParser parser = new MySQLExprParser(lexer);
    Expression expr = parser.expression();
    String output = output2MySQL(expr, sql);
    Assert.assertEquals("'abc\\'s' + id2 / id3", output);
    Assert.assertEquals(ArithmeticAddExpression.class, expr.getClass());
    BinaryOperatorExpression bex = (BinaryOperatorExpression) ((ArithmeticAddExpression) expr).getRightOprand();
    Assert.assertEquals(ArithmeticDivideExpression.class, bex.getClass());
    Assert.assertEquals(Identifier.class, bex.getRightOprand().getClass());
    lexer.nextToken();
    parser = new MySQLExprParser(lexer);
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("123 - 456 * (ii % d)", output);
    Assert.assertEquals(ArithmeticSubtractExpression.class, expr.getClass());
    sql = "(n'\"abc\"' \"abc\" /* */  '\\'s' + 1.123e1/ id3)*(.1e3-a||b)mod x'abc'&&(select 0b1001^b'0000')";
    lexer = new MySQLLexer(sql);
    parser = new MySQLExprParser(lexer);
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("(N'\"abc\"abc\\'s' + 11.23 / id3) * (1E+2 - a OR b) % x'abc' AND (SELECT b'1001' ^ b'0000')", output);
    Assert.assertEquals(LogicalAndExpression.class, expr.getClass());
    bex = (BinaryOperatorExpression) ((LogicalAndExpression) expr).getOperand(0);
    Assert.assertEquals(ArithmeticModExpression.class, bex.getClass());
    bex = (BinaryOperatorExpression) ((ArithmeticModExpression) bex).getLeftOprand();
    Assert.assertEquals(ArithmeticMultiplyExpression.class, bex.getClass());
    bex = (BinaryOperatorExpression) ((ArithmeticMultiplyExpression) bex).getLeftOprand();
    Assert.assertEquals(ArithmeticAddExpression.class, bex.getClass());
    Assert.assertEquals(LiteralString.class, ((ArithmeticAddExpression) bex).getLeftOprand().getClass());
    bex = (BinaryOperatorExpression) ((ArithmeticAddExpression) bex).getRightOprand();
    Assert.assertEquals(ArithmeticDivideExpression.class, bex.getClass());
    Assert.assertEquals(DMLSelectStatement.class, ((LogicalAndExpression) expr).getOperand(1).getClass());
    sql = "not! ~`select` in (1,current_date,`current_date`)like `all` div a between (c&&d) and (d|e)";
    lexer = new MySQLLexer(sql);
    parser = new MySQLExprParser(lexer);
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("NOT ! ~ `select` IN (1, CURDATE(), `current_date`) LIKE `all` DIV a BETWEEN (c AND d) AND d | e", output);
    Assert.assertEquals(LogicalNotExpression.class, expr.getClass());
    TernaryOperatorExpression tex = (TernaryOperatorExpression) ((LogicalNotExpression) expr).getOperand();
    Assert.assertEquals(BetweenAndExpression.class, tex.getClass());
    Assert.assertEquals(LikeExpression.class, tex.getFirst().getClass());
    Assert.assertEquals(LogicalAndExpression.class, tex.getSecond().getClass());
    Assert.assertEquals(BitOrExpression.class, tex.getThird().getClass());
    tex = (TernaryOperatorExpression) ((BetweenAndExpression) tex).getFirst();
    Assert.assertEquals(InExpression.class, tex.getFirst().getClass());
    Assert.assertEquals(ArithmeticIntegerDivideExpression.class, tex.getSecond().getClass());
    bex = (BinaryOperatorExpression) (InExpression) tex.getFirst();
    Assert.assertEquals(NegativeValueExpression.class, bex.getLeftOprand().getClass());
    Assert.assertEquals(InExpressionList.class, bex.getRightOprand().getClass());
    UnaryOperatorExpression uex = (UnaryOperatorExpression) ((NegativeValueExpression) bex.getLeftOprand());
    Assert.assertEquals(BitInvertExpression.class, uex.getOperand().getClass());
    sql = " binary case ~a||b&&c^d xor e when 2>any(select a ) then 3 else 4 end is not null =a";
    lexer = new MySQLLexer(sql);
    parser = new MySQLExprParser(lexer);
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("BINARY CASE ~ a OR b AND c ^ d XOR e WHEN 2 > ANY (SELECT a) THEN 3 ELSE 4 END IS NOT NULL = a", output);
    Assert.assertEquals(ComparisionEqualsExpression.class, expr.getClass());
    bex = (BinaryOperatorExpression) ((ComparisionEqualsExpression) expr);
    Assert.assertEquals(ComparisionIsExpression.class, bex.getLeftOprand().getClass());
    Assert.assertEquals(Identifier.class, bex.getRightOprand().getClass());
    ComparisionIsExpression cex = (ComparisionIsExpression) bex.getLeftOprand();
    Assert.assertEquals(CastBinaryExpression.class, cex.getOperand().getClass());
    uex = (UnaryOperatorExpression) cex.getOperand();
    Assert.assertEquals(CaseWhenOperatorExpression.class, uex.getOperand().getClass());
    CaseWhenOperatorExpression cwex = (CaseWhenOperatorExpression) uex.getOperand();
    Assert.assertEquals(LogicalOrExpression.class, cwex.getComparee().getClass());
    PolyadicOperatorExpression pex = (LogicalOrExpression) cwex.getComparee();
    Assert.assertEquals(BitInvertExpression.class, pex.getOperand(0).getClass());
    Assert.assertEquals(LogicalXORExpression.class, pex.getOperand(1).getClass());
    bex = (LogicalXORExpression) pex.getOperand(1);
    Assert.assertEquals(LogicalAndExpression.class, bex.getLeftOprand().getClass());
    Assert.assertEquals(Identifier.class, bex.getRightOprand().getClass());
    pex = (LogicalAndExpression) bex.getLeftOprand();
    Assert.assertEquals(Identifier.class, pex.getOperand(0).getClass());
    Assert.assertEquals(BitXORExpression.class, pex.getOperand(1).getClass());
    sql = " !interval(a,b)<=>a>>b collate x /?+a!=@@1 or @var sounds like -(a-b) mod -(d or e)";
    lexer = new MySQLLexer(sql);
    parser = new MySQLExprParser(lexer);
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("! INTERVAL(a, b) <=> a >> b COLLATE x / ? + a != @@1 OR @var SOUNDS LIKE - (a - b) % - (d OR e)", output);
    Assert.assertEquals(LogicalOrExpression.class, expr.getClass());
    pex = (LogicalOrExpression) expr;
    Assert.assertEquals(ComparisionNotEqualsExpression.class, pex.getOperand(0).getClass());
    Assert.assertEquals(SoundsLikeExpression.class, pex.getOperand(1).getClass());
    bex = (BinaryOperatorExpression) pex.getOperand(0);
    Assert.assertEquals(ComparisionNullSafeEqualsExpression.class, bex.getLeftOprand().getClass());
    Assert.assertEquals(SysVarPrimary.class, bex.getRightOprand().getClass());
    bex = (BinaryOperatorExpression) bex.getLeftOprand();
    Assert.assertEquals(NegativeValueExpression.class, bex.getLeftOprand().getClass());
    Assert.assertEquals(BitShiftExpression.class, bex.getRightOprand().getClass());
    bex = (BinaryOperatorExpression) bex.getRightOprand();
    Assert.assertEquals(Identifier.class, bex.getLeftOprand().getClass());
    Assert.assertEquals(ArithmeticAddExpression.class, bex.getRightOprand().getClass());
    bex = (BinaryOperatorExpression) bex.getRightOprand();
    Assert.assertEquals(ArithmeticDivideExpression.class, bex.getLeftOprand().getClass());
    Assert.assertEquals(Identifier.class, bex.getRightOprand().getClass());
    bex = (BinaryOperatorExpression) bex.getLeftOprand();
    Assert.assertEquals(CollateExpression.class, bex.getLeftOprand().getClass());
    Assert.assertEquals(ParamMarker.class, bex.getRightOprand().getClass());
    bex = (BinaryOperatorExpression) ((LogicalOrExpression) expr).getOperand(1);
    Assert.assertEquals(UsrDefVarPrimary.class, bex.getLeftOprand().getClass());
    Assert.assertEquals(ArithmeticModExpression.class, bex.getRightOprand().getClass());
    bex = (BinaryOperatorExpression) bex.getRightOprand();
    Assert.assertEquals(MinusExpression.class, bex.getLeftOprand().getClass());
    Assert.assertEquals(MinusExpression.class, bex.getRightOprand().getClass());
    uex = (UnaryOperatorExpression) bex.getLeftOprand();
    Assert.assertEquals(ArithmeticSubtractExpression.class, uex.getOperand().getClass());
    uex = (UnaryOperatorExpression) bex.getRightOprand();
    Assert.assertEquals(LogicalOrExpression.class, uex.getOperand().getClass());
}
Also used : LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) ArithmeticMultiplyExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) NegativeValueExpression(com.alibaba.cobar.parser.ast.expression.logical.NegativeValueExpression) TernaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) ArithmeticModExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression) MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) 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) ArithmeticAddExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression)

Example 3 with CaseWhenOperatorExpression

use of com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression in project cobar by alibaba.

the class MySQLExprParserTest method testPrimary.

public void testPrimary() throws Exception {
    String sql = "(1,2,existS (select id.* from t1))";
    MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
    Expression expr = parser.expression();
    String output = output2MySQL(expr, sql);
    Assert.assertEquals("ROW(1, 2, EXISTS (SELECT id.* FROM t1))", output);
    RowExpression row = (RowExpression) expr;
    Assert.assertEquals(3, row.getRowExprList().size());
    sql = "*";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("*", output);
    Assert.assertTrue(Wildcard.class.isAssignableFrom(expr.getClass()));
    sql = "case v1 when `index` then a when 2 then b else c end";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CASE v1 WHEN `index` THEN a WHEN 2 THEN b ELSE c END", output);
    CaseWhenOperatorExpression cw = (CaseWhenOperatorExpression) expr;
    Assert.assertEquals("v1", ((Identifier) cw.getComparee()).getIdText());
    Assert.assertEquals(2, cw.getWhenList().size());
    Assert.assertEquals("c", ((Identifier) cw.getElseResult()).getIdText());
    sql = "case  when 1=value then a  end";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CASE WHEN 1 = value THEN a END", output);
    cw = (CaseWhenOperatorExpression) expr;
    Assert.assertNull(cw.getComparee());
    Assert.assertEquals(1, cw.getWhenList().size());
    Assert.assertNull(cw.getElseResult());
    sql = "case  when 1=`in` then a  end";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CASE WHEN 1 = `in` THEN a END", output);
    sql = " ${INSENSITIVE}. ";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("${INSENSITIVE}", output);
    sql = "current_date, ";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CURDATE()", output);
    sql = "CurRent_Date  (  ) ";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CURDATE()", output);
    sql = "CurRent_TiMe   ";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CURTIME()", output);
    sql = "CurRent_TiMe  () ";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CURTIME()", output);
    sql = "CurRent_TimesTamp ";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("NOW()", output);
    sql = "CurRent_TimesTamp  ()";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("NOW()", output);
    sql = "localTimE";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("NOW()", output);
    sql = "localTimE  () ";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("NOW()", output);
    sql = "localTimesTamP  ";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("NOW()", output);
    sql = "localTimesTamP  () ";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("NOW()", output);
    sql = "CurRent_user ";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CURRENT_USER()", output);
    sql = "CurRent_user  () ";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CURRENT_USER()", output);
    sql = "default  () ";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("DEFAULT()", output);
    sql = "vaLueS(1,col1*2)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("VALUES(1, col1 * 2)", output);
    sql = "(1,2,mod(m,n))";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("ROW(1, 2, m % n)", output);
    sql = "chaR (77,121,'77.3')";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CHAR(77, 121, '77.3')", output);
    sql = "CHARSET(CHAR(0x65 USING utf8))";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CHARSET(CHAR(x'65' USING utf8))", output);
    sql = "CONVERT(_latin1'Müller' USING utf8)";
    parser = new MySQLExprParser(new MySQLLexer(sql));
    expr = parser.expression();
    output = output2MySQL(expr, sql);
    Assert.assertEquals("CONVERT(_latin1'Müller' USING utf8)", output);
// QS_TODO
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) 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) Wildcard(com.alibaba.cobar.parser.ast.expression.primary.Wildcard) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)

Example 4 with CaseWhenOperatorExpression

use of com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression in project cobar by alibaba.

the class MySQLExprParser method caseWhenExpression.

/**
 * first <code>CASE</code> has been consumed
 */
private Expression caseWhenExpression() throws SQLSyntaxErrorException {
    Expression comparee = null;
    if (lexer.token() != KW_WHEN) {
        comparee = expression();
    }
    List<Pair<Expression, Expression>> list = new LinkedList<Pair<Expression, Expression>>();
    for (; lexer.token() == KW_WHEN; ) {
        lexer.nextToken();
        Expression when = expression();
        match(KW_THEN);
        Expression then = expression();
        if (when == null || then == null)
            throw err("when or then is null in CASE WHEN expression");
        list.add(new Pair<Expression, Expression>(when, then));
    }
    if (list.isEmpty()) {
        throw err("at least one WHEN ... THEN branch for CASE ... WHEN syntax");
    }
    Expression elseValue = null;
    switch(lexer.token()) {
        case KW_ELSE:
            lexer.nextToken();
            elseValue = expression();
        default:
            matchIdentifier("END");
    }
    return new CaseWhenOperatorExpression(comparee, list, elseValue).setCacheEvalRst(cacheEvalRst);
}
Also used : CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) SubqueryAllExpression(com.alibaba.cobar.parser.ast.expression.misc.SubqueryAllExpression) MinusExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) 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) 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) BitInvertExpression(com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression) BitXORExpression(com.alibaba.cobar.parser.ast.expression.bit.BitXORExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) BitShiftExpression(com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression) BitOrExpression(com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression) ComparisionGreaterThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanOrEqualsExpression) MatchExpression(com.alibaba.cobar.parser.ast.expression.primary.MatchExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ComparisionGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanExpression) ComparisionLessOrGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessOrGreaterThanExpression) SubqueryAnyExpression(com.alibaba.cobar.parser.ast.expression.misc.SubqueryAnyExpression) RegexpExpression(com.alibaba.cobar.parser.ast.expression.string.RegexpExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) 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) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) ArithmeticSubtractExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression) SoundsLikeExpression(com.alibaba.cobar.parser.ast.expression.string.SoundsLikeExpression) ArithmeticIntegerDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) LogicalNotExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalNotExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) AssignmentExpression(com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) 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) ComparisionNotEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNotEqualsExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) ComparisionLessThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanExpression) ArithmeticModExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression) ArithmeticDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression) LinkedList(java.util.LinkedList) Pair(com.alibaba.cobar.parser.util.Pair)

Aggregations

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