Skip to content

Commit 9232fdb

Browse files
committed
batch* cmd docs
1 parent 26a0917 commit 9232fdb

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

docs/reference/query-annotations.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,104 @@ func (q *Queries) GetAuthor(ctx context.Context, id int64) (Author, error) {
9494
// ...
9595
}
9696
```
97+
98+
## `:batchexec`
99+
100+
The generated method will return a batch object. The batch object will have
101+
the following methods:
102+
- `Exec`, that takes a `func(int, error)` parameter,
103+
- `Close`, to close the batch operation early.
104+
105+
```sql
106+
-- name: DeleteBook :batchexec
107+
DELETE FROM books
108+
WHERE book_id = $1;
109+
```
110+
111+
```go
112+
type DeleteBookBatchResults struct {
113+
br pgx.BatchResults
114+
ind int
115+
}
116+
func (q *Queries) DeleteBook(ctx context.Context, bookID []int32) *DeleteBookBatchResults {
117+
//...
118+
}
119+
func (b *DeleteBookBatchResults) Exec(f func(int, error)) {
120+
//...
121+
}
122+
func (b *DeleteBookBatchResults) Close() error {
123+
//...
124+
}
125+
```
126+
127+
## `:batchmany`
128+
129+
The generated method will return a batch object. The batch object will have
130+
the following methods:
131+
- `Query`, that takes a `func(int, []T, error)` parameter, where `T` is your query's return type
132+
- `Close`, to close the batch operation early.
133+
134+
```sql
135+
-- name: BooksByTitleYear :batchmany
136+
SELECT * FROM books
137+
WHERE title = $1 AND year = $2;
138+
```
139+
140+
```go
141+
type BooksByTitleYearBatchResults struct {
142+
br pgx.BatchResults
143+
ind int
144+
}
145+
type BooksByTitleYearParams struct {
146+
Title string `json:"title"`
147+
Year int32 `json:"year"`
148+
}
149+
func (q *Queries) BooksByTitleYear(ctx context.Context, arg []BooksByTitleYearParams) *BooksByTitleYearBatchResults {
150+
//...
151+
}
152+
func (b *BooksByTitleYearBatchResults) Query(f func(int, []Book, error)) {
153+
//...
154+
}
155+
func (b *BooksByTitleYearBatchResults) Close() error {
156+
//...
157+
}
158+
```
159+
160+
## `:batchone`
161+
162+
The generated method will return a batch object. The batch object will have
163+
the following methods:
164+
- `QueryRow`, that takes a `func(int, T, error)` parameter, where `T` is your query's return type
165+
- `Close`, to close the batch operation early.
166+
167+
```sql
168+
-- name: CreateBook :batchone
169+
INSERT INTO books (
170+
author_id,
171+
isbn
172+
) VALUES (
173+
$1,
174+
$2
175+
)
176+
RETURNING book_id, author_id, isbn
177+
```
178+
179+
```go
180+
type CreateBookBatchResults struct {
181+
br pgx.BatchResults
182+
ind int
183+
}
184+
type CreateBookParams struct {
185+
AuthorID int32 `json:"author_id"`
186+
Isbn string `json:"isbn"`
187+
}
188+
func (q *Queries) CreateBook(ctx context.Context, arg []CreateBookParams) *CreateBookBatchResults {
189+
//...
190+
}
191+
func (b *CreateBookBatchResults) QueryRow(f func(int, Book, error)) {
192+
//...
193+
}
194+
func (b *CreateBookBatchResults) Close() error {
195+
//...
196+
}
197+
```

0 commit comments

Comments
 (0)