Search in sources :

Example 31 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.

the class MySQLMTSParserTest method testSavepint.

public void testSavepint() throws SQLSyntaxErrorException {
    String sql = "  savepoint 123e123e";
    MySQLMTSParser parser = new MySQLMTSParser(new MySQLLexer(sql));
    MTSSavepointStatement savepoint = parser.savepoint();
    String output = output2MySQL(savepoint, sql);
    Assert.assertEquals("SAVEPOINT 123e123e", output);
    Assert.assertEquals("123e123e", savepoint.getSavepoint().getIdText());
    sql = "  savepoint SAVEPOINT";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    savepoint = parser.savepoint();
    output = output2MySQL(savepoint, sql);
    Assert.assertEquals("SAVEPOINT SAVEPOINT", output);
    Assert.assertEquals("SAVEPOINT", savepoint.getSavepoint().getIdText());
    sql = "  savepoInt `select`";
    parser = new MySQLMTSParser(new MySQLLexer(sql));
    savepoint = parser.savepoint();
    output = output2MySQL(savepoint, sql);
    Assert.assertEquals("SAVEPOINT `select`", output);
    Assert.assertEquals("`select`", savepoint.getSavepoint().getIdText());
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) MTSSavepointStatement(com.alibaba.cobar.parser.ast.stmt.mts.MTSSavepointStatement)

Example 32 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.

the class Select method main.

public static void main(String[] args) throws SQLSyntaxErrorException {
    String sql = "   ( ( select union select union select)  as j join    (((select union (select)) as t    )   join t2 ) ,(select)as d), t3)";
    // String sql =
    // "((select) as s1, ((((   select  union select          ) as t2)) join (((t2),t4 as t))) ), t1 aS T1";
    // String sql =
    // "  (( select union select union select)  as j  ,(select)as d), t3";
    System.out.println(sql);
    MySQLLexer lexer = new MySQLLexer(sql);
    lexer.nextToken();
    SoloParser p = new SoloParser(lexer);
    Refs refs = p.refs();
    System.out.println(refs);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)

Example 33 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.

the class MySQLDMLDeleteParserTest method testDelete1.

public void testDelete1() throws SQLSyntaxErrorException {
    String sql = "deLetE LOW_PRIORITY from id1.id , id using t1 a where col1 =? ";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLDeleteParser parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    DMLDeleteStatement delete = parser.delete();
    parser.match(MySQLToken.EOF);
    String output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE LOW_PRIORITY id1.id, id FROM t1 AS A WHERE col1 = ?", output);
    sql = "deLetE from id1.id  using t1  ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE id1.id FROM t1", output);
    sql = "delete from offer.*,wp_image.* using offer a,wp_image b where a.member_id=b.member_id and a.member_id='abc' ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE offer.*, wp_image.* FROM offer AS A, wp_image AS B WHERE " + "a.member_id = b.member_id AND a.member_id = 'abc'", output);
    sql = "deLetE from id1.id where col1='adf' limit 1,?";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE FROM id1.id WHERE col1 = 'adf' LIMIT 1, ?", output);
    sql = "deLetE from id where col1='adf' ordEr by d liMit ? offset 2";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE FROM id WHERE col1 = 'adf' ORDER BY d LIMIT 2, ?", output);
    sql = "deLetE id.* from t1,t2 where col1='adf'            and col2=1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE id.* FROM t1, t2 WHERE col1 = 'adf' AND col2 = 1", output);
    sql = "deLetE id,id.t from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE id, id.t FROM t1", output);
    sql = "deLetE from t1 where t1.id1='abc' order by a limit 5";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE FROM t1 WHERE t1.id1 = 'abc' ORDER BY a LIMIT 0, 5", output);
    sql = "deLetE from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE FROM t1", output);
    sql = "deLetE ignore tb1.*,id1.t from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE IGNORE tb1.*, id1.t FROM t1", output);
    sql = "deLetE quick tb1.*,id1.t from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLDeleteParser(lexer, new MySQLExprParser(lexer));
    delete = parser.delete();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(delete, sql);
    Assert.assertEquals("DELETE QUICK tb1.*, id1.t FROM t1", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DMLDeleteStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLDeleteStatement)

Example 34 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.

the class MySQLDMLParserTest method testLimit.

