Files
Chronos/src/Titano.Data/DatabaseOptions.cs
T
Claude 46cee365dc feat(data): add in-house PostgreSQL wire protocol v3 client
TCP with optional TLS (disable/prefer/require/verify-full), SCRAM-SHA-256,
MD5, cleartext and trust authentication, simple and extended query with a
per-connection LRU of prepared statements, text-format parameters with
binary bytea, OID-based result decoding, COPY FROM STDIN, LISTEN/NOTIFY,
CancelRequest cancellation, typed PostgresException, and a connection
pool with health checks, idle eviction and one retry for reads that hit
a lost connection. DatabaseFactory opens either engine from options.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 09:40:40 +00:00

55 lines
2.7 KiB
C#

using Titano.Data.Postgres;
using Titano.Data.Sqlite;
namespace Titano.Data;
/// <summary>Which engine to open and how to reach it.</summary>
/// <remarks>
/// SQLite: <c>Data Source=&lt;path&gt;|:memory:;Pool Size=4</c>.
/// PostgreSQL: <c>Host=…;Port=5432;Database=…;Username=…;Password=…;SslMode=disable|prefer|require|verify-full</c>
/// plus the optional keys listed on <see cref="PostgresSettings"/>.
/// </remarks>
public sealed record DatabaseOptions
{
public required Dialect Dialect { get; init; }
public required string ConnectionString { get; init; }
public static DatabaseOptions Sqlite(string path) => new()
{
Dialect = Dialect.Sqlite,
ConnectionString = "Data Source='" + path.Replace("'", "''", StringComparison.Ordinal) + "'",
};
/// <summary>A private in-memory SQLite database that lives until the <see cref="IDatabase"/> is disposed.</summary>
public static DatabaseOptions SqliteInMemory() => new() { Dialect = Dialect.Sqlite, ConnectionString = "Data Source=:memory:" };
public static DatabaseOptions Postgres(string connectionString) => new() { Dialect = Dialect.Postgres, ConnectionString = connectionString };
/// <summary>
/// Infers the dialect: a string with <c>Host</c>/<c>Server</c> is PostgreSQL; <c>Data Source</c> or a bare
/// path (no <c>=</c>) is SQLite.
/// </summary>
public static DatabaseOptions Parse(string connectionString)
{
ArgumentException.ThrowIfNullOrWhiteSpace(connectionString);
if (!connectionString.Contains('=', StringComparison.Ordinal)) return Sqlite(connectionString.Trim());
var cs = new ConnectionString(connectionString);
if (cs.Contains("Host") || cs.Contains("Server")) return Postgres(connectionString);
if (cs.Contains("Data Source") || cs.Contains("Filename")) return new() { Dialect = Dialect.Sqlite, ConnectionString = connectionString };
throw new FormatException("The connection string names neither a PostgreSQL host nor a SQLite data source.");
}
}
/// <summary>Builds the <see cref="IDatabase"/> implementation for a set of options.</summary>
public static class DatabaseFactory
{
/// <summary>Creates the database object; SQLite opens the file now, PostgreSQL connects on the first session.</summary>
public static IDatabase Open(DatabaseOptions options) => options.Dialect switch
{
Dialect.Sqlite => SqliteDatabase.Open(SqliteSettings.Parse(options.ConnectionString)),
Dialect.Postgres => new PostgresDatabase(PostgresSettings.Parse(options.ConnectionString)),
_ => throw new ArgumentOutOfRangeException(nameof(options), options.Dialect, "Unknown dialect."),
};
}