Search in sources :

Example 1 with ResultAliasExpression

use of org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression in project datanucleus-rdbms by datanucleus.

the class SelectStatement method generateOrderingStatement.

/**
 * Convenience method to generate the ordering statement to add to the overall query statement.
 * @return The ordering statement
 */
protected SQLText generateOrderingStatement() {
    SQLText orderStmt = null;
    if (orderingExpressions != null && orderingExpressions.length > 0) {
        DatastoreAdapter dba = getDatastoreAdapter();
        if (dba.supportsOption(DatastoreAdapter.ORDERBY_USING_SELECT_COLUMN_INDEX)) {
            // Order using the indexes of the ordering columns in the SELECT
            orderStmt = new SQLText();
            for (int i = 0; i < orderingExpressions.length; ++i) {
                if (i > 0) {
                    orderStmt.append(',');
                }
                orderStmt.append(Integer.toString(orderingColumnIndexes[i]));
                if (orderingDirections[i]) {
                    orderStmt.append(" DESC");
                }
                if (orderNullDirectives != null && orderNullDirectives[i] != null && dba.supportsOption(DatastoreAdapter.ORDERBY_NULLS_DIRECTIVES)) {
                    // Apply "NULLS [FIRST | LAST]" since supported by this datastore
                    orderStmt.append(" " + (orderNullDirectives[i] == NullOrderingType.NULLS_FIRST ? "NULLS FIRST" : "NULLS LAST"));
                }
            }
        } else {
            // Order using column aliases "NUCORDER{i}"
            orderStmt = new SQLText();
            boolean needsSelect = dba.supportsOption(DatastoreAdapter.INCLUDE_ORDERBY_COLS_IN_SELECT);
            if (parent != null) {
                // Don't select ordering columns with subqueries, since we will select just the required column(s)
                needsSelect = false;
            }
            for (int i = 0; i < orderingExpressions.length; ++i) {
                SQLExpression orderExpr = orderingExpressions[i];
                boolean orderDirection = orderingDirections[i];
                NullOrderingType orderNullDirective = (orderNullDirectives != null ? orderNullDirectives[i] : null);
                if (i > 0) {
                    orderStmt.append(',');
                }
                if (needsSelect && !aggregated) {
                    if (orderExpr instanceof ResultAliasExpression) {
                        String orderStr = ((ResultAliasExpression) orderExpr).getResultAlias();
                        // make sure it is truncated for the datastore limits
                        orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierTruncatedToAdapterColumnLength(orderStr);
                        // put it in the case of the datastore
                        orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase(orderStr);
                        addOrderComponent(orderStmt, orderStr, orderExpr, orderDirection, orderNullDirective, dba);
                    } else {
                        // Order by the "NUCORDER?" if we need them to be selected and it isn't an aggregate
                        String orderString = "NUCORDER" + i;
                        if (orderExpr.getNumberOfSubExpressions() == 1) {
                            String orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase(orderString);
                            addOrderComponent(orderStmt, orderStr, orderExpr, orderDirection, orderNullDirective, dba);
                        } else {
                            ColumnMapping[] mappings = orderExpr.getJavaTypeMapping().getColumnMappings();
                            for (int j = 0; j < mappings.length; j++) {
                                String orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase(orderString + "_" + j);
                                addOrderComponent(orderStmt, orderStr, orderExpr, orderDirection, orderNullDirective, dba);
                                if (j < mappings.length - 1) {
                                    orderStmt.append(',');
                                }
                            }
                        }
                    }
                } else {
                    if (orderExpr instanceof ResultAliasExpression) {
                        String orderStr = ((ResultAliasExpression) orderExpr).getResultAlias();
                        // make sure it is truncated for the datastore limits
                        orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierTruncatedToAdapterColumnLength(orderStr);
                        // put it in the case of the datastore
                        orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase(orderStr);
                        addOrderComponent(orderStmt, orderStr, orderExpr, orderDirection, orderNullDirective, dba);
                    } else {
                        // Order by the "THIS.COLUMN" otherwise
                        addOrderComponent(orderStmt, orderExpr.toSQLText().toSQL(), orderExpr, orderDirection, orderNullDirective, dba);
                    }
                }
            }
        }
    }
    return orderStmt;
}
Also used : NullOrderingType(org.datanucleus.store.query.NullOrderingType) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) ColumnMapping(org.datanucleus.store.rdbms.mapping.column.ColumnMapping)

