Skip to content

Commit

Permalink
Microsoft.Data.Sqlite: Use sqlite3_load_extension
Browse files Browse the repository at this point in the history
Fixes #26220
  • Loading branch information
bricelam committed Aug 26, 2022
1 parent ee33069 commit 7b3c2e5
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions src/Microsoft.Data.Sqlite.Core/SqliteConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,21 +298,19 @@ public override void Open()
}
}

var extensionsEnabledForLoad = false;
if (_extensions != null
&& _extensions.Count != 0)
{
rc = sqlite3_enable_load_extension(Handle, 1);
rc = sqlite3_db_config(Handle, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, out _);
SqliteException.ThrowExceptionForRC(rc, Handle);
extensionsEnabledForLoad = true;

foreach (var item in _extensions)
{
LoadExtensionCore(item.file, item.proc);
}
}

if (_extensionsEnabled != extensionsEnabledForLoad)
if (_extensionsEnabled)
{
rc = sqlite3_enable_load_extension(Handle, _extensionsEnabled ? 1 : 0);
SqliteException.ThrowExceptionForRC(rc, Handle);
Expand Down Expand Up @@ -616,21 +614,13 @@ public virtual void LoadExtension(string file, string? proc = null)
{
int rc;

var extensionsEnabledForLoad = false;
if (!_extensionsEnabled)
{
rc = sqlite3_enable_load_extension(Handle, 1);
rc = sqlite3_db_config(Handle, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, out _);
SqliteException.ThrowExceptionForRC(rc, Handle);
extensionsEnabledForLoad = true;
}

LoadExtensionCore(file, proc);

if (extensionsEnabledForLoad)
{
rc = sqlite3_enable_load_extension(Handle, 0);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
}

_extensions ??= new HashSet<(string, string?)>();
Expand All @@ -639,19 +629,10 @@ public virtual void LoadExtension(string file, string? proc = null)

private void LoadExtensionCore(string file, string? proc)
{
if (proc == null)
{
// NB: SQLitePCL.raw doesn't expose sqlite3_load_extension()
this.ExecuteNonQuery(
"SELECT load_extension($file);",
new SqliteParameter("$file", file));
}
else
var rc = sqlite3_load_extension(Handle, utf8z.FromString(file), utf8z.FromString(proc), out var errmsg);
if (rc != SQLITE_OK)
{
this.ExecuteNonQuery(
"SELECT load_extension($file, $proc);",
new SqliteParameter("$file", file),
new SqliteParameter("$proc", proc));
throw new SqliteException(Resources.SqliteNativeError(rc, errmsg.utf8_to_string()), rc, rc);
}
}

Expand Down

0 comments on commit 7b3c2e5

Please sign in to comment.