Search in sources :

Example 1 with NullOrderingType

use of org.datanucleus.query.NullOrderingType 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.query.NullOrderingType) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) CaseExpression(org.datanucleus.query.expression.CaseExpression) BooleanSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) MapExpression(org.datanucleus.store.rdbms.sql.expression.MapExpression) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) NewObjectExpression(org.datanucleus.store.rdbms.sql.expression.NewObjectExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) Expression(org.datanucleus.query.expression.Expression) TypeExpression(org.datanucleus.query.expression.TypeExpression) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) CollectionExpression(org.datanucleus.store.rdbms.sql.expression.CollectionExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) ColumnExpression(org.datanucleus.store.rdbms.sql.expression.ColumnExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression)

Example 2 with NullOrderingType

use of org.datanucleus.query.NullOrderingType 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 {
            // TODO Cater for ResultAliasExpression, so we just put the order aliasName
            // 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 = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase(((ResultAliasExpression) orderExpr).getResultAlias());
                        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 {
                            DatastoreMapping[] mappings = orderExpr.getJavaTypeMapping().getDatastoreMappings();
                            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 = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase(((ResultAliasExpression) orderExpr).getResultAlias());
                        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.query.NullOrderingType) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression) DatastoreMapping(org.datanucleus.store.rdbms.mapping.datastore.DatastoreMapping) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter)

Aggregations

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