diff options
author | Brian S. O'Neill <bronee@gmail.com> | 2007-08-04 18:27:54 +0000 |
---|---|---|
committer | Brian S. O'Neill <bronee@gmail.com> | 2007-08-04 18:27:54 +0000 |
commit | ea1374c003ecc195295dfc42cc68a02f0d47e8af (patch) | |
tree | f0ee2f12414fe30e468d9d7a8c4356931ea58b79 /src/main/java/com/amazon | |
parent | a511a68520d9c3193096645714fde550931ee746 (diff) |
Added convenience method, Query.exists()
Diffstat (limited to 'src/main/java/com/amazon')
4 files changed, 30 insertions, 0 deletions
diff --git a/src/main/java/com/amazon/carbonado/Query.java b/src/main/java/com/amazon/carbonado/Query.java index e27e484..a729331 100644 --- a/src/main/java/com/amazon/carbonado/Query.java +++ b/src/main/java/com/amazon/carbonado/Query.java @@ -352,6 +352,16 @@ public interface Query<S extends Storable> { long count() throws FetchException;
/**
+ * Returns true if any results are matched by this query.
+ *
+ * @return true if any matches
+ * @throws IllegalStateException if any blank parameters in this query
+ * @throws FetchException if storage layer throws an exception
+ * @since 1.2
+ */
+ boolean exists() throws FetchException;
+
+ /**
* Print the native query to standard out, which is useful for performance
* analysis. Not all repositories have a native query format. An example
* native format is SQL.
diff --git a/src/main/java/com/amazon/carbonado/qe/EmptyQuery.java b/src/main/java/com/amazon/carbonado/qe/EmptyQuery.java index 31d9dd3..1a970e0 100644 --- a/src/main/java/com/amazon/carbonado/qe/EmptyQuery.java +++ b/src/main/java/com/amazon/carbonado/qe/EmptyQuery.java @@ -107,6 +107,13 @@ public final class EmptyQuery<S extends Storable> extends AbstractQuery<S> { }
/**
+ * Always returns false.
+ */
+ public boolean exists() {
+ return false;
+ }
+
+ /**
* Always throws an IllegalStateException.
*/
public Query<S> with(int value) {
diff --git a/src/main/java/com/amazon/carbonado/qe/StandardQuery.java b/src/main/java/com/amazon/carbonado/qe/StandardQuery.java index ec9bf4e..2d7995e 100644 --- a/src/main/java/com/amazon/carbonado/qe/StandardQuery.java +++ b/src/main/java/com/amazon/carbonado/qe/StandardQuery.java @@ -294,6 +294,15 @@ public abstract class StandardQuery<S extends Storable> extends AbstractQuery<S> }
}
+ public boolean exists() throws FetchException {
+ Cursor<S> cursor = fetch();
+ try {
+ return cursor.skipNext(1) > 0;
+ } finally {
+ cursor.close();
+ }
+ }
+
public boolean printNative(Appendable app, int indentLevel) throws IOException {
try {
return executor().printNative(app, indentLevel, mValues);
diff --git a/src/main/java/com/amazon/carbonado/spi/WrappedQuery.java b/src/main/java/com/amazon/carbonado/spi/WrappedQuery.java index ba3c013..bee05f0 100644 --- a/src/main/java/com/amazon/carbonado/spi/WrappedQuery.java +++ b/src/main/java/com/amazon/carbonado/spi/WrappedQuery.java @@ -167,6 +167,10 @@ public abstract class WrappedQuery<S extends Storable> implements Query<S> { return mQuery.count();
}
+ public boolean exists() throws FetchException {
+ return mQuery.exists();
+ }
+
public boolean printNative() {
return mQuery.printNative();
}
|