summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2007-08-04 18:27:54 +0000
committerBrian S. O'Neill <bronee@gmail.com>2007-08-04 18:27:54 +0000
commitea1374c003ecc195295dfc42cc68a02f0d47e8af (patch)
treef0ee2f12414fe30e468d9d7a8c4356931ea58b79 /src
parenta511a68520d9c3193096645714fde550931ee746 (diff)
Added convenience method, Query.exists()
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/amazon/carbonado/Query.java10
-rw-r--r--src/main/java/com/amazon/carbonado/qe/EmptyQuery.java7
-rw-r--r--src/main/java/com/amazon/carbonado/qe/StandardQuery.java9
-rw-r--r--src/main/java/com/amazon/carbonado/spi/WrappedQuery.java4
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();
}