[#917] Add various Cursor.fetchOneInto() convenience methods

This commit is contained in:
Lukas Eder 2011-11-11 14:03:33 +00:00
parent 66154de826
commit db6b0e2fcc
2 changed files with 63 additions and 5 deletions

View File

@ -103,6 +103,18 @@ public interface Cursor<R extends Record> extends FieldProvider, Iterable<R> {
*/
R fetchOne() throws DataAccessException;
/**
* Fetch the next record into a custom handler callback
* <p>
* This will conveniently close the <code>Cursor</code>, after the last
* <code>Record</code> was fetched.
*
* @param handler The handler callback
* @return Convenience result, returning the parameter handler itself
* @throws DataAccessException if something went wrong executing the query
*/
<H extends RecordHandler<R>> H fetchOneInto(H handler) throws DataAccessException;
/**
* Fetch results into a custom handler callback
*
@ -112,6 +124,22 @@ public interface Cursor<R extends Record> extends FieldProvider, Iterable<R> {
*/
<H extends RecordHandler<R>> H fetchInto(H handler) throws DataAccessException;
/**
* Map the next resulting record onto a custom type.
* <p>
* This is the same as calling <code>fetchOne().into(type)</code>. See
* {@link Record#into(Class)} for more details
*
* @param <E> The generic entity type.
* @param type The entity type.
* @see Record#into(Class)
* @see Result#into(Class)
* @throws DataAccessException if something went wrong executing the query
* @throws FetchIntoException wrapping any reflection exception that might
* have occurred while mapping records
*/
<E> E fetchOneInto(Class<? extends E> type) throws DataAccessException, FetchIntoException;
/**
* Map resulting records onto a custom type.
* <p>
@ -128,6 +156,22 @@ public interface Cursor<R extends Record> extends FieldProvider, Iterable<R> {
*/
<E> List<E> fetchInto(Class<? extends E> type) throws DataAccessException, FetchIntoException;
/**
* Map the next resulting record onto a custom record.
* <p>
* This is the same as calling <code>fetchOne().into(table)</code>. See
* {@link Record#into(Class)} for more details
*
* @param <Z> The generic table record type.
* @param table The table type.
* @see Record#into(Class)
* @see Result#into(Class)
* @throws DataAccessException if something went wrong executing the query
* @throws FetchIntoException wrapping any reflection exception that might
* have occurred while mapping records
*/
<Z extends TableRecord<Z>> Z fetchOneInto(Table<Z> table) throws DataAccessException, FetchIntoException;
/**
* Map resulting records onto a custom record.
* <p>
@ -142,7 +186,7 @@ public interface Cursor<R extends Record> extends FieldProvider, Iterable<R> {
* @throws FetchIntoException wrapping any reflection exception that might
* have occurred while mapping records
*/
<Z extends TableRecord<Z>> List<Z> fetchInto(Table<Z> table);
<Z extends TableRecord<Z>> List<Z> fetchInto(Table<Z> table) throws DataAccessException, FetchIntoException;
/**
* Explicitly close the underlying {@link PreparedStatement} and

View File

@ -154,21 +154,35 @@ class CursorImpl<R extends Record> implements Cursor<R> {
}
@Override
public final <H extends RecordHandler<R>> H fetchInto(H handler) {
R record = null;
public final <H extends RecordHandler<R>> H fetchOneInto(H handler) {
handler.next(fetchOne());
return handler;
}
while ((record = fetchOne()) != null) {
handler.next(record);
@Override
public final <H extends RecordHandler<R>> H fetchInto(H handler) {
while (hasNext()) {
fetchOneInto(handler);
}
return handler;
}
@Override
public final <E> E fetchOneInto(Class<? extends E> clazz) {
return fetchOne().into(clazz);
}
@Override
public final <E> List<E> fetchInto(Class<? extends E> clazz) {
return fetch().into(clazz);
}
@Override
public final <Z extends TableRecord<Z>> Z fetchOneInto(Table<Z> table) {
return fetchOne().into(table);
}
@Override
public final <Z extends TableRecord<Z>> List<Z> fetchInto(Table<Z> table) {
return fetch().into(table);