use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLExprParserTest method testMatchExpression.
public void testMatchExpression() throws Exception {
// QS_TODO
String sql = "MATCH (title,body) AGAINST ('database' WITH QUERY EXPANSION)";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("MATCH (title, body) AGAINST ('database' WITH QUERY EXPANSION)", output);
Assert.assertEquals("MATCH (title, body) AGAINST ('database' WITH QUERY EXPANSION)", output);
sql = "MATCH (title,body) AGAINST ( (abc in (d)) IN boolean MODE)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("MATCH (title, body) AGAINST ((abc IN (d)) IN BOOLEAN MODE)", output);
sql = "MATCH (title,body) AGAINST ('database')";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("MATCH (title, body) AGAINST ('database')", output);
sql = "MATCH (col1,col2,col3) AGAINST ((a:=b:=c) IN boolean MODE)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("MATCH (col1, col2, col3) AGAINST (a := b := c IN BOOLEAN MODE)", output);
sql = "MATCH (title,body) AGAINST ((a and (b ||c)) IN boolean MODE)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("MATCH (title, body) AGAINST (a AND (b OR c) IN BOOLEAN MODE)", output);
sql = "MATCH (title,body) AGAINST ((a between b and c) IN boolean MODE)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("MATCH (title, body) AGAINST (a BETWEEN b AND c IN BOOLEAN MODE)", output);
sql = "MATCH (title,body) AGAINST ((a between b and (abc in (d))) IN boolean MODE)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("MATCH (title, body) AGAINST ((a BETWEEN b AND abc IN (d)) IN BOOLEAN MODE)", output);
sql = "MATCH (title,body) AGAINST ((not not a) IN boolean MODE)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("MATCH (title, body) AGAINST (NOT NOT a IN BOOLEAN MODE)", output);
sql = "MATCH (title,body) AGAINST ((a is true) IN boolean MODE)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("MATCH (title, body) AGAINST (a IS TRUE IN BOOLEAN MODE)", output);
sql = "MATCH (title,body) AGAINST ((select a) IN boolean MODE)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("MATCH (title, body) AGAINST (SELECT a IN BOOLEAN MODE)", output);
sql = "MATCH (title,body) AGAINST ('database' IN NATURAL LANGUAGE MODE)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("MATCH (title, body) AGAINST ('database' IN NATURAL LANGUAGE MODE)", output);
sql = "MATCH (title,body) AGAINST ('database' IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("MATCH (title, body) AGAINST ('database' IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION)", output);
}
use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLExprParserTest method testUser.
public void testUser() throws Exception {
String sql = "'root'@'localhost'";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("'root'@'localhost'", output);
UserExpression usr = (UserExpression) expr;
Assert.assertEquals("'root'@'localhost'", usr.getUserAtHost());
sql = "root@localhost";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("root@localhost", output);
usr = (UserExpression) expr;
Assert.assertEquals("root@localhost", usr.getUserAtHost());
sql = "var@'localhost'";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("var@'localhost'", output);
usr = (UserExpression) expr;
Assert.assertEquals("var@'localhost'", usr.getUserAtHost());
}
use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLExprParserTest method testInterval.
public void testInterval() throws Exception {
// QS_TODO
String sql = "DATE_ADD('2009-01-01', INTERVAL (6/4) HOUR_MINUTE)";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_ADD('2009-01-01', INTERVAL (6 / 4) HOUR_MINUTE)", output);
sql = "'2008-12-31 23:59:59' + INTERVAL 1 SECOND";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("'2008-12-31 23:59:59' + INTERVAL 1 SECOND", output);
sql = " INTERVAL 1 DAY + '2008-12-31'";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("INTERVAL 1 DAY + '2008-12-31'", output);
sql = "DATE_ADD('2100-12-31 23:59:59',INTERVAL '1:1' MINUTE_SECOND)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_ADD('2100-12-31 23:59:59', INTERVAL '1:1' MINUTE_SECOND)", output);
sql = "DATE_SUB('2005-01-01 00:00:00',INTERVAL '1 1:1:1' DAY_SECOND)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_SUB('2005-01-01 00:00:00', INTERVAL '1 1:1:1' DAY_SECOND)", output);
sql = "DATE_ADD('1900-01-01 00:00:00',INTERVAL '-1 10' DAY_HOUR)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_ADD('1900-01-01 00:00:00', INTERVAL '-1 10' DAY_HOUR)", output);
sql = "DATE_SUB('1998-01-02', INTERVAL 31 DAY)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_SUB('1998-01-02', INTERVAL 31 DAY)", output);
sql = "DATE_ADD('1992-12-31 23:59:59.000002',INTERVAL '1.999999' SECOND_MICROSECOND)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_ADD('1992-12-31 23:59:59.000002', INTERVAL '1.999999' SECOND_MICROSECOND)", output);
sql = "DATE_ADD('2013-01-01', INTERVAL 1 HOUR)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_ADD('2013-01-01', INTERVAL 1 HOUR)", output);
sql = "DATE_ADD('2009-01-30', INTERVAL 1 MONTH)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_ADD('2009-01-30', INTERVAL 1 MONTH)", output);
sql = "DATE_ADD('1992-12-31 23:59:59.000002',INTERVAL '1:1.999999' minute_MICROSECOND)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_ADD('1992-12-31 23:59:59.000002', INTERVAL '1:1.999999' MINUTE_MICROSECOND)", output);
sql = "DATE_ADD('1992-12-31 23:59:59.000002',INTERVAL '1:1:1.999999' hour_MICROSECOND)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_ADD('1992-12-31 23:59:59.000002', INTERVAL '1:1:1.999999' HOUR_MICROSECOND)", output);
sql = "DATE_ADD('2100-12-31 23:59:59',INTERVAL '1:1:1' hour_SECOND)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_ADD('2100-12-31 23:59:59', INTERVAL '1:1:1' HOUR_SECOND)", output);
sql = "DATE_ADD('1992-12-31 23:59:59.000002',INTERVAL '1 1:1:1.999999' day_MICROSECOND)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_ADD('1992-12-31 23:59:59.000002', INTERVAL '1 1:1:1.999999' DAY_MICROSECOND)", output);
sql = "DATE_ADD('2100-12-31 23:59:59',INTERVAL '1 1:1' day_minute)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_ADD('2100-12-31 23:59:59', INTERVAL '1 1:1' DAY_MINUTE)", output);
sql = "DATE_ADD('2100-12-31',INTERVAL '1-1' year_month)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("DATE_ADD('2100-12-31', INTERVAL '1-1' YEAR_MONTH)", output);
sql = "INTERVAL(n1,n2,n3)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("INTERVAL(n1, n2, n3)", output);
sql = "INTERVAL a+b day";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("INTERVAL (a + b) DAY", output);
sql = "INTERVAL(select id from t1) day";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("INTERVAL (SELECT id FROM t1) DAY", output);
sql = "INTERVAL(('jklj'+a))day";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("INTERVAL ('jklj' + a) DAY", output);
}
use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLExprParserTest method testAnyAll.
public void testAnyAll() throws Exception {
String sql = "1 >= any (select id from t1 limit 1)";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("1 >= ANY (SELECT id FROM t1 LIMIT 0, 1)", output);
Assert.assertEquals(ComparisionGreaterThanOrEqualsExpression.class, expr.getClass());
sql = "1 >= any (select id from t1 limit 1) > aLl(select tb1.id from tb1 t1,tb2 as t2 where t1.id=t2.id limit 1)";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("1 >= ANY (SELECT id FROM t1 LIMIT 0, 1) > ALL (SELECT tb1.id FROM tb1 AS T1, tb2 AS T2 WHERE t1.id = t2.id LIMIT 0, 1)", output);
ComparisionGreaterThanExpression gt = (ComparisionGreaterThanExpression) expr;
ComparisionGreaterThanOrEqualsExpression ge = (ComparisionGreaterThanOrEqualsExpression) gt.getLeftOprand();
Assert.assertEquals(LiteralNumber.class, ge.getLeftOprand().getClass());
sql = "1 >= any + any";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("1 >= any + any", output);
}
use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.
the class MySQLExprParserTest method testBitHex.
public void testBitHex() throws Exception {
String sql = "x'89af' ";
MySQLExprParser parser = new MySQLExprParser(new MySQLLexer(sql));
Expression expr = parser.expression();
String output = output2MySQL(expr, sql);
Assert.assertEquals("x'89af'", output);
Assert.assertEquals("89af", ((LiteralHexadecimal) expr).getText());
sql = "_latin1 b'1011' ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("_latin1 b'1011'", output);
Assert.assertEquals("1011", ((LiteralBitField) expr).getText());
Assert.assertEquals("_latin1", ((LiteralBitField) expr).getIntroducer());
sql = "abc 0b1011 ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("abc", output);
sql = "_latin1 0xabc ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("_latin1 x'abc'", output);
sql = "jkl 0xabc ";
parser = new MySQLExprParser(new MySQLLexer(sql));
expr = parser.expression();
output = output2MySQL(expr, sql);
Assert.assertEquals("jkl", output);
}
Aggregations