Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: supporting optional arguments in named/favourite queries? #143

Open
tfh-cri opened this issue Jul 31, 2023 · 1 comment
Open

Feature: supporting optional arguments in named/favourite queries? #143

tfh-cri opened this issue Jul 31, 2023 · 1 comment

Comments

@tfh-cri
Copy link

tfh-cri commented Jul 31, 2023

Hopefully this is the right place, since as best I can tell, the code for \n NAME ARGS... as provided by pgcli actually lives in here.

It would be convenient if there was a way to specify optional arguments to be replaced in a named query, to allow something like:

\ns opt-args select * from some_table where ($?1 is null or some_field like '%$?1%')

so it could be invoked as either \n opt-args with no args to just query everything, or if \n opt-args somevalue to filter to only things matching that argument, without needing to have different queries with/without args (and remembering which you want to use at the time)

I've invented the $?<num> format in the above example which doesn't currently exist. That allows it to be distinguished from normal arguments as they are now, to avoid breaking existing behaviour of the invocation failing if there are unreplaced parameters left.

I'm currently imagining that the optional arg would just be substituted with the value NULL if not present, although a case could be made for a more bash-like ${VAR:-DEFAULT} to allow arbitrary replacement values if not provided.

It might make sense to allow optional aggregated params as well, although not sure if they should expand to an empty list, or also to NULL.

I think I can probably cobble up a PR if there's any interest, but figured it might be wise to check first, and maybe bikeshed the syntax before ploughing ahead 😄

@j-bennet
Copy link
Contributor

j-bennet commented Aug 1, 2023

Hi @tfh-cri ,

You're correct, the code for the named queries is here:

https://github.com/dbcli/pgspecial/blob/main/pgspecial/namedqueries.py
https://github.com/dbcli/pgspecial/blob/main/pgspecial/iocommands.py

You'd have to change this function to handle your new parameter type:

def subst_favorite_query_args(query, args):

You're welcome to draft a PR. I'd be curious to see some real-life examples of queries for which you want to use this feature.

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

No branches or pull requests

2 participants