Search in sources :

Example 1 with DMLUpdateStatement

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

the class MySQLDMLUpdateParser method update.

/**
     * 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 DMLUpdateStatement update() throws SQLSyntaxErrorException {
    match(KW_UPDATE);
    boolean lowPriority = false;
    boolean ignore = false;
    if (lexer.token() == KW_LOW_PRIORITY) {
        lexer.nextToken();
        lowPriority = true;
    }
    if (lexer.token() == KW_IGNORE) {
        lexer.nextToken();
        ignore = true;
    }
    TableReferences tableRefs = tableRefs();
    match(KW_SET);
    List<Pair<Identifier, Expression>> values;
    Identifier col = identifier();
    match(OP_EQUALS, OP_ASSIGN);
    Expression expr = exprParser.expression();
    if (lexer.token() == PUNC_COMMA) {
        values = new LinkedList<Pair<Identifier, Expression>>();
        values.add(new Pair<Identifier, Expression>(col, expr));
        for (; lexer.token() == PUNC_COMMA; ) {
            lexer.nextToken();
            col = identifier();
            match(OP_EQUALS, OP_ASSIGN);
            expr = exprParser.expression();
            values.add(new Pair<Identifier, Expression>(col, expr));
        }
    } else {
        values = new ArrayList<Pair<Identifier, Expression>>(1);
        values.add(new Pair<Identifier, Expression>(col, expr));
    }
    Expression where = null;
    if (lexer.token() == KW_WHERE) {
        lexer.nextToken();
        where = exprParser.expression();
    }
    OrderBy orderBy = null;
    Limit limit = null;
    if (tableRefs.isSingleTable()) {
        orderBy = orderBy();
        limit = limit();
    }
    return new DMLUpdateStatement(lowPriority, ignore, tableRefs, values, where, orderBy, limit);
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Expression(com.alibaba.cobar.parser.ast.expression.Expression) DMLUpdateStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLUpdateStatement) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences) Pair(com.alibaba.cobar.parser.util.Pair)

Example 2 with DMLUpdateStatement

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

the class ServerRouter method validateAST.

private static void validateAST(SQLStatement ast, TableConfig tc, RuleConfig rule, PartitionKeyVisitor visitor) throws SQLNonTransientException {
    if (ast instanceof DMLUpdateStatement) {
        List<Identifier> columns = null;
        List<String> ruleCols = rule.getColumns();
        DMLUpdateStatement update = (DMLUpdateStatement) ast;
        for (Pair<Identifier, Expression> pair : update.getValues()) {
            for (String ruleCol : ruleCols) {
                if (equals(pair.getKey().getIdTextUpUnescape(), ruleCol)) {
                    if (columns == null) {
                        columns = new ArrayList<Identifier>(ruleCols.size());
                    }
                    columns.add(pair.getKey());
                }
            }
        }
        if (columns == null) {
            return;
        }
        Map<String, String> alias = visitor.getTableAlias();
        for (Identifier column : columns) {
            String table = column.getLevelUnescapeUpName(2);
            table = alias.get(table);
            if (table != null && table.equals(tc.getName())) {
                throw new SQLFeatureNotSupportedException("partition key cannot be changed");
            }
        }
    }
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Expression(com.alibaba.cobar.parser.ast.expression.Expression) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) DMLUpdateStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLUpdateStatement)

Example 3 with DMLUpdateStatement

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

Aggregations

DMLUpdateStatement (com.alibaba.cobar.parser.ast.stmt.dml.DMLUpdateStatement)3 Expression (com.alibaba.cobar.parser.ast.expression.Expression)2 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)2 ReplacableExpression (com.alibaba.cobar.parser.ast.expression.ReplacableExpression)1 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)1 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)1 Limit (com.alibaba.cobar.parser.ast.fragment.Limit)1 OrderBy (com.alibaba.cobar.parser.ast.fragment.OrderBy)1 TableReferences (com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences)1 MySQLLexer (com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer)1 Pair (com.alibaba.cobar.parser.util.Pair)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1