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