Example 2 with ResultAliasExpression

use of org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression in project datanucleus-rdbms by datanucleus.

the class SelectStatement method addOrderingColumnsToSelect.

/**
 * Convenience method to add any necessary columns to the SELECT that are needed
 * by the ordering constraint.
 */
protected void addOrderingColumnsToSelect() {
    if (// Don't do this for subqueries, since we will be selecting just the necessary column(s)
    orderingExpressions != null && parent == null) {
        // Add any ordering columns to the SELECT
        DatastoreAdapter dba = getDatastoreAdapter();
        if (dba.supportsOption(DatastoreAdapter.ORDERBY_USING_SELECT_COLUMN_INDEX)) {
            // Order using the indexes of the ordering columns in the SELECT
            orderingColumnIndexes = new int[orderingExpressions.length];
            // Add the ordering columns to the selected list, saving the positions
            for (int i = 0; i < orderingExpressions.length; ++i) {
                orderingColumnIndexes[i] = selectItem(orderingExpressions[i].toSQLText(), null, !aggregated);
                if (unions != null && allowUnions) {
                    Iterator<SelectStatement> iterator = unions.iterator();
                    while (iterator.hasNext()) {
                        SelectStatement stmt = iterator.next();
                        stmt.selectItem(orderingExpressions[i].toSQLText(), null, !aggregated);
                    }
                }
            }
        } else if (dba.supportsOption(DatastoreAdapter.INCLUDE_ORDERBY_COLS_IN_SELECT)) {
            // Order using column aliases "NUCORDER{i}"
            for (int i = 0; i < orderingExpressions.length; ++i) {
                if (orderingExpressions[i] instanceof ResultAliasExpression) {
                // Nothing to do since this is ordering by a result alias
                } else if (orderingExpressions[i].getNumberOfSubExpressions() == 1 || aggregated) {
                    String orderExprAlias = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase("NUCORDER" + i);
                    if (unions != null && allowUnions) {
                        Iterator<SelectStatement> iterator = unions.iterator();
                        while (iterator.hasNext()) {
                            SelectStatement stmt = iterator.next();
                            stmt.selectItem(orderingExpressions[i].toSQLText(), aggregated ? null : orderExprAlias, !aggregated);
                        }
                    }
                    selectItem(orderingExpressions[i].toSQLText(), aggregated ? null : orderExprAlias, !aggregated);
                } else {
                    JavaTypeMapping m = orderingExpressions[i].getJavaTypeMapping();
                    ColumnMapping[] mappings = m.getColumnMappings();
                    for (int j = 0; j < mappings.length; j++) {
                        String alias = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase("NUCORDER" + i + "_" + j);
                        DatastoreIdentifier aliasId = rdbmsMgr.getIdentifierFactory().newColumnIdentifier(alias);
                        SQLColumn col = new SQLColumn(orderingExpressions[i].getSQLTable(), mappings[j].getColumn(), aliasId);
                        selectItem(new SQLText(col.getColumnSelectString()), alias, !aggregated);
                        if (unions != null && allowUnions) {
                            Iterator<SelectStatement> iterator = unions.iterator();
                            while (iterator.hasNext()) {
                                SelectStatement stmt = iterator.next();
                                stmt.selectItem(new SQLText(col.getColumnSelectString()), alias, !aggregated);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) ColumnMapping(org.datanucleus.store.rdbms.mapping.column.ColumnMapping)

Example 3 with ResultAliasExpression

use of org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression in project datanucleus-rdbms by datanucleus.

the class QueryToSQLMapper method compileOrdering.

/**
 * Method to compile the ordering clause of the query into the SQLStatement.
 * @param stmt SELECT statement
 */
protected void compileOrdering(SelectStatement stmt) {
    if (compilation.getExprOrdering() != null) {
        compileComponent = CompilationComponent.ORDERING;
        Expression[] orderingExpr = compilation.getExprOrdering();
        SQLExpression[] orderSqlExprs = new SQLExpression[orderingExpr.length];
        boolean[] directions = new boolean[orderingExpr.length];
        NullOrderingType[] nullOrders = new NullOrderingType[orderingExpr.length];
        for (int i = 0; i < orderingExpr.length; i++) {
            OrderExpression orderExpr = (OrderExpression) orderingExpr[i];
            Expression expr = orderExpr.getLeft();
            if (expr instanceof PrimaryExpression) {
                PrimaryExpression orderPrimExpr = (PrimaryExpression) expr;
                if (orderPrimExpr.getTuples().size() == 1 && resultAliases != null) {
                    if (resultAliases.contains(orderPrimExpr.getId().toLowerCase())) {
                        // Order by a result alias
                        orderSqlExprs[i] = new ResultAliasExpression(stmt, orderPrimExpr.getId());
                    }
                }
            }
            if (orderSqlExprs[i] == null) {
                orderSqlExprs[i] = (SQLExpression) orderExpr.getLeft().evaluate(this);
            }
            String orderDir = orderExpr.getSortOrder();
            directions[i] = ((orderDir == null || orderDir.equals("ascending")) ? false : true);
            nullOrders[i] = orderExpr.getNullOrder();
        }
        stmt.setOrdering(orderSqlExprs, directions, nullOrders);
        compileComponent = null;
    }
}
Also used : NullOrderingType(org.datanucleus.store.query.NullOrderingType) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) PrimaryExpression(org.datanucleus.store.query.expression.PrimaryExpression) PrimaryExpression(org.datanucleus.store.query.expression.PrimaryExpression) BooleanSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression) TypeConverterExpression(org.datanucleus.store.rdbms.sql.expression.TypeConverterExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) MapExpression(org.datanucleus.store.rdbms.sql.expression.MapExpression) ArrayExpression(org.datanucleus.store.query.expression.ArrayExpression) CaseExpression(org.datanucleus.store.query.expression.CaseExpression) CreatorExpression(org.datanucleus.store.query.expression.CreatorExpression) NewObjectExpression(org.datanucleus.store.rdbms.sql.expression.NewObjectExpression) OrderExpression(org.datanucleus.store.query.expression.OrderExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) ClassExpression(org.datanucleus.store.query.expression.ClassExpression) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) Expression(org.datanucleus.store.query.expression.Expression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) ParameterExpression(org.datanucleus.store.query.expression.ParameterExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) SubqueryExpression(org.datanucleus.store.query.expression.SubqueryExpression) ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression) VariableExpression(org.datanucleus.store.query.expression.VariableExpression) TypeExpression(org.datanucleus.store.query.expression.TypeExpression) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) CollectionExpression(org.datanucleus.store.rdbms.sql.expression.CollectionExpression) DyadicExpression(org.datanucleus.store.query.expression.DyadicExpression) ColumnExpression(org.datanucleus.store.rdbms.sql.expression.ColumnExpression) JoinExpression(org.datanucleus.store.query.expression.JoinExpression) InvokeExpression(org.datanucleus.store.query.expression.InvokeExpression) OrderExpression(org.datanucleus.store.query.expression.OrderExpression) ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression)

Aggregations

ResultAliasExpression (org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression)3 NullOrderingType (org.datanucleus.store.query.NullOrderingType)2 DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)2 ColumnMapping (org.datanucleus.store.rdbms.mapping.column.ColumnMapping)2 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)2 ArrayExpression (org.datanucleus.store.query.expression.ArrayExpression)1 CaseExpression (org.datanucleus.store.query.expression.CaseExpression)1 ClassExpression (org.datanucleus.store.query.expression.ClassExpression)1 CreatorExpression (org.datanucleus.store.query.expression.CreatorExpression)1 DyadicExpression (org.datanucleus.store.query.expression.DyadicExpression)1 Expression (org.datanucleus.store.query.expression.Expression)1 InvokeExpression (org.datanucleus.store.query.expression.InvokeExpression)1 JoinExpression (org.datanucleus.store.query.expression.JoinExpression)1 OrderExpression (org.datanucleus.store.query.expression.OrderExpression)1 ParameterExpression (org.datanucleus.store.query.expression.ParameterExpression)1 PrimaryExpression (org.datanucleus.store.query.expression.PrimaryExpression)1 SubqueryExpression (org.datanucleus.store.query.expression.SubqueryExpression)1 TypeExpression (org.datanucleus.store.query.expression.TypeExpression)1 VariableExpression (org.datanucleus.store.query.expression.VariableExpression)1 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)1