Repository navigation
Optional parameters for named queries, written $?N (#143) - #166
Open
leo7martins wants to merge 2 commits into
Open
leo7martins wants to merge 2 commits into
leo7martins wants to merge 2 commits into
Conversation
Named queries gain optional placeholders $?1, $?2, ...: an argument
supplied at that position is substituted the same way as a regular
$N parameter, while a missing one is replaced by null. This lets one
saved query serve both filtered and unfiltered runs, e.g.:
\ns opt select * from abc where ($?1 is null or a like '%$?1%')
- the gate in execute_named_query now also fires when the query only
contains optional placeholders (a literal '$1' is no longer required
to reach the substitution step)
- unknown-argument error now mentions both the $N and $?N forms
- unit tests for the substitution function (supplied, missing, mixed
mandatory/optional, extra-argument error) plus an end-to-end dbtest
running a saved query with and without its optional argument
- changelog entry and usage example
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #143.
Named queries gain optional parameters, written exactly as proposed in the issue:
$?1,$?2, ... An argument supplied at that position is substituted the same way as a regular$N; a missing one is replaced bynull, so one saved query can serve both filtered and unfiltered runs:What changed (following the pointers @j-bennet gave in the issue, in
pgspecial/iocommands.py):subst_favorite_query_argsrecognizes$?Nalongside$Nin the substitution loop. Mandatory behavior is unchanged: an argument with no$N/$?Nslot still errors out (the message now mentions both forms), and$*/$@aggregation works as before.$?Nplaceholders becomenull.execute_named_querynow also fires when the query only contains optional placeholders. Previously the gate required a literal$1in the query, so a saved query written only with$?1would have been sent to the server unsubstituted. (This is also why the e2e test below matters.)Semantics notes
''(empty string) to skip an optional slot in the middle.$Nand$?Nexist for the same index,$Ntakes precedence (unusual; documented in the docstring).Tests (in
tests/test_named_queries.py): four unit tests for the substitution function (supplied, missing-becomes-null, mandatory+optional mix, extra-argument error) plus an end-to-end@dbtestthat saves a query with only an optional parameter and runs it against a live database with and without the argument.Verification
python -m pytest -q tests/test_named_queries.py tests/test_internal.py: 24 passed, 1 skipped. The PostgreSQL integration test was skipped because no PostgreSQL instance was available..pre-commit-config.yaml:ruff checkandruff format --diffpassed.tox -e styleinstalled an unpinned Ruff 0.17.6 and reported legacy-code findings in the restored untouched sections; this does not reproduce with the repository-pinned Ruff version.git diff --checkandgit diff upstream/main --check: passed.One unrelated observation for the maintainers (not addressed here): the
cursorfixture intests/conftest.pyreturns its cursor from inside thewithblock, so it is already closed by the time the test body runs (psycopg.InterfaceError: the cursor is closed). The dbtest added here uses theconnectionfixture with a local cursor instead.Disclosure: this PR was prepared by an AI coding agent (Mistral Vibe), including the implementation, tests, PR text, and reported local test/lint results. Those results have not been independently verified by a human, and no human code review or independent human testing is claimed. GitHub Actions is awaiting maintainer approval and has run no jobs; maintainer review and CI validation are still required.