From e7b12f687f09c9e0302af23f64e1dd295f85b228 Mon Sep 17 00:00:00 2001 From: Alexander Marek Date: Fri, 22 Aug 2025 12:15:24 +0200 Subject: [PATCH 1/2] Added support for loading extensions (e.g. mod_spatialite) --- src/SQLite.cs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/SQLite.cs b/src/SQLite.cs index 72525c56..5677605d 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -706,6 +706,28 @@ public void EnableLoadExtension (bool enabled) throw SQLiteException.New (r, msg); } } + + /// + /// Load extension. + /// + public void LoadExtension (string filename) + { + SQLite3.Result r = SQLite3.LoadExtension (Handle, filename, null, out var msg); + if (r != SQLite3.Result.OK) { + throw SQLiteException.New (r, msg); + } + } + + /// + /// Load extension. + /// + public void LoadExtension (string filename, string customInitFunctionName) + { + SQLite3.Result r = SQLite3.LoadExtension (Handle, filename, customInitFunctionName, out var msg); + if (r != SQLite3.Result.OK) { + throw SQLiteException.New (r, msg); + } + } #if !USE_SQLITEPCL_RAW static byte[] GetNullTerminatedUtf8 (string s) @@ -5050,6 +5072,9 @@ public enum ConfigOption : int [DllImport(LibraryPath, EntryPoint = "sqlite3_enable_load_extension", CallingConvention=CallingConvention.Cdecl)] public static extern Result EnableLoadExtension (IntPtr db, int onoff); + [DllImport (LibraryPath, EntryPoint = "sqlite3_load_extension", CallingConvention = CallingConvention.Cdecl)] + public static extern Result LoadExtension (IntPtr db, [MarshalAs (UnmanagedType.LPStr)] string filename, [MarshalAs (UnmanagedType.LPStr)] string initFunctionName, [MarshalAs(UnmanagedType.LPStr)] out string errorMsg); + [DllImport(LibraryPath, EntryPoint = "sqlite3_close", CallingConvention=CallingConvention.Cdecl)] public static extern Result Close (IntPtr db); @@ -5401,6 +5426,16 @@ public static Result EnableLoadExtension (Sqlite3DatabaseHandle db, int onoff) { return (Result)Sqlite3.sqlite3_enable_load_extension (db, onoff); } + + public static Result LoadExtension (Sqlite3DatabaseHandle db, string fileName, string initFunctionName, out string errorMessage) + { + var result = (Result)Sqlite3.sqlite3_load_extension (db, SQLitePCL.utf8z.FromString (fileName), + SQLitePCL.utf8z.FromString (initFunctionName), out SQLitePCL.utf8z pzErrMsg); + + errorMessage = pzErrMsg.utf8_to_string (); + + return result; + } public static int LibVersionNumber () { From 147694dfe3df5a82b8e5f48c013f4adc89b29ab9 Mon Sep 17 00:00:00 2001 From: "alexander.marek" Date: Wed, 27 Aug 2025 09:06:22 +0200 Subject: [PATCH 2/2] fixed tests (by default they fail with "unable to find dll sqlite3") --- tests/SQLite.Tests/SQLite.Tests.csproj | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/SQLite.Tests/SQLite.Tests.csproj b/tests/SQLite.Tests/SQLite.Tests.csproj index 5c14b15d..25a7da11 100644 --- a/tests/SQLite.Tests/SQLite.Tests.csproj +++ b/tests/SQLite.Tests/SQLite.Tests.csproj @@ -12,8 +12,17 @@ + + + + USE_SQLITEPCL_RAW;RELEASE + + + USE_SQLITEPCL_RAW;DEBUG + + SQLite.cs