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

Supporting optional parameters #43

Open
slifty opened this issue Nov 28, 2023 · 3 comments
Open

Supporting optional parameters #43

slifty opened this issue Nov 28, 2023 · 3 comments

Comments

@slifty
Copy link

slifty commented Nov 28, 2023

This might be a terrible idea but I'm raising it just in case!

Would it be possible to provide support for optional parameters in .sql files? For instance maybe :id? would create a parameter that will not error when a value is omitted.

My use case is that I'm looking into creating a single updateById.sql file for one of my entities, and I'd like to allow calling code to only pass the fields that need to be updated. I'd be constructing the query itself to omit updates to fields with no value, for instance using something like:

UPDATE some_table SET
  column_1 = COALESCE(:param_1?, column_1),
  column_2 = COALESCE(:param_2?, column_2)
WHERE id = some_id;

Currently if I don't pass values to param_1 and param_2 tinypg will throw a Missing expected key error.

@slifty
Copy link
Author

slifty commented Mar 15, 2024

I have another use case for this! I'm select statements with optional filters (e.g. optional search terms, or forieng key relationships, etc)

At this point I'm forced to pass a placeholder value, but I would much rather have the ability to pass null / undefined and have tinypg insert NULL.

Is there any interest in supporting this type of feature?

@slifty
Copy link
Author

slifty commented Mar 29, 2024

One more use case -- I'd like to have a select query that can optionally take limit / offset values.

Ideally I could pass NULL to my limit in the event that I don't want to limit in a given instance, so the query would be something like LIMIT :limit and accept null

@slifty
Copy link
Author

slifty commented Mar 29, 2024

I'm realizing maybe the name of this issue should be translate undefined values to NULL since that would be more flexible and solve the problem directly.

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

1 participant