diff --git a/jooq-dialect/src/main/java/org/jooq/impl/FieldMapsForUpsertReplace.java b/jooq-dialect/src/main/java/org/jooq/impl/FieldMapsForUpsertReplace.java index 72ac591..c854e63 100644 --- a/jooq-dialect/src/main/java/org/jooq/impl/FieldMapsForUpsertReplace.java +++ b/jooq-dialect/src/main/java/org/jooq/impl/FieldMapsForUpsertReplace.java @@ -1,9 +1,5 @@ package org.jooq.impl; -import org.jooq.*; -import org.jooq.Record; -import org.jooq.RenderContext.CastMode; - import java.util.AbstractMap; import java.util.AbstractSet; import java.util.ArrayList; @@ -15,7 +11,14 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; - +import java.util.stream.IntStream; +import org.jooq.Context; +import org.jooq.DataType; +import org.jooq.Field; +import org.jooq.Record; +import org.jooq.RenderContext.CastMode; +import org.jooq.Select; +import org.jooq.Table; import static org.jooq.impl.Keywords.K_VALUES; import static org.jooq.impl.Tools.BooleanDataKey.DATA_STORE_ASSIGNMENT; @@ -72,24 +75,13 @@ public static void toSQLUpsertSelect(Context ctx, Select select) { } public Select upsertSelect() { - Select select = null; - Map, List>> v = valuesFlattened(); - for (int i = 0; i < rows; i++) { - int row = i; - Select iteration = DSL.select(Tools.map( - v.entrySet(), e -> patchDefault0(e.getValue().get(row), e.getKey()) - )); - - if (select == null) { - select = iteration; - } else { - select = select.unionAll(iteration); - } - } - - return select; + return IntStream.range(0, rows) + .mapToObj(row -> (Select) DSL.select(Tools.map(v.entrySet(), + e -> patchDefault0(e.getValue().get(row), e.getKey())))) + .reduce(Select::unionAll) + .orElse(null); } private void toSQL92Values(Context ctx) { diff --git a/jooq-dialect/src/main/java/org/jooq/impl/UpsertReplaceQueryImpl.java b/jooq-dialect/src/main/java/org/jooq/impl/UpsertReplaceQueryImpl.java index dc0418d..b74c40e 100644 --- a/jooq-dialect/src/main/java/org/jooq/impl/UpsertReplaceQueryImpl.java +++ b/jooq-dialect/src/main/java/org/jooq/impl/UpsertReplaceQueryImpl.java @@ -80,7 +80,7 @@ public void accept(Context ctx) { Table t = InlineDerivedTable.inlineDerivedTable(ctx, table(ctx)); if (t instanceof InlineDerivedTable i) { - copy( + try (var copyUpsertReplaceQuery = copy( d -> { if (!d.upsertReplaceMaps.isEmpty()) { Table m = DSL.table(name("t")); @@ -97,7 +97,9 @@ public void accept(Context ctx) { } }, i.table - ).accept0(ctx); + )) { + copyUpsertReplaceQuery.accept0(ctx); + } } else { accept0(ctx); }