namespace Titano.Data; /// A database (SQLite file or PostgreSQL server) that hands out pooled sessions (BRIEF §6.1). public interface IDatabase : IAsyncDisposable { Dialect Dialect { get; } /// Rents a connection from the pool; dispose the session to return it. Task OpenAsync(CancellationToken cancellationToken); } /// /// One pooled connection. SQL uses positional $1…$n 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. /// public interface IDbSession : IAsyncDisposable { Dialect Dialect { get; } /// True between and commit/rollback. bool InTransaction { get; } /// Runs one statement and returns the number of rows it inserted, updated or deleted. Task ExecuteAsync(string sql, DbParam[] parameters, CancellationToken cancellationToken); /// Streams the rows of one statement. IAsyncEnumerable QueryAsync(string sql, DbParam[] parameters, CancellationToken cancellationToken); /// Runs a parameterless multi-statement script (migrations, maintenance). Task ExecuteScriptAsync(string script, CancellationToken cancellationToken); /// /// Starts a transaction (BEGIN IMMEDIATE on SQLite, so writers queue up front instead of failing later). /// Nested transactions are not supported. /// Task BeginAsync(CancellationToken cancellationToken); /// /// Inserts many rows: COPY … FROM STDIN 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. /// Task BulkInsertAsync(string table, IReadOnlyList columns, IAsyncEnumerable rows, CancellationToken cancellationToken); Task ExecuteAsync(string sql, params DbParam[] parameters) => ExecuteAsync(sql, parameters, CancellationToken.None); IAsyncEnumerable QueryAsync(string sql, params DbParam[] parameters) => QueryAsync(sql, parameters, CancellationToken.None); Task BeginAsync() => BeginAsync(CancellationToken.None); Task BulkInsertAsync(string table, IReadOnlyList columns, IAsyncEnumerable rows) => BulkInsertAsync(table, columns, rows, CancellationToken.None); } /// An open transaction; disposing it without rolls it back. public interface IDbTransaction : IAsyncDisposable { Task CommitAsync(CancellationToken cancellationToken = default); Task RollbackAsync(CancellationToken cancellationToken = default); }