public void testLimit() throws SQLSyntaxErrorException {
    String sql = "limit 1,2";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLParser parser = getDMLParser(lexer);
    Limit limit = parser.limit();
    String output = output2MySQL(limit, sql);
    Assert.assertEquals(1, limit.getOffset());
    Assert.assertEquals(2, limit.getSize());
    Assert.assertEquals("LIMIT 1, 2", output);
    sql = "limit 1,?";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(1, limit.getOffset());
    Assert.assertEquals(new ParamMarker(1), limit.getSize());
    Assert.assertEquals("LIMIT 1, ?", output);
    sql = "limit ?,9";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(new ParamMarker(1), limit.getOffset());
    Assert.assertEquals(9, limit.getSize());
    Assert.assertEquals("LIMIT ?, 9", output);
    sql = "limit ?,?";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(new ParamMarker(1), limit.getOffset());
    Assert.assertEquals(new ParamMarker(2), limit.getSize());
    Assert.assertEquals("LIMIT ?, ?", output);
    sql = "limit ? d";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(0, limit.getOffset());
    Assert.assertEquals(new ParamMarker(1), limit.getSize());
    Assert.assertEquals("LIMIT 0, ?", output);
    sql = "limit 9 f";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(0, limit.getOffset());
    Assert.assertEquals(9, limit.getSize());
    Assert.assertEquals("LIMIT 0, 9", output);
    sql = "limit 9 ofFset 0";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(0, limit.getOffset());
    Assert.assertEquals(9, limit.getSize());
    Assert.assertEquals("LIMIT 0, 9", output);
    sql = "limit ? offset 0";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(0, limit.getOffset());
    Assert.assertEquals(new ParamMarker(1), limit.getSize());
    Assert.assertEquals("LIMIT 0, ?", output);
    sql = "limit ? offset ?";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(new ParamMarker(2), limit.getOffset());
    Assert.assertEquals(new ParamMarker(1), limit.getSize());
    Assert.assertEquals("LIMIT ?, ?", output);
    sql = "limit 9 offset ?";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    limit = parser.limit();
    output = output2MySQL(limit, sql);
    Assert.assertEquals(new ParamMarker(1), limit.getOffset());
    Assert.assertEquals(9, limit.getSize());
    Assert.assertEquals("LIMIT ?, 9", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) ParamMarker(com.alibaba.cobar.parser.ast.expression.primary.ParamMarker) Limit(com.alibaba.cobar.parser.ast.fragment.Limit)

Example 35 with MySQLLexer

use of com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer in project cobar by alibaba.

the class MySQLDMLReplaceParserTest method testReplace.

public void testReplace() throws SQLSyntaxErrorException {
    String sql = "ReplaCe LOW_PRIORITY intO test.t1 seT t1.id1:=?, id2='123'";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLReplaceParser parser = new MySQLDMLReplaceParser(lexer, new MySQLExprParser(lexer));
    DMLReplaceStatement replace = parser.replace();
    parser.match(MySQLToken.EOF);
    Assert.assertNotNull(replace);
    String output = output2MySQL(replace, sql);
    Assert.assertEquals("REPLACE LOW_PRIORITY INTO test.t1 (t1.id1, id2) VALUES (?, '123')", output);
    sql = "ReplaCe   test.t1 seT t1.id1:=? ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLReplaceParser(lexer, new MySQLExprParser(lexer));
    replace = parser.replace();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(replace, sql);
    Assert.assertEquals("REPLACE INTO test.t1 (t1.id1) VALUES (?)", output);
    sql = "ReplaCe t1 value (123,?) ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLReplaceParser(lexer, new MySQLExprParser(lexer));
    replace = parser.replace();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(replace, sql);
    Assert.assertEquals("REPLACE INTO t1 VALUES (123, ?)", output);
    sql = "ReplaCe LOW_PRIORITY t1 valueS (12e-2), (?)";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLReplaceParser(lexer, new MySQLExprParser(lexer));
    replace = parser.replace();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(replace, sql);
    Assert.assertEquals("REPLACE LOW_PRIORITY INTO t1 VALUES (0.12), (?)", output);
    sql = "ReplaCe LOW_PRIORITY t1 select id from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLReplaceParser(lexer, new MySQLExprParser(lexer));
    replace = parser.replace();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(replace, sql);
    Assert.assertEquals("REPLACE LOW_PRIORITY INTO t1 SELECT id FROM t1", output);
    sql = "ReplaCe delayed t1 select id from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLReplaceParser(lexer, new MySQLExprParser(lexer));
    replace = parser.replace();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(replace, sql);
    Assert.assertEquals("REPLACE DELAYED INTO t1 SELECT id FROM t1", output);
    sql = "ReplaCe LOW_PRIORITY t1 (select id from t1) ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLReplaceParser(lexer, new MySQLExprParser(lexer));
    replace = parser.replace();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(replace, sql);
    Assert.assertEquals("REPLACE LOW_PRIORITY INTO t1 SELECT id FROM t1", output);
    sql = "ReplaCe LOW_PRIORITY t1 (t1.col1) valueS (123),('12''34')";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLReplaceParser(lexer, new MySQLExprParser(lexer));
    replace = parser.replace();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(replace, sql);
    Assert.assertEquals("REPLACE LOW_PRIORITY INTO t1 (t1.col1) VALUES (123), ('12\\'34')", output);
    sql = "ReplaCe LOW_PRIORITY t1 (col1, t1.col2) VALUE (123,'123\\'4') ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLReplaceParser(lexer, new MySQLExprParser(lexer));
    replace = parser.replace();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(replace, sql);
    Assert.assertEquals("REPLACE LOW_PRIORITY INTO t1 (col1, t1.col2) VALUES (123, '123\\'4')", output);
    sql = "REPLACE LOW_PRIORITY t1 (col1, t1.col2) select id from t3 ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLReplaceParser(lexer, new MySQLExprParser(lexer));
    replace = parser.replace();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(replace, sql);
    Assert.assertEquals("REPLACE LOW_PRIORITY INTO t1 (col1, t1.col2) SELECT id FROM t3", output);
    sql = "replace LOW_PRIORITY  intO t1 (col1) ( select id from t3) ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLReplaceParser(lexer, new MySQLExprParser(lexer));
    replace = parser.replace();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(replace, sql);
    Assert.assertEquals("REPLACE LOW_PRIORITY INTO t1 (col1) SELECT id FROM t3", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DMLReplaceStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLReplaceStatement)

Aggregations

MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)40 Expression (com.alibaba.cobar.parser.ast.expression.Expression)18 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)17 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)17 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)17 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)16 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)16 TernaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression)16 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)16 ArithmeticAddExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression)16 ArithmeticDivideExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression)16 ArithmeticIntegerDivideExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression)16 ArithmeticModExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression)16 ArithmeticMultiplyExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression)16 ArithmeticSubtractExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression)16 MinusExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression)16 BitAndExpression (com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression)16 BitInvertExpression (com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression)16 BitOrExpression (com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression)16 BitShiftExpression (com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression)16