Skip to content

Commit 5c5ae9b

Browse files
authored
feat(pgx): Add support for batch operations (#1437)
1 parent 6f783cd commit 5c5ae9b

File tree

32 files changed

+1436
-3
lines changed

32 files changed

+1436
-3
lines changed

docs/reference/query-annotations.md

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

examples/batch/postgresql/batch.go

Lines changed: 237 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/batch/postgresql/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)