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
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>
99 lines
5.2 KiB
C#
99 lines
5.2 KiB
C#
using System.Data.Common;
|
|
using Khaemhat.Data.Schema;
|
|
|
|
namespace Khaemhat.Data;
|
|
|
|
/// <summary>
|
|
/// What differs between SQL engines (SPEC §5.1): quoting, parameters, paging, insert/upsert forms, case-insensitive
|
|
/// search, advisory locks, value conversion and physical types. Everything above the providers uses only this
|
|
/// interface, so no code outside <c>Khaemhat.Data.Sqlite</c> and <c>Khaemhat.Data.PostgreSql</c> knows the engine.
|
|
/// </summary>
|
|
public interface ISqlDialect
|
|
{
|
|
/// <summary>The engine.</summary>
|
|
DbEngine Engine { get; }
|
|
|
|
/// <summary>The current UTC timestamp, in the same form the dialect stores <see cref="ColumnType.DateTimeUtc"/>.</summary>
|
|
string CurrentTimestampUtc { get; }
|
|
|
|
/// <summary>Quotes a validated identifier.</summary>
|
|
/// <param name="identifier">The identifier (snake_case).</param>
|
|
/// <returns>The quoted identifier.</returns>
|
|
string Quote(string identifier);
|
|
|
|
/// <summary>The placeholder of a named parameter, for example <c>@p0</c>.</summary>
|
|
/// <param name="name">The parameter name without prefix.</param>
|
|
/// <returns>The placeholder.</returns>
|
|
string Parameter(string name);
|
|
|
|
/// <summary>The paging clause, appended after <c>ORDER BY</c>.</summary>
|
|
/// <param name="offset">Rows to skip.</param>
|
|
/// <param name="limit">Rows to return.</param>
|
|
/// <returns>The clause, starting with a space.</returns>
|
|
string Paging(int offset, int limit);
|
|
|
|
/// <summary>An insert that returns the generated key.</summary>
|
|
/// <param name="table">The table.</param>
|
|
/// <param name="columns">The inserted columns.</param>
|
|
/// <param name="parameters">The parameter placeholders, one per column.</param>
|
|
/// <param name="keyColumn">The generated key column.</param>
|
|
/// <returns>The statement.</returns>
|
|
string InsertReturningId(string table, IReadOnlyList<string> columns, IReadOnlyList<string> parameters, string keyColumn);
|
|
|
|
/// <summary>An insert that updates the existing row on a conflict of <paramref name="conflictColumns"/>.</summary>
|
|
/// <param name="table">The table.</param>
|
|
/// <param name="columns">The inserted columns.</param>
|
|
/// <param name="parameters">The parameter placeholders, one per column.</param>
|
|
/// <param name="conflictColumns">The columns of the unique key.</param>
|
|
/// <param name="updateColumns">The columns to update on conflict.</param>
|
|
/// <returns>The statement.</returns>
|
|
string Upsert(
|
|
string table, IReadOnlyList<string> columns, IReadOnlyList<string> parameters,
|
|
IReadOnlyList<string> conflictColumns, IReadOnlyList<string> updateColumns);
|
|
|
|
/// <summary>A case-insensitive <c>LIKE</c> with <c>\</c> as escape character.</summary>
|
|
/// <param name="column">The column (or expression).</param>
|
|
/// <param name="pattern">The pattern, normally a parameter.</param>
|
|
/// <returns>The condition.</returns>
|
|
SqlFragment CaseInsensitiveLike(SqlFragment column, SqlFragment pattern);
|
|
|
|
/// <summary>
|
|
/// A statement that takes a lock held until the end of the current transaction (numerators, closings, scheduler);
|
|
/// empty where writes are already serialized.
|
|
/// </summary>
|
|
/// <param name="key">The lock key, for example <c>numerator:FV:2026</c>.</param>
|
|
/// <returns>The statement, possibly empty.</returns>
|
|
SqlFragment AdvisoryLock(string key);
|
|
|
|
/// <summary>Converts a CLR value to what the driver stores for <paramref name="type"/>; <c>null</c> becomes <see cref="DBNull"/>.</summary>
|
|
/// <param name="value">The value.</param>
|
|
/// <param name="type">The logical type.</param>
|
|
/// <returns>The value for the driver.</returns>
|
|
object ToDbValue(object? value, ColumnType type);
|
|
|
|
/// <summary>Converts a value read from the driver to the canonical CLR type of <paramref name="type"/> (see <see cref="ColumnTypes.ClrType"/>).</summary>
|
|
/// <param name="value">The value read.</param>
|
|
/// <param name="type">The logical type.</param>
|
|
/// <returns>The CLR value, or <see langword="null"/>.</returns>
|
|
object? FromDbValue(object? value, ColumnType type);
|
|
|
|
/// <summary>The physical type of a column.</summary>
|
|
/// <param name="type">The logical type.</param>
|
|
/// <param name="length">The length for <see cref="ColumnType.String"/>.</param>
|
|
/// <param name="scale">The scale for <see cref="ColumnType.Decimal"/>.</param>
|
|
/// <returns>The SQL type.</returns>
|
|
string SqlType(ColumnType type, int? length, int? scale);
|
|
|
|
/// <summary>A constant as SQL literal, for DDL defaults only (values in statements are always parameters).</summary>
|
|
/// <param name="value">The constant.</param>
|
|
/// <param name="type">The logical type.</param>
|
|
/// <returns>The literal.</returns>
|
|
string Literal(object? value, ColumnType type);
|
|
|
|
/// <summary>Sets the value (converted with <see cref="ToDbValue"/>) and the driver type of a parameter.</summary>
|
|
/// <param name="parameter">The parameter.</param>
|
|
/// <param name="value">The CLR value.</param>
|
|
/// <param name="type">The logical type, or <see langword="null"/> to infer it from the value.</param>
|
|
void ConfigureParameter(DbParameter parameter, object? value, ColumnType? type);
|
|
}
|