use of com.alibaba.cobar.parser.ast.expression.Expression 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());
}
use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.
the class MySQLExprParserTest method testComparision.
public void testComparision() throws Exception {
String sql = "a betwEen b and c Not between d and e";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("a BETWEEN b AND c NOT BETWEEN d AND e", output);
BetweenAndExpression ba = (BetweenAndExpression) expr;
Assert.assertEquals("a", ((Identifier) ba.getFirst()).getIdText());
Assert.assertEquals("b", ((Identifier) ba.getSecond()).getIdText());
Assert.assertEquals(false, ba.isNot());
ba = (BetweenAndExpression) ba.getThird();
Assert.assertEquals("c", ((Identifier) ba.getFirst()).getIdText());
Assert.assertEquals("d", ((Identifier) ba.getSecond()).getIdText());
Assert.assertEquals("e", ((Identifier) ba.getThird()).getIdText());
Assert.assertEquals(true, ba.isNot());
sql = "a between b between c and d and e between f and g";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a BETWEEN b BETWEEN c AND d AND e BETWEEN f AND g", output);
ba = (BetweenAndExpression) expr;
BetweenAndExpression ba2 = (BetweenAndExpression) ba.getSecond();
BetweenAndExpression ba3 = (BetweenAndExpression) ba.getThird();
Assert.assertEquals("a", ((Identifier) ba.getFirst()).getIdText());
Assert.assertEquals("b", ((Identifier) ba2.getFirst()).getIdText());
Assert.assertEquals("c", ((Identifier) ba2.getSecond()).getIdText());
Assert.assertEquals("d", ((Identifier) ba2.getThird()).getIdText());
Assert.assertEquals("e", ((Identifier) ba3.getFirst()).getIdText());
Assert.assertEquals("f", ((Identifier) ba3.getSecond()).getIdText());
Assert.assertEquals("g", ((Identifier) ba3.getThird()).getIdText());
sql = "((select a)) between (select b) and (select d) ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("(SELECT a) BETWEEN (SELECT b) AND (SELECT d)", output);
sql = "a rliKe b not REGEXP c";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a REGEXP b NOT REGEXP c", output);
RegexpExpression re = (RegexpExpression) expr;
RegexpExpression re2 = (RegexpExpression) re.getLeftOprand();
Assert.assertEquals("a", ((Identifier) re2.getLeftOprand()).getIdText());
Assert.assertEquals("b", ((Identifier) re2.getRightOprand()).getIdText());
Assert.assertEquals("c", ((Identifier) re.getRightOprand()).getIdText());
Assert.assertEquals(true, re.isNot());
Assert.assertEquals(false, re2.isNot());
sql = "((a)) like (((b)))escape (((d)))";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a LIKE b ESCAPE d", output);
sql = "((select a)) like (((select b)))escape (((select d)))";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("(SELECT a) LIKE (SELECT b) ESCAPE (SELECT d)", output);
sql = "a like b NOT LIKE c escape d";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a LIKE b NOT LIKE c ESCAPE d", output);
LikeExpression le = (LikeExpression) expr;
LikeExpression le2 = (LikeExpression) le.getFirst();
Assert.assertEquals("a", ((Identifier) le2.getFirst()).getIdText());
Assert.assertEquals("b", ((Identifier) le2.getSecond()).getIdText());
Assert.assertEquals("c", ((Identifier) le.getSecond()).getIdText());
Assert.assertEquals("d", ((Identifier) le.getThird()).getIdText());
Assert.assertEquals(true, le.isNot());
Assert.assertEquals(false, le2.isNot());
sql = "b NOT LIKE c ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("b NOT LIKE c", output);
sql = "a in (b) not in (select id from t1)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a IN (b) NOT IN (SELECT id FROM t1)", output);
InExpression in = (InExpression) expr;
InExpression in2 = (InExpression) in.getLeftOprand();
Assert.assertEquals("a", ((Identifier) in2.getLeftOprand()).getIdText());
Assert.assertTrue(QueryExpression.class.isAssignableFrom(in.getRightOprand().getClass()));
Assert.assertEquals(true, in.isNot());
Assert.assertEquals(false, in2.isNot());
sql = "(select a)is not null";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("(SELECT a) IS NOT NULL", output);
sql = "a is not null is not false is not true is not UNKNOWn is null is false is true is unknown";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a IS NOT NULL IS NOT FALSE IS NOT TRUE IS NOT UNKNOWN IS NULL IS FALSE IS TRUE IS UNKNOWN", output);
ComparisionIsExpression is = (ComparisionIsExpression) expr;
ComparisionIsExpression is2 = (ComparisionIsExpression) is.getOperand();
ComparisionIsExpression is3 = (ComparisionIsExpression) is2.getOperand();
ComparisionIsExpression is4 = (ComparisionIsExpression) is3.getOperand();
ComparisionIsExpression is5 = (ComparisionIsExpression) is4.getOperand();
ComparisionIsExpression is6 = (ComparisionIsExpression) is5.getOperand();
ComparisionIsExpression is7 = (ComparisionIsExpression) is6.getOperand();
ComparisionIsExpression is8 = (ComparisionIsExpression) is7.getOperand();
Assert.assertEquals(ComparisionIsExpression.IS_UNKNOWN, is.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_TRUE, is2.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_FALSE, is3.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_NULL, is4.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_NOT_UNKNOWN, is5.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_NOT_TRUE, is6.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_NOT_FALSE, is7.getMode());
Assert.assertEquals(ComparisionIsExpression.IS_NOT_NULL, is8.getMode());
Assert.assertEquals("a", ((Identifier) is8.getOperand()).getIdText());
sql = "a = b <=> c >= d > e <= f < g <> h != i";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a = b <=> c >= d > e <= f < g <> h != i", output);
ComparisionNotEqualsExpression neq = (ComparisionNotEqualsExpression) expr;
ComparisionLessOrGreaterThanExpression lg = (ComparisionLessOrGreaterThanExpression) neq.getLeftOprand();
ComparisionLessThanExpression l = (ComparisionLessThanExpression) lg.getLeftOprand();
ComparisionLessThanOrEqualsExpression leq = (ComparisionLessThanOrEqualsExpression) l.getLeftOprand();
ComparisionGreaterThanExpression g = (ComparisionGreaterThanExpression) leq.getLeftOprand();
ComparisionGreaterThanOrEqualsExpression geq = (ComparisionGreaterThanOrEqualsExpression) g.getLeftOprand();
ComparisionNullSafeEqualsExpression nseq = (ComparisionNullSafeEqualsExpression) geq.getLeftOprand();
ComparisionEqualsExpression eq = (ComparisionEqualsExpression) nseq.getLeftOprand();
Assert.assertEquals("i", ((Identifier) neq.getRightOprand()).getIdText());
Assert.assertEquals("h", ((Identifier) lg.getRightOprand()).getIdText());
Assert.assertEquals("g", ((Identifier) l.getRightOprand()).getIdText());
Assert.assertEquals("f", ((Identifier) leq.getRightOprand()).getIdText());
Assert.assertEquals("e", ((Identifier) g.getRightOprand()).getIdText());
Assert.assertEquals("d", ((Identifier) geq.getRightOprand()).getIdText());
Assert.assertEquals("c", ((Identifier) nseq.getRightOprand()).getIdText());
Assert.assertEquals("b", ((Identifier) eq.getRightOprand()).getIdText());
Assert.assertEquals("a", ((Identifier) eq.getLeftOprand()).getIdText());
sql = "a sounds like b sounds like c";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a SOUNDS LIKE b SOUNDS LIKE c", output);
SoundsLikeExpression sl = (SoundsLikeExpression) expr;
SoundsLikeExpression sl2 = (SoundsLikeExpression) sl.getLeftOprand();
Assert.assertEquals("a", ((Identifier) sl2.getLeftOprand()).getIdText());
Assert.assertEquals("b", ((Identifier) sl2.getRightOprand()).getIdText());
Assert.assertEquals("c", ((Identifier) sl.getRightOprand()).getIdText());
sql = "a like b escape c";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a LIKE b ESCAPE c", output);
sql = "(select a) collate z";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("(SELECT a) COLLATE z", output);
sql = "val1 IN (1,2,'a')";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("val1 IN (1, 2, 'a')", output);
}
use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.
the class MySQLExprParserTest method testPrimarySystemVar.
public void testPrimarySystemVar() throws Exception {
String sql = "@@gloBal . /*dd*/ `all`";
MySQLLexer lexer = new MySQLLexer(sql);
MySQLExprParser parser = new MySQLExprParser(lexer);
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("@@global.`all`", output);
Assert.assertEquals(SysVarPrimary.class, expr.getClass());
SysVarPrimary sysvar = (SysVarPrimary) expr;
Assert.assertEquals(VariableScope.GLOBAL, sysvar.getScope());
Assert.assertEquals("`all`", sysvar.getVarText());
sql = "@@Session . /*dd*/ any";
lexer = new MySQLLexer(sql);
parser = new MySQLExprParser(lexer);
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("@@any", output);
Assert.assertEquals(SysVarPrimary.class, expr.getClass());
sysvar = (SysVarPrimary) expr;
Assert.assertEquals(VariableScope.SESSION, sysvar.getScope());
Assert.assertEquals("any", sysvar.getVarText());
sql = "@@LOCAl . /*dd*/ `usage`";
lexer = new MySQLLexer(sql);
parser = new MySQLExprParser(lexer);
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("@@`usage`", output);
Assert.assertEquals(SysVarPrimary.class, expr.getClass());
sysvar = (SysVarPrimary) expr;
Assert.assertEquals(VariableScope.SESSION, sysvar.getScope());
Assert.assertEquals("`usage`", sysvar.getVarText());
sql = "@@LOCAl . /*dd*/ `var1`";
lexer = new MySQLLexer(sql);
parser = new MySQLExprParser(lexer);
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("@@`var1`", output);
Assert.assertEquals(SysVarPrimary.class, expr.getClass());
sysvar = (SysVarPrimary) expr;
Assert.assertEquals(VariableScope.SESSION, sysvar.getScope());
Assert.assertEquals("`var1`", sysvar.getVarText());
sql = "@@var1 ,";
lexer = new MySQLLexer(sql);
parser = new MySQLExprParser(lexer);
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("@@var1", output);
Assert.assertEquals(SysVarPrimary.class, expr.getClass());
sysvar = (SysVarPrimary) expr;
Assert.assertEquals(VariableScope.SESSION, sysvar.getScope());
Assert.assertEquals("var1", sysvar.getVarText());
sql = "@@`case``1` ,@@_";
lexer = new MySQLLexer(sql);
parser = new MySQLExprParser(lexer);
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("@@`case``1`", output);
Assert.assertEquals(SysVarPrimary.class, expr.getClass());
sysvar = (SysVarPrimary) expr;
Assert.assertEquals(VariableScope.SESSION, sysvar.getScope());
Assert.assertEquals("`case``1`", sysvar.getVarText());
lexer.nextToken();
parser = new MySQLExprParser(lexer);
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("@@_", output);
Assert.assertEquals(SysVarPrimary.class, expr.getClass());
sysvar = (SysVarPrimary) expr;
Assert.assertEquals(VariableScope.SESSION, sysvar.getScope());
Assert.assertEquals("_", sysvar.getVarText());
}
use of com.alibaba.cobar.parser.ast.expression.Expression 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.Expression in project cobar by alibaba.
the class MySQLExprParserTest method testAssignment.
public void testAssignment() throws Exception {
String sql = "a /*dd*/:=b:=c";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("a := b := c", output);
Assert.assertEquals(AssignmentExpression.class, expr.getClass());
AssignmentExpression ass = (AssignmentExpression) expr;
Assert.assertEquals(AssignmentExpression.class, ass.getRightOprand().getClass());
ass = (AssignmentExpression) ass.getRightOprand();
Assert.assertEquals("b", ((Identifier) ass.getLeftOprand()).getIdText());
sql = "c=@var:=1";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("c = (@var := 1)", output);
Assert.assertEquals(ComparisionEqualsExpression.class, expr.getClass());
ass = (AssignmentExpression) ((BinaryOperatorExpression) expr).getRightOprand();
UsrDefVarPrimary usr = (UsrDefVarPrimary) ass.getLeftOprand();
Assert.assertEquals("@var", usr.getVarText());
sql = "a:=b or c &&d :=0b1101 or b'01'&0xabc";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a := b OR c AND d := b'1101' OR b'01' & x'abc'", output);
Assert.assertEquals(AssignmentExpression.class, expr.getClass());
ass = (AssignmentExpression) expr;
Assert.assertEquals(AssignmentExpression.class, ass.getRightOprand().getClass());
ass = (AssignmentExpression) ass.getRightOprand();
Assert.assertEquals(LogicalOrExpression.class, ass.getLeftOprand().getClass());
Assert.assertEquals(LogicalOrExpression.class, ass.getRightOprand().getClass());
LogicalOrExpression lor = (LogicalOrExpression) ass.getLeftOprand();
Assert.assertEquals(LogicalAndExpression.class, lor.getOperand(1).getClass());
lor = (LogicalOrExpression) ass.getRightOprand();
Assert.assertEquals(LiteralBitField.class, lor.getOperand(0).getClass());
Assert.assertEquals(BitAndExpression.class, lor.getOperand(1).getClass());
sql = "a:=((b or (c &&d)) :=((0b1101 or (b'01'&0xabc))))";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a := b OR c AND d := b'1101' OR b'01' & x'abc'", output);
Assert.assertEquals(AssignmentExpression.class, expr.getClass());
ass = (AssignmentExpression) expr;
Assert.assertEquals(AssignmentExpression.class, ass.getRightOprand().getClass());
ass = (AssignmentExpression) ass.getRightOprand();
Assert.assertEquals(LogicalOrExpression.class, ass.getLeftOprand().getClass());
Assert.assertEquals(LogicalOrExpression.class, ass.getRightOprand().getClass());
lor = (LogicalOrExpression) ass.getLeftOprand();
Assert.assertEquals(LogicalAndExpression.class, lor.getOperand(1).getClass());
lor = (LogicalOrExpression) ass.getRightOprand();
Assert.assertEquals(LiteralBitField.class, lor.getOperand(0).getClass());
Assert.assertEquals(BitAndExpression.class, lor.getOperand(1).getClass());
sql = "(a:=b) or c &&(d :=0b1101 or b'01')&0xabc ^null";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("(a := b) OR c AND (d := b'1101' OR b'01') & x'abc' ^ NULL", output);
Assert.assertEquals(LogicalOrExpression.class, expr.getClass());
lor = (LogicalOrExpression) expr;
Assert.assertEquals(AssignmentExpression.class, lor.getOperand(0).getClass());
Assert.assertEquals(LogicalAndExpression.class, lor.getOperand(1).getClass());
LogicalAndExpression land = (LogicalAndExpression) lor.getOperand(1);
Assert.assertEquals(Identifier.class, land.getOperand(0).getClass());
Assert.assertEquals(BitAndExpression.class, land.getOperand(1).getClass());
BitAndExpression band = (BitAndExpression) land.getOperand(1);
Assert.assertEquals(AssignmentExpression.class, band.getLeftOprand().getClass());
Assert.assertEquals(BitXORExpression.class, band.getRightOprand().getClass());
ass = (AssignmentExpression) band.getLeftOprand();
Assert.assertEquals(LogicalOrExpression.class, ass.getRightOprand().getClass());
BitXORExpression bxor = (BitXORExpression) band.getRightOprand();
Assert.assertEquals(LiteralHexadecimal.class, bxor.getLeftOprand().getClass());
Assert.assertEquals(LiteralNull.class, bxor.getRightOprand().getClass());
}
Aggregations