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:
+ *
+ * @param query The wrapped query
+ * @return The
+ * -- Original query:
+ * SELECT id, title FROM book WHERE title LIKE '%a%'
+ *
+ * -- Wrapped query:
+ * SELECT EXISTS (
+ * SELECT id, title FROM book WHERE title LIKE '%a%'
+ * )
+ *
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
+ *
+ * @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.
+ * SELECT EXISTS(SELECT * FROM table)
+ * This executes
+ *
+ * @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 SELECT EXISTS(SELECT * FROM table WHERE condition)
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);