Search in sources :

Example 1 with OrderExpression

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

use of org.datanucleus.query.expression.OrderExpression in project datanucleus-core by datanucleus.

the class QueryUtils method orderCandidates.

/**
 * Convenience method to order the input List of objects to the ordering defined by the compilation.
 * @param candidates Candidates
 * @param ordering Ordering expression(s)
 * @param state Map of state information (see JavaQueryEvaluator)
 * @param candidateAlias Candidate alias
 * @param ec ExecutionContext
 * @param clr ClassLoader resolver
 * @param parameterValues Any parameter values (maybe used by the ordering clause)
 * @param imports Imports for the query
 * @param queryLanguage The language of this query (JDOQL, JPQL etc)
 * @return The ordered List of candidates
 */
public static List orderCandidates(List candidates, final Expression[] ordering, final Map state, final String candidateAlias, final ExecutionContext ec, final ClassLoaderResolver clr, final Map parameterValues, final Imports imports, final String queryLanguage) {
    if (ordering == null) {
        // Nothing to do
        return candidates;
    }
    Object[] o = candidates.toArray();
    Arrays.sort(o, new Comparator() {

        public int compare(Object obj1, Object obj2) {
            for (int i = 0; i < ordering.length; i++) {
                state.put(candidateAlias, obj1);
                Object a = ordering[i].evaluate(new InMemoryExpressionEvaluator(ec, parameterValues, state, imports, clr, candidateAlias, queryLanguage));
                state.put(candidateAlias, obj2);
                Object b = ordering[i].evaluate(new InMemoryExpressionEvaluator(ec, parameterValues, state, imports, clr, candidateAlias, queryLanguage));
                if (a instanceof InMemoryFailure || b instanceof InMemoryFailure) {
                    throw new NucleusException("Error encountered in in-memory evaluation of ordering. Consult the log for details");
                }
                OrderExpression orderExpr = (OrderExpression) ordering[i];
                // Put any null values at the end
                if (a == null && b == null) {
                    return 0;
                } else if (a == null) {
                    if (orderExpr.getNullOrder() != null) {
                        // Use specified null handling
                        return orderExpr.getNullOrder() == NullOrderingType.NULLS_FIRST ? 1 : -1;
                    }
                    // Default to putting nulls at the end
                    return -1;
                } else if (b == null) {
                    if (orderExpr.getNullOrder() != null) {
                        // Use specified null handling
                        return orderExpr.getNullOrder() == NullOrderingType.NULLS_FIRST ? -1 : 1;
                    }
                    // Default to putting nulls at the end
                    return 1;
                } else {
                    int result = ((Comparable) a).compareTo(b);
                    if (result != 0) {
                        if (orderExpr.getSortOrder() == null || orderExpr.getSortOrder().equals("ascending")) {
                            // Ascending
                            return result;
                        }
                        // Descending
                        return -1 * result;
                    }
                }
            }
            return 0;
        }
    });
    return Arrays.asList(o);
}
Also used : InMemoryExpressionEvaluator(org.datanucleus.query.inmemory.InMemoryExpressionEvaluator) InMemoryFailure(org.datanucleus.query.inmemory.InMemoryFailure) OrderExpression(org.datanucleus.query.expression.OrderExpression) NucleusException(org.datanucleus.exceptions.NucleusException) Comparator(java.util.Comparator)

Example 3 with OrderExpression

use of org.datanucleus.query.expression.OrderExpression in project tests by datanucleus.

the class JDOQLCompilerTest method testOrderNulls.

/**
 * Test for order clause.
 */
public void testOrderNulls() {
    // Test use of implicit variable in filter
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, null, null, "name ASC", null, null, null, null, null, null);
        compilation = compiler.compile(null, null);
    } catch (NucleusUserException ne) {
        NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    Expression[] orderExprs1 = compilation.getExprOrdering();
    assertEquals(1, orderExprs1.length);
    assertTrue(orderExprs1[0] instanceof OrderExpression);
    OrderExpression orderExpr1 = (OrderExpression) orderExprs1[0];
    assertEquals("name", ((PrimaryExpression) orderExpr1.getLeft()).getId());
    assertEquals("ascending", orderExpr1.getSortOrder());
    assertNull(orderExpr1.getNullOrder());
    // Test use of NULLS
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, null, null, "name ASC NULLS FIRST", null, null, null, null, null, null);
        compilation = compiler.compile(null, null);
    } catch (NucleusUserException ne) {
        NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    Expression[] orderExprs2 = compilation.getExprOrdering();
    assertEquals(1, orderExprs2.length);
    assertTrue(orderExprs2[0] instanceof OrderExpression);
    OrderExpression orderExpr2 = (OrderExpression) orderExprs2[0];
    assertEquals("name", ((PrimaryExpression) orderExpr2.getLeft()).getId());
    assertEquals("ascending", orderExpr2.getSortOrder());
    assertEquals(NullOrderingType.NULLS_FIRST, orderExpr2.getNullOrder());
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Product(org.datanucleus.samples.store.Product) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

Aggregations

OrderExpression (org.datanucleus.query.expression.OrderExpression)3 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)2 Expression (org.datanucleus.query.expression.Expression)2 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)2 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)2 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)2 VariableExpression (org.datanucleus.query.expression.VariableExpression)2 Comparator (java.util.Comparator)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 NullOrderingType (org.datanucleus.query.NullOrderingType)1 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)1 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)1 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)1 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 JoinExpression (org.datanucleus.query.expression.JoinExpression)1 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)1