Support batching different queries together.#4342
Open
jgiles wants to merge 2 commits intosqlc-dev:mainfrom
Open
Support batching different queries together.#4342jgiles wants to merge 2 commits intosqlc-dev:mainfrom
jgiles wants to merge 2 commits intosqlc-dev:mainfrom
Conversation
Fix sqlc-dev#4195 by adding an emit_query_batch codegen option that creates a QueryBatch type when using pgx v5. When emit_query_batch: true is set in config (pgx/v5 only), sqlc generates: - A QueryBatch struct wrapping pgx.Batch - NewQueryBatch() constructor - Queue* methods for each query (:one, :many, :exec, :execrows, :execresult) - ExecuteBatch() on Queries to send all queued queries in one round-trip This expands the batching support of existing :batchone/:batchmany/:batchexec annotations, which required separate query definitions and only supported baching the same query with different parameters. Batching multiple instances of the same query with different parameters is also supported via the new interface. While there is probably not a good reason to use both in the same package, you can generate and use both without error for backwards compatibility. Key design decisions - Follows pgx v5's recommended QueuedQuery callback pattern - QueryBatch.Batch is exported so users can mix generated Queue* calls with custom pgx batch operations - :exec queries have no callback (consistent with pgx - errors propagate via Close/ExecuteBatch) - :one callbacks receive a bool indicating whether a row was found Changes - internal/codegen/golang/gen.go - Wire up EmitQueryBatch option, file generation, and validation - internal/codegen/golang/imports.go - Add queryBatchImports() with import logic that skips struct field types (struct definitions live in query.sql.go, not the batch file). Extract queryUsesType() from inline closure for reuse. - internal/codegen/golang/opts/options.go - Add EmitQueryBatch and OutputQueryBatchFileName options - internal/codegen/golang/templates/pgx/queryBatchCode.tmpl - New template for all query batch methods - internal/codegen/golang/templates/template.tmpl - Add queryBatchFile and queryBatchCode template definitions Test coverage - emit_query_batch - Full test with :one, :many, :exec, :execrows, :execresult - emit_query_batch_db_arg - With emit_methods_with_db_argument: true - emit_query_batch_minimal - Non-struct return types (verifies import handling for e.g. pgtype.Timestamptz) - emit_query_batch_overrides - With custom type overrides (verifies batch file doesn't import types only used in struct fields) - emit_query_batch_with_batch - Combines old-style :batchexec with emit_query_batch (verifies both batch.go and query_batch.sql.go coexist correctly) Documentation - docs/reference/config.md - Added emit_query_batch and output_query_batch_file_name - docs/reference/query-annotations.md - New section with usage examples contrasting with :batch* annotations
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.
Fix #4195 by adding an emit_query_batch codegen option that creates a QueryBatch type when using pgx v5.
When emit_query_batch: true is set in config (pgx/v5 only), sqlc generates:
This expands the batching support of existing :batchone/:batchmany/:batchexec annotations, which required separate query definitions and only supported baching the same query with different parameters. Batching multiple instances of the same query with different parameters is also supported via the new interface. While there is probably not a good reason to use both in the same package, you can generate and use both without error for backwards compatibility.
Key design decisions
Changes
Test coverage
Documentation