namespace Chronos.Data; /// Logical type of a : decides the SQLite binding and the PostgreSQL parameter type. public enum DbValueType { /// Inferred from the CLR type of the value; a null stays untyped. Auto, Text, Int32, Int64, Double, Bool, Uuid, /// An instant, stored in UTC (timestamptz / ISO-8601 text ending in Z). Timestamp, /// A wall-clock date and time without offset (timestamp / ISO-8601 text without zone). LocalDateTime, Bytes, /// JSON text (jsonb on PostgreSQL, TEXT on SQLite). Json, } /// /// A positional statement parameter: the n-th parameter binds to $n in the SQL text on both dialects. /// Implicit conversions keep the CLR type even for nulls, so (Guid?)null is still a typed uuid. /// 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); } /// Optional label used in diagnostics only; binding is by position. public string? Name { get; } /// /// The value in the canonical CLR form of : string (Text, Json), int, long, double, bool, /// Guid, DateTimeOffset (Timestamp), DateTime (LocalDateTime) or byte[]. /// public object? Value { get; } /// Effective type; only for an untyped null. public DbValueType Type { get; } /// An untyped SQL NULL. 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); /// Maps a CLR value to the type it binds as; throws for unsupported types. 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); /// Unspecified-kind values are wall-clock times; UTC/local kinds are instants. 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"}"; }