batchedInsertOrReplace method

  1. @override
List<Statement> batchedInsertOrReplace(
  1. QualifiedTablename table,
  2. List<String> columns,
  3. List<Map<String, Object?>> records,
  4. List<String> conflictCols,
  5. List<String> updateCols,
  6. int maxSqlParameters,
)
override

Inserts a batch of rows into a table, replacing them if they already exist.

Implementation

@override
List<Statement> batchedInsertOrReplace(
  QualifiedTablename table,
  List<String> columns,
  List<Map<String, Object?>> records,
  List<String> conflictCols,
  List<String> updateCols,
  int maxSqlParameters,
) {
  final baseSql =
      '''INSERT INTO $table (${columns.map(quote).join(', ')}) VALUES ''';
  final statements = prepareInsertBatchedStatements(
    baseSql,
    columns,
    records,
    maxSqlParameters,
  );
  return statements
      .map(
        (stmt) => Statement(
          '''
      ${stmt.sql}
      ON CONFLICT (${conflictCols.map(quote).join(', ')}) DO UPDATE
      SET ${updateCols.map((col) => '${quote(col)} = EXCLUDED.${quote(col)}').join(', ')};
    ''',
          stmt.args,
        ),
      )
      .toList();
}