Path: redb/redb.SQLite/Data/SqliteBulkOperations.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.SQLite/Data/SqliteBulkOperations.cs
----
using redb.Core.Data;
using redb.Core.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace redb.SQLite.Data
{
///
/// SQLite implementation of IBulkOperations.
/// SQLite has no COPY/binary-import protocol — the fast path is a single
/// transaction wrapping chunked multi-row INSERTs (one prepared statement
/// per chunk). The big win versus N individual statements is the single
/// transaction (one fsync instead of one per row). Parameter count per
/// statement is chunked under SQLite's limit.
///
public class SqliteBulkOperations : IBulkOperations
{
private readonly IRedbConnection _db;
// Conservative cap on bound parameters per statement (SQLite default
// SQLITE_MAX_VARIABLE_NUMBER is 999 on older builds, 32766 on 3.32+).
private const int MaxParamsPerStatement = 900;
public SqliteBulkOperations(IRedbConnection db)
{
_db = db ?? throw new ArgumentNullException(nameof(db));
}
// ===== INSERTS =====
private static readonly string[] ObjectColumns =
{
"_id", "_id_parent", "_id_scheme", "_name", "_id_owner", "_id_who_change",
"_date_create", "_date_modify", "_date_begin", "_date_complete", "_key",
"_value_long", "_value_string", "_value_guid", "_value_bool", "_value_double",
"_value_numeric", "_value_datetime", "_value_bytes", "_note", "_hash", "_value_unique"
};
private static object?[] ObjectRowValues(RedbObjectRow o) => new object?[]
{
o.Id, o.IdParent, o.IdScheme, o.Name, o.IdOwner, o.IdWhoChange,
o.DateCreate, o.DateModify, o.DateBegin, o.DateComplete, o.Key,
o.ValueLong, o.ValueString, o.ValueGuid, o.ValueBool, o.ValueDouble,
o.ValueNumeric, o.ValueDatetime, o.ValueBytes, o.Note, o.Hash, o.ValueUnique
};
private static readonly string[] ValueColumns =
{
"_id", "_id_structure", "_id_object", "_String", "_Long", "_Guid",
"_Double", "_DateTimeOffset", "_Boolean", "_ByteArray", "_Numeric",
"_ListItem", "_Object", "_unique", "_array_parent_id", "_array_index"
};
private static object?[] ValueRowValues(RedbValue v) => new object?[]
{
v.Id, v.IdStructure, v.IdObject, v.String, v.Long, v.Guid,
v.Double, v.DateTimeOffset, v.Boolean, v.ByteArray, v.Numeric,
v.ListItem, v.Object, v.Unique, v.ArrayParentId, v.ArrayIndex
};
public async Task BulkInsertObjectsAsync(IEnumerable objects, CancellationToken cancellationToken = default)
{
var rows = objects.Select(ObjectRowValues).ToList();
await BulkInsertAsync("_objects", ObjectColumns, rows, cancellationToken);
}
public async Task BulkInsertValuesAsync(IEnumerable values, CancellationToken cancellationToken = default)
{
var rows = values.Select(ValueRowValues).ToList();
await BulkInsertAsync("_values", ValueColumns, rows, cancellationToken);
}
///
/// Insert into using chunked
/// multi-row INSERTs inside one transaction.
///
private async Task BulkInsertAsync(string table, string[] columns, List