Search in sources :

Example 1 with SQLOrderBy

use of com.alibaba.druid.sql.ast.SQLOrderBy in project druid by alibaba.

the class MySqlStatementParser method parseDeleteStatement.

public MySqlDeleteStatement parseDeleteStatement() {
    MySqlDeleteStatement deleteStatement = new MySqlDeleteStatement();
    if (lexer.token() == Token.DELETE) {
        lexer.nextToken();
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
        }
        if (lexer.token() == Token.HINT) {
            this.getExprParser().parseHints(deleteStatement.getHints());
        }
        if (identifierEquals(LOW_PRIORITY)) {
            deleteStatement.setLowPriority(true);
            lexer.nextToken();
        }
        if (identifierEquals("QUICK")) {
            deleteStatement.setQuick(true);
            lexer.nextToken();
        }
        if (identifierEquals(IGNORE)) {
            deleteStatement.setIgnore(true);
            lexer.nextToken();
        }
        if (lexer.token() == Token.IDENTIFIER) {
            deleteStatement.setTableSource(createSQLSelectParser().parseTableSource());
            if (lexer.token() == Token.FROM) {
                lexer.nextToken();
                SQLTableSource tableSource = createSQLSelectParser().parseTableSource();
                deleteStatement.setFrom(tableSource);
            }
        } else if (lexer.token() == Token.FROM) {
            lexer.nextToken();
            deleteStatement.setTableSource(createSQLSelectParser().parseTableSource());
        } else {
            throw new ParserException("syntax error");
        }
        if (identifierEquals("USING")) {
            lexer.nextToken();
            SQLTableSource tableSource = createSQLSelectParser().parseTableSource();
            deleteStatement.setUsing(tableSource);
        }
    }
    if (lexer.token() == (Token.WHERE)) {
        lexer.nextToken();
        SQLExpr where = this.exprParser.expr();
        deleteStatement.setWhere(where);
    }
    if (lexer.token() == (Token.ORDER)) {
        SQLOrderBy orderBy = exprParser.parseOrderBy();
        deleteStatement.setOrderBy(orderBy);
    }
    deleteStatement.setLimit(this.exprParser.parseLimit());
    return deleteStatement;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) MySqlDeleteStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 2 with SQLOrderBy

use of com.alibaba.druid.sql.ast.SQLOrderBy in project druid by alibaba.

the class MySqlExprParser method parseAggregateExprRest.

protected SQLAggregateExpr parseAggregateExprRest(SQLAggregateExpr aggregateExpr) {
    if (lexer.token() == Token.ORDER) {
        SQLOrderBy orderBy = this.parseOrderBy();
        aggregateExpr.putAttribute("ORDER BY", orderBy);
    }
    if (identifierEquals("SEPARATOR")) {
        lexer.nextToken();
        SQLExpr seperator = this.primary();
        aggregateExpr.putAttribute("SEPARATOR", seperator);
    }
    return aggregateExpr;
}
Also used : SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 3 with SQLOrderBy

use of com.alibaba.druid.sql.ast.SQLOrderBy in project druid by alibaba.

the class DB2SelectParser method query.

