Search in sources :

Example 1 with Dual

use of com.alibaba.cobar.parser.ast.fragment.tableref.Dual 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)1 GroupBy (com.alibaba.cobar.parser.ast.fragment.GroupBy)1 Limit (com.alibaba.cobar.parser.ast.fragment.Limit)1 OrderBy (com.alibaba.cobar.parser.ast.fragment.OrderBy)1 Dual (com.alibaba.cobar.parser.ast.fragment.tableref.Dual)1 TableReference (com.alibaba.cobar.parser.ast.fragment.tableref.TableReference)1 TableReferences (com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences)1 DMLSelectStatement (com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement)1 Pair (com.alibaba.cobar.parser.util.Pair)1 ArrayList (java.util.ArrayList)1