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