onDuplicateKeyUpdate method
MySQL/MariaDB update on any duplicate unique key, matching native semantics.
set receives the existing row and the proposed incoming row. It must
return at least one assignment; this method does not execute the insert.
Implementation
Mutation<F> onDuplicateKeyUpdate({
required List<Assignment> Function(F existing, F incoming) set,
}) {
final fieldsFactory = this.fieldsFactory;
if (mutationKind != MutationKind.insert || fieldsFactory == null) {
throw const OrmException(
'MUTATION.CONFLICT',
'Upsert applies to an insert.',
);
}
final incoming = fieldsFactory(TableRef(queryState.source.schema));
final assignments = set(queryFields, incoming);
if (assignments.isEmpty) {
throw const OrmException(
'MUTATION.EMPTY',
'Conflict update needs assignments.',
);
}
return Mutation.internal(
database,
queryFields,
queryState,
mutationKind,
writeAssignments,
fieldsFactory: fieldsFactory,
conflict: _Conflict(
const [],
List.unmodifiable(assignments),
incoming.table,
),
);
}