[#3746] Add DSLContext.fetchExists(Select<?>)

This commit is contained in:
lukaseder 2014-11-07 19:26:31 +01:00
parent d3170f223c
commit 1ce98dc089
2 changed files with 58 additions and 0 deletions

View File

@ -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.
* <p>
* This wraps a pre-existing <code>SELECT</code> query in another one to
* check for result existence, without modifying the original
* <code>SELECT</code>. An example: <code><pre>
* -- Original query:
* SELECT id, title FROM book WHERE title LIKE '%a%'
*
* -- Wrapped query:
* SELECT EXISTS (
* SELECT id, title FROM book WHERE title LIKE '%a%'
* )
* </pre></code>
*
* @param query The wrapped query
* @return The <code>EXISTS(...)</code> result
* @throws DataAccessException if something went wrong executing the query
*/
boolean fetchExists(Select<?> query) throws DataAccessException;
/**
* Check if a table has any records.
* <p>
* This executes <code><pre>SELECT EXISTS(SELECT * FROM table)</pre></code>
*
* @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.
* <p>
* This executes <code><pre>SELECT EXISTS(SELECT * FROM table WHERE condition)</pre></code>
*
* @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 <code>DSLContext</code>.
*

View File

@ -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);