upsert method

  1. @override
Future<int> upsert(
  1. List<Map<String, dynamic>> values, {
  2. required List<String> uniqueBy,
  3. List<String>? update,
})
override

Insert or update (UPSERT) - MySQL: INSERT ... ON DUPLICATE KEY UPDATE

Implementation

@override
Future<int> upsert(
  List<Map<String, dynamic>> values, {
  required List<String> uniqueBy,
  List<String>? update,
}) async {
  if (values.isEmpty) return 0;
  final sql =
      grammar.compileUpsert(_getQueryComponents(), values, uniqueBy, update);
  final bindings = values.expand((e) => e.values).toList() +
      (update != null
          ? values.expand((e) => update.map((c) => e[c])).toList()
          : []);
  final response = await connection.execute(sql, bindings);
  return response.affectedRows ?? 0;
}