@Override
public SQLSelectQuery query() {
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLSelectQuery select = query();
        accept(Token.RPAREN);
        return queryRest(select);
    }
    accept(Token.SELECT);
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
    }
    DB2SelectQueryBlock queryBlock = new DB2SelectQueryBlock();
    if (lexer.token() == Token.DISTINCT) {
        queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
        lexer.nextToken();
    } else if (lexer.token() == Token.UNIQUE) {
        queryBlock.setDistionOption(SQLSetQuantifier.UNIQUE);
        lexer.nextToken();
    } else if (lexer.token() == Token.ALL) {
        queryBlock.setDistionOption(SQLSetQuantifier.ALL);
        lexer.nextToken();
    }
    parseSelectList(queryBlock);
    parseFrom(queryBlock);
    parseWhere(queryBlock);
    parseGroupBy(queryBlock);
    if (lexer.token() == Token.ORDER) {
        SQLOrderBy orderBy = parseOrderBy();
        queryBlock.setOrderBy(orderBy);
    }
    for (; ; ) {
        if (lexer.token() == Token.FETCH) {
            lexer.nextToken();
            accept(Token.FIRST);
            SQLExpr first = this.exprParser.primary();
            queryBlock.setFirst(first);
            if (identifierEquals("ROW") || identifierEquals("ROWS")) {
                lexer.nextToken();
            }
            accept(Token.ONLY);
            continue;
        }
        if (lexer.token() == Token.WITH) {
            lexer.nextToken();
            if (identifierEquals("RR")) {
                queryBlock.setIsolation(Isolation.RR);
            } else if (identifierEquals("RS")) {
                queryBlock.setIsolation(Isolation.RS);
            } else if (identifierEquals("CS")) {
                queryBlock.setIsolation(Isolation.CS);
            } else if (identifierEquals("UR")) {
                queryBlock.setIsolation(Isolation.UR);
            } else {
                throw new ParserException("TODO");
            }
            lexer.nextToken();
            continue;
        }
        if (lexer.token() == Token.FOR) {
            lexer.nextToken();
            if (lexer.token() == Token.UPDATE) {
                queryBlock.setForUpdate(true);
                lexer.nextToken();
            } else {
                acceptIdentifier("READ");
                accept(Token.ONLY);
                queryBlock.setForReadOnly(true);
            }
        }
        if (lexer.token() == Token.OPTIMIZE) {
            lexer.nextToken();
            accept(Token.FOR);
            queryBlock.setOptimizeFor(this.expr());
            if (identifierEquals("ROW")) {
                lexer.nextToken();
            } else {
                acceptIdentifier("ROWS");
            }
        }
        break;
    }
    return queryRest(queryBlock);
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) DB2SelectQueryBlock(com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 4 with SQLOrderBy

use of com.alibaba.druid.sql.ast.SQLOrderBy in project druid by alibaba.

the class OracleExprParser method parseAggregateExpr.

protected SQLAggregateExpr parseAggregateExpr(String methodName) {
    methodName = methodName.toUpperCase();
    SQLAggregateExpr aggregateExpr;
    if (lexer.token() == Token.UNIQUE) {
        aggregateExpr = new SQLAggregateExpr(methodName, SQLAggregateOption.UNIQUE);
        lexer.nextToken();
    } else if (lexer.token() == (Token.ALL)) {
        aggregateExpr = new SQLAggregateExpr(methodName, SQLAggregateOption.ALL);
        lexer.nextToken();
    } else if (lexer.token() == (Token.DISTINCT)) {
        aggregateExpr = new SQLAggregateExpr(methodName, SQLAggregateOption.DISTINCT);
        lexer.nextToken();
    } else {
        aggregateExpr = new SQLAggregateExpr(methodName);
    }
    exprList(aggregateExpr.getArguments(), aggregateExpr);
    if (lexer.stringVal().equalsIgnoreCase("IGNORE")) {
        lexer.nextToken();
        identifierEquals("NULLS");
        aggregateExpr.setIgnoreNulls(true);
    }
    accept(Token.RPAREN);
    if (identifierEquals("WITHIN")) {
        lexer.nextToken();
        accept(Token.GROUP);
        accept(Token.LPAREN);
        SQLOrderBy withinGroup = this.parseOrderBy();
        aggregateExpr.setWithinGroup(withinGroup);
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.KEEP) {
        lexer.nextToken();
        SQLKeep keep = new SQLKeep();
        accept(Token.LPAREN);
        acceptIdentifier("DENSE_RANK");
        if (identifierEquals("FIRST")) {
            lexer.nextToken();
            keep.setDenseRank(DenseRank.FIRST);
        } else {
            acceptIdentifier("LAST");
            keep.setDenseRank(DenseRank.LAST);
        }
        SQLOrderBy orderBy = this.parseOrderBy();
        keep.setOrderBy(orderBy);
        aggregateExpr.setKeep(keep);
        accept(Token.RPAREN);
    }
    if (lexer.token() == Token.OVER) {
        OracleAnalytic over = new OracleAnalytic();
        lexer.nextToken();
        accept(Token.LPAREN);
        if (identifierEquals("PARTITION")) {
            lexer.nextToken();
            accept(Token.BY);
            if (lexer.token() == (Token.LPAREN)) {
                lexer.nextToken();
                exprList(over.getPartitionBy(), over);
                accept(Token.RPAREN);
            } else {
                exprList(over.getPartitionBy(), over);
            }
        }
        over.setOrderBy(parseOrderBy());
        if (over.getOrderBy() != null) {
            OracleAnalyticWindowing windowing = null;
            if (lexer.stringVal().equalsIgnoreCase("ROWS")) {
                lexer.nextToken();
                windowing = new OracleAnalyticWindowing();
                windowing.setType(OracleAnalyticWindowing.Type.ROWS);
            } else if (lexer.stringVal().equalsIgnoreCase("RANGE")) {
                lexer.nextToken();
                windowing = new OracleAnalyticWindowing();
                windowing.setType(OracleAnalyticWindowing.Type.RANGE);
            }
            if (windowing != null) {
                if (lexer.stringVal().equalsIgnoreCase("CURRENT")) {
                    lexer.nextToken();
                    if (lexer.stringVal().equalsIgnoreCase("ROW")) {
                        lexer.nextToken();
                        windowing.setExpr(new SQLIdentifierExpr("CURRENT ROW"));
                        over.setWindowing(windowing);
                    }
                    throw new ParserException("syntax error");
                }
                if (lexer.stringVal().equalsIgnoreCase("UNBOUNDED")) {
                    lexer.nextToken();
                    if (lexer.stringVal().equalsIgnoreCase("PRECEDING")) {
                        lexer.nextToken();
                        windowing.setExpr(new SQLIdentifierExpr("UNBOUNDED PRECEDING"));
                    } else {
                        throw new ParserException("syntax error");
                    }
                }
                over.setWindowing(windowing);
            }
        }
        accept(Token.RPAREN);
        aggregateExpr.setOver(over);
    }
    return aggregateExpr;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLKeep(com.alibaba.druid.sql.ast.SQLKeep) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) OracleAnalytic(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalytic) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr) OracleAnalyticWindowing(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalyticWindowing)

