@SuppressWarnings("unchecked") private final Merge<R> toMerge(Configuration configuration) { Table<R> i = getInto(); if (i.getPrimaryKey() != null) { Condition condition = null; List<Field<?>> key = new ArrayList<Field<?>>(); for (Field<?> f : i.getPrimaryKey().getFields()) { Field<Object> field = (Field<Object>) f; Field<Object> value = (Field<Object>) insertMaps.getMap().get(field); key.add(value); Condition other = field.equal(value); if (condition == null) { condition = other; } else { condition = condition.and(other); } } MergeOnConditionStep<R> on = create(configuration).mergeInto(i).usingDual().on(condition); // [#1295] Use UPDATE clause only when with ON DUPLICATE KEY UPDATE, // not with ON DUPLICATE KEY IGNORE MergeNotMatchedStep<R> notMatched = on; if (onDuplicateKeyUpdate) { notMatched = on.whenMatchedThenUpdate().set(updateMap); } return notMatched .whenNotMatchedThenInsert(insertMaps.getMap().keySet()) .values(insertMaps.getMap().values()); } else { throw new IllegalStateException( "The ON DUPLICATE KEY IGNORE/UPDATE clause cannot be simulated when inserting into non-updatable tables : " + getInto()); } }
/** Extracted method for type-safety */ private <Z> Condition condition(Table<?> pivot, Field<Z> field) { return field.equal(pivot.field(field)); }