Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.Data.Sqlite: Use sqlite3_load_extension #28903

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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