Search in sources :

Example 1 with OrderExpression

use of org.datanucleus.store.query.expression.OrderExpression 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

NullOrderingType (org.datanucleus.store.query.NullOrderingType)1 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 BooleanExpression (org.datanucleus.store.rdbms.sql.expression.BooleanExpression)1 BooleanSubqueryExpression (org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression)1 CollectionExpression (org.datanucleus.store.rdbms.sql.expression.CollectionExpression)1 ColumnExpression (org.datanucleus.store.rdbms.sql.expression.ColumnExpression)1 MapExpression (org.datanucleus.store.rdbms.sql.expression.MapExpression)1