summaryrefslogtreecommitdiff
path: root/src/main/java/com
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2007-10-14 05:44:35 +0000
committerBrian S. O'Neill <bronee@gmail.com>2007-10-14 05:44:35 +0000
commit1e81bfc705cafe88b804a8a6b1b8616c8773d280 (patch)
tree01266dc8995a350f6681997173205c59857f6abb /src/main/java/com
parenta2b87f48775ca6687de0eb4af4b846bd1f0cdebf (diff)
Support ordering by derived properties.
Diffstat (limited to 'src/main/java/com')
-rw-r--r--src/main/java/com/amazon/carbonado/info/ChainedProperty.java20
-rw-r--r--src/main/java/com/amazon/carbonado/qe/FilteredQueryExecutor.java10
-rw-r--r--src/main/java/com/amazon/carbonado/qe/SortedQueryExecutor.java10
-rw-r--r--src/main/java/com/amazon/carbonado/qe/UnionQueryExecutor.java14
-rw-r--r--src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java62
5 files changed, 99 insertions, 17 deletions
diff --git a/src/main/java/com/amazon/carbonado/info/ChainedProperty.java b/src/main/java/com/amazon/carbonado/info/ChainedProperty.java
index 575bcbf..e067350 100644
--- a/src/main/java/com/amazon/carbonado/info/ChainedProperty.java
+++ b/src/main/java/com/amazon/carbonado/info/ChainedProperty.java
@@ -184,6 +184,26 @@ public class ChainedProperty<S extends Storable> implements Appender {
}
/**
+ * Returns true if any property in the chain is derived.
+ *
+ * @see com.amazon.carbonado.Derived
+ * @since 1.2
+ */
+ public boolean isDerived() {
+ if (mPrime.isDerived()) {
+ return true;
+ }
+ if (mChain != null) {
+ for (StorableProperty<?> prop : mChain) {
+ if (prop.isDerived()) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
* Returns the last property in the chain, or the prime property if chain
* is empty.
*/
diff --git a/src/main/java/com/amazon/carbonado/qe/FilteredQueryExecutor.java b/src/main/java/com/amazon/carbonado/qe/FilteredQueryExecutor.java
index 494d353..6f662a4 100644
--- a/src/main/java/com/amazon/carbonado/qe/FilteredQueryExecutor.java
+++ b/src/main/java/com/amazon/carbonado/qe/FilteredQueryExecutor.java
@@ -75,6 +75,16 @@ public class FilteredQueryExecutor<S extends Storable> extends AbstractQueryExec
return mExecutor.getOrdering();
}
+ /**
+ * Prints native query of the wrapped executor.
+ */
+ @Override
+ public boolean printNative(Appendable app, int indentLevel, FilterValues<S> values)
+ throws IOException
+ {
+ return mExecutor.printNative(app, indentLevel, values);
+ }
+
public boolean printPlan(Appendable app, int indentLevel, FilterValues<S> values)
throws IOException
{
diff --git a/src/main/java/com/amazon/carbonado/qe/SortedQueryExecutor.java b/src/main/java/com/amazon/carbonado/qe/SortedQueryExecutor.java
index a142b72..5419fe6 100644
--- a/src/main/java/com/amazon/carbonado/qe/SortedQueryExecutor.java
+++ b/src/main/java/com/amazon/carbonado/qe/SortedQueryExecutor.java
@@ -115,6 +115,16 @@ public class SortedQueryExecutor<S extends Storable> extends AbstractQueryExecut
return mHandledOrdering.concat(mRemainderOrdering);
}
+ /**
+ * Prints native query of the wrapped executor.
+ */
+ @Override
+ public boolean printNative(Appendable app, int indentLevel, FilterValues<S> values)
+ throws IOException
+ {
+ return mExecutor.printNative(app, indentLevel, values);
+ }
+
public boolean printPlan(Appendable app, int indentLevel, FilterValues<S> values)
throws IOException
{
diff --git a/src/main/java/com/amazon/carbonado/qe/UnionQueryExecutor.java b/src/main/java/com/amazon/carbonado/qe/UnionQueryExecutor.java
index aa3577f..c73952c 100644
--- a/src/main/java/com/amazon/carbonado/qe/UnionQueryExecutor.java
+++ b/src/main/java/com/amazon/carbonado/qe/UnionQueryExecutor.java
@@ -122,6 +122,20 @@ public class UnionQueryExecutor<S extends Storable> extends AbstractQueryExecuto
return mTotalOrdering;
}
+ /**
+ * Prints native queries of the wrapped executors.
+ */
+ @Override
+ public boolean printNative(Appendable app, int indentLevel, FilterValues<S> values)
+ throws IOException
+ {
+ boolean result = false;
+ for (QueryExecutor<S> executor : mExecutors) {
+ result |= executor.printNative(app, indentLevel, values);
+ }
+ return result;
+ }
+
public boolean printPlan(Appendable app, int indentLevel, FilterValues<S> values)
throws IOException
{
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<S extends Storable> extends StandardQueryFactory<S>
}
}
- // 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<S> sqlOrdering = ordering;
+ OrderingList<S> remainderOrdering = null;
+
+ if (ordering != null && ordering.size() > 0) {
ordinal = 0;
for (OrderedProperty<S> 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<S> 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<S> executor = new Executor(filter,
+ sqlOrdering,
+ selectBuilder.build(),
+ fromWhereBuilder.build(),
+ propertyFilters,
+ propertyFilterNullable);
+
+ if (remainderOrdering != null && remainderOrdering.size() > 0) {
+ // FIXME: use MergeSortBuffer
+ executor = new SortedQueryExecutor<S>
+ (null, executor, sqlOrdering, remainderOrdering);
}
- return new Executor(filter,
- ordering,
- selectBuilder.build(),
- fromWhereBuilder.build(),
- propertyFilters,
- propertyFilterNullable);
+ return executor;
}
}
@@ -940,7 +966,9 @@ class JDBCStorage<S extends Storable> extends StandardQueryFactory<S>
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) {