Search in sources :

Example 6 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 7 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 8 with SQLSelectOrderByItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem 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)

Example 9 with SQLSelectOrderByItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem in project sharding-jdbc by dangdangdotcom.

the class MySQLSelectVisitor method visit.

public boolean visit(final SQLOrderBy x) {
    for (SQLSelectOrderByItem each : x.getItems()) {
        SQLExpr expr = each.getExpr();
        OrderByType orderByType = null == each.getType() ? OrderByType.ASC : OrderByType.valueOf(each.getType());
        if (expr instanceof SQLIntegerExpr) {
            getParseContext().addOrderByColumn(((SQLIntegerExpr) expr).getNumber().intValue(), orderByType);
        } else if (expr instanceof SQLIdentifierExpr) {
            getParseContext().addOrderByColumn(Optional.<String>absent(), ((SQLIdentifierExpr) expr).getName(), orderByType);
        } else if (expr instanceof SQLPropertyExpr) {
            SQLPropertyExpr sqlPropertyExpr = (SQLPropertyExpr) expr;
            getParseContext().addOrderByColumn(Optional.of(sqlPropertyExpr.getOwner().toString()), sqlPropertyExpr.getName(), orderByType);
        }
    }
    return super.visit(x);
}
Also used : SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) OrderByType(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn.OrderByType)

Example 10 with SQLSelectOrderByItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem in project Mycat-Server by MyCATApache.

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)

Aggregations

SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)12 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)3 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)1 SQLOrderingSpecification (com.alibaba.druid.sql.ast.SQLOrderingSpecification)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)1 SQLConstraint (com.alibaba.druid.sql.ast.statement.SQLConstraint)1 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)1 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)1 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)1 SQLUpdateSetItem (com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem)1 Limit (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit)1 MySqlUpdateStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement)1 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)1 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)1 OdpsSelectQueryBlock (com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock)1