Add new entrypoint option to --load-extensions. (#1789)

Thanks, @asg017
This commit is contained in:
Alex Garcia 2022-08-23 11:34:30 -07:00 committed by GitHub
commit 1d64c9a8da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 140 additions and 2 deletions

View file

@ -559,7 +559,13 @@ class Datasette:
if self.sqlite_extensions:
conn.enable_load_extension(True)
for extension in self.sqlite_extensions:
conn.execute("SELECT load_extension(?)", [extension])
# "extension" is either a string path to the extension
# or a 2-item tuple that specifies which entrypoint to load.
if isinstance(extension, tuple):
path, entrypoint = extension
conn.execute("SELECT load_extension(?, ?)", [path, entrypoint])
else:
conn.execute("SELECT load_extension(?)", [extension])
if self.setting("cache_size_kb"):
conn.execute(f"PRAGMA cache_size=-{self.setting('cache_size_kb')}")
# pylint: disable=no-member

View file

@ -21,6 +21,7 @@ from .app import (
pm,
)
from .utils import (
LoadExtension,
StartupError,
check_connection,
find_spatialite,
@ -128,9 +129,10 @@ def sqlite_extensions(fn):
return click.option(
"sqlite_extensions",
"--load-extension",
type=LoadExtension(),
envvar="SQLITE_EXTENSIONS",
multiple=True,
help="Path to a SQLite extension to load",
help="Path to a SQLite extension to load, and optional entrypoint",
)(fn)

View file

@ -833,6 +833,17 @@ class StaticMount(click.ParamType):
self.fail(f"{value} is not a valid directory path", param, ctx)
return path, dirpath
# The --load-extension parameter can optionally include a specific entrypoint.
# This is done by appending ":entrypoint_name" after supplying the path to the extension
class LoadExtension(click.ParamType):
name = "path:entrypoint?"
def convert(self, value, param, ctx):
if ":" not in value:
return value
path, entrypoint = value.split(":", 1)
return path, entrypoint
def format_bytes(bytes):
current = float(bytes)