updateMany method

Future<int> updateMany(
  1. List<MssqlKeyedPatch> patches, {
  2. int? expectAffected,
})

Staging JOIN update of patches by their keys.

Temp table + INSERT + UPDATE on this session, dropped in finally. expectAffected wraps the UPDATE in a guard that rolls back on mismatch.

Implementation

Future<int> updateMany(
  List<MssqlKeyedPatch> patches, {
  int? expectAffected,
}) async {
  if (patches.isEmpty) return 0;
  final stamped = <MssqlKeyedPatch>[
    for (final patch in patches)
      MssqlKeyedPatch(key: patch.key, patch: _stampUpdate(patch.patch)),
  ];
  Future<int> run(MssqlSession s) => MssqlBulkWriter<TRow>(
    s,
    binding: binding,
    dialect: dialect,
    clock: clock,
    changes: changes,
    scopes: scopes,
  )._updateMany(stamped);

  if (expectAffected != null) {
    final outcome = await _engine.guard(
      operation: 'updateMany',
      expected: expectAffected,
      write: (engine) async {
        final inner = await run(engine.session);
        return MssqlWriteOutcome(
          affectedRows: inner,
          affectedRowsSource: MssqlAffectedRowsSource.outputRows,
        );
      },
    );
    return outcome.affectedRows;
  }
  return _transact(run);
}