executeInsert method

Future<int> executeInsert(
  1. String table,
  2. Map<String, Object?> values, {
  3. OnConflictAction? onConflict,
})

Implementation

Future<int> executeInsert(
  String table,
  Map<String, Object?> values, {
  final OnConflictAction? onConflict,
}) async {
  _assertColumnNames(values);
  final entries = values.entries.toList();
  final columnList = entries.map((e) => e.key).join(',');
  final bindList = entries.map((e) => _bindForEntry(e)).join(',');
  final String onConflictSql;
  if (onConflict is OnConflictActionDoUpdate) {
    final set = entries
        .where((element) => !onConflict.indexColumns.contains(element.key))
        .map((e) => '${e.key} = EXCLUDED.${e.key}')
        .join(', ');
    final where = entries
        .where((element) => onConflict.indexColumns.contains(element.key))
        .map((e) => '$table.${e.key} = EXCLUDED.${e.key}')
        .join(' AND ');
    onConflictSql = ' ON CONFLICT (${onConflict.indexColumns.join(', ')}) '
        ' DO UPDATE SET $set WHERE $where';
  } else {
    onConflictSql = '';
  }
  return await execute(
    'INSERT INTO $table ($columnList) VALUES ($bindList) $onConflictSql',
    values: values.map((key, value) =>
        MapEntry(key, value is CustomBind ? value.value : value)),
    expectedResultCount: 1,
    useExtendedQuery: true,
  );
}