Search in sources :

Example 76 with Expression

use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.

the class MySQLDMLParser method buildTableReference.

@SuppressWarnings("unchecked")
private TableReference buildTableReference(TableReference ref) throws SQLSyntaxErrorException {
    for (; ; ) {
        Expression on;
        List<String> using;
        TableReference temp;
        boolean isOut = false;
        boolean isLeft = true;
        switch(lexer.token()) {
            case KW_INNER:
            case KW_CROSS:
                lexer.nextToken();
            case KW_JOIN:
                lexer.nextToken();
                temp = tableFactor();
                switch(lexer.token()) {
                    case KW_ON:
                        lexer.nextToken();
                        on = exprParser.expression();
                        ref = new InnerJoin(ref, temp, on);
                        break;
                    case KW_USING:
                        lexer.nextToken();
                        match(PUNC_LEFT_PAREN);
                        using = idNameList();
                        ref = new InnerJoin(ref, temp, using);
                        break;
                    default:
                        ref = new InnerJoin(ref, temp);
                        break;
                }
                break;
            case KW_STRAIGHT_JOIN:
                lexer.nextToken();
                temp = tableFactor();
                switch(lexer.token()) {
                    case KW_ON:
                        lexer.nextToken();
                        on = exprParser.expression();
                        ref = new StraightJoin(ref, temp, on);
                        break;
                    default:
                        ref = new StraightJoin(ref, temp);
                        break;
                }
                break;
            case KW_RIGHT:
                isLeft = false;
            case KW_LEFT:
                lexer.nextToken();
                if (lexer.token() == KW_OUTER) {
                    lexer.nextToken();
                }
                match(KW_JOIN);
                temp = tableReference();
                switch(lexer.token()) {
                    case KW_ON:
                        lexer.nextToken();
                        on = exprParser.expression();
                        ref = new OuterJoin(isLeft, ref, temp, on);
                        break;
                    case KW_USING:
                        lexer.nextToken();
                        match(PUNC_LEFT_PAREN);
                        using = idNameList();
                        ref = new OuterJoin(isLeft, ref, temp, using);
                        break;
                    default:
                        Object condition = temp.removeLastConditionElement();
                        if (condition instanceof Expression) {
                            ref = new OuterJoin(isLeft, ref, temp, (Expression) condition);
                        } else if (condition instanceof List) {
                            ref = new OuterJoin(isLeft, ref, temp, (List<String>) condition);
                        } else {
                            throw err("conditionExpr cannot be null for outer join");
                        }
                        break;
                }
                break;
            case KW_NATURAL:
                lexer.nextToken();
                switch(lexer.token()) {
                    case KW_RIGHT:
                        isLeft = false;
                    case KW_LEFT:
                        lexer.nextToken();
                        if (lexer.token() == KW_OUTER) {
                            lexer.nextToken();
                        }
                        isOut = true;
                    case KW_JOIN:
                        lexer.nextToken();
                        temp = tableFactor();
                        ref = new NaturalJoin(isOut, isLeft, ref, temp);
                        break;
                    default:
                        throw err("unexpected token after NATURAL for natural join:" + lexer.token());
                }
                break;
            default:
                return ref;
        }
    }
}
Also used : TableReference(com.alibaba.cobar.parser.ast.fragment.tableref.TableReference) StraightJoin(com.alibaba.cobar.parser.ast.fragment.tableref.StraightJoin) Expression(com.alibaba.cobar.parser.ast.expression.Expression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) InnerJoin(com.alibaba.cobar.parser.ast.fragment.tableref.InnerJoin) OuterJoin(com.alibaba.cobar.parser.ast.fragment.tableref.OuterJoin) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) NaturalJoin(com.alibaba.cobar.parser.ast.fragment.tableref.NaturalJoin)

Example 77 with Expression

use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.

the class MySQLDMLParser method groupBy.

/**
     * nothing has been pre-consumed
     * 
     * @return null if there is no order by
     */
protected GroupBy groupBy() throws SQLSyntaxErrorException {
    if (lexer.token() != KW_GROUP) {
        return null;
    }
    lexer.nextToken();
    match(KW_BY);
    Expression expr = exprParser.expression();
    SortOrder order = SortOrder.ASC;
    GroupBy groupBy;
    switch(lexer.token()) {
        case KW_DESC:
            order = SortOrder.DESC;
        case KW_ASC:
            lexer.nextToken();
        default:
            break;
    }
    switch(lexer.token()) {
        case KW_WITH:
            lexer.nextToken();
            matchIdentifier("ROLLUP");
            return new GroupBy(expr, order, true);
        case PUNC_COMMA:
            break;
        default:
            return new GroupBy(expr, order, false);
    }
    for (groupBy = new GroupBy().addOrderByItem(expr, order); 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();
            default:
                break;
        }
        groupBy.addOrderByItem(expr, order);
        if (lexer.token() == KW_WITH) {
            lexer.nextToken();
            matchIdentifier("ROLLUP");
            return groupBy.setWithRollup();
        }
    }
    return groupBy;
}
Also used : GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) 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 78 with Expression

use of com.alibaba.cobar.parser.ast.expression.Expression 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 79 with Expression

use of com.alibaba.cobar.parser.ast.expression.Expression in project cobar by alibaba.

the class MySQLDMLSelectParser method selectExprList.

private List<Pair<Expression, String>> selectExprList() throws SQLSyntaxErrorException {
    Expression expr = exprParser.expression();
    String alias = as();
    List<Pair<Expression, String>> list;
    if (lexer.token() == PUNC_COMMA) {
        list = new LinkedList<Pair<Expression, String>>();
        list.add(new Pair<Expression, String>(expr, alias));
    } else {
        list = new ArrayList<Pair<Expression, String>>(1);
        list.add(new Pair<Expression, String>(expr, alias));
        return list;
    }
    for (; lexer.token() == PUNC_COMMA; list.add(new Pair<Expression, String>(expr, alias))) {
        lexer.nextToken();
        expr = exprParser.expression();
        alias = as();
    }
    return list;
}
Also used : Expression(com.alibaba.cobar.parser.ast.expression.Expression) Pair(com.alibaba.cobar.parser.util.Pair)

Example 80 with Expression

use of com.alibaba.cobar.parser.ast.expression.Expression 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)

Aggregations

Expression (com.alibaba.cobar.parser.ast.expression.Expression)128 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)96 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)93 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)91 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)88 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)88 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)87 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)87 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)87 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)87 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)87 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)87 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)87 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)87 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)76 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)71 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)58 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)58 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)58 ArithmeticAddExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression)45