镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

docs: explain how to use the batch object - #4633

Open
jotikrishna wants to merge 1 commit into
sqlc-dev:mainfrom
jotikrishna:docs/batch-object-usage
Open

jotikrishna wants to merge 1 commit into
sqlc-dev:mainfrom
jotikrishna:docs/batch-object-usage

Conversation

@jotikrishna

Copy link
Copy Markdown

Problem

The :batchexec, :batchmany and :batchone sections of docs/reference/query-annotations.md list the batch object's methods but never explain them:

  • what the func(int, error) callback actually receives, and what its int means
  • what happens when one statement in the batch fails
  • what Close does to statements that have not run yet ("close the batch operation early" is the only description)
  • whether Close is 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 Close semantics, 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:

  • the callback is invoked once per queued statement, in order; the int is the statement's 0-based position in the batch, matching the slice index you passed in
  • a failing statement does not stop iteration - if the third of ten statements fails, the callback still receives results for the fourth through tenth, so errors must be handled inside the callback
  • Close before or during iteration skips the remaining statements, and their callbacks instead receive ErrBatchAlreadyClosed - once per remaining statement, so the callback always sees the full batch
  • Close is called automatically when the iteration method returns (defer b.br.Close()), so a fully iterated batch needs no explicit Close

Everything stated is taken from the generated code itself - internal/codegen/golang/templates/pgx/batchCode.tmpl and the internal/endtoend/testdata/batch/postgresql/pgx/v5/go/batch.go golden file - not paraphrased from other documentation. I verified the callback and Close semantics by executing the generated loops against a pgx.BatchResults implementation (statements consumed in order, post-Close callbacks delivering ErrBatchAlreadyClosed, iteration continuing past statement errors).

Testing

The repo's docs content contract is enforced by internal/docs (added in #4584):

$ go test ./internal/docs/
ok  	github.com/sqlc-dev/sqlc/internal/docs	0.828s

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 slugify rules, 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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documentation for batch object returned by :batch* annotations could be more clear

1 participant