Pin bytea_output=hex and standard_conforming_strings=on in the startup packet instead of tracking ParameterStatus values nobody read, so the legacy bytea escape decoder goes away. A PostgreSQL connection now refuses a new command while a row stream is still open, and the pool discards such a connection. Drop unused SQLite declarations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
103 lines
6.5 KiB
C#
103 lines
6.5 KiB
C#
using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace Titano.Data.Sqlite;
|
|
|
|
/// <summary>
|
|
/// P/Invoke surface of the system SQLite library: <c>winsqlite3.dll</c> (Windows 10 1709+), <c>libsqlite3.so.0</c>
|
|
/// (Linux) or <c>libsqlite3.dylib</c> (macOS). Strings cross the boundary as UTF-8 encoded by hand; the default
|
|
/// calling convention matches winsqlite3's <c>__stdcall</c> exports on x86 and is cdecl elsewhere.
|
|
/// </summary>
|
|
internal static unsafe class SqliteNative
|
|
{
|
|
private const string Library = "titano-sqlite3";
|
|
private static int _resolverInstalled;
|
|
|
|
public const int SQLITE_OK = 0;
|
|
public const int SQLITE_INTERRUPT = 9;
|
|
public const int SQLITE_BUSY = 5;
|
|
public const int SQLITE_LOCKED = 6;
|
|
public const int SQLITE_CONSTRAINT = 19;
|
|
public const int SQLITE_ROW = 100;
|
|
public const int SQLITE_DONE = 101;
|
|
|
|
public const int SQLITE_CONSTRAINT_FOREIGNKEY = SQLITE_CONSTRAINT | (3 << 8);
|
|
public const int SQLITE_CONSTRAINT_PRIMARYKEY = SQLITE_CONSTRAINT | (6 << 8);
|
|
public const int SQLITE_CONSTRAINT_UNIQUE = SQLITE_CONSTRAINT | (8 << 8);
|
|
|
|
public const int SQLITE_OPEN_READONLY = 0x00000001;
|
|
public const int SQLITE_OPEN_READWRITE = 0x00000002;
|
|
public const int SQLITE_OPEN_CREATE = 0x00000004;
|
|
public const int SQLITE_OPEN_URI = 0x00000040;
|
|
public const int SQLITE_OPEN_NOMUTEX = 0x00008000;
|
|
|
|
public const int SQLITE_INTEGER = 1;
|
|
public const int SQLITE_FLOAT = 2;
|
|
public const int SQLITE_TEXT = 3;
|
|
public const int SQLITE_BLOB = 4;
|
|
public const int SQLITE_NULL = 5;
|
|
|
|
/// <summary>Destructor sentinel that makes SQLite copy bound text/blobs before the call returns.</summary>
|
|
public static readonly nint SQLITE_TRANSIENT = -1;
|
|
|
|
/// <summary>Routes the logical library name to the platform's system SQLite (once per process).</summary>
|
|
public static void EnsureResolver()
|
|
{
|
|
if (Interlocked.Exchange(ref _resolverInstalled, 1) == 0)
|
|
NativeLibrary.SetDllImportResolver(typeof(SqliteNative).Assembly, Resolve);
|
|
}
|
|
|
|
private static nint Resolve(string name, Assembly assembly, DllImportSearchPath? searchPath)
|
|
{
|
|
if (name != Library) return 0;
|
|
string[] candidates = OperatingSystem.IsWindows() ? ["winsqlite3.dll"]
|
|
: OperatingSystem.IsMacOS() ? ["libsqlite3.dylib", "/usr/lib/libsqlite3.dylib"]
|
|
: ["libsqlite3.so.0", "libsqlite3.so"];
|
|
foreach (var candidate in candidates)
|
|
{
|
|
if (NativeLibrary.TryLoad(candidate, assembly, searchPath, out var handle)) return handle;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_libversion_number();
|
|
[DllImport(Library, ExactSpelling = true)] public static extern byte* sqlite3_libversion();
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_open_v2(byte* filename, out nint db, int flags, byte* vfs);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_close_v2(nint db);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_extended_result_codes(nint db, int onoff);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_busy_timeout(nint db, int ms);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_exec(nint db, byte* sql, nint callback, nint arg, out byte* errmsg);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern void sqlite3_free(void* p);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern byte* sqlite3_errmsg(nint db);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern void sqlite3_interrupt(nint db);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_changes(nint db);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_total_changes(nint db);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_get_autocommit(nint db);
|
|
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_prepare_v2(nint db, byte* sql, int nBytes, out nint stmt, out byte* tail);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_step(nint stmt);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_reset(nint stmt);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_clear_bindings(nint stmt);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_finalize(nint stmt);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_stmt_readonly(nint stmt);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_bind_parameter_count(nint stmt);
|
|
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_bind_null(nint stmt, int index);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_bind_int64(nint stmt, int index, long value);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_bind_double(nint stmt, int index, double value);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_bind_text(nint stmt, int index, byte* text, int nBytes, nint destructor);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_bind_blob(nint stmt, int index, void* data, int nBytes, nint destructor);
|
|
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_column_count(nint stmt);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern byte* sqlite3_column_name(nint stmt, int index);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_column_type(nint stmt, int index);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern long sqlite3_column_int64(nint stmt, int index);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern double sqlite3_column_double(nint stmt, int index);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern byte* sqlite3_column_text(nint stmt, int index);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern void* sqlite3_column_blob(nint stmt, int index);
|
|
[DllImport(Library, ExactSpelling = true)] public static extern int sqlite3_column_bytes(nint stmt, int index);
|
|
|
|
/// <summary>Decodes a NUL-terminated UTF-8 string owned by SQLite.</summary>
|
|
public static string? Utf8(byte* p) => p is null ? null : Marshal.PtrToStringUTF8((nint)p);
|
|
}
|