Search in sources :

Example 36 with MySQLLexer

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

the class MySQLDMLSelectParserTest method testSelectChinese.

public void testSelectChinese() throws SQLSyntaxErrorException {
    String sql = "SELect t1.id , t2.* from t1, test.t2 where test.t1.id='中''‘文' and t1.id=test.t2.id";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLSelectParser parser = new MySQLDMLSelectParser(lexer, new MySQLExprParser(lexer));
    DMLSelectStatement select = parser.select();
    Assert.assertNotNull(select);
    String output = output2MySQL(select, sql);
    Assert.assertEquals("SELECT t1.id, t2.* FROM t1, test.t2 WHERE test.t1.id = '中\\'‘文' AND t1.id = test.t2.id", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement)

Example 37 with MySQLLexer

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

the class MySQLDMLUpdateParserTest method testUpdate.

/**
     * nothing has been pre-consumed <code><pre>
     * 'UPDATE' 'LOW_PRIORITY'? 'IGNORE'? table_reference
     *   'SET' colName ('='|'=') (expr|'DEFAULT') (',' colName ('='|'=') (expr|'DEFAULT'))*
     *     ('WHERE' cond)?
     *     {singleTable}? => ('ORDER' 'BY' orderBy)?  ('LIMIT' count)?
     * </pre></code>
     */
public void testUpdate() throws SQLSyntaxErrorException {
    String sql = "upDate LOw_PRIORITY IGNORE test.t1 sEt t1.col1=?, col2=DefaulT";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLUpdateParser parser = new MySQLDMLUpdateParser(lexer, new MySQLExprParser(lexer));
    DMLUpdateStatement update = parser.update();
    String output = output2MySQL(update, sql);
    Assert.assertNotNull(update);
    Assert.assertEquals("UPDATE LOW_PRIORITY IGNORE test.t1 SET t1.col1 = ?, col2 = DEFAULT", output);
    sql = "upDate  IGNORE (t1) set col2=DefaulT order bY t1.col2 ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLUpdateParser(lexer, new MySQLExprParser(lexer));
    update = parser.update();
    output = output2MySQL(update, sql);
    Assert.assertEquals("UPDATE IGNORE t1 SET col2 = DEFAULT ORDER BY t1.col2", output);
    sql = "upDate   (test.t1) SET col2=DefaulT order bY t1.col2 limit ? offset 1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLUpdateParser(lexer, new MySQLExprParser(lexer));
    update = parser.update();
    output = output2MySQL(update, sql);
    Assert.assertEquals("UPDATE test.t1 SET col2 = DEFAULT ORDER BY t1.col2 LIMIT 1, ?", output);
    sql = "upDate LOW_PRIORITY  t1, test.t2 SET col2=DefaulT , col2='123''4'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLUpdateParser(lexer, new MySQLExprParser(lexer));
    update = parser.update();
    output = output2MySQL(update, sql);
    Assert.assertEquals("UPDATE LOW_PRIORITY t1, test.t2 SET col2 = DEFAULT, col2 = '123\\'4'", output);
    sql = "upDate LOW_PRIORITY  t1, test.t2 SET col2:=DefaulT , col2='123''4' where id='a'";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLUpdateParser(lexer, new MySQLExprParser(lexer));
    update = parser.update();
    output = output2MySQL(update, sql);
    Assert.assertEquals("UPDATE LOW_PRIORITY t1, test.t2 SET col2 = DEFAULT, col2 = '123\\'4' WHERE id = 'a'", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DMLUpdateStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLUpdateStatement)

Example 38 with MySQLLexer

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

the class LiteralHexadecimalTest method testUtf8.

public void testUtf8() throws Exception {
    String sql = "x'E982B1E7A195275C73'";
    LiteralHexadecimal hex = (LiteralHexadecimal) new MySQLExprParser(new MySQLLexer(sql), "utf-8").expression();
    Assert.assertEquals("邱硕'\\s", hex.evaluation(null));
    sql = "x'd0A'";
    hex = (LiteralHexadecimal) new MySQLExprParser(new MySQLLexer(sql), "utf-8").expression();
    Assert.assertEquals("\r\n", hex.evaluation(null));
}
Also used : LiteralHexadecimal(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralHexadecimal) MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) MySQLExprParser(com.alibaba.cobar.parser.recognizer.mysql.syntax.MySQLExprParser)

Example 39 with MySQLLexer

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

the class MySQLDALParserTest method testdesc.

public void testdesc() throws Exception {
    String sql = "desc tb1";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDALParser parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    SQLStatement desc = (DescTableStatement) parser.desc();
    parser.match(MySQLToken.EOF);
    String output = output2MySQL(desc, sql);
    Assert.assertEquals("DESC tb1", output);
    sql = "desc db.tb1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    desc = (DescTableStatement) parser.desc();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(desc, sql);
    Assert.assertEquals("DESC db.tb1", output);
    sql = "describe db.tb1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDALParser(lexer, new MySQLExprParser(lexer));
    desc = (DescTableStatement) parser.desc();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(desc, sql);
    Assert.assertEquals("DESC db.tb1", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DescTableStatement(com.alibaba.cobar.parser.ast.stmt.ddl.DescTableStatement) SQLStatement(com.alibaba.cobar.parser.ast.stmt.SQLStatement)

Example 40 with MySQLLexer

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

the class MySQLDDLParserTest method testTruncate.

public void testTruncate() throws Exception {
    String sql = "Truncate table tb1";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDDLParser parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    DDLStatement trun = (DDLTruncateStatement) parser.truncate();
    parser.match(MySQLToken.EOF);
    String output = output2MySQL(trun, sql);
    Assert.assertEquals("TRUNCATE TABLE tb1", output);
    sql = "Truncate tb1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDDLParser(lexer, new MySQLExprParser(lexer));
    trun = (DDLTruncateStatement) parser.truncate();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(trun, sql);
    Assert.assertEquals("TRUNCATE TABLE tb1", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DDLStatement(com.alibaba.cobar.parser.ast.stmt.ddl.DDLStatement) DDLTruncateStatement(com.alibaba.cobar.parser.ast.stmt.ddl.DDLTruncateStatement)

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