Files
Khaemhat/src/Khaemhat.Data/IUnitOfWork.cs
T
Alby96andClaude Opus 5.5 738c43ba0a
CI / Build and test (SQLite) (pull_request) Successful in 45s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 3s
CI / Docker image (pull_request) Successful in 51s
feat: data layer with SQLite and PostgreSQL providers, migrations and encrypted company registry (M1)
Every later milestone stores its data through this layer, so it has to behave the same on both engines and be safe
by construction:

- Khaemhat.Data: explicit entity maps, typed queries over a safe expression subset, SqlFragment where every
  interpolated value becomes a parameter, unit of work with transactions, repositories with audit columns and
  optimistic concurrency on row_version, retries on transient errors.
- SQLite provider (STRICT tables, WAL, exact decimals as integers scaled by 10^6, Unicode case-insensitive LIKE,
  serialised writes) and PostgreSQL 16+ provider (numeric(19,6), timestamptz, jsonb, advisory locks, database
  creation and server info). Databases are configured from postgres:// and sqlite:// URLs whose password never
  appears in messages.
- C# migrations with system and company catalogues, schema_version with checksums, one transaction per migration
  under a lock, refusal of unknown applied versions; first system tables sys_companies and sys_settings.
- CompanyRegistry stores each company's database URL encrypted with the instance key (AES-256-GCM, HKDF-derived key,
  purpose as associated data), so a copied row cannot be read for another company.
- tools/Khaemhat.DataModelTool, scripts/gen-datamodel.sh and scripts/new-migration.sh: the current schema section of
  docs/DATA-MODEL.md is generated from the migrations and a test fails when it is stale.
- C# 14 binds array.Contains to MemoryExtensions over spans; the query translator unwraps the span conversion.
- Tests run every data scenario on SQLite and PostgreSQL (PostgreSQL in CI); ADR 0018 records where the contracts
  differ from SPEC 5.1. Docs, wiki, changelog and status updated.

Closes #7, #8, #9, #10, #11, #12, #13, #14

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:11:34 +01:00

94 lines
4.5 KiB
C#

using System.Data.Common;
using Khaemhat.Core.Entities;
namespace Khaemhat.Data;
/// <summary>
/// An open connection to one database, with an optional explicit transaction (SPEC §5.1). Application services open
/// one per operation, call <see cref="BeginAsync"/> for writes that must be atomic, and dispose it; a transaction not
/// committed is rolled back on dispose.
/// </summary>
public interface IUnitOfWork : IAsyncDisposable
{
/// <summary>The open connection.</summary>
DbConnection Connection { get; }
/// <summary>The current transaction, if any.</summary>
DbTransaction? Transaction { get; }
/// <summary>The dialect of the database.</summary>
ISqlDialect Dialect { get; }
/// <summary>Starts a transaction. On SQLite it also enters the write scope of the database.</summary>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>A task.</returns>
Task BeginAsync(CancellationToken cancellationToken = default);
/// <summary>Commits the transaction.</summary>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>A task.</returns>
Task CommitAsync(CancellationToken cancellationToken = default);
/// <summary>Rolls back the transaction.</summary>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>A task.</returns>
Task RollbackAsync(CancellationToken cancellationToken = default);
/// <summary>The repository of an entity type.</summary>
/// <typeparam name="T">The entity type, which needs a registered map.</typeparam>
/// <typeparam name="TKey">The key type.</typeparam>
/// <returns>The repository.</returns>
IRepository<T, TKey> Repository<T, TKey>()
where T : class, IEntity<TKey>, new();
/// <summary>Executes a statement that changes data or schema.</summary>
/// <param name="sql">The statement.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The number of affected rows.</returns>
Task<int> ExecuteAsync(SqlFragment sql, CancellationToken cancellationToken = default);
/// <summary>Reads the first row (or <see langword="null"/>), mapped to <typeparamref name="T"/>.</summary>
/// <typeparam name="T">A scalar, a mapped entity, or a class/record whose members match the columns by snake_case name.</typeparam>
/// <param name="sql">The query.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The row, or <see langword="null"/>.</returns>
Task<T?> QuerySingleAsync<T>(SqlFragment sql, CancellationToken cancellationToken = default);
/// <summary>Reads all rows, mapped to <typeparamref name="T"/>.</summary>
/// <typeparam name="T">A scalar, a mapped entity, or a class/record whose members match the columns by snake_case name.</typeparam>
/// <param name="sql">The query.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The rows.</returns>
Task<IReadOnlyList<T>> QueryAsync<T>(SqlFragment sql, CancellationToken cancellationToken = default);
/// <summary>
/// Opens a reader for large results (exports, reports) without loading them in memory. Dispose the reader before
/// running anything else on this unit of work. Values are raw driver values: convert them with
/// <see cref="ISqlDialect.FromDbValue"/>.
/// </summary>
/// <param name="sql">The query.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The reader.</returns>
Task<DbDataReader> StreamAsync(SqlFragment sql, CancellationToken cancellationToken = default);
}
/// <summary>Opens units of work on the system database or on a company database (SPEC §5.1).</summary>
public interface IUnitOfWorkFactory
{
/// <summary>Opens a unit of work.</summary>
/// <param name="scope">The system database, or a company.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The open unit of work.</returns>
Task<IUnitOfWork> CreateAsync(DatabaseScope scope, CancellationToken cancellationToken = default);
}
/// <summary>Finds the database of a scope: the system database, or a company through the registry.</summary>
public interface IDatabaseLocator
{
/// <summary>The database of a scope.</summary>
/// <param name="scope">The scope.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The target.</returns>
ValueTask<DatabaseTarget> LocateAsync(DatabaseScope scope, CancellationToken cancellationToken = default);
}