Search in sources :

Example 1 with Pair

use of com.alibaba.cobar.parser.util.Pair 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 Pair

use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.

the class ServerRouter method replacePartitionKeyOperand.

private static void replacePartitionKeyOperand(Map<String, Map<Object, Set<Pair<Expression, ASTNode>>>> index, List<String> cols) {
    if (cols == null) {
        return;
    }
    for (String col : cols) {
        Map<Object, Set<Pair<Expression, ASTNode>>> map = index.get(col);
        if (map == null) {
            continue;
        }
        for (Set<Pair<Expression, ASTNode>> set : map.values()) {
            if (set == null) {
                continue;
            }
            for (Pair<Expression, ASTNode> p : set) {
                Expression expr = p.getKey();
                ASTNode parent = p.getValue();
                if (PartitionKeyVisitor.isPartitionKeyOperandSingle(expr, parent)) {
                    ((ReplacableExpression) expr).setReplaceExpr(ReplacableExpression.BOOL_FALSE);
                } else if (PartitionKeyVisitor.isPartitionKeyOperandIn(expr, parent)) {
                    ((ReplacableExpression) parent).setReplaceExpr(ReplacableExpression.BOOL_FALSE);
                }
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) 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) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) ASTNode(com.alibaba.cobar.parser.ast.ASTNode) Pair(com.alibaba.cobar.parser.util.Pair)

Example 3 with Pair

use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.

the class MySQLDMLInsertParser method onDuplicateUpdate.

/**
 * @return null for not exist
 */
private List<Pair<Identifier, Expression>> onDuplicateUpdate() throws SQLSyntaxErrorException {
    if (lexer.token() != KW_ON) {
        return null;
    }
    lexer.nextToken();
    matchIdentifier("DUPLICATE");
    match(KW_KEY);
    match(KW_UPDATE);
    List<Pair<Identifier, Expression>> list;
    Identifier col = identifier();
    match(OP_EQUALS, OP_ASSIGN);
    Expression expr = exprParser.expression();
    if (lexer.token() == PUNC_COMMA) {
        list = new LinkedList<Pair<Identifier, Expression>>();
        list.add(new Pair<Identifier, Expression>(col, expr));
        for (; lexer.token() == PUNC_COMMA; ) {
            lexer.nextToken();
            col = identifier();
            match(OP_EQUALS, OP_ASSIGN);
            expr = exprParser.expression();
            list.add(new Pair<Identifier, Expression>(col, expr));
        }
        return list;
    }
    list = new ArrayList<Pair<Identifier, Expression>>(1);
    list.add(new Pair<Identifier, Expression>(col, expr));
    return list;
}
Also used : 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) Pair(com.alibaba.cobar.parser.util.Pair)

Example 4 with Pair

use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.

the class MySQLOutputASTVisitor method visit.

@Override
public void visit(CaseWhenOperatorExpression node) {
    appendable.append("CASE");
    Expression comparee = node.getComparee();
    if (comparee != null) {
        appendable.append(' ');
        comparee.accept(this);
    }
    List<Pair<Expression, Expression>> whenList = node.getWhenList();
    for (Pair<Expression, Expression> whenthen : whenList) {
        appendable.append(" WHEN ");
        Expression when = whenthen.getKey();
        when.accept(this);
        appendable.append(" THEN ");
        Expression then = whenthen.getValue();
        then.accept(this);
    }
    Expression elseRst = node.getElseResult();
    if (elseRst != null) {
        appendable.append(" ELSE ");
        elseRst.accept(this);
    }
    appendable.append(" END");
}
Also used : BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) MatchExpression(com.alibaba.cobar.parser.ast.expression.primary.MatchExpression) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) TernaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) Pair(com.alibaba.cobar.parser.util.Pair)

Example 5 with Pair

use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.

the class MySQLOutputASTVisitor method visit.

@Override
public void visit(DMLSelectStatement node) {
    appendable.append("SELECT ");
    final DMLSelectStatement.SelectOption option = node.getOption();
    switch(option.resultDup) {
        case ALL:
            break;
        case DISTINCT:
            appendable.append("DISTINCT ");
            break;
        case DISTINCTROW:
            appendable.append("DISTINCTROW ");
            break;
        default:
            throw new IllegalArgumentException("unknown option for SELECT: " + option);
    }
    if (option.highPriority) {
        appendable.append("HIGH_PRIORITY ");
    }
    if (option.straightJoin) {
        appendable.append("STRAIGHT_JOIN ");
    }
    switch(option.resultSize) {
        case SQL_BIG_RESULT:
            appendable.append("SQL_BIG_RESULT ");
            break;
        case SQL_SMALL_RESULT:
            appendable.append("SQL_SMALL_RESULT ");
            break;
        case UNDEF:
            break;
        default:
            throw new IllegalArgumentException("unknown option for SELECT: " + option);
    }
    if (option.sqlBufferResult) {
        appendable.append("SQL_BUFFER_RESULT ");
    }
    switch(option.queryCache) {
        case SQL_CACHE:
            appendable.append("SQL_CACHE ");
            break;
        case SQL_NO_CACHE:
            appendable.append("SQL_NO_CACHE ");
            break;
        case UNDEF:
            break;
        default:
            throw new IllegalArgumentException("unknown option for SELECT: " + option);
    }
    if (option.sqlCalcFoundRows) {
        appendable.append("SQL_CALC_FOUND_ROWS ");
    }
    boolean isFst = true;
    List<Pair<Expression, String>> exprList = node.getSelectExprList();
    for (Pair<Expression, String> p : exprList) {
        if (isFst)
            isFst = false;
        else
            appendable.append(", ");
        p.getKey().accept(this);
        String alias = p.getValue();
        if (alias != null) {
            appendable.append(" AS ").append(alias);
        }
    }
    TableReferences from = node.getTables();
    if (from != null) {
        appendable.append(" FROM ");
        from.accept(this);
    }
    Expression where = node.getWhere();
    if (where != null) {
        appendable.append(" WHERE ");
        where.accept(this);
    }
    GroupBy group = node.getGroup();
    if (group != null) {
        appendable.append(' ');
        group.accept(this);
    }
    Expression having = node.getHaving();
    if (having != null) {
        appendable.append(" HAVING ");
        having.accept(this);
    }
    OrderBy order = node.getOrder();
    if (order != null) {
        appendable.append(' ');
        order.accept(this);
    }
    Limit limit = node.getLimit();
    if (limit != null) {
        appendable.append(' ');
        limit.accept(this);
    }
    switch(option.lockMode) {
        case FOR_UPDATE:
            appendable.append(" FOR UPDATE");
            break;
        case LOCK_IN_SHARE_MODE:
            appendable.append(" LOCK IN SHARE MODE");
            break;
        case UNDEF:
            break;
        default:
            throw new IllegalArgumentException("unknown option for SELECT: " + option);
    }
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) MatchExpression(com.alibaba.cobar.parser.ast.expression.primary.MatchExpression) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) TernaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) Pair(com.alibaba.cobar.parser.util.Pair)

Aggregations

Pair (com.alibaba.cobar.parser.util.Pair)22 Expression (com.alibaba.cobar.parser.ast.expression.Expression)20 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)13 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)12 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)11 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)10 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)8 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)8 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)8 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)8 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)8 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)8 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)8 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)8 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)8 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)8 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)8 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)8 ASTNode (com.alibaba.cobar.parser.ast.ASTNode)6 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)6