discontinuedPackage connect_database has been discontinued.

bulkDelete method

Future<Result<int>> bulkDelete(
  1. String tableName,
  2. List ids,
  3. String idColumn, {
  4. int batchSize = 1000,
})

Bulk deletes multiple records using WHERE IN clause. tableName - The table to delete from ids - List of ID values to delete idColumn - The column name to use as identifier (usually primary key) batchSize - Number of IDs to delete per batch (default: 1000)

Implementation

Future<Result<int>> bulkDelete(
  String tableName,
  List<dynamic> ids,
  String idColumn, {
  int batchSize = 1000,
}) async {
  if (ids.isEmpty) return const Success(0);

  int totalAffected = 0;
  int batchCount = (ids.length / batchSize).ceil();

  debugPrint(
      'BulkDelete: Starting ${ids.length} records in $batchCount batches of $batchSize');

  for (var i = 0; i < ids.length; i += batchSize) {
    final end = (i + batchSize < ids.length) ? i + batchSize : ids.length;
    final batch = ids.sublist(i, end);

    debugPrint(
        'BulkDelete: Processing batch ${((i / batchSize) + 1).toInt()}/$batchCount (${batch.length} records)');

    final whereClause = _buildInClause(batch);
    final sql = 'DELETE FROM $tableName WHERE $idColumn IN ($whereClause)';

    debugPrint('BulkDelete: SQL length ${sql.length} characters');
    final result = await odbc.execute(sql);

    if (result.isSuccess()) {
      // DELETE doesn't return affected rows, assume success
      // All IDs in the batch should have been deleted
      totalAffected += batch.length;
      debugPrint(
          'BulkDelete: Batch succeeded, total affected: $totalAffected');
    } else {
      final error = result.exceptionOrNull();
      debugPrint('BulkDelete error: $error');
      return Failure(error ?? Exception('Unknown error in Bulk Delete'));
    }
  }

  debugPrint('BulkDelete: Completed, total affected: $totalAffected');
  return Success(totalAffected);
}