Search in sources :

Example 6 with TableReference

use of com.alibaba.cobar.parser.ast.fragment.tableref.TableReference 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 7 with TableReference

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

the class MySQLDMLParser method trsOrQuery.

/**
     * @return type of {@link QueryExpression} or {@link TableReferences}
     */
private Object trsOrQuery() throws SQLSyntaxErrorException {
    Object ref;
    switch(lexer.token()) {
        case KW_SELECT:
            DMLSelectStatement select = selectPrimary();
            return buildUnionSelect(select);
        case PUNC_LEFT_PAREN:
            lexer.nextToken();
            ref = trsOrQuery();
            match(PUNC_RIGHT_PAREN);
            if (ref instanceof QueryExpression) {
                if (ref instanceof DMLSelectStatement) {
                    QueryExpression rst = buildUnionSelect((DMLSelectStatement) ref);
                    if (rst != ref) {
                        return rst;
                    }
                }
                String alias = as();
                if (alias != null) {
                    ref = new SubqueryFactor((QueryExpression) ref, alias);
                } else {
                    return ref;
                }
            }
            // ---- build factor complete---------------
            ref = buildTableReference((TableReference) ref);
            // ---- build ref complete---------------
            break;
        default:
            ref = tableReference();
    }
    List<TableReference> list;
    if (lexer.token() == PUNC_COMMA) {
        list = new LinkedList<TableReference>();
        list.add((TableReference) ref);
        for (; lexer.token() == PUNC_COMMA; ) {
            lexer.nextToken();
            ref = tableReference();
            list.add((TableReference) ref);
        }
        return new TableReferences(list);
    }
    list = new ArrayList<TableReference>(1);
    list.add((TableReference) ref);
    return new TableReferences(list);
}
Also used : TableReference(com.alibaba.cobar.parser.ast.fragment.tableref.TableReference) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement) SubqueryFactor(com.alibaba.cobar.parser.ast.fragment.tableref.SubqueryFactor) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences)

Example 8 with TableReference

use of com.alibaba.cobar.parser.ast.fragment.tableref.TableReference 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 9 with TableReference

use of com.alibaba.cobar.parser.ast.fragment.tableref.TableReference 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 TableReference

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

the class PartitionKeyVisitor method visit.

@Override
public void visit(StraightJoin node) {
    TableReference tr1 = node.getLeftTableRef();
    TableReference tr2 = node.getRightTableRef();
    Expression on = node.getOnCond();
    visitChild(1, verdictColumn, verdictGroupFunc, tr1, tr2);
    visitChild(2, verdictColumn && isVerdictPassthroughWhere(on), false, on);
}
Also used : TableReference(com.alibaba.cobar.parser.ast.fragment.tableref.TableReference) 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)

Aggregations

TableReference (com.alibaba.cobar.parser.ast.fragment.tableref.TableReference)15 Expression (com.alibaba.cobar.parser.ast.expression.Expression)12 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)11 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)10 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)10 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)9 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)9 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)9 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)9 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)9 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)9 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)9 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)9 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)9 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)9 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)9 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)9 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)9 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)9 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)9