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;
}
}
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);
}
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());
}
Aggregations