diff --git a/jOOQ/src/main/java/org/jooq/Cursor.java b/jOOQ/src/main/java/org/jooq/Cursor.java index 6d5f538dd1..037c3612a4 100644 --- a/jOOQ/src/main/java/org/jooq/Cursor.java +++ b/jOOQ/src/main/java/org/jooq/Cursor.java @@ -103,6 +103,18 @@ public interface Cursor extends FieldProvider, Iterable { */ R fetchOne() throws DataAccessException; + /** + * Fetch the next record into a custom handler callback + *

+ * This will conveniently close the Cursor, after the last + * Record 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 fetchOneInto(H handler) throws DataAccessException; + /** * Fetch results into a custom handler callback * @@ -112,6 +124,22 @@ public interface Cursor extends FieldProvider, Iterable { */ > H fetchInto(H handler) throws DataAccessException; + /** + * Map the next resulting record onto a custom type. + *

+ * This is the same as calling fetchOne().into(type). See + * {@link Record#into(Class)} for more details + * + * @param 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 fetchOneInto(Class type) throws DataAccessException, FetchIntoException; + /** * Map resulting records onto a custom type. *

@@ -128,6 +156,22 @@ public interface Cursor extends FieldProvider, Iterable { */ List fetchInto(Class type) throws DataAccessException, FetchIntoException; + /** + * Map the next resulting record onto a custom record. + *

+ * This is the same as calling fetchOne().into(table). See + * {@link Record#into(Class)} for more details + * + * @param 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 fetchOneInto(Table table) throws DataAccessException, FetchIntoException; + /** * Map resulting records onto a custom record. *

@@ -142,7 +186,7 @@ public interface Cursor extends FieldProvider, Iterable { * @throws FetchIntoException wrapping any reflection exception that might * have occurred while mapping records */ - > List fetchInto(Table table); + > List fetchInto(Table table) throws DataAccessException, FetchIntoException; /** * Explicitly close the underlying {@link PreparedStatement} and diff --git a/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java b/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java index bbc574c24b..5187007f43 100644 --- a/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/CursorImpl.java @@ -154,21 +154,35 @@ class CursorImpl implements Cursor { } @Override - public final > H fetchInto(H handler) { - R record = null; + public final > H fetchOneInto(H handler) { + handler.next(fetchOne()); + return handler; + } - while ((record = fetchOne()) != null) { - handler.next(record); + @Override + public final > H fetchInto(H handler) { + while (hasNext()) { + fetchOneInto(handler); } return handler; } + @Override + public final E fetchOneInto(Class clazz) { + return fetchOne().into(clazz); + } + @Override public final List fetchInto(Class clazz) { return fetch().into(clazz); } + @Override + public final > Z fetchOneInto(Table table) { + return fetchOne().into(table); + } + @Override public final > List fetchInto(Table table) { return fetch().into(table);