From ea1374c003ecc195295dfc42cc68a02f0d47e8af Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sat, 4 Aug 2007 18:27:54 +0000 Subject: Added convenience method, Query.exists() --- src/main/java/com/amazon/carbonado/Query.java | 10 ++++++++++ src/main/java/com/amazon/carbonado/qe/EmptyQuery.java | 7 +++++++ src/main/java/com/amazon/carbonado/qe/StandardQuery.java | 9 +++++++++ src/main/java/com/amazon/carbonado/spi/WrappedQuery.java | 4 ++++ 4 files changed, 30 insertions(+) (limited to 'src/main/java') 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 @@ -351,6 +351,16 @@ public interface Query { */ 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 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 @@ -106,6 +106,13 @@ public final class EmptyQuery extends AbstractQuery { return 0; } + /** + * Always returns false. + */ + public boolean exists() { + return false; + } + /** * Always throws an IllegalStateException. */ 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 extends AbstractQuery } } + public boolean exists() throws FetchException { + Cursor 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 implements Query { return mQuery.count(); } + public boolean exists() throws FetchException { + return mQuery.exists(); + } + public boolean printNative() { return mQuery.printNative(); } -- cgit v1.2.3