Add AdditionalQuery
as function parameter for :one
and :many
queries
#2636
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.
What is this?
This PR is implementation for my proposal in #2021 to allow additional query as function parameter in
:one
and:many
queries. I've explained my reasoning there, but I will rewrite it again here for clarity.Why?
One of the most common feature request for CRUD and BI apps is to make an advanced data filter where the users can freely define the parameters to specify which data to be shown. However, since
sqlc
act as compiler for SQL queries to Go code, once the compilation finished it can't be modified anymore. Thanks to this, advanced data filter is impossible to done properly withsqlc
.To solve this issue, this PR add
AdditionalQuery
as function parameter in the compiled:one
and:many
queries. This way we still able to create advanced data filter while retaining the advantage of compiling the SQL queries.Example
For example, let's say we have queries like this:
From this query,
sqlc
will generate following Go code:As you can see, currently our query for FetchProducts is run immediately by db.QueryContext, making it impossible to define custom SQL filter.
To allow custom SQL filter, this PR will add
AdditionalQuery
as function parameter for SELECT queries. Here is the generated Go code with this PR:With that new parameter, now we can define custom filter like this:
Difference with the proposal
If you've read my proposal before, you'll notice there is one difference between the proposal and this PR: in proposal I suggest to wrap the SELECT query inside sub-query.
I decided to not implement it because there might be performance issue coming from using sub-query. Not to mention from the user perspective (i.e. other developers who use sqlc), usually they don't like if the compiler modify their SQL query without any information.
Pros and cons of this PR
The pros are:
I'm not really sure about the cons. I've used this PR in production and it seems to be fine. However I'm only using it for basic SELECT queries, so there might be some edge cases that I'm not aware of.
Status of this PR
I've separated this PR into several commits:
make regen
.diff_output
anddiff_no_output
.process_plugin_disabled
.