Search in sources :

Example 1 with SQLSelectOrderByItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem in project druid by alibaba.

the class SQLUtils method toOrderByItem.

public static SQLSelectOrderByItem toOrderByItem(String sql, String dbType) {
    SQLExprParser parser = SQLParserUtils.createExprParser(sql, dbType);
    SQLSelectOrderByItem orderByItem = parser.parseSelectOrderByItem();
    if (parser.getLexer().token() != Token.EOF) {
        throw new ParserException("illegal sql expr : " + sql);
    }
    return orderByItem;
}
Also used : SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)

Example 2 with SQLSelectOrderByItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem in project druid by alibaba.

the class OdpsSelectParser method query.

@Override
public SQLSelectQuery query() {
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLSelectQuery select = query();
        accept(Token.RPAREN);
        return queryRest(select);
    }
    OdpsSelectQueryBlock queryBlock = new OdpsSelectQueryBlock();
    if (lexer.hasComment() && lexer.isKeepComments()) {
        queryBlock.addBeforeComment(lexer.readAndResetComments());
    }
    accept(Token.SELECT);
    if (lexer.token() == Token.HINT) {
        this.exprParser.parseHints(queryBlock.getHints());
    }
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
    }
    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);
    queryBlock.setOrderBy(this.exprParser.parseOrderBy());
    if (lexer.token() == Token.DISTRIBUTE) {
        lexer.nextToken();
        accept(Token.BY);
        this.exprParser.exprList(queryBlock.getDistributeBy(), queryBlock);
        if (identifierEquals("SORT")) {
            lexer.nextToken();
            accept(Token.BY);
            for (; ; ) {
                SQLExpr expr = this.expr();
                SQLSelectOrderByItem sortByItem = new SQLSelectOrderByItem(expr);
                if (lexer.token() == Token.ASC) {
                    sortByItem.setType(SQLOrderingSpecification.ASC);
                    lexer.nextToken();
                } else if (lexer.token() == Token.DESC) {
                    sortByItem.setType(SQLOrderingSpecification.DESC);
                    lexer.nextToken();
                }
                queryBlock.getSortBy().add(sortByItem);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                } else {
                    break;
                }
            }
        }
    }
    if (lexer.token() == Token.LIMIT) {
        lexer.nextToken();
        queryBlock.setLimit(new SQLLimit(this.expr()));
    }
    return queryRest(queryBlock);
}
Also used : SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) OdpsSelectQueryBlock(com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 3 with SQLSelectOrderByItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem in project druid by alibaba.

the class SQLExprParser method parseSelectOrderByItem.

public SQLSelectOrderByItem parseSelectOrderByItem() {
    SQLSelectOrderByItem item = new SQLSelectOrderByItem();
    item.setExpr(expr());
    if (lexer.token() == Token.ASC) {
        lexer.nextToken();
        item.setType(SQLOrderingSpecification.ASC);
    } else if (lexer.token() == Token.DESC) {
        lexer.nextToken();
        item.setType(SQLOrderingSpecification.DESC);
    }
    if (identifierEquals("NULLS")) {
        lexer.nextToken();
        if (identifierEquals("FIRST")) {
            lexer.nextToken();
            item.setNullsOrderType(SQLSelectOrderByItem.NullsOrderType.NullsFirst);
        } else if (identifierEquals("LAST")) {
            lexer.nextToken();
            item.setNullsOrderType(SQLSelectOrderByItem.NullsOrderType.NullsLast);
        } else {
            throw new ParserException("TODO " + lexer.token());
        }
    }
    return item;
}
Also used : SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)

Example 4 with SQLSelectOrderByItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem in project Mycat_plus by coderczp.

the class DruidSelectOracleParser method parseOrderAggGroupOracle.

protected void parseOrderAggGroupOracle(SQLStatement stmt, RouteResultset rrs, OracleSelectQueryBlock mysqlSelectQuery, SchemaConfig schema) {
    Map<String, String> aliaColumns = parseAggGroupCommon(schema, stmt, rrs, mysqlSelectQuery);
    OracleSelect oracleSelect = (OracleSelect) mysqlSelectQuery.getParent();
    if (oracleSelect.getOrderBy() != null) {
        List<SQLSelectOrderByItem> orderByItems = oracleSelect.getOrderBy().getItems();
        rrs.setOrderByCols(buildOrderByCols(orderByItems, aliaColumns));
    }
    isNeedParseOrderAgg = false;
}
Also used : OracleSelect(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)

Example 5 with SQLSelectOrderByItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem in project Mycat_plus by coderczp.

the class DruidSelectSqlServerParser method parseOrderAggGroupSqlServer.

private void parseOrderAggGroupSqlServer(SchemaConfig schema, SQLStatement stmt, RouteResultset rrs, SQLServerSelectQueryBlock mysqlSelectQuery) {
    Map<String, String> aliaColumns = parseAggGroupCommon(schema, stmt, rrs, mysqlSelectQuery);
    SQLServerSelect oracleSelect = (SQLServerSelect) mysqlSelectQuery.getParent();
    if (oracleSelect.getOrderBy() != null) {
        List<SQLSelectOrderByItem> orderByItems = oracleSelect.getOrderBy().getItems();
        rrs.setOrderByCols(buildOrderByCols(orderByItems, aliaColumns));
    }
}
Also used : SQLServerSelect(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelect) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)

Aggregations

SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)34 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)11 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)5 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLListExpr (com.alibaba.druid.sql.ast.expr.SQLListExpr)3 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)3 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)3 SQLConstraint (com.alibaba.druid.sql.ast.statement.SQLConstraint)3 SQLSelectGroupByClause (com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause)3 Order (com.actiontech.dble.plan.Order)2 SQLExprImpl (com.alibaba.druid.sql.ast.SQLExprImpl)2 SQLName (com.alibaba.druid.sql.ast.SQLName)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLOrderingSpecification (com.alibaba.druid.sql.ast.SQLOrderingSpecification)2 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)2 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)2 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)2 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)2 SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)2