Example 5 with SQLOrderBy

use of com.alibaba.druid.sql.ast.SQLOrderBy in project druid by alibaba.

the class OracleSelectParser method select.

public OracleSelect select() {
    OracleSelect select = new OracleSelect();
    withSubquery(select);
    SQLSelectQuery query = query();
    select.setQuery(query);
    SQLOrderBy orderBy = this.parseOrderBy();
    select.setOrderBy(orderBy);
    if (orderBy != null && query instanceof SQLSelectQueryBlock) {
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
        parseFetchClause(queryBlock);
    }
    if (lexer.token() == (Token.FOR)) {
        lexer.nextToken();
        accept(Token.UPDATE);
        OracleSelectForUpdate forUpdate = new OracleSelectForUpdate();
        if (lexer.token() == Token.OF) {
            lexer.nextToken();
            this.exprParser.exprList(forUpdate.getOf(), forUpdate);
        }
        if (lexer.token() == Token.NOWAIT) {
            lexer.nextToken();
            forUpdate.setNotWait(true);
        } else if (lexer.token() == Token.WAIT) {
            lexer.nextToken();
            forUpdate.setWait(this.exprParser.primary());
        } else if (identifierEquals("SKIP")) {
            lexer.nextToken();
            acceptIdentifier("LOCKED");
            forUpdate.setSkipLocked(true);
        }
        select.setForUpdate(forUpdate);
    }
    if (select.getOrderBy() == null) {
        select.setOrderBy(this.exprParser.parseOrderBy());
    }
    if (lexer.token() == Token.WITH) {
        lexer.nextToken();
        if (identifierEquals("READ")) {
            lexer.nextToken();
            if (identifierEquals("ONLY")) {
                lexer.nextToken();
            } else {
                throw new ParserException("syntax error");
            }
            select.setRestriction(new OracleSelectRestriction.ReadOnly());
        } else if (lexer.token() == (Token.CHECK)) {
            lexer.nextToken();
            if (identifierEquals("OPTION")) {
                lexer.nextToken();
            } else {
                throw new ParserException("syntax error");
            }
            OracleSelectRestriction.CheckOption checkOption = new OracleSelectRestriction.CheckOption();
            if (lexer.token() == Token.CONSTRAINT) {
                lexer.nextToken();
                throw new ParserException("TODO");
            }
            select.setRestriction(checkOption);
        } else {
            throw new ParserException("syntax error");
        }
    }
    return select;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) OracleSelect(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect) OracleSelectForUpdate(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectForUpdate) OracleSelectRestriction(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectRestriction)

Aggregations

SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)28 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)17 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)11 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)10 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)10 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)8 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)8 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)8 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)6 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)6 SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)5 DB2SelectQueryBlock (com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock)5 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)5 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)5 SQLOver (com.alibaba.druid.sql.ast.SQLOver)4 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)4 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)4 OracleSelect (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 List (java.util.List)4