-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch.go
77 lines (65 loc) · 1.84 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
73
74
75
76
77
package dotpgx
import (
"context"
"github.com/jackc/pgx"
"github.com/jackc/pgx/pgtype"
)
// Batch represents a pgx btach and the loaded query map.
// Support is still primitive and limited for our own use in migrations.
type Batch struct {
// Pgx provides direct access to the pgx batch object
Pgx *pgx.Batch
qm queryMap
}
// BeginBatch starts a new pgx batch.
func (db *DB) BeginBatch() *Batch {
return &Batch{
Pgx: db.Pool.BeginBatch(),
qm: db.qm,
}
}
// BeginBatch starts a new pgx batch inside the current transaction
func (tx *Tx) BeginBatch() *Batch {
return &Batch{
Pgx: tx.Ptx.BeginBatch(),
qm: tx.qm,
}
}
// Queue a query by name
func (b *Batch) Queue(name string, arguments []interface{}, parameterOIDs []pgtype.OID, resultFormatCodes []int16) (err error) {
q, err := b.qm.getQuery(name)
if err != nil {
return
}
b.Pgx.Queue(q.getSQL(), arguments, parameterOIDs, resultFormatCodes)
return
}
// QueueAll the registered queries, sorted by name.
func (b *Batch) QueueAll() {
mutex.Lock()
index := b.qm.sort()
for _, v := range index {
b.Queue(v, nil, nil, nil)
}
mutex.Unlock()
}
// Close the batch operation
func (b *Batch) Close() error {
return b.Pgx.Close()
}
// Send the batch
func (b *Batch) Send() error {
return b.Pgx.Send(context.TODO(), nil)
}
// ExecResults reads the results from the next query in the batch as if the query has been sent with Exec.
func (b *Batch) ExecResults() (pgx.CommandTag, error) {
return b.Pgx.ExecResults()
}
// QueryResults reads the results from the next query in the batch as if the query has been sent with Query.
func (b *Batch) QueryResults() (*pgx.Rows, error) {
return b.Pgx.QueryResults()
}
// QueryRowResults reads the results from the next query in the batch as if the query has been sent with QueryRow.
func (b *Batch) QueryRowResults() *pgx.Row {
return b.Pgx.QueryRowResults()
}