Repository navigation
docs: explain how to use the batch object - #4633
Open
jotikrishna wants to merge 1 commit into
Open
jotikrishna wants to merge 1 commit into
jotikrishna wants to merge 1 commit into
Conversation
The :batchexec, :batchmany and :batchone sections listed the batch object's methods but never said what the callback receives, what its int argument means, what Close does to statements that have not run yet, or when Close is even needed. Issue sqlc-dev#4631 reports exactly this: readers cannot tell what to do with the returned batch object. Add a 'Using the batch object' section that documents the callback contract and Close semantics, and point each batch annotation at it: - the callback is invoked once per queued statement, in order, and the int is the statement's 0-based position in the batch - a failing statement does not stop iteration; errors must be handled inside the callback - Close before or during iteration skips the remaining statements and delivers ErrBatchAlreadyClosed once per remaining statement - Close is called automatically when the iteration method returns, so a fully iterated batch needs no explicit Close The behaviour described is taken from the generated code itself (internal/codegen/golang/templates/pgx/batchCode.tmpl and the batch endtoend golden files), not paraphrased from other docs. Fixes sqlc-dev#4631
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.
Problem
The
:batchexec,:batchmanyand:batchonesections ofdocs/reference/query-annotations.mdlist the batch object's methods but never explain them:func(int, error)callback actually receives, and what itsintmeansClosedoes to statements that have not run yet ("close the batch operation early" is the only description)Closeis needed at all when you iterate the whole batch#4631 reports exactly this: a reader cannot tell what to do with the returned batch object from the docs alone.
Solution
Add a Using the batch object section documenting the callback contract and
Closesemantics, with a complete usage example and an early-abort example. Each of the three batch annotations now points at it with a same-page anchor link.Documented behaviour:
intis the statement's 0-based position in the batch, matching the slice index you passed inClosebefore or during iteration skips the remaining statements, and their callbacks instead receiveErrBatchAlreadyClosed- once per remaining statement, so the callback always sees the full batchCloseis called automatically when the iteration method returns (defer b.br.Close()), so a fully iterated batch needs no explicitCloseEverything stated is taken from the generated code itself -
internal/codegen/golang/templates/pgx/batchCode.tmpland theinternal/endtoend/testdata/batch/postgresql/pgx/v5/go/batch.gogolden file - not paraphrased from other documentation. I verified the callback andClosesemantics by executing the generated loops against apgx.BatchResultsimplementation (statements consumed in order, post-Closecallbacks deliveringErrBatchAlreadyClosed, iteration continuing past statement errors).Testing
The repo's docs content contract is enforced by
internal/docs(added in #4584):Baseline before the edit: the same test passed, so the change introduces no new contract violations. All anchor links in the new section resolve under the linter's
slugifyrules, and the added content is plain CommonMark plus one GitHub alert (both allowed by the contract).Not run: the full end-to-end suite requires live PostgreSQL/MySQL instances via Docker, which I don't have available; the documentation change does not touch code paths, and the docs test above is the gate that covers it.
Impact
Readers of the batch annotations can now use the returned batch object without reading sqlc's template source. The three existing sections keep their shape and gain one pointer line each.
Fixes #4631