Search in sources :

Example 1 with DMLInsertStatement

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

the class MySQLDMLInsertParserTest method testInsert.

public void testInsert() throws SQLSyntaxErrorException {
    String sql = "insErt HIGH_PRIORITY intO test.t1 seT t1.id1=?, id2 := '123'";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLInsertParser parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    DMLInsertStatement insert = parser.insert();
    parser.match(MySQLToken.EOF);
    String output = output2MySQL(insert, sql);
    Assert.assertNotNull(insert);
    Assert.assertEquals("INSERT HIGH_PRIORITY INTO test.t1 (t1.id1, id2) VALUES (?, '123')", output);
    sql = "insErt  IGNORE test.t1 seT t1.id1:=? oN dupLicatE key UPDATE ex.col1=?, col2=12";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT IGNORE INTO test.t1 (t1.id1) VALUES (?) ON DUPLICATE KEY UPDATE ex.col1 = ?, col2 = 12", output);
    sql = "insErt t1 value (123,?) oN dupLicatE key UPDATE ex.col1=?";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT INTO t1 VALUES (123, ?) ON DUPLICATE KEY UPDATE ex.col1 = ?", output);
    sql = "insErt LOW_PRIORITY t1 valueS (12e-2,1,2), (?),(default)";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY INTO t1 VALUES (0.12, 1, 2), (?), (DEFAULT)", output);
    sql = "insErt LOW_PRIORITY t1 select id from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY INTO t1 SELECT id FROM t1", output);
    sql = "insErt delayed t1 select id from t1";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT DELAYED INTO t1 SELECT id FROM t1", output);
    sql = "insErt LOW_PRIORITY t1 (select id from t1) oN dupLicatE key UPDATE ex.col1=?, col2=12";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY INTO t1 SELECT id FROM t1 ON DUPLICATE KEY UPDATE ex.col1 = ?, col2 = 12", output);
    sql = "insErt LOW_PRIORITY t1 (t1.col1) valueS (123),('12''34')";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY INTO t1 (t1.col1) VALUES (123), ('12\\'34')", output);
    sql = "insErt LOW_PRIORITY t1 (col1, t1.col2) VALUE (123,'123\\'4') oN dupLicatE key UPDATE ex.col1=?";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY INTO t1 (col1, t1.col2) VALUES (123, '123\\'4') ON DUPLICATE KEY UPDATE ex.col1 = ?", output);
    sql = "insErt LOW_PRIORITY t1 (col1, t1.col2) select id from t3 oN dupLicatE key UPDATE ex.col1=?";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY INTO t1 (col1, t1.col2) SELECT id FROM t3 ON DUPLICATE KEY UPDATE ex.col1 = ?", output);
    sql = "insErt LOW_PRIORITY IGNORE intO t1 (col1) ( select id from t3) ";
    lexer = new MySQLLexer(sql);
    parser = new MySQLDMLInsertParser(lexer, new MySQLExprParser(lexer));
    insert = parser.insert();
    parser.match(MySQLToken.EOF);
    output = output2MySQL(insert, sql);
    Assert.assertEquals("INSERT LOW_PRIORITY IGNORE INTO t1 (col1) SELECT id FROM t3", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) DMLInsertStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLInsertStatement)

Example 2 with DMLInsertStatement

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

the class ServerRouteTest method testRouteInsertLong.

public void testRouteInsertLong() throws Exception {
    StringBuilder sb = new StringBuilder("insert into offer_detail (offer_id, gmt) values ");
    for (int i = 0; i < 1024; ++i) {
        if (i > 0)
            sb.append(", ");
        sb.append("(" + i + ", now())");
    }
    SchemaConfig schema = schemaMap.get("cndb");
    RouteResultset rrs = ServerRouter.route(schema, sb.toString(), null, null);
    Assert.assertEquals(-1l, rrs.getLimitSize());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 128);
    IndexedNodeNameAsserter nameAsserter = new IndexedNodeNameAsserter("detail_dn", 0, 128);
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, new ParseredSQLAsserter() {

        @Override
        protected void assertAST(SQLStatement stmt, int nodeIndex) {
            DMLInsertStatement insert = (DMLInsertStatement) stmt;
            List<RowExpression> rows = insert.getRowList();
            Assert.assertNotNull(rows);
            Assert.assertEquals(8, rows.size());
            List<Integer> vals = new ArrayList<Integer>(8);
            for (RowExpression row : rows) {
                int val = ((Number) row.getRowExprList().get(0).evaluation(null)).intValue();
                vals.add(val);
            }
            Assert.assertEquals(8, vals.size());
            for (int i = 8 * nodeIndex; i < 8 * nodeIndex + 8; ++i) {
                Assert.assertTrue(vals.contains(i));
            }
        }
    });
    for (RouteResultsetNode node : nodeMap.values()) {
        asserter.assertNode(node);
    }
}
Also used : SchemaConfig(com.alibaba.cobar.config.model.SchemaConfig) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) SQLStatement(com.alibaba.cobar.parser.ast.stmt.SQLStatement) DMLInsertStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLInsertStatement) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with DMLInsertStatement

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

