-
Notifications
You must be signed in to change notification settings - Fork 26
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
Generate the same type for queries with the same SELECT ... FROM #44
Comments
Thanks for the solid write up. I also want this functionality. Technically, it’s not terribly challenging. The main design problem is how to handle edge cases. I think behavior is something like:
As a workaround until this is landed you can use cleverness with SQL predicates to use a query for multiple query patterns. Here’s one we use -- FindItems finds Items.
-- name: FindItems :many
SELECT
name,
vendor_names,
default_price
FROM item_api
WHERE tenant_id = simc37_int(pggen.arg('tenant_id'))
AND (
simc.is_wildcard(pggen.arg('item_id'))
OR item_id = simc.default_current_resource_id(pggen.arg('item_id'))
)
AND temporal.select_partition(pggen.arg('partition'), asr)
AND (pggen.arg('filter') = '' OR categories ~ (pggen.arg('filter') || '.*')::lquery)
ORDER BY item_id, lower(asr) DESC; |
If you could point me in the right direction (and if you are interested) @jschaf then I could take a shot at writing a PR with this functionality. Is your suggested solution that you would be able to (optionally) write:
And then have that query generate |
Sure, I'm happy to take a look at PRs and either merge after code review or combine with some edits from me (giving you author credit). For syntax, maybe:
I think going forward, key=value is the clearest syntax. I cargoculted the syntax from sqlc but we're in new territory with the out type param. |
You can do this by using the composite type for the table. Instead of |
@kirk-anchor that doesn't seem to be the case for me, I have a select with the column names exactly the same in 2 queries and I get different structs. @mbark did you get any working code for someone to continue with? |
You have to select the composite type, |
First of all: thanks so much for this library! It's just incredible -- it feels exactly like how I want to work with databases in go.
Background
Most of our tables are representations of an
rpc
type, so that we have an entityCostCenter
that is stored in thecost_center
table.Then we have several different ways of querying for the
CostCenter
(FindByID
,FindBySubscriber
,ListBySubscribers
etc). All of these queries always doSELECT * FROM cost_center
and then do the same mapping from the returned database type to our internalrpc
type.This requires repeating the mapping from the pggen-generated row type to the
rpc
type for each query -- even though the structs have exactly the same structure.Suggestion
Add a way to have
SELECT
statements that return the exact same thing, also use the same type.e.g. currently we have this generated code (removing comments and batching for readability):
But
FindByIDRow
,FindBySubscriberRow
andListBySubscribersRow
are all identical structs (except for the name).What would be nice is something like:
The text was updated successfully, but these errors were encountered: