Files
Chronos/src/Chronos.Data/DbParam.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

126 lines
6.1 KiB
C#

namespace Chronos.Data;
/// <summary>Logical type of a <see cref="DbParam"/>: decides the SQLite binding and the PostgreSQL parameter type.</summary>
public enum DbValueType
{
/// <summary>Inferred from the CLR type of the value; a null stays untyped.</summary>
Auto,
Text,
Int32,
Int64,
Double,
Bool,
Uuid,
/// <summary>An instant, stored in UTC (<c>timestamptz</c> / ISO-8601 text ending in <c>Z</c>).</summary>
Timestamp,
/// <summary>A wall-clock date and time without offset (<c>timestamp</c> / ISO-8601 text without zone).</summary>
LocalDateTime,
Bytes,
/// <summary>JSON text (<c>jsonb</c> on PostgreSQL, <c>TEXT</c> on SQLite).</summary>
Json,
}
/// <summary>
/// A positional statement parameter: the n-th parameter binds to <c>$n</c> in the SQL text on both dialects.
/// Implicit conversions keep the CLR type even for nulls, so <c>(Guid?)null</c> is still a typed uuid.
/// </summary>
public readonly struct DbParam
{
public DbParam(object? value, DbValueType type = DbValueType.Auto, string? name = null)
{
Name = name;
Type = type == DbValueType.Auto && value is not null ? Infer(value) : type;
Value = value is null ? null : Normalize(value, Type);
}
/// <summary>Optional label used in diagnostics only; binding is by position.</summary>
public string? Name { get; }
/// <summary>
/// The value in the canonical CLR form of <see cref="Type"/>: string (Text, Json), int, long, double, bool,
/// Guid, DateTimeOffset (Timestamp), DateTime (LocalDateTime) or byte[].
/// </summary>
public object? Value { get; }
/// <summary>Effective type; <see cref="DbValueType.Auto"/> only for an untyped null.</summary>
public DbValueType Type { get; }
/// <summary>An untyped SQL NULL.</summary>
public static DbParam Null => default;
public static DbParam Text(string? value) => new(value, DbValueType.Text);
public static DbParam Json(string? value) => new(value, DbValueType.Json);
public static DbParam Bytes(byte[]? value) => new(value, DbValueType.Bytes);
/// <summary>Maps a CLR value to the type it binds as; throws for unsupported types.</summary>
public static DbValueType Infer(object value) => value switch
{
string => DbValueType.Text,
int or short or byte or sbyte or ushort => DbValueType.Int32,
long or uint or ulong => DbValueType.Int64,
double or float => DbValueType.Double,
bool => DbValueType.Bool,
Guid => DbValueType.Uuid,
DateTimeOffset => DbValueType.Timestamp,
DateTime d => d.Kind == DateTimeKind.Unspecified ? DbValueType.LocalDateTime : DbValueType.Timestamp,
byte[] => DbValueType.Bytes,
Enum => DbValueType.Int64,
_ => throw new NotSupportedException($"Parameter values of type {value.GetType().Name} are not supported."),
};
private static object Normalize(object value, DbValueType type)
{
var inv = System.Globalization.CultureInfo.InvariantCulture;
return type switch
{
DbValueType.Text or DbValueType.Json => value as string ?? Convert.ToString(value, inv) ?? string.Empty,
DbValueType.Int32 => value is int ? value : Convert.ToInt32(value, inv),
DbValueType.Int64 => value is long ? value : Convert.ToInt64(value, inv),
DbValueType.Double => value is double ? value : Convert.ToDouble(value, inv),
DbValueType.Bool => value is bool ? value : Convert.ToBoolean(value, inv),
DbValueType.Uuid => value is Guid ? value : Guid.Parse(Convert.ToString(value, inv)!),
DbValueType.Timestamp => value switch
{
DateTimeOffset o => o,
DateTime { Kind: DateTimeKind.Unspecified } d => new DateTimeOffset(d, TimeSpan.Zero),
DateTime d => new DateTimeOffset(d.ToUniversalTime()),
_ => throw new NotSupportedException($"Cannot bind {value.GetType().Name} as a timestamp."),
},
DbValueType.LocalDateTime => value switch
{
DateTime d => d,
DateTimeOffset o => o.DateTime,
_ => throw new NotSupportedException($"Cannot bind {value.GetType().Name} as a date-time."),
},
DbValueType.Bytes => value as byte[] ?? throw new NotSupportedException($"Cannot bind {value.GetType().Name} as bytes."),
_ => value,
};
}
public static implicit operator DbParam(string? value) => new(value, DbValueType.Text);
public static implicit operator DbParam(byte[]? value) => new(value, DbValueType.Bytes);
public static implicit operator DbParam(int value) => new(value, DbValueType.Int32);
public static implicit operator DbParam(int? value) => new(value, DbValueType.Int32);
public static implicit operator DbParam(long value) => new(value, DbValueType.Int64);
public static implicit operator DbParam(long? value) => new(value, DbValueType.Int64);
public static implicit operator DbParam(double value) => new(value, DbValueType.Double);
public static implicit operator DbParam(double? value) => new(value, DbValueType.Double);
public static implicit operator DbParam(bool value) => new(value, DbValueType.Bool);
public static implicit operator DbParam(bool? value) => new(value, DbValueType.Bool);
public static implicit operator DbParam(Guid value) => new(value, DbValueType.Uuid);
public static implicit operator DbParam(Guid? value) => new(value, DbValueType.Uuid);
public static implicit operator DbParam(DateTimeOffset value) => new(value, DbValueType.Timestamp);
public static implicit operator DbParam(DateTimeOffset? value) => new(value, DbValueType.Timestamp);
/// <summary>Unspecified-kind values are wall-clock times; UTC/local kinds are instants.</summary>
public static implicit operator DbParam(DateTime value) => new(value);
public static implicit operator DbParam(DateTime? value) => value is { } v ? new(v) : new(null, DbValueType.LocalDateTime);
public override string ToString() => $"{Name ?? "?"}:{Type}={Value ?? "NULL"}";
}