the class MySQLDMLInsertParser method insert.

/**
 * nothing has been pre-consumed <code><pre>
 * 'INSERT' ('LOW_PRIORITY'|'DELAYED'|'HIGH_PRIORITY')? 'IGNORE'? 'INTO'? tbname
 *  (  'SET' colName ('='|':=') (expr|'DEFAULT') (',' colName ('='|':=') (expr|'DEFAULT'))*
 *   | '(' (  colName (',' colName)* ')' ( ('VALUES'|'VALUE') value (',' value)*
 *                                        | '(' 'SELECT' ... ')'
 *                                        | 'SELECT' ...
 *                                       )
 *          | 'SELECT' ... ')'
 *         )
 *   |('VALUES'|'VALUE') value  ( ',' value )*
 *   | 'SELECT' ...
 *  )
 * ( 'ON' 'DUPLICATE' 'KEY' 'UPDATE' colName ('='|':=') expr ( ',' colName ('='|':=') expr)* )?
 *
 * value := '(' (expr|'DEFAULT') ( ',' (expr|'DEFAULT'))* ')'
 * </pre></code>
 */
public DMLInsertStatement insert() throws SQLSyntaxErrorException {
    match(KW_INSERT);
    DMLInsertStatement.InsertMode mode = DMLInsertStatement.InsertMode.UNDEF;
    boolean ignore = false;
    switch(lexer.token()) {
        case KW_LOW_PRIORITY:
            lexer.nextToken();
            mode = DMLInsertStatement.InsertMode.LOW;
            break;
        case KW_DELAYED:
            lexer.nextToken();
            mode = DMLInsertStatement.InsertMode.DELAY;
            break;
        case KW_HIGH_PRIORITY:
            lexer.nextToken();
            mode = DMLInsertStatement.InsertMode.HIGH;
            break;
    }
    if (lexer.token() == KW_IGNORE) {
        ignore = true;
        lexer.nextToken();
    }
    if (lexer.token() == KW_INTO) {
        lexer.nextToken();
    }
    Identifier table = identifier();
    List<Pair<Identifier, Expression>> dupUpdate;
    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));
            dupUpdate = onDuplicateUpdate();
            return new DMLInsertStatement(mode, ignore, table, columnNameList, rowList, dupUpdate);
        case IDENTIFIER:
            if (!"VALUE".equals(lexer.stringValueUppercase())) {
                break;
            }
        case KW_VALUES:
            lexer.nextToken();
            columnNameList = null;
            rowList = rowList();
            dupUpdate = onDuplicateUpdate();
            return new DMLInsertStatement(mode, ignore, table, columnNameList, rowList, dupUpdate);
        case KW_SELECT:
            columnNameList = null;
            select = select();
            dupUpdate = onDuplicateUpdate();
            return new DMLInsertStatement(mode, ignore, table, columnNameList, select, dupUpdate);
        case PUNC_LEFT_PAREN:
            switch(lexer.nextToken()) {
                case PUNC_LEFT_PAREN:
                case KW_SELECT:
                    columnNameList = null;
                    select = selectPrimary();
                    match(PUNC_RIGHT_PAREN);
                    dupUpdate = onDuplicateUpdate();
                    return new DMLInsertStatement(mode, ignore, table, columnNameList, select, dupUpdate);
            }
            columnNameList = idList();
            match(PUNC_RIGHT_PAREN);
            switch(lexer.token()) {
                case PUNC_LEFT_PAREN:
                case KW_SELECT:
                    select = selectPrimary();
                    dupUpdate = onDuplicateUpdate();
                    return new DMLInsertStatement(mode, ignore, table, columnNameList, select, dupUpdate);
                case KW_VALUES:
                    lexer.nextToken();
                    break;
                default:
                    matchIdentifier("VALUE");
            }
            rowList = rowList();
            dupUpdate = onDuplicateUpdate();
            return new DMLInsertStatement(mode, ignore, table, columnNameList, rowList, dupUpdate);
    }
    throw err("unexpected token for insert: " + lexer.token());
}
Also used : RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) DMLInsertStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLInsertStatement) 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) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) Pair(com.alibaba.cobar.parser.util.Pair)

Aggregations

DMLInsertStatement (com.alibaba.cobar.parser.ast.stmt.dml.DMLInsertStatement)3 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)2 SchemaConfig (com.alibaba.cobar.config.model.SchemaConfig)1 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 SQLStatement (com.alibaba.cobar.parser.ast.stmt.SQLStatement)1 MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)1 Pair (com.alibaba.cobar.parser.util.Pair)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1