Description
Hello, thanks for maintaining pgtyped.
Is your feature request related to a problem? Please describe.
The currently prescribed approach for dynamic queries works well for conditional where clauses, but is not adaptable when we need conditional joins, or optional CTE steps etc.
The advise for splitting complex queries is generally sound, but suffers from lack of composability. So if we have multiple join based filters, we would need a query for each possible combination which gets messy.
Describe the solution you'd like
In the BI solution Metabase, there is minimal support for optional clauses through [[ ... ]]
.
Example usage:
SELECT count(*)
FROM products
[[ JOIN category on products.category_id = category.id and category.id = {{category_id}} ]]
[[ JOIN product_tags on product_tags.product_id = products.id
JOIN tags on tags.id = product_tags.tag_id and tags.name = {{tag_name}} ]]
If a variable used within these square brackets is not defined the entire clause is omitted. This is conceptually simple but works well for a wide variety of scenarios.
Combined with a trailing comment trick, it also serves well as overridable default
WHERE column = [[ {{ your_parameter }} --]]your_default_value
I was wondering if this/similar solution could be adopted in pgtyped as well.