From 68000eb6fccd217e8f04c556cc2a1d4bacb5897a Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 19 Aug 2021 11:21:02 +0200 Subject: [PATCH] [jOOQ/jOOQ#11120] Add DSLContext.fetchMap(ResultQuery>) and fetchGroups(ResultQuery>) --- jOOQ/src/main/java/org/jooq/DSLContext.java | 49 +++++++++++++++++++ .../java/org/jooq/impl/DefaultDSLContext.java | 15 ++++++ 2 files changed, 64 insertions(+) diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index 75e49c623a..5635b015e0 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -12607,6 +12607,55 @@ public interface DSLContext extends Scope { @Support List fetchValues(TableField 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. + *

+ * 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. + *

+ * 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()}. + *

+ * The resulting map is iteration order preserving. + * + * @return A Map containing grouped results. This will never be + * null. + * @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 + Map fetchMap(ResultQuery> 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. + *

+ * Unlike {@link #fetchMap(ResultQuery)}, this method allows for non-unique + * keys in the result set. + *

+ * 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()}. + *

+ * The resulting map is iteration order preserving. + * + * @return A Map containing grouped results. This will never be + * null. + * @throws DataAccessException if something went wrong executing the query + * @see ResultQuery#fetchGroups(Field, Field) + */ + @NotNull + @Support + Map> fetchGroups(ResultQuery> query) throws DataAccessException; + /** * Execute a "Query by Example" (QBE) based on an example record. * diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index c12b9812f8..0b77354310 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -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 Map fetchMap(ResultQuery> query) { + return Tools.attach(query, configuration(), () -> query.collect(intoMap())); + } + + @Override + public Map> fetchGroups(ResultQuery> query) { + return Tools.attach(query, configuration(), () -> query.collect(intoGroups())); + } + private final > T value1(R record) { if (record == null) return null;