From 1ce98dc089ddaf40eb0df23e903609c7bf8109e4 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Fri, 7 Nov 2014 19:26:31 +0100 Subject: [PATCH] [#3746] Add DSLContext.fetchExists(Select) --- jOOQ/src/main/java/org/jooq/DSLContext.java | 43 +++++++++++++++++++ .../java/org/jooq/impl/DefaultDSLContext.java | 15 +++++++ 2 files changed, 58 insertions(+) diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index bb06977955..970bb4511a 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -5863,6 +5863,49 @@ public interface DSLContext extends Scope { */ int fetchCount(Table table, Condition condition) throws DataAccessException; + /** + * Check if a {@link Select} would return any records, if it were executed. + *

+ * This wraps a pre-existing SELECT query in another one to + * check for result existence, without modifying the original + * SELECT. An example:

+     * -- Original query:
+     * SELECT id, title FROM book WHERE title LIKE '%a%'
+     *
+     * -- Wrapped query:
+     * SELECT EXISTS (
+     *   SELECT id, title FROM book WHERE title LIKE '%a%'
+     * )
+     * 
+ * + * @param query The wrapped query + * @return The EXISTS(...) result + * @throws DataAccessException if something went wrong executing the query + */ + boolean fetchExists(Select query) throws DataAccessException; + + /** + * Check if a table has any records. + *

+ * This executes

SELECT EXISTS(SELECT * FROM table)
+ * + * @param table The table whose records to count + * @return The number or records in the table + * @throws DataAccessException if something went wrong executing the query + */ + boolean fetchExists(Table table) throws DataAccessException; + + /** + * Check if a table has any records that satisfy a condition. + *

+ * This executes

SELECT EXISTS(SELECT * FROM table WHERE condition)
+ * + * @param table The table whose records to count + * @return The number or records in the table + * @throws DataAccessException if something went wrong executing the query + */ + boolean fetchExists(Table table, Condition condition) throws DataAccessException; + /** * Execute a {@link Query} in the context of this DSLContext. * diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index 45e2e904ee..c9019da144 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -2241,6 +2241,21 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return selectCount().from(table).where(condition).fetchOne(0, int.class); } + @Override + public boolean fetchExists(Select query) throws DataAccessException { + return selectOne().whereExists(query).fetchOne() != null; + } + + @Override + public boolean fetchExists(Table table) throws DataAccessException { + return fetchExists(table, trueCondition()); + } + + @Override + public boolean fetchExists(Table table, Condition condition) throws DataAccessException { + return fetchExists(selectOne().from(table).where(condition)); + } + @Override public int execute(Query query) { final Configuration previous = Utils.getConfiguration(query);