updateWhere method
Writes values to every row matching where, and returns how many
changed.
The counterpart to deleteWhere, and it exists for the same reason that one does. Dropping to MssqlUpdate to write a set of rows works, but it leaves the repository's two obligations behind: the updated-at column is not stamped, and the scopes are not applied — so a soft-deleting table would have its trashed rows quietly updated along with the live ones.
values are column names to values, bound through the binding, so a null
is typed and a converter is applied. An expression passes through as SQL:
{'Hits': MssqlArithmetic(Col('Hits'), MssqlArithmeticOperator.add, 1)}
compiles to
[Hits] = [Hits] + @p0.
Implementation
Future<int> updateWhere(
MssqlCondition where,
Map<String, Object?> values,
) async {
_checkDecimalAgreement();
if (values.isEmpty) {
throw ArgumentError.value(
values,
'values',
'Updating ${binding.qualifiedName} would write no columns.',
);
}
_checkColumnSubset(values.keys.toSet());
final bound = _stampUpdate(
MssqlWriteAssignments(<String, MssqlWriteValue>{
for (final entry in values.entries)
// An expression is SQL the caller wrote; only a plain value is
// bound through the column's own codec.
entry.key: entry.value is MssqlExpression
? MssqlServerValue(entry.value! as MssqlExpression)
: MssqlBoundValue(binding.column(entry.key)!.bind(entry.value)),
}),
);
final engine = await _engine();
final outcome = await engine.updateWhere(
operation: 'updateWhere',
values: bound,
where: <MssqlCondition>[where],
scopes: _scopeConditions,
);
return outcome.affectedRows;
}