/
batch.go
68 lines (59 loc) · 1.21 KB
/
batch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.17.2
// source: batch.go
package querytest
import (
"context"
"database/sql"
"errors"
"github.com/jackc/pgx/v4"
)
const insertValues = `-- name: InsertValues :batchone
INSERT INTO foo (a, b)
VALUES ($1, $2)
ON CONFLICT DO NOTHING
RETURNING a, b
`
type InsertValuesBatchResults struct {
br pgx.BatchResults
tot int
closed bool
}
type InsertValuesParams struct {
A sql.NullInt32
B sql.NullInt32
}
func (q *Queries) InsertValues(ctx context.Context, arg []*InsertValuesParams) *InsertValuesBatchResults {
batch := &pgx.Batch{}
for _, a := range arg {
vals := []interface{}{
a.A,
a.B,
}
batch.Queue(insertValues, vals...)
}
br := q.db.SendBatch(ctx, batch)
return &InsertValuesBatchResults{br, len(arg), false}
}
func (b *InsertValuesBatchResults) QueryRow(f func(int, *Foo, error)) {
defer b.br.Close()
for t := 0; t < b.tot; t++ {
var i Foo
if b.closed {
if f != nil {
f(t, nil, errors.New("batch already closed"))
}
continue
}
row := b.br.QueryRow()
err := row.Scan(&i.A, &i.B)
if f != nil {
f(t, &i, err)
}
}
}
func (b *InsertValuesBatchResults) Close() error {
b.closed = true
return b.br.Close()
}