use of org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression 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 {
// 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 = ((ResultAliasExpression) orderExpr).getResultAlias();
// make sure it is truncated for the datastore limits
orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierTruncatedToAdapterColumnLength(orderStr);
// put it in the case of the datastore
orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase(orderStr);
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 {
ColumnMapping[] mappings = orderExpr.getJavaTypeMapping().getColumnMappings();
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 = ((ResultAliasExpression) orderExpr).getResultAlias();
// make sure it is truncated for the datastore limits
orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierTruncatedToAdapterColumnLength(orderStr);
// put it in the case of the datastore
orderStr = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase(orderStr);
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;
}
use of org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression in project datanucleus-rdbms by datanucleus.
the class SelectStatement method addOrderingColumnsToSelect.
/**
* Convenience method to add any necessary columns to the SELECT that are needed
* by the ordering constraint.
*/
protected void addOrderingColumnsToSelect() {
if (// Don't do this for subqueries, since we will be selecting just the necessary column(s)
orderingExpressions != null && parent == null) {
// Add any ordering columns to the SELECT
DatastoreAdapter dba = getDatastoreAdapter();
if (dba.supportsOption(DatastoreAdapter.ORDERBY_USING_SELECT_COLUMN_INDEX)) {
// Order using the indexes of the ordering columns in the SELECT
orderingColumnIndexes = new int[orderingExpressions.length];
// Add the ordering columns to the selected list, saving the positions
for (int i = 0; i < orderingExpressions.length; ++i) {
orderingColumnIndexes[i] = selectItem(orderingExpressions[i].toSQLText(), null, !aggregated);
if (unions != null && allowUnions) {
Iterator<SelectStatement> iterator = unions.iterator();
while (iterator.hasNext()) {
SelectStatement stmt = iterator.next();
stmt.selectItem(orderingExpressions[i].toSQLText(), null, !aggregated);
}
}
}
} else if (dba.supportsOption(DatastoreAdapter.INCLUDE_ORDERBY_COLS_IN_SELECT)) {
// Order using column aliases "NUCORDER{i}"
for (int i = 0; i < orderingExpressions.length; ++i) {
if (orderingExpressions[i] instanceof ResultAliasExpression) {
// Nothing to do since this is ordering by a result alias
} else if (orderingExpressions[i].getNumberOfSubExpressions() == 1 || aggregated) {
String orderExprAlias = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase("NUCORDER" + i);
if (unions != null && allowUnions) {
Iterator<SelectStatement> iterator = unions.iterator();
while (iterator.hasNext()) {
SelectStatement stmt = iterator.next();
stmt.selectItem(orderingExpressions[i].toSQLText(), aggregated ? null : orderExprAlias, !aggregated);
}
}
selectItem(orderingExpressions[i].toSQLText(), aggregated ? null : orderExprAlias, !aggregated);
} else {
JavaTypeMapping m = orderingExpressions[i].getJavaTypeMapping();
ColumnMapping[] mappings = m.getColumnMappings();
for (int j = 0; j < mappings.length; j++) {
String alias = rdbmsMgr.getIdentifierFactory().getIdentifierInAdapterCase("NUCORDER" + i + "_" + j);
DatastoreIdentifier aliasId = rdbmsMgr.getIdentifierFactory().newColumnIdentifier(alias);
SQLColumn col = new SQLColumn(orderingExpressions[i].getSQLTable(), mappings[j].getColumn(), aliasId);
selectItem(new SQLText(col.getColumnSelectString()), alias, !aggregated);
if (unions != null && allowUnions) {
Iterator<SelectStatement> iterator = unions.iterator();
while (iterator.hasNext()) {
SelectStatement stmt = iterator.next();
stmt.selectItem(new SQLText(col.getColumnSelectString()), alias, !aggregated);
}
}
}
}
}
}
}
}
use of org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression 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