updateSomeColumnsOf<T> method

Future<bool> updateSomeColumnsOf<T>(
  1. List<String> columnsToUpadate,
  2. List<String> whereColumns,
  3. List<Object> values, {
  4. String whereMcop = 'AND',
})

Updates one or more columns of the entity T.

  • columnsToUpadate: names of the columns to update, in the order they appear at the start of values.
  • whereColumns: names of the columns appearing in the WHERE clause, in the order they appear at the end of values.
  • values: values for columnsToUpadate followed by values for whereColumns.
  • whereMcop: operator used to join multiple WHERE conditions. Defaults to 'AND'. Any SQL boolean operator works: 'OR', 'AND'.

Example:

final ok = await DataAccess.instance.updateSomeColumnsOf<Person>(
  ['salary', 'married', 'year'],
  ['name'],
  [9000000, true, 45, 'Alpha'],
);

Implementation

Future<bool> updateSomeColumnsOf<T>(
  List<String> columnsToUpadate,
  List<String> whereColumns,
  List<Object> values, {
  String whereMcop = 'AND',
}) async {
  int affected = 0;
  await (await db).transaction((txn) async {
    try {
      affected = await txn.rawUpdate(
        'UPDATE ${T.toString()} '
        'SET ${_preparedColumns(columnsToUpadate, ',')} '
        'WHERE ${_preparedColumns(whereColumns, whereMcop)}',
        _checkForBoolAndDateTime(values),
      );
    } catch (e, s) {
      debugPrint('updateSomeColumnsOf failed: $e\n$s');
    }
  });
  return affected > 0;
}