Search in sources :

Example 26 with SQLOrderBy

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

the class SQLSelectBuilderImpl method orderBy.

@Override
public SQLSelectBuilderImpl orderBy(String... columns) {
    SQLSelect select = this.getSQLSelect();
    SQLOrderBy orderBy = select.getOrderBy();
    if (orderBy == null) {
        orderBy = createOrderBy();
        select.setOrderBy(orderBy);
    }
    for (String column : columns) {
        SQLSelectOrderByItem orderByItem = SQLUtils.toOrderByItem(column, dbType);
        orderBy.addItem(orderByItem);
    }
    return this;
}
Also used : SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy)

Example 27 with SQLOrderBy

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

the class OrderByResolve method visit.

public boolean visit(SQLSelect x) {
    SQLSelectQueryBlock queryBlock = x.getQueryBlock();
    if (queryBlock == null) {
        return super.visit(x);
    }
    if (x.getOrderBy() != null && queryBlock.isForUpdate() && queryBlock.getOrderBy() == null) {
        queryBlock.setOrderBy(x.getOrderBy());
        x.setOrderBy(null);
    }
    SQLOrderBy orderBy = queryBlock.getOrderBy();
    if (orderBy == null) {
        return super.visit(x);
    }
    if (!queryBlock.selectItemHasAllColumn(false)) {
        List<SQLSelectOrderByItem> notContainsOrderBy = new ArrayList<SQLSelectOrderByItem>();
        for (SQLSelectOrderByItem orderByItem : orderBy.getItems()) {
            SQLExpr orderByExpr = orderByItem.getExpr();
            if (orderByExpr instanceof SQLName) {
                if (((SQLName) orderByExpr).hashCode64() == DBMS_RANDOM_VALUE) {
                    continue;
                }
                long hashCode64 = ((SQLName) orderByExpr).nameHashCode64();
                SQLSelectItem selectItem = queryBlock.findSelectItem(hashCode64);
                if (selectItem == null) {
                    queryBlock.addSelectItem(orderByExpr.clone());
                }
            }
        }
        if (notContainsOrderBy.size() > 0) {
            for (SQLSelectOrderByItem orderByItem : notContainsOrderBy) {
                queryBlock.addSelectItem(orderByItem.getExpr());
            }
            OracleSelectQueryBlock queryBlock1 = new OracleSelectQueryBlock();
            queryBlock1.setFrom(queryBlock, "x");
            x.setQuery(queryBlock1);
        }
    }
    return super.visit(x);
}
Also used : SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) ArrayList(java.util.ArrayList) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 28 with SQLOrderBy

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

the class SQLRefactorVisitor method visit.

public boolean visit(SQLIdentifierExpr x) {
    TableMapping mapping = null;
    if (groupByLevel > 0 || havingLevel > 0) {
        SQLSelectQueryBlock queryBlock = null;
        for (SQLObject parent = x.getParent(); parent != null; parent = parent.getParent()) {
            if (parent instanceof SQLSelectQueryBlock) {
                queryBlock = (SQLSelectQueryBlock) parent;
                break;
            }
        }
        boolean matchAlias = false;
        if (queryBlock != null) {
            for (SQLSelectItem item : queryBlock.getSelectList()) {
                if (item.alias_hash() == x.hashCode64()) {
                    matchAlias = true;
                    break;
                }
            }
        }
        if (matchAlias) {
            SQLObject parent = x.getParent();
            if (parent instanceof SQLOrderBy || parent instanceof SQLSelectGroupByClause) {
                return false;
            }
            if (havingLevel > 0) {
                boolean agg = false;
                for (; parent != null; parent = parent.getParent()) {
                    if (parent instanceof SQLSelectQueryBlock) {
                        break;
                    }
                    if (parent instanceof SQLAggregateExpr) {
                        agg = true;
                        break;
                    }
                }
                if (!agg) {
                    return false;
                }
            }
        }
    }
    SQLObject ownerObject = x.getResolvedOwnerObject();
    if (ownerObject instanceof SQLExprTableSource) {
        mapping = findMapping((SQLExprTableSource) ownerObject);
    }
    if (mapping == null) {
        return false;
    }
    String srcName = x.getName();
    String mappingColumn = mapping.getMappingColumn(srcName);
    if (mappingColumn != null) {
        x.setName(quote(mappingColumn));
    }
    SQLObject parent = x.getParent();
    if (parent instanceof SQLSelectItem && ((SQLSelectItem) parent).getAlias() == null) {
        ((SQLSelectItem) parent).setAlias(srcName);
    }
    return false;
}
Also used : SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelectGroupByClause(com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

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