IDatabase/IDbSession/IDbTransaction with positional $n parameters, typed DbParam and DbRow, SqlDialect helpers, and a P/Invoke binding to the system SQLite (winsqlite3 / libsqlite3) with WAL pragmas, statement cache, serialized writes, busy retry, interrupt-based cancellation, VACUUM INTO backups and restore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
136 lines
5.2 KiB
C#
136 lines
5.2 KiB
C#
using System.Globalization;
|
|
|
|
namespace Titano.Data;
|
|
|
|
/// <summary>
|
|
/// Canonical text forms of values without a native SQLite type, also used for PostgreSQL text parameters.
|
|
/// Date-times are fixed-width ISO-8601 with microseconds, so text order equals chronological order.
|
|
/// </summary>
|
|
internal static class TextValues
|
|
{
|
|
private const string TimestampFormat = "yyyy-MM-dd'T'HH:mm:ss.ffffff'Z'";
|
|
private const string LocalFormat = "yyyy-MM-dd'T'HH:mm:ss.ffffff";
|
|
|
|
public static string FormatTimestamp(DateTimeOffset value) => value.UtcDateTime.ToString(TimestampFormat, CultureInfo.InvariantCulture);
|
|
|
|
public static string FormatLocal(DateTime value) => value.ToString(LocalFormat, CultureInfo.InvariantCulture);
|
|
|
|
public static string FormatGuid(Guid value) => value.ToString("D");
|
|
|
|
/// <summary>Parses an ISO-8601 or PostgreSQL timestamp; a missing offset means UTC.</summary>
|
|
public static DateTimeOffset ParseTimestamp(ReadOnlySpan<char> text)
|
|
{
|
|
if (!TryParse(text, out var value, out var offset))
|
|
throw new FormatException($"Invalid timestamp '{text}'.");
|
|
if (value == DateTime.MaxValue || value == DateTime.MinValue)
|
|
return new DateTimeOffset(value, TimeSpan.Zero);
|
|
return new DateTimeOffset(DateTime.SpecifyKind(value - (offset ?? TimeSpan.Zero), DateTimeKind.Utc));
|
|
}
|
|
|
|
/// <summary>Parses a wall-clock date-time (unspecified kind); a value carrying an offset is returned in UTC.</summary>
|
|
public static DateTime ParseLocal(ReadOnlySpan<char> text)
|
|
{
|
|
if (!TryParse(text, out var value, out var offset))
|
|
throw new FormatException($"Invalid date-time '{text}'.");
|
|
return offset is { } o ? DateTime.SpecifyKind(value - o, DateTimeKind.Utc) : value;
|
|
}
|
|
|
|
/// <summary>Accepts <c>yyyy-MM-dd[(T| )HH:mm[:ss[.f…]]][Z|±HH[[:]mm[[:]ss]]]</c> and ±infinity.</summary>
|
|
private static bool TryParse(ReadOnlySpan<char> s, out DateTime value, out TimeSpan? offset)
|
|
{
|
|
value = default;
|
|
offset = null;
|
|
s = s.Trim();
|
|
if (s.SequenceEqual("infinity"))
|
|
{
|
|
value = DateTime.MaxValue;
|
|
return true;
|
|
}
|
|
if (s.SequenceEqual("-infinity"))
|
|
{
|
|
value = DateTime.MinValue;
|
|
return true;
|
|
}
|
|
if (s.Length < 10 || s[4] != '-' || s[7] != '-'
|
|
|| !Digits(s[..4], out var year) || !Digits(s.Slice(5, 2), out var month) || !Digits(s.Slice(8, 2), out var day))
|
|
return false;
|
|
|
|
int hour = 0, minute = 0, second = 0;
|
|
long fraction = 0;
|
|
var i = 10;
|
|
if (i < s.Length && s[i] is 'T' or ' ')
|
|
{
|
|
if (s.Length < i + 6 || s[i + 3] != ':' || !Digits(s.Slice(i + 1, 2), out hour) || !Digits(s.Slice(i + 4, 2), out minute))
|
|
return false;
|
|
i += 6;
|
|
if (i < s.Length && s[i] == ':')
|
|
{
|
|
if (s.Length < i + 3 || !Digits(s.Slice(i + 1, 2), out second))
|
|
return false;
|
|
i += 3;
|
|
if (i < s.Length && s[i] == '.')
|
|
{
|
|
var start = ++i;
|
|
var digits = 0;
|
|
for (; i < s.Length && char.IsAsciiDigit(s[i]); i++)
|
|
{
|
|
if (digits++ < 7) fraction = fraction * 10 + (s[i] - '0');
|
|
}
|
|
if (i == start) return false;
|
|
for (; digits < 7; digits++) fraction *= 10;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (i < s.Length)
|
|
{
|
|
if (s[i] == 'Z' && i == s.Length - 1)
|
|
{
|
|
offset = TimeSpan.Zero;
|
|
}
|
|
else if (s[i] is '+' or '-')
|
|
{
|
|
var negative = s[i++] == '-';
|
|
if (!OffsetPart(s, ref i, out var oh, colon: false)) return false;
|
|
var om = 0;
|
|
var os = 0;
|
|
if (i < s.Length && !OffsetPart(s, ref i, out om, colon: true)) return false;
|
|
if (i < s.Length && !OffsetPart(s, ref i, out os, colon: true)) return false;
|
|
if (i != s.Length) return false;
|
|
var span = new TimeSpan(oh, om, os);
|
|
offset = negative ? -span : span;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (year < 1 || month is < 1 or > 12 || day < 1 || day > DateTime.DaysInMonth(year, month)
|
|
|| hour > 23 || minute > 59 || second > 59)
|
|
return false;
|
|
value = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Unspecified).AddTicks(fraction);
|
|
return true;
|
|
}
|
|
|
|
private static bool OffsetPart(ReadOnlySpan<char> s, ref int i, out int value, bool colon)
|
|
{
|
|
if (colon && s[i] == ':') i++;
|
|
value = 0;
|
|
if (s.Length < i + 2 || !Digits(s.Slice(i, 2), out value)) return false;
|
|
i += 2;
|
|
return true;
|
|
}
|
|
|
|
private static bool Digits(ReadOnlySpan<char> s, out int value)
|
|
{
|
|
value = 0;
|
|
foreach (var c in s)
|
|
{
|
|
if (!char.IsAsciiDigit(c)) return false;
|
|
value = value * 10 + (c - '0');
|
|
}
|
|
return true;
|
|
}
|
|
}
|