Search in sources :

Example 6 with OrderBy

use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.

the class MySQLDMLParser method orderBy.

/**
     * nothing has been pre-consumed
     * 
     * @return null if there is no order by
     */
protected OrderBy orderBy() throws SQLSyntaxErrorException {
    if (lexer.token() != KW_ORDER) {
        return null;
    }
    lexer.nextToken();
    match(KW_BY);
    Expression expr = exprParser.expression();
    SortOrder order = SortOrder.ASC;
    OrderBy orderBy;
    switch(lexer.token()) {
        case KW_DESC:
            order = SortOrder.DESC;
        case KW_ASC:
            if (lexer.nextToken() != PUNC_COMMA) {
                return new OrderBy(expr, order);
            }
        case PUNC_COMMA:
            orderBy = new OrderBy();
            orderBy.addOrderByItem(expr, order);
            break;
        default:
            return new OrderBy(expr, order);
    }
    for (; lexer.token() == PUNC_COMMA; ) {
        lexer.nextToken();
        order = SortOrder.ASC;
        expr = exprParser.expression();
        switch(lexer.token()) {
            case KW_DESC:
                order = SortOrder.DESC;
            case KW_ASC:
                lexer.nextToken();
        }
        orderBy.addOrderByItem(expr, order);
    }
    return orderBy;
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) Expression(com.alibaba.cobar.parser.ast.expression.Expression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) SortOrder(com.alibaba.cobar.parser.ast.fragment.SortOrder)

Example 7 with OrderBy

use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.

the class MySQLDMLSelectParser method select.

@Override
public DMLSelectStatement select() throws SQLSyntaxErrorException {
    match(KW_SELECT);
    DMLSelectStatement.SelectOption option = selectOption();
    List<Pair<Expression, String>> exprList = selectExprList();
    TableReferences tables = null;
    Expression where = null;
    GroupBy group = null;
    Expression having = null;
    OrderBy order = null;
    Limit limit = null;
    boolean dual = false;
    if (lexer.token() == KW_FROM) {
        if (lexer.nextToken() == KW_DUAL) {
            lexer.nextToken();
            dual = true;
            List<TableReference> trs = new ArrayList<TableReference>(1);
            trs.add(new Dual());
            tables = new TableReferences(trs);
        } else {
            tables = tableRefs();
        }
    }
    if (lexer.token() == KW_WHERE) {
        lexer.nextToken();
        where = exprParser.expression();
    }
    if (!dual) {
        group = groupBy();
        if (lexer.token() == KW_HAVING) {
            lexer.nextToken();
            having = exprParser.expression();
        }
        order = orderBy();
    }
    limit = limit();
    if (!dual) {
        switch(lexer.token()) {
            case KW_FOR:
                lexer.nextToken();
                match(KW_UPDATE);
                option.lockMode = DMLSelectStatement.LockMode.FOR_UPDATE;
                break;
            case KW_LOCK:
                lexer.nextToken();
                match(KW_IN);
                matchIdentifier("SHARE");
                matchIdentifier("MODE");
                option.lockMode = DMLSelectStatement.LockMode.LOCK_IN_SHARE_MODE;
                break;
        }
    }
    return new DMLSelectStatement(option, exprList, tables, where, group, having, order, limit);
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) ArrayList(java.util.ArrayList) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences) TableReference(com.alibaba.cobar.parser.ast.fragment.tableref.TableReference) Expression(com.alibaba.cobar.parser.ast.expression.Expression) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) Dual(com.alibaba.cobar.parser.ast.fragment.tableref.Dual) Pair(com.alibaba.cobar.parser.util.Pair)

Example 8 with OrderBy

use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.

the class MySQLDMLDeleteParser method delete.

/**
     * first token is {@link MySQLToken#KW_DELETE} <code><pre>
     * 'DELETE' 'LOW_PRIORITY'? 'QUICK'? 'IGNORE'? (
     *     'FROM' tid ( (',' tid)* 'USING' table_refs ('WHERE' cond)?  
     *                | ('WHERE' cond)? ('ORDER' 'BY' ids)? ('LIMIT' count)?  )  // single table
     *    | tid (',' tid)* 'FROM' table_refs ('WHERE' cond)? )
     * </pre></code>
     */
