use of com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression in project cobar by alibaba.
the class MySQLExprParser method comparisionExpression.
/**
* <code>BETWEEN ... AND</code> has lower precedence than other comparison
* operator
*/
private Expression comparisionExpression() throws SQLSyntaxErrorException {
Expression temp;
for (Expression fst = bitOrExpression(null, null); ; ) {
switch(lexer.token()) {
case KW_NOT:
lexer.nextToken();
switch(lexer.token()) {
case KW_BETWEEN:
lexer.nextToken();
Expression snd = comparisionExpression();
match(KW_AND);
Expression trd = comparisionExpression();
return new BetweenAndExpression(true, fst, snd, trd).setCacheEvalRst(cacheEvalRst);
case KW_RLIKE:
case KW_REGEXP:
lexer.nextToken();
temp = bitOrExpression(null, null);
fst = new RegexpExpression(true, fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case KW_LIKE:
lexer.nextToken();
temp = bitOrExpression(null, null);
Expression escape = null;
if (equalsIdentifier("ESCAPE") >= 0) {
lexer.nextToken();
escape = bitOrExpression(null, null);
}
fst = new LikeExpression(true, fst, temp, escape).setCacheEvalRst(cacheEvalRst);
continue;
case KW_IN:
if (lexer.nextToken() != PUNC_LEFT_PAREN) {
lexer.addCacheToke(KW_IN);
return fst;
}
Expression in = rightOprandOfIn();
fst = new InExpression(true, fst, in).setCacheEvalRst(cacheEvalRst);
continue;
default:
throw err("unexpect token after NOT: " + lexer.token());
}
case KW_BETWEEN:
lexer.nextToken();
Expression snd = comparisionExpression();
match(KW_AND);
Expression trd = comparisionExpression();
return new BetweenAndExpression(false, fst, snd, trd).setCacheEvalRst(cacheEvalRst);
case KW_RLIKE:
case KW_REGEXP:
lexer.nextToken();
temp = bitOrExpression(null, null);
fst = new RegexpExpression(false, fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case KW_LIKE:
lexer.nextToken();
temp = bitOrExpression(null, null);
Expression escape = null;
if (equalsIdentifier("ESCAPE") >= 0) {
lexer.nextToken();
escape = bitOrExpression(null, null);
}
fst = new LikeExpression(false, fst, temp, escape).setCacheEvalRst(cacheEvalRst);
continue;
case KW_IN:
if (lexer.nextToken() != PUNC_LEFT_PAREN) {
lexer.addCacheToke(KW_IN);
return fst;
}
temp = rightOprandOfIn();
fst = new InExpression(false, fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case KW_IS:
switch(lexer.nextToken()) {
case KW_NOT:
switch(lexer.nextToken()) {
case LITERAL_NULL:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NOT_NULL).setCacheEvalRst(cacheEvalRst);
continue;
case LITERAL_BOOL_FALSE:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NOT_FALSE).setCacheEvalRst(cacheEvalRst);
continue;
case LITERAL_BOOL_TRUE:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NOT_TRUE).setCacheEvalRst(cacheEvalRst);
continue;
default:
matchIdentifier("UNKNOWN");
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NOT_UNKNOWN).setCacheEvalRst(cacheEvalRst);
continue;
}
case LITERAL_NULL:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_NULL).setCacheEvalRst(cacheEvalRst);
continue;
case LITERAL_BOOL_FALSE:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_FALSE).setCacheEvalRst(cacheEvalRst);
continue;
case LITERAL_BOOL_TRUE:
lexer.nextToken();
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_TRUE).setCacheEvalRst(cacheEvalRst);
continue;
default:
matchIdentifier("UNKNOWN");
fst = new ComparisionIsExpression(fst, ComparisionIsExpression.IS_UNKNOWN).setCacheEvalRst(cacheEvalRst);
continue;
}
case OP_EQUALS:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_NULL_SAFE_EQUALS:
lexer.nextToken();
temp = bitOrExpression(null, null);
fst = new ComparisionNullSafeEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_GREATER_OR_EQUALS:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionGreaterThanOrEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_GREATER_THAN:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionGreaterThanExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_LESS_OR_EQUALS:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionLessThanOrEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_LESS_THAN:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionLessThanExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_LESS_OR_GREATER:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionLessOrGreaterThanExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
case OP_NOT_EQUALS:
lexer.nextToken();
temp = anyAllExpression();
fst = new ComparisionNotEqualsExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
default:
if (equalsIdentifier("SOUNDS") >= 0) {
lexer.nextToken();
match(KW_LIKE);
temp = bitOrExpression(null, null);
fst = new SoundsLikeExpression(fst, temp).setCacheEvalRst(cacheEvalRst);
continue;
}
return fst;
}
}
}
use of com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression in project cobar by alibaba.
the class PartitionKeyVisitor method visit.
@Override
public void visit(BetweenAndExpression node) {
Expression fst = node.getFirst();
Expression snd = node.getSecond();
Expression trd = node.getThird();
visitChild(2, false, false, fst, snd, trd);
if (verdictColumn && !node.isNot() && fst instanceof Identifier) {
Identifier col = (Identifier) fst;
String table = tableAlias.get(col.getLevelUnescapeUpName(2));
if (isRuledColumn(table, col.getIdTextUpUnescape())) {
Object e1 = snd.evaluation(evaluationParameter);
Object e2 = trd.evaluation(evaluationParameter);
if (e1 != Expression.UNEVALUATABLE && e2 != Expression.UNEVALUATABLE && e1 != null && e2 != null) {
if (compareEvaluatedValue(e1, e2)) {
addColumnValue(table, col.getIdTextUpUnescape(), e1, node, null);
}
}
}
}
}
use of com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression 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.comparison.BetweenAndExpression 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.comparison.BetweenAndExpression in project cobar by alibaba.
the class MySQLOutputASTVisitor method visit.
@Override
public void visit(BetweenAndExpression node) {
Expression comparee = node.getFirst();
boolean paren = comparee.getPrecedence() <= node.getPrecedence();
if (paren)
appendable.append('(');
comparee.accept(this);
if (paren)
appendable.append(')');
if (node.isNot())
appendable.append(" NOT BETWEEN ");
else
appendable.append(" BETWEEN ");
Expression start = node.getSecond();
paren = start.getPrecedence() < node.getPrecedence();
if (paren)
appendable.append('(');
start.accept(this);
if (paren)
appendable.append(')');
appendable.append(" AND ");
Expression end = node.getThird();
paren = end.getPrecedence() < node.getPrecedence();
if (paren)
appendable.append('(');
end.accept(this);
if (paren)
appendable.append(')');
}
Aggregations