Search in sources :

Example 6 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 7 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 8 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 9 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)

Example 10 with SQLOrderBy

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

the class EqualTest_orderBy method test_exits.

public void test_exits() throws Exception {
    String sql = "ORDER BY f1";
    String sql_c = "ORDER BY f2";
    SQLOrderBy exprA, exprB, exprC;
    {
        SQLExprParser parser = new SQLExprParser(sql);
        exprA = (SQLOrderBy) parser.parseOrderBy();
    }
    {
        SQLExprParser parser = new SQLExprParser(sql);
        exprB = (SQLOrderBy) parser.parseOrderBy();
    }
    {
        SQLExprParser parser = new SQLExprParser(sql_c);
        exprC = (SQLOrderBy) parser.parseOrderBy();
    }
    Assert.assertEquals(exprA, exprB);
    Assert.assertNotEquals(exprA, exprC);
    Assert.assertTrue(exprA.equals(exprA));
    Assert.assertFalse(exprA.equals(new Object()));
    Assert.assertEquals(exprA.hashCode(), exprB.hashCode());
    Assert.assertEquals(new SQLOrderBy(), new SQLOrderBy());
    Assert.assertEquals(new SQLOrderBy().hashCode(), new SQLOrderBy().hashCode());
    Assert.assertEquals(new SQLSelectOrderByItem(), new SQLSelectOrderByItem());
    Assert.assertEquals(new SQLSelectOrderByItem().hashCode(), new SQLSelectOrderByItem().hashCode());
}
Also used : SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLExprParser(com.alibaba.druid.sql.parser.SQLExprParser)

Aggregations

SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)16 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)8 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)6 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)6 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)5 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)5 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)5 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 SQLOver (com.alibaba.druid.sql.ast.SQLOver)3 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)3 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)3 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)3 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)3 SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)3 DB2SelectQueryBlock (com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock)3 OracleSelect (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect)3 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)3 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)2 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)2