feat(#2348, #2997): add sqlc.nembed() for nullable embeds#4341
Open
luismingati wants to merge 1 commit intosqlc-dev:mainfrom
Open
feat(#2348, #2997): add sqlc.nembed() for nullable embeds#4341luismingati wants to merge 1 commit intosqlc-dev:mainfrom
luismingati wants to merge 1 commit intosqlc-dev:mainfrom
Conversation
…beds Introduces `sqlc.nembed()` function that generates nullable (pointer) embed structs in Go codegen. When a LEFT JOIN may produce NULL rows, nembed scans into temporary pointer variables and conditionally constructs the embedded struct only when at least one field is non-nil. Closes sqlc-dev#2348 Closes sqlc-dev#2997 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.
Summary
sqlc.nembed()function for nullable embed support in Go codegensqlc.nembed(table)generates a pointer-typed embed struct (*Struct) that isnilwhen all joined columns are NULLpgxanddatabase/sql(stdlib) drivers, including batch queriesMotivation
Closes #2348
Closes #2997
When using
sqlc.embed()with LEFT JOINs, the embedded struct is always populated even when the joined row doesn't exist, resulting in zero-valued fields.sqlc.nembed()(nullable embed) solves this by returning a pointer that isnilwhen no matching row exists.Test plan
sqlc_nembedwith pgx and stdlib driversgo test --tags=examples -timeout 20m ./...🤖 Generated with Claude Code