-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor.go
More file actions
185 lines (160 loc) · 3.6 KB
/
Copy pathcursor.go
File metadata and controls
185 lines (160 loc) · 3.6 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package pgxcursor
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
// Queryable is the interface that wraps the Query method.
type Queryable interface {
// Begin starts a pseudo nested transaction.
Begin(ctx context.Context) (pgx.Tx, error)
// Exec executes a query that doesn't return rows.
Exec(ctx context.Context, query string, args ...any) (pgconn.CommandTag, error)
// Query executes a query that returns rows.
Query(ctx context.Context, query string, args ...any) (pgx.Rows, error)
}
// Querier represents a PostgreSQL cursor querier.
type Querier struct {
// Capacity is the maximum number of rows to fetch for each iteration.
Capacity int
// Querier is the interface that wraps the Query method.
Querier Queryable
}
// Query executes a query that returns rows.
func (c *Querier) Query(ctx context.Context, query string, args ...any) (pgx.Rows, error) {
// predefined cursor name
name := fmt.Sprintf("c%x", uuid.New())
// begin a transaction
tx, err := c.Querier.Begin(ctx)
if err != nil {
return nil, err
}
query = fmt.Sprintf("DECLARE %q CURSOR FOR %s", name, query)
// declare the cursor
if _, err := tx.Exec(ctx, query, args...); err != nil {
// rollback the transaction
tx.Rollback(ctx)
// return the error
return nil, err
}
// prepare the cursor
cursor := &Rows{
tx: tx,
ctx: ctx,
cap: c.Capacity,
name: name,
}
return cursor, nil
}
var _ pgx.Rows = &Rows{}
// Rows is a wrapper around pgx.Rows.
type Rows struct {
cap int
err error
name string
tx pgx.Tx
rows pgx.Rows
ctx context.Context
}
// Err implements pgx.Rows.
func (r *Rows) Err() error {
if r.rows != nil {
return r.rows.Err()
}
return r.err
}
// Conn implements pgx.Rows.
func (r *Rows) Conn() *pgx.Conn {
return r.tx.Conn()
}
// Close implements pgx.Rows.
func (r *Rows) Close() {
if r.rows != nil {
// close the rows
r.close()
}
// rollback the transaction
if err := r.tx.Rollback(r.ctx); err != nil {
r.err = err
}
}
// FieldDescriptions implements pgx.Rows.
func (r *Rows) FieldDescriptions() []pgconn.FieldDescription {
if r.rows != nil {
return r.rows.FieldDescriptions()
}
// noop
return nil
}
// CommandTag implements pgx.Rows.
func (r *Rows) CommandTag() pgconn.CommandTag {
if r.rows != nil {
return r.rows.CommandTag()
}
// noop
return pgconn.CommandTag{}
}
// Next implements pgx.Rows.
func (r *Rows) Next() bool {
if r.rows == nil {
// move the cursor
return r.next()
}
if !r.rows.Next() {
// close the rows
r.close()
// move to the next row
return r.next()
}
return true
}
// Scan implements pgx.Rows.
func (r *Rows) Scan(dest ...any) error {
if r.rows != nil {
return r.rows.Scan(dest...)
}
// noop
return nil
}
// RawValues implements pgx.Rows.
func (r *Rows) RawValues() [][]byte {
if r.rows != nil {
return r.rows.RawValues()
}
// noop
return nil
}
// Values implements pgx.Rows.
func (r *Rows) Values() ([]any, error) {
if r.rows != nil {
return r.rows.Values()
}
// noop
return nil, nil
}
// next fetches the next rows.
func (r *Rows) next() bool {
var query string
// prepare the query
if r.cap > 0 {
query = fmt.Sprintf("FETCH %d FROM %v", r.cap, r.name)
} else {
query = fmt.Sprintf("FETCH NEXT FROM %v", r.name)
}
// if name is empty, then the cursor is not declared
if r.rows, r.err = r.tx.Query(r.ctx, query); r.err != nil {
return false
}
return r.rows.Next()
}
// close closes the rows and sets the error if any.
func (r *Rows) close() {
// close the rows
r.rows.Close()
// set the error if any
r.err = r.rows.Err()
// reset the rows
r.rows = nil
}