SqlBuilder.update constructor

SqlBuilder.update(
  1. String table,
  2. Map<String, Object?> values, {
  3. String? where,
  4. List<Object?>? whereArgs,
  5. ConflictAlgorithm? conflictAlgorithm,
})

Convenience method for updating rows in the database.

@param table the table to update in @param values a map from column names to new column values. null is a valid value that will be translated to NULL. @param whereClause the optional WHERE clause to apply when updating. Passing null will update all rows. @param whereArgs You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings. @param conflictAlgorithm for update conflict resolver

Implementation

SqlBuilder.update(String table, Map<String, Object?> values,
    {String? where,
    List<Object?>? whereArgs,
    ConflictAlgorithm? conflictAlgorithm}) {
  if (values.isEmpty) {
    throw ArgumentError('Empty values');
  }
  checkWhereArgs(whereArgs);

  final update = StringBuffer();
  update.write('UPDATE');
  if (conflictAlgorithm != null) {
    update.write(' ${_conflictValues[conflictAlgorithm.index]}');
  }
  update.write(' ${_escapeName(table)}');
  update.write(' SET ');

  final bindArgs = <Object?>[];
  var i = 0;

  for (var colName in values.keys) {
    update.write((i++ > 0) ? ', ' : '');
    update.write(_escapeName(colName));
    final value = values[colName];
    if (value != null) {
      checkNonNullValue(value);
      bindArgs.add(value);
      update.write(' = ?');
    } else {
      update.write(' = NULL');
    }
  }

  if (whereArgs != null) {
    bindArgs.addAll(whereArgs);
  }

  _writeClause(update, ' WHERE ', where);

  sql = update.toString();
  arguments = bindArgs;
}