From 92d483b83e43d6d9f01a32d77d26eb49f298e352 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 28 Nov 2012 19:28:04 +0100 Subject: [PATCH] [#1984] Add ResultQuery.fetchOneInto() --- jOOQ/src/main/java/org/jooq/ResultQuery.java | 75 ++++++++++++++++--- .../jooq/impl/AbstractDelegatingSelect.java | 10 +++ .../org/jooq/impl/AbstractResultQuery.java | 12 +++ 3 files changed, 85 insertions(+), 12 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/ResultQuery.java b/jOOQ/src/main/java/org/jooq/ResultQuery.java index 2f50632261..a606a3c4bd 100644 --- a/jOOQ/src/main/java/org/jooq/ResultQuery.java +++ b/jOOQ/src/main/java/org/jooq/ResultQuery.java @@ -291,7 +291,7 @@ public interface ResultQuery extends Query { * @throws DataAccessException if something went wrong executing the query * @throws InvalidResultException if the query returned more than one record */ - T fetchOne(Field field) throws DataAccessException; + T fetchOne(Field field) throws DataAccessException, InvalidResultException; /** * Execute the query and return return at most one resulting value for a @@ -305,7 +305,7 @@ public interface ResultQuery extends Query { * @throws DataAccessException if something went wrong executing the query * @throws InvalidResultException if the query returned more than one record */ - T fetchOne(Field field, Class type) throws DataAccessException; + T fetchOne(Field field, Class type) throws DataAccessException, InvalidResultException; /** * Execute the query and return return at most one resulting value for a @@ -319,7 +319,8 @@ public interface ResultQuery extends Query { * @throws DataAccessException if something went wrong executing the query * @throws InvalidResultException if the query returned more than one record */ - U fetchOne(Field field, Converter converter) throws DataAccessException; + U fetchOne(Field field, Converter converter) throws DataAccessException, + InvalidResultException; /** * Execute the query and return return at most one resulting value for a @@ -333,7 +334,7 @@ public interface ResultQuery extends Query { * @throws DataAccessException if something went wrong executing the query * @throws InvalidResultException if the query returned more than one record */ - Object fetchOne(int fieldIndex) throws DataAccessException; + Object fetchOne(int fieldIndex) throws DataAccessException, InvalidResultException; /** * Execute the query and return return at most one resulting value for a @@ -347,7 +348,7 @@ public interface ResultQuery extends Query { * @throws DataAccessException if something went wrong executing the query * @throws InvalidResultException if the query returned more than one record */ - T fetchOne(int fieldIndex, Class type) throws DataAccessException; + T fetchOne(int fieldIndex, Class type) throws DataAccessException, InvalidResultException; /** * Execute the query and return return at most one resulting value for a @@ -361,7 +362,7 @@ public interface ResultQuery extends Query { * @throws DataAccessException if something went wrong executing the query * @throws InvalidResultException if the query returned more than one record */ - U fetchOne(int fieldIndex, Converter converter) throws DataAccessException; + U fetchOne(int fieldIndex, Converter converter) throws DataAccessException, InvalidResultException; /** * Execute the query and return return at most one resulting value for a @@ -375,7 +376,7 @@ public interface ResultQuery extends Query { * @throws DataAccessException if something went wrong executing the query * @throws InvalidResultException if the query returned more than one record */ - Object fetchOne(String fieldName) throws DataAccessException; + Object fetchOne(String fieldName) throws DataAccessException, InvalidResultException; /** * Execute the query and return return at most one resulting value for a @@ -389,7 +390,7 @@ public interface ResultQuery extends Query { * @throws DataAccessException if something went wrong executing the query * @throws InvalidResultException if the query returned more than one record */ - T fetchOne(String fieldName, Class type) throws DataAccessException; + T fetchOne(String fieldName, Class type) throws DataAccessException, InvalidResultException; /** * Execute the query and return return at most one resulting value for a @@ -403,7 +404,7 @@ public interface ResultQuery extends Query { * @throws DataAccessException if something went wrong executing the query * @throws InvalidResultException if the query returned more than one record */ - U fetchOne(String fieldName, Converter converter) throws DataAccessException; + U fetchOne(String fieldName, Converter converter) throws DataAccessException, InvalidResultException; /** * Execute the query and return at most one resulting record. @@ -417,7 +418,7 @@ public interface ResultQuery extends Query { * @throws DataAccessException if something went wrong executing the query * @throws InvalidResultException if the query returned more than one record */ - R fetchOne() throws DataAccessException; + R fetchOne() throws DataAccessException, InvalidResultException; /** * Execute the query and return at most one resulting record. @@ -456,7 +457,7 @@ public interface ResultQuery extends Query { * @see Result#intoMaps() * @see Record#intoMap() */ - Map fetchOneMap() throws DataAccessException; + Map fetchOneMap() throws DataAccessException, InvalidResultException; /** * Execute the query and return a {@link Map} with one of the result's @@ -786,7 +787,7 @@ public interface ResultQuery extends Query { * @throws DataAccessException if something went wrong executing the query * @throws InvalidResultException if the query returned more than one record */ - Object[] fetchOneArray() throws DataAccessException; + Object[] fetchOneArray() throws DataAccessException, InvalidResultException; /** * Map resulting records onto a custom type. @@ -804,6 +805,30 @@ public interface ResultQuery extends Query { */ List fetchInto(Class type) throws DataAccessException, MappingException; + /** + * Map resulting records onto a custom type. + *

+ * This is the same as calling

+     * E result = null;
+     * Record r = q.fetchOne();
+     *
+     * if (r != null)
+     *     result = r.into(type);
+     * 
. See {@link Record#into(Class)} for more details + * + * @param The generic entity type. + * @param type The entity type. + * @return The resulting record or null if the query returns no + * records. + * @see Record#into(Class) + * @see Result#into(Class) + * @throws DataAccessException if something went wrong executing the query + * @throws MappingException wrapping any reflection or data type conversion + * exception that might have occurred while mapping records + * @throws InvalidResultException if the query returned more than one record + */ + E fetchOneInto(Class type) throws DataAccessException, MappingException, InvalidResultException; + /** * Map resulting records onto a custom record. *

@@ -822,6 +847,32 @@ public interface ResultQuery extends Query { */ Result fetchInto(Table table) throws DataAccessException; + /** + * Map resulting records onto a custom record. + *

+ * This is the same as calling

+     * Z result = null;
+     * Record r = q.fetchOne();
+     *
+     * if (r != null)
+     *     result = r.into(table);
+     * 
. See {@link Record#into(Table)} for more details + *

+ * The resulting record is attached to the original {@link Configuration} by + * default. Use {@link Settings#isAttachRecords()} to override this + * behaviour. + * + * @param The generic table record type. + * @param table The table type. + * @return The resulting record or null if the query returns no + * records. + * @see Record#into(Table) + * @see Result#into(Table) + * @throws DataAccessException if something went wrong executing the query + * @throws InvalidResultException if the query returned more than one record + */ + Z fetchOneInto(Table table) throws DataAccessException, InvalidResultException; + /** * Fetch results into a custom handler callback *

diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractDelegatingSelect.java b/jOOQ/src/main/java/org/jooq/impl/AbstractDelegatingSelect.java index dd63f7223b..8636d3cb8a 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractDelegatingSelect.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractDelegatingSelect.java @@ -334,6 +334,16 @@ abstract class AbstractDelegatingSelect return getDelegate().fetchInto(type); } + @Override + public final E fetchOneInto(Class type) { + return getDelegate().fetchOneInto(type); + } + + @Override + public final Z fetchOneInto(Table table) { + return getDelegate().fetchOneInto(table); + } + @Override public final Result fetchInto(Table table) { return getDelegate().fetchInto(table); diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java b/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java index 33de568e84..dc4f5eecf1 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java @@ -559,11 +559,23 @@ abstract class AbstractResultQuery extends AbstractQuery imple return fetch().into(type); } + @Override + public final E fetchOneInto(Class type) { + R record = fetchOne(); + return record == null ? null : record.into(type); + } + @Override public final Result fetchInto(Table table) { return fetch().into(table); } + @Override + public final Z fetchOneInto(Table table) { + R record = fetchOne(); + return record == null ? null : record.into(table); + } + @Override public final > H fetchInto(H handler) { return fetch().into(handler);