Add a :type annotation to share a row type between queries - #4630
Open
egimbernat wants to merge 1 commit into
Open
egimbernat wants to merge 1 commit into
egimbernat wants to merge 1 commit into
Conversation
A query that returns more than one column gets a struct named after it, so queries selecting the same columns return different types. `:type <TypeName>` after the command names the row type instead, and queries that use the same name return one struct. Generation fails if their columns give different fields, if the name is a model's, or if the query returns a single column. Plugins receive the name as Query.type_name. ParseQueryNameAndType now returns the Metadata, so its callers no longer rebuild it from parts. Fixes sqlc-dev#781 Fixes sqlc-dev#3595 Co-authored-by: ali <ali.dehkharghani@megadevs.de>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Queries that select the same columns each get their own row struct (
GetBookRow,ListBooksByAuthorRow, …) even when the structs are identical, so callers end up converting between them. This adds an optional:type <TypeName>after the command:Both methods return
BookWithAuthor, which is emitted once. It works withsqlc.embedas well.Compatibility is checked strictly, as #3595 asks: generation fails when two queries naming the same type would give different fields (name, Go type or struct tag, in order), and the error names the first differing column:
It also fails if the name is already a model's or the query returns a single column, since there is no struct to name. An explicit
:typewins over the existing reuse of a table's model when the columns happen to match one.Changes
metadata.ParseQueryNameAndTypenow returnsMetadata(with the newTypeName) instead of two strings, so its three callers no longer rebuild it from parts.plugin.Querygetstype_name = 9(regenerated withbuf generate), so plugins for other languages can honour the annotation. The two JSON plugin goldens gain the field.rowTypesininternal/codegen/golang/result.go.howto/row_types.md, plus a note inreference/query-annotations.md.query_type_annotation(plain columns, aliases,sqlc.embed, and a query whose columns match a model), and four failing cases withstderr.txt: mismatched columns, a model's name, a single column, and malformed annotations.go test --tags=examples ./...passes locally against PostgreSQL 16 and MySQL 9.Fixes #781
Fixes #3595
Related: #2252, which asks to return an existing model; this names a shared type instead.