Search in sources :

Example 1 with DMLReplaceStatement

use of com.alibaba.cobar.parser.ast.stmt.dml.DMLReplaceStatement in project cobar by alibaba.

the class MySQLDMLReplaceParser method replace.

/**
     * nothing has been pre-consumed <code><pre>
     * 'REPLACE' ('LOW_PRIORITY' | 'DELAYED')? ('INTO')? tableName
     *  (  'SET' colName ('='|':=') (expr|'DEFAULT') (',' colName ('='|':=') (expr|'DEFAULT'))*
     *   | '(' (  colName (','colName)* ')' (  '(' 'SELECT' ... ')'
     *                                       | 'SELECT' ...
     *                                       |('VALUES'|'VALUE') value ( ',' value )*
     *                                      )
     *          | 'SELECT' ... ')'
     *         )
     *   | 'SELECT' ...
     *   |('VALUES'|'VALUE') value ( ',' value )*
     *  )
     * value := '(' (expr|'DEFAULT') ( ',' (expr|'DEFAULT'))* ')'
     * </pre></code>
     */
public DMLReplaceStatement replace() throws SQLSyntaxErrorException {
    match(KW_REPLACE);
    DMLReplaceStatement.ReplaceMode mode = DMLReplaceStatement.ReplaceMode.UNDEF;
    switch(lexer.token()) {
        case KW_LOW_PRIORITY:
            lexer.nextToken();
            mode = DMLReplaceStatement.ReplaceMode.LOW;
            break;
        case KW_DELAYED:
            lexer.nextToken();
            mode = DMLReplaceStatement.ReplaceMode.DELAY;
            break;
    }
    if (lexer.token() == KW_INTO) {
        lexer.nextToken();
    }
    Identifier table = identifier();
    List<Identifier> columnNameList;
    List<RowExpression> rowList;
    QueryExpression select;
    List<Expression> tempRowValue;
    switch(lexer.token()) {
        case KW_SET:
            lexer.nextToken();
            columnNameList = new LinkedList<Identifier>();
            tempRowValue = new LinkedList<Expression>();
            for (; ; lexer.nextToken()) {
                Identifier id = identifier();
                match(OP_EQUALS, OP_ASSIGN);
                Expression expr = exprParser.expression();
                columnNameList.add(id);
                tempRowValue.add(expr);
                if (lexer.token() != PUNC_COMMA) {
                    break;
                }
            }
            rowList = new ArrayList<RowExpression>(1);
            rowList.add(new RowExpression(tempRowValue));
            return new DMLReplaceStatement(mode, table, columnNameList, rowList);
        case IDENTIFIER:
            if (!"VALUE".equals(lexer.stringValueUppercase())) {
                break;
            }
        case KW_VALUES:
            lexer.nextToken();
            columnNameList = null;
            rowList = rowList();
            return new DMLReplaceStatement(mode, table, columnNameList, rowList);
        case KW_SELECT:
            columnNameList = null;
            select = select();
            return new DMLReplaceStatement(mode, table, columnNameList, select);
        case PUNC_LEFT_PAREN:
            switch(lexer.nextToken()) {
                case PUNC_LEFT_PAREN:
                case KW_SELECT:
                    columnNameList = null;
                    select = selectPrimary();
                    match(PUNC_RIGHT_PAREN);
                    return new DMLReplaceStatement(mode, table, columnNameList, select);
            }
            columnNameList = idList();
            match(PUNC_RIGHT_PAREN);
            switch(lexer.token()) {
                case PUNC_LEFT_PAREN:
                case KW_SELECT:
                    select = selectPrimary();
                    return new DMLReplaceStatement(mode, table, columnNameList, select);
                case KW_VALUES:
                    lexer.nextToken();
                    break;
                default:
                    matchIdentifier("VALUE");
            }
            rowList = rowList();
            return new DMLReplaceStatement(mode, table, columnNameList, rowList);
    }
    throw err("unexpected token for replace: " + lexer.token());
}
Also used : DMLReplaceStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLReplaceStatement) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Expression(com.alibaba.cobar.parser.ast.expression.Expression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)

Example 2 with DMLReplaceStatement

use of com.alibaba.cobar.parser.ast.stmt.dml.DMLReplaceStatement 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

DMLReplaceStatement (com.alibaba.cobar.parser.ast.stmt.dml.DMLReplaceStatement)2 Expression (com.alibaba.cobar.parser.ast.expression.Expression)1 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)1 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)1 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)1 MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)1