From 1e81bfc705cafe88b804a8a6b1b8616c8773d280 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 14 Oct 2007 05:44:35 +0000 Subject: Support ordering by derived properties. --- .../amazon/carbonado/repo/jdbc/JDBCStorage.java | 62 ++++++++++++++++------ 1 file changed, 45 insertions(+), 17 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/repo/jdbc') diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java index c0f23a2..a841d3a 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java @@ -63,6 +63,7 @@ import com.amazon.carbonado.qe.QueryExecutor; import com.amazon.carbonado.qe.QueryExecutorCache; import com.amazon.carbonado.qe.QueryExecutorFactory; import com.amazon.carbonado.qe.QueryFactory; +import com.amazon.carbonado.qe.SortedQueryExecutor; import com.amazon.carbonado.qe.StandardQuery; import com.amazon.carbonado.qe.StandardQueryFactory; import com.amazon.carbonado.sequence.SequenceValueProducer; @@ -395,29 +396,54 @@ class JDBCStorage extends StandardQueryFactory } } - // Append order-by clause. - if (ordering != null && ordering.size() != 0) { - selectBuilder.append(" ORDER BY "); + // Append order-by clause. Remainder ordering is required if a derived + // property is used. + + OrderingList sqlOrdering = ordering; + OrderingList remainderOrdering = null; + + if (ordering != null && ordering.size() > 0) { ordinal = 0; for (OrderedProperty orderedProperty : ordering) { - if (ordinal > 0) { - selectBuilder.append(','); - } - selectBuilder.appendColumn(alias == null ? null : jn, - orderedProperty.getChainedProperty()); - if (orderedProperty.getDirection() == Direction.DESCENDING) { - selectBuilder.append(" DESC"); + if (orderedProperty.getChainedProperty().isDerived()) { + sqlOrdering = ordering.subList(0, ordinal); + remainderOrdering = ordering.subList(ordinal, ordering.size()); + break; } ordinal++; } + + if (sqlOrdering != null && sqlOrdering.size() > 0) { + selectBuilder.append(" ORDER BY "); + ordinal = 0; + for (OrderedProperty orderedProperty : sqlOrdering) { + if (ordinal > 0) { + selectBuilder.append(','); + } + selectBuilder.appendColumn(alias == null ? null : jn, + orderedProperty.getChainedProperty()); + if (orderedProperty.getDirection() == Direction.DESCENDING) { + selectBuilder.append(" DESC"); + } + ordinal++; + } + } + } + + QueryExecutor executor = new Executor(filter, + sqlOrdering, + selectBuilder.build(), + fromWhereBuilder.build(), + propertyFilters, + propertyFilterNullable); + + if (remainderOrdering != null && remainderOrdering.size() > 0) { + // FIXME: use MergeSortBuffer + executor = new SortedQueryExecutor + (null, executor, sqlOrdering, remainderOrdering); } - return new Executor(filter, - ordering, - selectBuilder.build(), - fromWhereBuilder.build(), - propertyFilters, - propertyFilterNullable); + return executor; } } @@ -940,7 +966,9 @@ class JDBCStorage extends StandardQueryFactory if (ordering != null) { for (OrderedProperty orderedProperty : ordering) { ChainedProperty chained = orderedProperty.getChainedProperty(); - mRootJoinNode.addJoin(mRepository, chained, mAliasGenerator); + if (!chained.isDerived()) { + mRootJoinNode.addJoin(mRepository, chained, mAliasGenerator); + } } } } catch (RepositoryException e) { -- cgit v1.2.3