Path: redb/redb.Export/Providers/IDataProvider.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.Export/Providers/IDataProvider.cs
----
using System.Data.Common;
namespace redb.Export.Providers;
///
/// Abstracts database-specific operations required by the export/import pipeline.
///
/// Each implementation handles connection management, bulk-insert strategy,
/// constraint toggling, and sequence manipulation for its target RDBMS.
///
///
public interface IDataProvider : IAsyncDisposable
{
///
/// Short provider identifier (e.g. "postgres", "mssql").
///
string Name { get; }
///
/// Opens a connection to the database using the supplied connection string.
///
/// ADO.NET connection string.
/// Cancellation token.
Task OpenAsync(string connectionString, CancellationToken ct = default);
///
/// Returns the underlying instance.
/// Must be called after .
///
DbConnection Connection { get; }
///
/// Truncates all REDB tables in the correct foreign-key order, leaving the schema intact.
///
/// Cancellation token.
Task CleanDatabaseAsync(CancellationToken ct = default);
///
/// Returns the current value of the global_identity sequence.
///
/// Cancellation token.
Task GetSequenceValueAsync(CancellationToken ct = default);
///
/// Resets the global_identity sequence to the specified value
/// (typically the value stored in the export footer).
///
/// New sequence value.
/// Cancellation token.
Task SetSequenceValueAsync(long value, CancellationToken ct = default);
// Contract of the pair: GetSequenceValueAsync returns the LAST id handed out, and SetSequenceValueAsync(v) makes
// the NEXT id handed out v + 1. SQL Server's RESTART WITH names the next value itself, so it restarts at v + 1
// (review 2026-09-24: it restarted at v, and the first object saved after an import took an id already in use).
///
/// Starts the import transaction. Everything the import writes from here on - cleaning, rows, the
/// sequence, the constraint mode - commits or rolls back as one: an import that fails half-way (a broken
/// file, a cancelled run, a server error) leaves the database as it was, cleaning included.
///
/// Cancellation token.
Task BeginImportAsync(CancellationToken ct = default);
/// Commits the import transaction and restores the session state the import changed.
/// Cancellation token.
Task CommitImportAsync(CancellationToken ct = default);
///
/// Rolls back whatever the import wrote and restores the session state it changed. Called on every path
/// that did not commit; does nothing after a commit.
///
Task AbortImportAsync();
///
/// Disables foreign-key constraints and triggers so that rows can be
/// bulk-inserted in arbitrary order.
///
/// Cancellation token.
Task DisableConstraintsAsync(CancellationToken ct = default);
///
/// Re-enables foreign-key constraints and triggers after bulk import.
///
/// Cancellation token.
Task EnableConstraintsAsync(CancellationToken ct = default);
///
/// Performs a bulk insert of the supplied
/// into the specified database table using the most efficient provider-specific mechanism
/// (e.g. COPY FROM STDIN for PostgreSQL, SqlBulkCopy for SQL Server).
///
/// Target table name (e.g. "_objects").
/// Data to insert.
/// Cancellation token.
Task BulkInsertAsync(string tableName, System.Data.DataTable data, CancellationToken ct = default);
///
/// Executes a statement that returns no rows (DDL included). Inside an import it runs in the
/// import transaction, so it commits or rolls back with everything else the import wrote.
///
/// Statement text in the provider's dialect.
/// Cancellation token.
Task ExecuteAsync(string sql, CancellationToken ct = default);
///
/// Reads a date column of a redb table into its portable form.
/// PostgreSQL and SQL Server store these columns natively. SQLite stores them as a UTC Julian
/// day (REAL), and the number has to be decoded the way the redb SQLite provider decodes it.
/// The write side of the same seam is , which stores a
/// in the form the provider reads.
///
/// Reader positioned on a row.
/// Ordinal of a non-null date column.
DateTimeOffset ReadDateTimeOffset(DbDataReader reader, int ordinal);
///
/// Reads a uuid-semantic column value into its portable form.
/// PostgreSQL/MSSQL store these columns natively; SQLite stores them as a 16-byte BLOB in
/// RFC 4122 (text) order - _users._hash, _schemes._structure_hash,
/// _objects._hash, _values._unique. Reading such a BLOB through the driver's
/// GetGuid would byte-swap the first three groups; this seam keeps the JSONL canonical.
/// Genuine binary columns (_value_bytes, _values._ByteArray) are NOT uuids and
/// never go through this conversion.
///
Guid GuidFromDb(object raw);
///
/// Converts a portable into the value this provider stores in a
/// uuid-semantic column (see ): the itself for
/// PostgreSQL/MSSQL, the RFC 4122-ordered 16-byte BLOB for SQLite.
///
object GuidToDb(Guid value);
}