public DMLDeleteStatement delete() throws SQLSyntaxErrorException {
    match(KW_DELETE);
    boolean lowPriority = false;
    boolean quick = false;
    boolean ignore = false;
    loopOpt: for (; ; lexer.nextToken()) {
        switch(lexer.token()) {
            case KW_LOW_PRIORITY:
                lowPriority = true;
                break;
            case KW_IGNORE:
                ignore = true;
                break;
            case IDENTIFIER:
                SpecialIdentifier si = specialIdentifiers.get(lexer.stringValueUppercase());
                if (SpecialIdentifier.QUICK == si) {
                    quick = true;
                    break;
                }
            default:
                break loopOpt;
        }
    }
    List<Identifier> tempList;
    TableReferences tempRefs;
    Expression tempWhere;
    if (lexer.token() == KW_FROM) {
        lexer.nextToken();
        Identifier id = identifier();
        tempList = new ArrayList<Identifier>(1);
        tempList.add(id);
        switch(lexer.token()) {
            case PUNC_COMMA:
                tempList = buildIdList(id);
            case KW_USING:
                lexer.nextToken();
                tempRefs = tableRefs();
                if (lexer.token() == KW_WHERE) {
                    lexer.nextToken();
                    tempWhere = exprParser.expression();
                    return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs, tempWhere);
                }
                return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs);
            case KW_WHERE:
            case KW_ORDER:
            case KW_LIMIT:
                break;
            default:
                return new DMLDeleteStatement(lowPriority, quick, ignore, id);
        }
        tempWhere = null;
        OrderBy orderBy = null;
        Limit limit = null;
        if (lexer.token() == KW_WHERE) {
            lexer.nextToken();
            tempWhere = exprParser.expression();
        }
        if (lexer.token() == KW_ORDER) {
            orderBy = orderBy();
        }
        if (lexer.token() == KW_LIMIT) {
            limit = limit();
        }
        return new DMLDeleteStatement(lowPriority, quick, ignore, id, tempWhere, orderBy, limit);
    }
    tempList = idList();
    match(KW_FROM);
    tempRefs = tableRefs();
    if (lexer.token() == KW_WHERE) {
        lexer.nextToken();
        tempWhere = exprParser.expression();
        return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs, tempWhere);
    }
    return new DMLDeleteStatement(lowPriority, quick, ignore, tempList, tempRefs);
}
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) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences) DMLDeleteStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLDeleteStatement)

Example 9 with OrderBy

use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(DMLSelectStatement node) {
    boolean verdictGroup = true;
    List<Expression> exprList = node.getSelectExprListWithoutAlias();
    if (verdictGroupFunc) {
        for (Expression expr : exprList) {
            if (!isGroupFuncPassthroughSelect(expr)) {
                groupFuncType = GROUP_CANCEL;
                verdictGroup = false;
                break;
            }
        }
        limit(node.getLimit());
    }
    visitChild(2, false, verdictGroupFunc && verdictGroup, exprList);
    TableReference tr = node.getTables();
    visitChild(1, verdictColumn, verdictGroupFunc && verdictGroup, tr);
    Expression where = node.getWhere();
    visitChild(2, verdictColumn, false, where);
    GroupBy group = node.getGroup();
    visitChild(2, false, false, group);
    Expression having = node.getHaving();
    visitChild(2, verdictColumn, false, having);
    OrderBy order = node.getOrder();
    visitChild(2, false, false, order);
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) TableReference(com.alibaba.cobar.parser.ast.fragment.tableref.TableReference) GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) 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) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) 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) 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)

Example 10 with OrderBy

use of com.alibaba.cobar.parser.ast.fragment.OrderBy in project cobar by alibaba.

the class PartitionKeyVisitor method visit.

@Override
public void visit(DMLUpdateStatement node) {
    TableReference tr = node.getTableRefs();
    visitChild(1, false, false, tr);
    List<Pair<Identifier, Expression>> assignmentList = node.getValues();
    if (assignmentList != null && !assignmentList.isEmpty()) {
        List<ASTNode> list = new ArrayList<ASTNode>(assignmentList.size() * 2);
        for (Pair<Identifier, Expression> p : assignmentList) {
            if (p == null)
                continue;
            list.add(p.getKey());
            list.add(p.getValue());
        }
        visitChild(2, false, false, list);
    }
    Expression where = node.getWhere();
    visitChild(2, verdictColumn, false, where);
    OrderBy order = node.getOrderBy();
    visitChild(2, false, false, order);
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) TableReference(com.alibaba.cobar.parser.ast.fragment.tableref.TableReference) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) 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) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) 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) 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) ArrayList(java.util.ArrayList) ASTNode(com.alibaba.cobar.parser.ast.ASTNode) Pair(com.alibaba.cobar.parser.util.Pair)

Aggregations

OrderBy (com.alibaba.cobar.parser.ast.fragment.OrderBy)12 Expression (com.alibaba.cobar.parser.ast.expression.Expression)10 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)7 Limit (com.alibaba.cobar.parser.ast.fragment.Limit)7 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)6 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)6 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)6 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)6 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)6 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)6 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)6 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)6 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)6 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)6 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)6 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)6 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)6 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)6 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)6 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)6