feat(data): add database abstractions and SQLite binding
IDatabase/IDbSession/IDbTransaction with positional $n parameters, typed DbParam and DbRow, SqlDialect helpers, and a P/Invoke binding to the system SQLite (winsqlite3 / libsqlite3) with WAL pragmas, statement cache, serialized writes, busy retry, interrupt-based cancellation, VACUUM INTO backups and restore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
14 files changed
+1880
No files matched your search
@@ -0,0 +1,60 @@
|
||||
namespace Titano.Data;
|
||||
|
||||
/// <summary>A database (SQLite file or PostgreSQL server) that hands out pooled sessions (BRIEF §6.1).</summary>
|
||||
public interface IDatabase : IAsyncDisposable
|
||||
{
|
||||
Dialect Dialect { get; }
|
||||
|
||||
/// <summary>Rents a connection from the pool; dispose the session to return it.</summary>
|
||||
Task<IDbSession> OpenAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One pooled connection. SQL uses positional <c>$1…$n</c> placeholders on both dialects. A session is not
|
||||
/// thread-safe: run one command at a time and finish (or dispose) a query enumeration before the next command.
|
||||
/// </summary>
|
||||
public interface IDbSession : IAsyncDisposable
|
||||
{
|
||||
Dialect Dialect { get; }
|
||||
|
||||
/// <summary>True between <see cref="BeginAsync(CancellationToken)"/> and commit/rollback.</summary>
|
||||
bool InTransaction { get; }
|
||||
|
||||
/// <summary>Runs one statement and returns the number of rows it inserted, updated or deleted.</summary>
|
||||
Task<int> ExecuteAsync(string sql, DbParam[] parameters, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Streams the rows of one statement.</summary>
|
||||
IAsyncEnumerable<DbRow> QueryAsync(string sql, DbParam[] parameters, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Runs a parameterless multi-statement script (migrations, maintenance).</summary>
|
||||
Task ExecuteScriptAsync(string script, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Starts a transaction (<c>BEGIN IMMEDIATE</c> on SQLite, so writers queue up front instead of failing later).
|
||||
/// Nested transactions are not supported.
|
||||
/// </summary>
|
||||
Task<IDbTransaction> BeginAsync(CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Inserts many rows: <c>COPY … FROM STDIN</c> on PostgreSQL, multi-row INSERTs in one transaction on SQLite.
|
||||
/// Each row array is consumed before the next one is requested, so producers may reuse it.
|
||||
/// </summary>
|
||||
Task<long> BulkInsertAsync(string table, IReadOnlyList<string> columns, IAsyncEnumerable<object?[]> rows, CancellationToken cancellationToken);
|
||||
|
||||
Task<int> ExecuteAsync(string sql, params DbParam[] parameters) => ExecuteAsync(sql, parameters, CancellationToken.None);
|
||||
|
||||
IAsyncEnumerable<DbRow> QueryAsync(string sql, params DbParam[] parameters) => QueryAsync(sql, parameters, CancellationToken.None);
|
||||
|
||||
Task<IDbTransaction> BeginAsync() => BeginAsync(CancellationToken.None);
|
||||
|
||||
Task<long> BulkInsertAsync(string table, IReadOnlyList<string> columns, IAsyncEnumerable<object?[]> rows) =>
|
||||
BulkInsertAsync(table, columns, rows, CancellationToken.None);
|
||||
}
|
||||
|
||||
/// <summary>An open transaction; disposing it without <see cref="CommitAsync"/> rolls it back.</summary>
|
||||
public interface IDbTransaction : IAsyncDisposable
|
||||
{
|
||||
Task CommitAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
Task RollbackAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
Reference in new issue
Block a user