Skip to content

Optional parameters for named queries, written $?N (#143) - #166

Open
leo7martins wants to merge 2 commits into
dbcli:mainfrom
leo7martins:named-query-optional-params
Open

leo7martins wants to merge 2 commits into
dbcli:mainfrom
leo7martins:named-query-optional-params

Conversation

@leo7martins

@leo7martins leo7martins commented Nov 21, 2026 •

Copy link
Copy Markdown

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 by null, so one saved query can serve both filtered and unfiltered runs:

\ns opt select * from abc where ($?1 is null or a like %\'$?1%\')

> 
 opt            # lists everything
> 
 opt cleo       # ... where name like '%cleo%'

What changed (following the pointers @j-bennet gave in the issue, in pgspecial/iocommands.py):

  • subst_favorite_query_args recognizes $?N alongside $N in the substitution loop. Mandatory behavior is unchanged: an argument with no $N/$?N slot still errors out (the message now mentions both forms), and $*/$@ aggregation works as before.
  • After substitution, remaining $?N placeholders become null.
  • The gate in execute_named_query now also fires when the query only contains optional placeholders. Previously the gate required a literal $1 in the query, so a saved query written only with $?1 would have been sent to the server unsubstituted. (This is also why the e2e test below matters.)
  • Docstring, an example in the named-queries usage help, and a changelog entry.
  • Unrelated formatting and import changes were reverted so the diff stays focused on optional parameters.

Semantics notes

  • Parameters stay positional: an optional slot consumes its argument when present. Pass '' (empty string) to skip an optional slot in the middle.
  • When both $N and $?N exist for the same index, $N takes 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 @dbtest that 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.
  • Ruff 0.11.7, matching .pre-commit-config.yaml: ruff check and ruff format --diff passed.
  • tox -e style installed 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 --check and git diff upstream/main --check: passed.

One unrelated observation for the maintainers (not addressed here): the cursor fixture in tests/conftest.py returns its cursor from inside the with block, so it is already closed by the time the test body runs (psycopg.InterfaceError: the cursor is closed). The dbtest added here uses the connection fixture 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.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: supporting optional arguments in named/favourite queries?

1 participant