[jOOQ/jOOQ#11120] Add DSLContext.fetchMap(ResultQuery<? extends Record2<K, V>>) and fetchGroups(ResultQuery<? extends Record2<K, V>>)

This commit is contained in:
Lukas Eder 2021-08-19 11:21:02 +02:00
parent bb8d9ff6fd
commit 68000eb6fc
2 changed files with 64 additions and 0 deletions

View File

@ -12607,6 +12607,55 @@ public interface DSLContext extends Scope {
@Support
<T> List<T> fetchValues(TableField<?, T> field) throws DataAccessException;
/**
* Execute the query and return a {@link Map} with the first column as the
* map key and the second column as the map value.
* <p>
* An exception is thrown, if the keys turn out to be non-unique in the
* result set. Use {@link #fetchGroups(ResultQuery)} instead, if your keys
* are non-unique.
* <p>
* Whether this fetches an intermediate {@link Result} (accessible by
* {@link ExecuteListener} implementations), or streams records directly to
* the collector producing the result is governed by
* {@link Settings#getFetchIntermediateResult()}.
* <p>
* The resulting map is iteration order preserving.
*
* @return A Map containing grouped results. This will never be
* <code>null</code>.
* @throws DataAccessException if something went wrong executing the query
* @throws InvalidResultException if the key list is non-unique in the
* result set.
* @see ResultQuery#fetchMap(Field, Field)
*/
@NotNull
@Support
<K, V> Map<K, V> fetchMap(ResultQuery<? extends Record2<K, V>> query) throws DataAccessException;
/**
* Execute the query and return a {@link Map} with the first column as the
* map key and the second column as the map values.
* <p>
* Unlike {@link #fetchMap(ResultQuery)}, this method allows for non-unique
* keys in the result set.
* <p>
* Whether this fetches an intermediate {@link Result} (accessible by
* {@link ExecuteListener} implementations), or streams records directly to
* the collector producing the result is governed by
* {@link Settings#getFetchIntermediateResult()}.
* <p>
* The resulting map is iteration order preserving.
*
* @return A Map containing grouped results. This will never be
* <code>null</code>.
* @throws DataAccessException if something went wrong executing the query
* @see ResultQuery#fetchGroups(Field, Field)
*/
@NotNull
@Support
<K, V> Map<K, List<V>> fetchGroups(ResultQuery<? extends Record2<K, V>> query) throws DataAccessException;
/**
* Execute a "Query by Example" (QBE) based on an example record.
*

View File

@ -41,6 +41,8 @@ import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;
import static org.jooq.Records.intoGroups;
import static org.jooq.Records.intoMap;
import static org.jooq.conf.ParamType.INLINED;
import static org.jooq.conf.ParamType.NAMED;
import static org.jooq.conf.ParamType.NAMED_OR_INLINED;
@ -212,6 +214,7 @@ import org.jooq.Record6;
import org.jooq.Record7;
import org.jooq.Record8;
import org.jooq.Record9;
import org.jooq.Records;
import org.jooq.RenderContext;
import org.jooq.Result;
import org.jooq.ResultQuery;
@ -284,6 +287,8 @@ import org.jooq.tools.jdbc.MockDataProvider;
import org.jooq.tools.jdbc.MockRunnable;
import org.jooq.util.xml.jaxb.InformationSchema;
import org.jetbrains.annotations.NotNull;
import io.r2dbc.spi.ConnectionFactory;
/**
@ -4653,6 +4658,16 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
return fetchValues(select(field).from(field.getTable()));
}
@Override
public <K, V> Map<K, V> fetchMap(ResultQuery<? extends Record2<K, V>> query) {
return Tools.attach(query, configuration(), () -> query.collect(intoMap()));
}
@Override
public <K, V> Map<K, List<V>> fetchGroups(ResultQuery<? extends Record2<K, V>> query) {
return Tools.attach(query, configuration(), () -> query.collect(intoGroups()));
}
private final <T, R extends Record1<T>> T value1(R record) {
if (record == null)
return null;