sqlite-utils query --functions option, refs #471

This commit is contained in:
Simon Willison 2022-08-26 21:53:55 -07:00
commit 31f062d4a7
4 changed files with 106 additions and 1 deletions

View file

@ -1633,6 +1633,9 @@ def drop_view(path, view, ignore, load_extension):
type=(str, str),
help="Named :parameters for SQL query",
)
@click.option(
"--functions", help="Python code defining one or more custom SQL functions"
)
@load_extension_option
def query(
path,
@ -1649,6 +1652,7 @@ def query(
raw,
param,
load_extension,
functions,
):
"""Execute SQL query and return the results as JSON
@ -1665,6 +1669,19 @@ def query(
_load_extensions(db, load_extension)
db.register_fts4_bm25()
# Register any Python functions as SQL functions:
if functions:
sqlite3.enable_callback_tracebacks(True)
globals = {}
try:
exec(functions, globals)
except SyntaxError as ex:
raise click.ClickException("Error in functions definition: {}".format(ex))
# Register all callables in the locals dict:
for name, value in globals.items():
if callable(value) and not name.startswith("_"):
db.register_function(value, name=name)
_execute_query(
db, sql, param, raw, table, csv, tsv, no_headers, fmt, nl, arrays, json_cols
)