Search in sources :

Example 6 with TableReferences

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

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

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

Aggregations

TableReferences (com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences)8 Expression (com.alibaba.cobar.parser.ast.expression.Expression)6 Limit (com.alibaba.cobar.parser.ast.fragment.Limit)5 OrderBy (com.alibaba.cobar.parser.ast.fragment.OrderBy)5 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)4 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)3 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)3 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)3 SubqueryFactor (com.alibaba.cobar.parser.ast.fragment.tableref.SubqueryFactor)3 TableReference (com.alibaba.cobar.parser.ast.fragment.tableref.TableReference)3 DMLSelectStatement (com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement)3 Pair (com.alibaba.cobar.parser.util.Pair)3 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)2 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)2 TernaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression)2 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)2 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)2 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)2 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)2 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)2