update method

Future<int> update(
  1. bool where(
    1. Row
    ),
  2. Map<String, dynamic> updates
)

Update rows matching where. Returns affected count.

Implementation

Future<int> update(
  bool Function(Row) where,
  Map<String, dynamic> updates,
) async {
  int count = 0;
  for (final pid in await _allPageIds()) {
    final rows    = await _readPageRows(pid);
    bool changed  = false;
    for (final row in rows) {
      if (where(row)) {
        for (final entry in indexes.entries) {
          final old = row[entry.key];
          if (old != null) entry.value.delete(old, row.id);
        }
        for (final e in updates.entries) {
          row.values[e.key] = e.value;
        }
        // Coerce updated values to match the column schema types
        meta.schema.validate(row.values);
        for (final entry in indexes.entries) {
          final nv = row[entry.key];
          if (nv != null) entry.value.insert(nv, row.id);
        }
        count++;
        changed = true;
      }
    }
    if (changed) await _writePageRows(pid, rows);
  }
  return count;
}