mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 17:34:32 +02:00
--functions can take filenames, can be used multiple times (#681)
Closes #659 The --functions option now accepts: - File paths ending in .py (e.g., --functions my_funcs.py) - Multiple invocations (e.g., --functions foo.py --functions 'def bar(): ...') - Inline Python code (existing behavior) Implementation follows the same pattern as llm's --functions flag (simonw/llm@a880c123). Changes: - Added multiple=True to --functions Click option in query, bulk, and memory commands - Modified _register_functions() to detect and read .py files - Updated _maybe_register_functions() to iterate over multiple function sources - Removed unused bytes/bytearray handling - Added comprehensive tests for file paths and multiple invocations - Updated documentation with examples 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Shorter help for --functions --------- Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
parent
c872d27bb3
commit
0bbc68089c
6 changed files with 155 additions and 12 deletions
|
|
@ -1375,7 +1375,9 @@ def upsert(
|
|||
@click.argument("file", type=click.File("rb"), required=True)
|
||||
@click.option("--batch-size", type=int, default=100, help="Commit every X records")
|
||||
@click.option(
|
||||
"--functions", help="Python code defining one or more custom SQL functions"
|
||||
"--functions",
|
||||
help="Python code or file path defining custom SQL functions",
|
||||
multiple=True,
|
||||
)
|
||||
@import_options
|
||||
@load_extension_option
|
||||
|
|
@ -1764,7 +1766,9 @@ def drop_view(path, view, ignore, load_extension):
|
|||
help="Named :parameters for SQL query",
|
||||
)
|
||||
@click.option(
|
||||
"--functions", help="Python code defining one or more custom SQL functions"
|
||||
"--functions",
|
||||
help="Python code or file path defining custom SQL functions",
|
||||
multiple=True,
|
||||
)
|
||||
@load_extension_option
|
||||
def query(
|
||||
|
|
@ -1828,7 +1832,9 @@ def query(
|
|||
)
|
||||
@click.argument("sql")
|
||||
@click.option(
|
||||
"--functions", help="Python code defining one or more custom SQL functions"
|
||||
"--functions",
|
||||
help="Python code or file path defining custom SQL functions",
|
||||
multiple=True,
|
||||
)
|
||||
@click.option(
|
||||
"--attach",
|
||||
|
|
@ -3292,6 +3298,13 @@ def _load_extensions(db, load_extension):
|
|||
|
||||
def _register_functions(db, functions):
|
||||
# Register any Python functions as SQL functions:
|
||||
# Check if this is a file path
|
||||
if "\n" not in functions and functions.endswith(".py"):
|
||||
try:
|
||||
functions = pathlib.Path(functions).read_text()
|
||||
except FileNotFoundError:
|
||||
raise click.ClickException("File not found: {}".format(functions))
|
||||
|
||||
sqlite3.enable_callback_tracebacks(True)
|
||||
globals = {}
|
||||
try:
|
||||
|
|
@ -3310,9 +3323,10 @@ def _value_or_none(value):
|
|||
return value
|
||||
|
||||
|
||||
def _maybe_register_functions(db, functions):
|
||||
functions = _value_or_none(functions)
|
||||
if isinstance(functions, (bytes, bytearray)):
|
||||
functions = functions.decode("utf-8")
|
||||
if isinstance(functions, str) and functions.strip():
|
||||
_register_functions(db, functions)
|
||||
def _maybe_register_functions(db, functions_list):
|
||||
if not functions_list:
|
||||
return
|
||||
for functions in functions_list:
|
||||
functions = _value_or_none(functions)
|
||||
if isinstance(functions, str) and functions.strip():
|
||||
_register_functions(db, functions)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue