[#3169] Add more ResultQuery.fetchAnyXXX() convenience methods

This commit is contained in:
Lukas Eder 2014-04-08 14:36:45 +02:00
parent 991b87bbc9
commit 52fbd24e42
3 changed files with 453 additions and 121 deletions

View File

@ -444,6 +444,204 @@ public interface ResultQuery<R extends Record> extends Query {
*/
R fetchOne() throws DataAccessException, InvalidResultException;
/**
* Execute the query and return at most one resulting record as a name/value
* map.
*
* @return The resulting record or <code>null</code> if the query returns no
* records.
* @throws DataAccessException if something went wrong executing the query
* @throws InvalidResultException if the query returned more than one record
* @see Result#intoMaps()
* @see Record#intoMap()
*/
Map<String, Object> fetchOneMap() throws DataAccessException, InvalidResultException;
/**
* Execute the query and return at most one resulting record as an array
* <p>
* You can access data like this
* <code><pre>query.fetchOneArray()[fieldIndex]</pre></code>
*
* @return The resulting record or <code>null</code> if the query returns no
* records.
* @throws DataAccessException if something went wrong executing the query
* @throws InvalidResultException if the query returned more than one record
*/
Object[] fetchOneArray() throws DataAccessException, InvalidResultException;
/**
* Map resulting records onto a custom type.
* <p>
* This is the same as calling <code><pre>
* E result = null;
* Record r = q.fetchOne();
*
* if (r != null)
* result = r.into(type);
* </pre></code>. See {@link Record#into(Class)} for more details
*
* @param <E> The generic entity type.
* @param type The entity type.
* @return The resulting record or <code>null</code> 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
* @see DefaultRecordMapper
*/
<E> E fetchOneInto(Class<? extends E> type) throws DataAccessException, MappingException, InvalidResultException;
/**
* Map resulting records onto a custom record.
* <p>
* This is the same as calling <code><pre>
* Z result = null;
* Record r = q.fetchOne();
*
* if (r != null)
* result = r.into(table);
* </pre></code>. See {@link Record#into(Table)} for more details
* <p>
* The resulting record is attached to the original {@link Configuration} by
* default. Use {@link Settings#isAttachRecords()} to override this
* behaviour.
*
* @param <Z> The generic table record type.
* @param table The table type.
* @return The resulting record or <code>null</code> 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 extends Record> Z fetchOneInto(Table<Z> table) throws DataAccessException, InvalidResultException;
/**
* Execute the query and return return at most one resulting value for a
* field from the generated result.
* <p>
* This is the same as calling {@link #fetchOne()} and then
* {@link Record#getValue(Field)}
*
* @return The resulting value 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
*/
<T> T fetchAny(Field<T> field) throws DataAccessException;
/**
* Execute the query and return return at most one resulting value for a
* field from the generated result.
* <p>
* This is the same as calling {@link #fetchOne()} and then
* {@link Record#getValue(Field, Class)}
*
* @return The resulting value 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
*/
<T> T fetchAny(Field<?> field, Class<? extends T> type) throws DataAccessException;
/**
* Execute the query and return return at most one resulting value for a
* field from the generated result.
* <p>
* This is the same as calling {@link #fetchOne()} and then
* {@link Record#getValue(Field, Converter)}
*
* @return The resulting value 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
*/
<T, U> U fetchAny(Field<T> field, Converter<? super T, U> converter) throws DataAccessException;
/**
* Execute the query and return return at most one resulting value for a
* field index from the generated result.
* <p>
* This is the same as calling {@link #fetchOne()} and then
* {@link Record#getValue(int)}
*
* @return The resulting value or <code>null</code> if the query returned no
* records.
* @throws DataAccessException if something went wrong executing the query
*/
Object fetchAny(int fieldIndex) throws DataAccessException;
/**
* Execute the query and return return at most one resulting value for a
* field index from the generated result.
* <p>
* This is the same as calling {@link #fetchOne()} and then
* {@link Record#getValue(int, Class)}
*
* @return The resulting value or <code>null</code> if the query returned no
* records.
* @throws DataAccessException if something went wrong executing the query
*/
<T> T fetchAny(int fieldIndex, Class<? extends T> type) throws DataAccessException;
/**
* Execute the query and return return at most one resulting value for a
* field index from the generated result.
* <p>
* This is the same as calling {@link #fetchOne()} and then
* {@link Record#getValue(int, Converter)}
*
* @return The resulting value 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
*/
<U> U fetchAny(int fieldIndex, Converter<?, U> converter) throws DataAccessException;
/**
* Execute the query and return return at most one resulting value for a
* field name from the generated result.
* <p>
* This is the same as calling {@link #fetchOne()} and then
* {@link Record#getValue(int)}
*
* @return The resulting value or <code>null</code> if the query returned no
* records.
* @throws DataAccessException if something went wrong executing the query
*/
Object fetchAny(String fieldName) throws DataAccessException;
/**
* Execute the query and return return at most one resulting value for a
* field name from the generated result.
* <p>
* This is the same as calling {@link #fetchOne()} and then
* {@link Record#getValue(String, Class)}
*
* @return The resulting value or <code>null</code> if the query returned no
* records.
* @throws DataAccessException if something went wrong executing the query
*/
<T> T fetchAny(String fieldName, Class<? extends T> type) throws DataAccessException;
/**
* Execute the query and return return at most one resulting value for a
* field name from the generated result.
* <p>
* This is the same as calling {@link #fetchOne()} and then
* {@link Record#getValue(String, Converter)}
*
* @return The resulting value or <code>null</code> if the query returned no
* records.
* @throws DataAccessException if something went wrong executing the query
*/
<U> U fetchAny(String fieldName, Converter<?, U> converter) throws DataAccessException;
/**
* Execute the query and return at most one resulting record.
* <p>
@ -457,6 +655,80 @@ public interface ResultQuery<R extends Record> extends Query {
*/
R fetchAny() throws DataAccessException;
/**
* Execute the query and return at most one resulting record as a name/value
* map.
*
* @return The resulting record or <code>null</code> if the query returns no
* records.
* @throws DataAccessException if something went wrong executing the query
* @see Result#intoMaps()
* @see Record#intoMap()
*/
Map<String, Object> fetchAnyMap() throws DataAccessException;
/**
* Execute the query and return at most one resulting record as an array
* <p>
* You can access data like this
* <code><pre>query.fetchAnyArray()[fieldIndex]</pre></code>
*
* @return The resulting record or <code>null</code> if the query returns no
* records.
* @throws DataAccessException if something went wrong executing the query
*/
Object[] fetchAnyArray() throws DataAccessException;
/**
* Map resulting records onto a custom type.
* <p>
* This is the same as calling <code><pre>
* E result = null;
* Record r = q.fetchAny();
*
* if (r != null)
* result = r.into(type);
* </pre></code>. See {@link Record#into(Class)} for more details
*
* @param <E> The generic entity type.
* @param type The entity type.
* @return The resulting record or <code>null</code> 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
* @see DefaultRecordMapper
*/
<E> E fetchAnyInto(Class<? extends E> type) throws DataAccessException, MappingException;
/**
* Map resulting records onto a custom record.
* <p>
* This is the same as calling <code><pre>
* Z result = null;
* Record r = q.fetchOne();
*
* if (r != null)
* result = r.into(table);
* </pre></code>. See {@link Record#into(Table)} for more details
* <p>
* The resulting record is attached to the original {@link Configuration} by
* default. Use {@link Settings#isAttachRecords()} to override this
* behaviour.
*
* @param <Z> The generic table record type.
* @param table The table type.
* @return The resulting record or <code>null</code> 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 extends Record> Z fetchAnyInto(Table<Z> table) throws DataAccessException, InvalidResultException;
/**
* Execute the query and return the generated result as a list of name/value
* maps.
@ -470,19 +742,6 @@ public interface ResultQuery<R extends Record> extends Query {
*/
List<Map<String, Object>> fetchMaps() throws DataAccessException;
/**
* Execute the query and return at most one resulting record as a name/value
* map.
*
* @return The resulting record or <code>null</code> if the query returns no
* records.
* @throws DataAccessException if something went wrong executing the query
* @throws InvalidResultException if the query returned more than one record
* @see Result#intoMaps()
* @see Record#intoMap()
*/
Map<String, Object> fetchOneMap() throws DataAccessException, InvalidResultException;
/**
* Execute the query and return a {@link Map} with one of the result's
* columns as key and the corresponding records as value.
@ -884,19 +1143,6 @@ public interface ResultQuery<R extends Record> extends Query {
*/
<T, U> U[] fetchArray(Field<T> field, Converter<? super T, U> converter) throws DataAccessException;
/**
* Execute the query and return at most one resulting record as an array
* <p>
* You can access data like this
* <code><pre>query.fetchOneArray()[fieldIndex]</pre></code>
*
* @return The resulting record or <code>null</code> if the query returns no
* records.
* @throws DataAccessException if something went wrong executing the query
* @throws InvalidResultException if the query returned more than one record
*/
Object[] fetchOneArray() throws DataAccessException, InvalidResultException;
/**
* Map resulting records onto a custom type.
* <p>
@ -914,31 +1160,6 @@ public interface ResultQuery<R extends Record> extends Query {
*/
<E> List<E> fetchInto(Class<? extends E> type) throws DataAccessException, MappingException;
/**
* Map resulting records onto a custom type.
* <p>
* This is the same as calling <code><pre>
* E result = null;
* Record r = q.fetchOne();
*
* if (r != null)
* result = r.into(type);
* </pre></code>. See {@link Record#into(Class)} for more details
*
* @param <E> The generic entity type.
* @param type The entity type.
* @return The resulting record or <code>null</code> 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
* @see DefaultRecordMapper
*/
<E> E fetchOneInto(Class<? extends E> type) throws DataAccessException, MappingException, InvalidResultException;
/**
* Map resulting records onto a custom record.
* <p>
@ -957,32 +1178,6 @@ public interface ResultQuery<R extends Record> extends Query {
*/
<Z extends Record> Result<Z> fetchInto(Table<Z> table) throws DataAccessException;
/**
* Map resulting records onto a custom record.
* <p>
* This is the same as calling <code><pre>
* Z result = null;
* Record r = q.fetchOne();
*
* if (r != null)
* result = r.into(table);
* </pre></code>. See {@link Record#into(Table)} for more details
* <p>
* The resulting record is attached to the original {@link Configuration} by
* default. Use {@link Settings#isAttachRecords()} to override this
* behaviour.
*
* @param <Z> The generic table record type.
* @param table The table type.
* @return The resulting record or <code>null</code> 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 extends Record> Z fetchOneInto(Table<Z> table) throws DataAccessException, InvalidResultException;
/**
* Fetch results into a custom handler callback.
* <p>

View File

@ -476,6 +476,78 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return Utils.fetchOne(fetchLazy());
}
@Override
public final Map<String, Object> fetchOneMap() {
R record = fetchOne();
return record == null ? null : record.intoMap();
}
@Override
public final Object[] fetchOneArray() {
R record = fetchOne();
return record == null ? null : record.intoArray();
}
@Override
public final <E> E fetchOneInto(Class<? extends E> type) {
R record = fetchOne();
return record == null ? null : record.into(type);
}
@Override
public final <Z extends Record> Z fetchOneInto(Table<Z> table) {
R record = fetchOne();
return record == null ? null : record.into(table);
}
@Override
public final <T> T fetchAny(Field<T> field) {
R record = fetchAny();
return record == null ? null : record.getValue(field);
}
@Override
public final <T> T fetchAny(Field<?> field, Class<? extends T> type) {
return Convert.convert(fetchAny(field), type);
}
@Override
public final <T, U> U fetchAny(Field<T> field, Converter<? super T, U> converter) {
return Convert.convert(fetchAny(field), converter);
}
@Override
public final Object fetchAny(int fieldIndex) {
R record = fetchAny();
return record == null ? null : record.getValue(fieldIndex);
}
@Override
public final <T> T fetchAny(int fieldIndex, Class<? extends T> type) {
return Convert.convert(fetchAny(fieldIndex), type);
}
@Override
public final <U> U fetchAny(int fieldIndex, Converter<?, U> converter) {
return Convert.convert(fetchAny(fieldIndex), converter);
}
@Override
public final Object fetchAny(String fieldName) {
R record = fetchAny();
return record == null ? null : record.getValue(fieldName);
}
@Override
public final <T> T fetchAny(String fieldName, Class<? extends T> type) {
return Convert.convert(fetchAny(fieldName), type);
}
@Override
public final <U> U fetchAny(String fieldName, Converter<?, U> converter) {
return Convert.convert(fetchAny(fieldName), converter);
}
@Override
public final R fetchAny() {
Cursor<R> c = fetchLazy();
@ -488,6 +560,30 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
}
}
@Override
public final Map<String, Object> fetchAnyMap() {
R record = fetchAny();
return record == null ? null : record.intoMap();
}
@Override
public final Object[] fetchAnyArray() {
R record = fetchAny();
return record == null ? null : record.intoArray();
}
@Override
public final <E> E fetchAnyInto(Class<? extends E> type) {
R record = fetchAny();
return record == null ? null : record.into(type);
}
@Override
public final <Z extends Record> Z fetchAnyInto(Table<Z> table) {
R record = fetchAny();
return record == null ? null : record.into(table);
}
@Override
public final <K> Map<K, R> fetchMap(Field<K> key) {
return fetch().intoMap(key);
@ -528,12 +624,6 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return fetch().intoMaps();
}
@Override
public final Map<String, Object> fetchOneMap() {
R record = fetchOne();
return record == null ? null : record.intoMap();
}
@Override
public final <K> Map<K, Result<R>> fetchGroups(Field<K> key) {
return fetch().intoGroups(key);
@ -629,34 +719,16 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return null;
}
@Override
public final Object[] fetchOneArray() {
R record = fetchOne();
return record == null ? null : record.intoArray();
}
@Override
public final <T> List<T> fetchInto(Class<? extends T> type) {
return fetch().into(type);
}
@Override
public final <E> E fetchOneInto(Class<? extends E> type) {
R record = fetchOne();
return record == null ? null : record.into(type);
}
@Override
public final <Z extends Record> Result<Z> fetchInto(Table<Z> table) {
return fetch().into(table);
}
@Override
public final <Z extends Record> Z fetchOneInto(Table<Z> table) {
R record = fetchOne();
return record == null ? null : record.into(table);
}
@Override
public final <H extends RecordHandler<? super R>> H fetchInto(H handler) {
return fetch().into(handler);

View File

@ -2372,11 +2372,96 @@ class SelectImpl<R extends Record, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
return getDelegate().fetchOne();
}
@Override
public final Map<String, Object> fetchOneMap() {
return getDelegate().fetchOneMap();
}
@Override
public final Object[] fetchOneArray() {
return getDelegate().fetchOneArray();
}
@Override
public final <E> E fetchOneInto(Class<? extends E> type) {
return getDelegate().fetchOneInto(type);
}
@Override
public final <Z extends Record> Z fetchOneInto(Table<Z> table) {
return getDelegate().fetchOneInto(table);
}
@Override
public final <T> T fetchAny(Field<T> field) {
return getDelegate().fetchAny(field);
}
@Override
public final <T> T fetchAny(Field<?> field, Class<? extends T> type) {
return getDelegate().fetchAny(field, type);
}
@Override
public final <T, U> U fetchAny(Field<T> field, Converter<? super T, U> converter) {
return getDelegate().fetchAny(field, converter);
}
@Override
public final Object fetchAny(int fieldIndex) {
return getDelegate().fetchAny(fieldIndex);
}
@Override
public final <T> T fetchAny(int fieldIndex, Class<? extends T> type) {
return getDelegate().fetchAny(fieldIndex, type);
}
@Override
public final <U> U fetchAny(int fieldIndex, Converter<?, U> converter) {
return getDelegate().fetchAny(fieldIndex, converter);
}
@Override
public final Object fetchAny(String fieldName) {
return getDelegate().fetchAny(fieldName);
}
@Override
public final <T> T fetchAny(String fieldName, Class<? extends T> type) {
return getDelegate().fetchAny(fieldName, type);
}
@Override
public final <U> U fetchAny(String fieldName, Converter<?, U> converter) {
return getDelegate().fetchAny(fieldName, converter);
}
@Override
public final R fetchAny() {
return getDelegate().fetchAny();
}
@Override
public final Map<String, Object> fetchAnyMap() {
return getDelegate().fetchAnyMap();
}
@Override
public final Object[] fetchAnyArray() {
return getDelegate().fetchAnyArray();
}
@Override
public final <E> E fetchAnyInto(Class<? extends E> type) {
return getDelegate().fetchAnyInto(type);
}
@Override
public final <Z extends Record> Z fetchAnyInto(Table<Z> table) {
return getDelegate().fetchAnyInto(table);
}
@Override
public final <K> Map<K, R> fetchMap(Field<K> key) {
return getDelegate().fetchMap(key);
@ -2417,11 +2502,6 @@ class SelectImpl<R extends Record, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
return getDelegate().fetchMaps();
}
@Override
public final Map<String, Object> fetchOneMap() {
return getDelegate().fetchOneMap();
}
@Override
public final <K> Map<K, Result<R>> fetchGroups(Field<K> key) {
return getDelegate().fetchGroups(key);
@ -2507,26 +2587,11 @@ class SelectImpl<R extends Record, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
return getDelegate().fetchArray(field, converter);
}
@Override
public final Object[] fetchOneArray() {
return getDelegate().fetchOneArray();
}
@Override
public final <T> List<T> fetchInto(Class<? extends T> type) {
return getDelegate().fetchInto(type);
}
@Override
public final <E> E fetchOneInto(Class<? extends E> type) {
return getDelegate().fetchOneInto(type);
}
@Override
public final <Z extends Record> Z fetchOneInto(Table<Z> table) {
return getDelegate().fetchOneInto(table);
}
@Override
public final <Z extends Record> Result<Z> fetchInto(Table<Z> table) {
return getDelegate().fetchInto(table);