Add fetchOne(RecordMapper) shortcut method to ResultQuery and implement it in implementing classes

This commit is contained in:
Laurent Pireyn 2014-11-26 10:34:46 +01:00
parent 59d8879aaf
commit 2fa50aeaf9
3 changed files with 22 additions and 0 deletions

View File

@ -457,6 +457,17 @@ public interface ResultQuery<R extends Record> extends Query, Iterable<R> {
*/
R fetchOne() throws DataAccessException, InvalidResultException;
/**
* Execute the query and return at most one resulting value into a
* custom mapper callback.
*
* @return The custom mapped record or <code>null</code> if the query returned no
* records.
* @throws DataAccessException if something went wrong executing the query
* @throws InvalidResultException if the query returned more than one record
*/
<E> E fetchOne(RecordMapper<? super R, E> mapper) throws DataAccessException, InvalidResultException;
/**
* Execute the query and return at most one resulting record as a name/value
* map.

View File

@ -440,6 +440,12 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return Utils.fetchOne(fetchLazy());
}
@Override
public final <E> E fetchOne(RecordMapper<? super R, E> mapper) {
R record = fetchOne();
return record == null ? null : mapper.map(record);
}
@Override
public final Map<String, Object> fetchOneMap() {
R record = fetchOne();

View File

@ -2436,6 +2436,11 @@ class SelectImpl<R extends Record, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
return getDelegate().fetchOne();
}
@Override
public final <E> E fetchOne(RecordMapper<? super R, E> mapper) {
return getDelegate().fetchOne(mapper);
}
@Override
public final Map<String, Object> fetchOneMap() {
return getDelegate().fetchOneMap();