diff --git a/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java b/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java index c46a9f62a6..a88c2722fe 100644 --- a/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java @@ -48,6 +48,7 @@ import static org.jooq.Clause.INSERT_RETURNING; import static org.jooq.Clause.INSERT_SELECT; // ... // ... +import static org.jooq.SQLDialect.DERBY; import static org.jooq.SQLDialect.H2; import static org.jooq.SQLDialect.MARIADB; // ... @@ -121,9 +122,10 @@ import org.jooq.tools.StringUtils; */ final class InsertQueryImpl extends AbstractStoreQuery implements InsertQuery { - private static final long serialVersionUID = 4466005417945353842L; - private static final Clause[] CLAUSES = { INSERT }; - private static final Set SUPPORT_INSERT_IGNORE = SQLDialect.supportedBy(MARIADB, MYSQL); + private static final long serialVersionUID = 4466005417945353842L; + private static final Clause[] CLAUSES = { INSERT }; + private static final Set SUPPORT_INSERT_IGNORE = SQLDialect.supportedBy(MARIADB, MYSQL); + private static final Set NO_SUPPORT_DERIVED_COLUMN_LIST_IN_MERGE_USING = SQLDialect.supportedBy(DERBY, H2); private final FieldMapForUpdate updateMap; private final FieldMapsForInsert insertMaps; @@ -819,12 +821,24 @@ final class InsertQueryImpl extends AbstractStoreQuery impl || onConstraint != null || !table().getKeys().isEmpty()) { - // [#6375] INSERT .. VALUES and INSERT .. SELECT distinction also in MERGE - Table t = select == null - ? dual() - : select.asTable("t", fieldNameStrings(insertMaps.fields().toArray(EMPTY_FIELD))); + Select s = select; - MergeOnConditionStep on = select == null + // [#10461] Multi row inserts need to be emulated using select + if (s == null && insertMaps.maps().size() > 1) + s = insertMaps.insertSelect(); + + // [#6375] INSERT .. VALUES and INSERT .. SELECT distinction also in MERGE + Table t; + if (s != null) { + t = s.asTable("t", fieldNameStrings(insertMaps.fields().toArray(EMPTY_FIELD))); + + if (NO_SUPPORT_DERIVED_COLUMN_LIST_IN_MERGE_USING.contains(configuration.dialect())) + t = selectFrom(t).asTable("t"); + } + else + t = dual(); + + MergeOnConditionStep on = s == null ? configuration.dsl().mergeInto(table()) .usingDual() .on(matchByConflictingKeys(configuration, insertMaps.lastMap())) @@ -840,7 +854,7 @@ final class InsertQueryImpl extends AbstractStoreQuery impl ? on.whenMatchedAnd(condition.getWhere()).thenUpdate().set(updateMap) : on.whenMatchedThenUpdate().set(updateMap); - return select == null + return s == null ? notMatched.whenNotMatchedThenInsert(insertMaps.fields()) .values(insertMaps.lastMap().values()) : notMatched.whenNotMatchedThenInsert(insertMaps.fields())