-
Notifications
You must be signed in to change notification settings - Fork 1
/
batch.go
72 lines (62 loc) · 1.31 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
69
70
71
72
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.25.0
// source: batch.go
package postgres
import (
"context"
"errors"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)
var (
ErrBatchAlreadyClosed = errors.New("batch already closed")
)
const aCLUpdate = `-- name: ACLUpdate :batchexec
INSERT INTO acl(uuid, uri, permissions)
VALUES ($1, $2, $3::text[])
ON CONFLICT(uuid, uri) DO UPDATE SET
permissions = $3::text[]
`
type ACLUpdateBatchResults struct {
br pgx.BatchResults
tot int
closed bool
}
type ACLUpdateParams struct {
UUID uuid.UUID
URI string
Permissions []string
}
func (q *Queries) ACLUpdate(ctx context.Context, arg []ACLUpdateParams) *ACLUpdateBatchResults {
batch := &pgx.Batch{}
for _, a := range arg {
vals := []interface{}{
a.UUID,
a.URI,
a.Permissions,
}
batch.Queue(aCLUpdate, vals...)
}
br := q.db.SendBatch(ctx, batch)
return &ACLUpdateBatchResults{br, len(arg), false}
}
func (b *ACLUpdateBatchResults) Exec(f func(int, error)) {
defer b.br.Close()
for t := 0; t < b.tot; t++ {
if b.closed {
if f != nil {
f(t, ErrBatchAlreadyClosed)
}
continue
}
_, err := b.br.Exec()
if f != nil {
f(t, err)
}
}
}
func (b *ACLUpdateBatchResults) Close() error {
b.closed = true
return b.br.Close()
}