[#6358] Add ResultQuery.fetchSet(RecordMapper)

This commit is contained in:
lukaseder 2018-02-20 12:45:23 +01:00
parent f5c4fa2929
commit 381a218038
4 changed files with 42 additions and 18 deletions

View File

@ -2809,6 +2809,14 @@ public interface Result<R extends Record> extends List<R>, Attachable {
<T, U> U[] intoArray(Field<T> field, Converter<? super T, ? extends U> converter) throws IllegalArgumentException,
DataTypeException;
/**
* Map results into a custom mapper callback.
*
* @param mapper The mapper callback
* @return The custom mapped records
*/
<E> Set<E> intoSet(RecordMapper<? super R, E> mapper);
/**
* Return all values for a field index from the result.
*

View File

@ -3418,6 +3418,15 @@ public interface ResultQuery<R extends Record> extends Query, Iterable<R> {
*/
<T, U> U[] fetchArray(Field<T> field, Converter<? super T, ? extends U> converter) throws DataAccessException;
/**
* Fetch results into a custom mapper callback.
*
* @param mapper The mapper callback
* @return The result. This will never be <code>null</code>.
* @throws DataAccessException if something went wrong executing the query
*/
<E> Set<E> fetchSet(RecordMapper<? super R, E> mapper) throws DataAccessException;
/**
* Execute the query and return all values for a field index from the
* generated result.

View File

@ -86,6 +86,7 @@ import org.jooq.Results;
import org.jooq.SQLDialect;
import org.jooq.Table;
import org.jooq.conf.SettingsTools;
import org.jooq.exception.DataAccessException;
import org.jooq.tools.Convert;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.jdbc.MockResultSet;
@ -1349,6 +1350,11 @@ abstract class AbstractResultQuery<R extends Record> extends AbstractQuery imple
return fetch().intoArray(field, converter);
}
@Override
public final <E> Set<E> fetchSet(RecordMapper<? super R, E> mapper) throws DataAccessException {
return fetch().intoSet(mapper);
}
@Override
public final Set<?> fetchSet(int fieldIndex) {
return fetch().intoSet(fieldIndex);

View File

@ -343,9 +343,8 @@ final class ResultImpl<R extends Record> implements Result<R> {
public final List<?> getValues(int fieldIndex) {
List<Object> result = new ArrayList<Object>(size());
for (R record : this) {
for (R record : this)
result.add(record.get(fieldIndex));
}
return result;
}
@ -1750,9 +1749,8 @@ final class ResultImpl<R extends Record> implements Result<R> {
public final List<Map<String, Object>> intoMaps() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (R record : this) {
for (R record : this)
list.add(record.intoMap());
}
return list;
}
@ -2567,6 +2565,16 @@ final class ResultImpl<R extends Record> implements Result<R> {
return Convert.convertArray(intoArray(field), converter);
}
@Override
public final <E> Set<E> intoSet(RecordMapper<? super R, E> mapper) {
Set<E> result = new LinkedHashSet<E>();
for (R record : this)
result.add(mapper.map(record));
return result;
}
@Override
public final Set<?> intoSet(int fieldIndex) {
return new LinkedHashSet<Object>(getValues(fieldIndex));
@ -2756,9 +2764,8 @@ final class ResultImpl<R extends Record> implements Result<R> {
List<E> list = new ArrayList<E>(size());
RecordMapper<R, E> mapper = Tools.configuration(this).recordMapperProvider().provide(fields, type);
for (R record : this) {
for (R record : this)
list.add(mapper.map(record));
}
return list;
}
@ -2767,18 +2774,16 @@ final class ResultImpl<R extends Record> implements Result<R> {
public final <Z extends Record> Result<Z> into(Table<Z> table) {
Result<Z> list = new ResultImpl<Z>(configuration(), table.fields());
for (R record : this) {
for (R record : this)
list.add(record.into(table));
}
return list;
}
@Override
public final <H extends RecordHandler<? super R>> H into(H handler) {
for (R record : this) {
for (R record : this)
handler.next(record);
}
return handler;
}
@ -2792,9 +2797,8 @@ final class ResultImpl<R extends Record> implements Result<R> {
public final <E> List<E> map(RecordMapper<? super R, E> mapper) {
List<E> result = new ArrayList<E>();
for (R record : this) {
for (R record : this)
result.add(mapper.map(record));
}
return result;
}
@ -2897,13 +2901,10 @@ final class ResultImpl<R extends Record> implements Result<R> {
@Override
public final Result<R> intern(int... fieldIndexes) {
for (int fieldIndex : fieldIndexes) {
if (fields.fields[fieldIndex].getType() == String.class) {
for (Record record : this) {
for (int fieldIndex : fieldIndexes)
if (fields.fields[fieldIndex].getType() == String.class)
for (Record record : this)
((AbstractRecord) record).intern0(fieldIndex);
}
}
}
return this;
}