update method
Update some datapoints in the database
Implementation
Future<int> update(
{required String table,
required Map<String, String?> row,
required String where,
bool verbose = false}) async {
/// [table] is the table to use, [row] is a map of the data to update
/// and [where] the sql where clause
///
/// Returns a future with a count of the updated rows
var updated = 0;
await _mutex.synchronized(() async {
if (!_isReady) {
throw DatabaseNotReady();
}
final timer = Stopwatch()..start();
try {
var pairs = "";
final n = row.length - 1;
var i = 0;
final datapoint = <String?>[];
final buf = StringBuffer();
for (final el in row.keys) {
buf..write("$pairs")..write("$el")..write("= ?");
pairs = buf.toString();
datapoint.add(row[el]);
if (i < n) {
pairs = ", ";
}
i++;
}
final q = 'UPDATE $table SET $pairs WHERE $where';
await _db!.transaction((txn) async {
updated = await txn.rawUpdate(q, datapoint);
}).catchError((dynamic e) => throw WriteQueryException(
"Can not update data in table $table $e"));
final qStr = "$q $datapoint";
timer.stop();
_changeFeedController.sink.add(DatabaseChangeEvent(
type: DatabaseChange.update,
value: updated,
query: qStr,
table: table,
data: row,
executionTime: timer.elapsedMicroseconds));
if (verbose) {
final msg = "$q $row in ${timer.elapsedMilliseconds} ms";
print(msg);
}
return updated;
} catch (e) {
rethrow;
}
});
return updated;
}