Search in sources :

Example 1 with OracleAnalytic

use of com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalytic 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)

Aggregations

SQLKeep (com.alibaba.druid.sql.ast.SQLKeep)1 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)1 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 OracleAnalytic (com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalytic)1 OracleAnalyticWindowing (com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAnalyticWindowing)1 ParserException (com.alibaba.druid.sql.parser.ParserException)1