use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLMTSParserTest method testRelease.
public void testRelease() throws SQLSyntaxErrorException {
String sql = "Release sAVEPOINT 1234e ";
MySQLMTSParser parser = new MySQLMTSParser(new MySQLLexer(sql));
MTSReleaseStatement savepoint = parser.release();
String output = output2MySQL(savepoint, sql);
Assert.assertEquals("RELEASE SAVEPOINT 1234e", output);
Assert.assertEquals("1234e", savepoint.getSavepoint().getIdText());
sql = "Release SAVEPOINT sAVEPOINT";
parser = new MySQLMTSParser(new MySQLLexer(sql));
savepoint = parser.release();
output = output2MySQL(savepoint, sql);
Assert.assertEquals("RELEASE SAVEPOINT sAVEPOINT", output);
Assert.assertEquals("sAVEPOINT", savepoint.getSavepoint().getIdText());
}
use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer 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
}
use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLExprParserTest method testBit.
public void testBit() throws Exception {
String sql = "0b01001001 | 3 & 1.2 <<d >> 0x0f";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("b'01001001' | 3 & 1.2 << d >> x'0f'", output);
BitOrExpression or = (BitOrExpression) expr;
BitAndExpression and = (BitAndExpression) or.getRightOprand();
BitShiftExpression rs = (BitShiftExpression) and.getRightOprand();
BitShiftExpression ls = (BitShiftExpression) rs.getLeftOprand();
Assert.assertEquals("d", ((Identifier) ls.getRightOprand()).getIdText());
Assert.assertTrue(rs.isRightShift());
Assert.assertFalse(ls.isRightShift());
sql = "true + b & false ^ d - null ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("TRUE + b & FALSE ^ d - NULL", output);
and = (BitAndExpression) expr;
ArithmeticAddExpression add = (ArithmeticAddExpression) and.getLeftOprand();
ArithmeticSubtractExpression sub = (ArithmeticSubtractExpression) and.getRightOprand();
BitXORExpression xor = (BitXORExpression) sub.getLeftOprand();
Assert.assertEquals("d", ((Identifier) xor.getRightOprand()).getIdText());
Assert.assertEquals("b", ((Identifier) add.getRightOprand()).getIdText());
}
use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLExprParserTest method testString.
public void testString() throws Exception {
String sql = "_latin1'abc\\'d' 'ef\"'";
MySQLLexer lexer = new MySQLLexer(sql);
MySQLExprParser parser = new MySQLExprParser(lexer);
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("_latin1'abc\\'def\"'", output);
sql = "n'abc\\'d' \"ef'\"\"\"";
lexer = new MySQLLexer(sql);
parser = new MySQLExprParser(lexer);
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("N'abc\\'def\\'\"'", output);
sql = "`char`'an'";
lexer = new MySQLLexer(sql);
parser = new MySQLExprParser(lexer);
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("`char`", output);
sql = "_latin1 n'abc' ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("_latin1", output);
}
use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLExprParserTest method testLogical.
public void testLogical() throws Exception {
String sql = "a || b Or c";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("a OR b OR c", output);
Assert.assertEquals(LogicalOrExpression.class, expr.getClass());
LogicalOrExpression or = (LogicalOrExpression) expr;
Assert.assertEquals(3, or.getArity());
Assert.assertEquals("b", ((Identifier) or.getOperand(1)).getIdText());
sql = "a XOR b xOr c";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a XOR b XOR c", output);
Assert.assertEquals(LogicalXORExpression.class, expr.getClass());
LogicalXORExpression xor = (LogicalXORExpression) expr;
Assert.assertEquals(LogicalXORExpression.class, xor.getLeftOprand().getClass());
xor = (LogicalXORExpression) xor.getLeftOprand();
Assert.assertEquals("b", ((Identifier) xor.getRightOprand()).getIdText());
sql = "a XOR( b xOr c)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a XOR (b XOR c)", output);
xor = (LogicalXORExpression) expr;
LogicalXORExpression xor2 = (LogicalXORExpression) xor.getRightOprand();
Assert.assertEquals("a", ((Identifier) xor.getLeftOprand()).getIdText());
Assert.assertEquals("b", ((Identifier) xor2.getLeftOprand()).getIdText());
Assert.assertEquals("c", ((Identifier) xor2.getRightOprand()).getIdText());
sql = "a and b && c";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("a AND b AND c", output);
Assert.assertEquals(LogicalAndExpression.class, expr.getClass());
LogicalAndExpression and = (LogicalAndExpression) expr;
Assert.assertEquals(3, or.getArity());
Assert.assertEquals("b", ((Identifier) and.getOperand(1)).getIdText());
sql = "not NOT Not a";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("NOT NOT NOT a", output);
Assert.assertEquals(LogicalNotExpression.class, expr.getClass());
LogicalNotExpression not = (LogicalNotExpression) ((LogicalNotExpression) expr).getOperand();
Assert.assertEquals(LogicalNotExpression.class, not.getClass());
not = (LogicalNotExpression) not.getOperand();
Assert.assertEquals(LogicalNotExpression.class, not.getClass());
Assert.assertEquals("a", ((Identifier) not.getOperand()).getIdText());
}
Aggregations