-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconn.go
More file actions
299 lines (245 loc) · 7.47 KB
/
conn.go
File metadata and controls
299 lines (245 loc) · 7.47 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package pg
import (
"context"
"errors"
// Packages
pgx "github.com/jackc/pgx/v5"
)
////////////////////////////////////////////////////////////////////////////////
// TYPES
type Conn interface {
// Return a new connection with bound parameters
With(...any) Conn
// Return a new connection with bound queries
WithQueries(...*Queries) Conn
// Return a connection to a remote database
Remote(database string) Conn
// Perform a transaction within a function
Tx(context.Context, func(Conn) error) error
// Perform a bulk operation within a function (and indicate whether this
// should be in a transaction)
Bulk(context.Context, func(Conn) error) error
// Execute a query
Exec(context.Context, string) error
// Perform an insert
Insert(context.Context, Reader, Writer) error
// Perform an update
Update(context.Context, Reader, Selector, Writer) error
// Perform a delete
Delete(context.Context, Reader, Selector) error
// Perform a get
Get(context.Context, Reader, Selector) error
// Perform a list. If the reader is a ListReader, then the
// count of items is also calculated
List(context.Context, Reader, Selector) error
}
// Op represents a database operation type.
type Op uint
// Row is a pgx.Row for scanning query results.
type Row pgx.Row
// Reader scans a database row into an object.
type Reader interface {
// Scan row into a result
Scan(Row) error
}
// ListReader scans database rows and counts total results.
type ListReader interface {
Reader
// Scan count into the result
ScanCount(Row) error
}
// Writer binds object fields for insert or update operations.
type Writer interface {
// Set bind parameters for an insert
Insert(*Bind) (string, error)
// Set bind parameters for an update
Update(*Bind) error
}
// Selector binds parameters for get, update, or delete operations.
type Selector interface {
// Set bind parameters for getting, updating or deleting
Select(*Bind, Op) (string, error)
}
// Concrete connection
type conn struct {
conn pgx.Tx
bind *Bind
}
// Ensure interfaces are satisfied
var _ Conn = (*conn)(nil)
////////////////////////////////////////////////////////////////////////////////
// GLOBALS
// Operations
const (
None Op = iota
Get
Insert
Update
Delete
List
)
func (o Op) String() string {
switch o {
case Get:
return "GET"
case Insert:
return "INSERT"
case Update:
return "UPDATE"
case Delete:
return "DELETE"
case List:
return "LIST"
}
return "UNKNOWN"
}
////////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS - CONN
// Return a new connection with new bound parameters
func (p *conn) With(params ...any) Conn {
return &conn{p.conn, p.bind.Copy(params...)}
}
// Return a new connection with bound queries
func (p *conn) WithQueries(queries ...*Queries) Conn {
return &conn{p.conn, p.bind.withQueries(queries...)}
}
// Return a connection to a remote database
func (p *conn) Remote(database string) Conn {
return &conn{p.conn, p.bind.withRemote(database)}
}
// Perform a transaction, then commit or rollback
func (p *conn) Tx(ctx context.Context, fn func(Conn) error) error {
return tx(ctx, p.conn, p.bind, fn)
}
// Perform a bulk operation and indicate whether this should be in
// a transaction
func (p *conn) Bulk(ctx context.Context, fn func(Conn) error) error {
return bulk(ctx, p.conn, p.bind, fn)
}
// Execute a query
func (p *conn) Exec(ctx context.Context, query string) error {
return p.bind.exec(ctx, p.conn, query)
}
// Perform an insert, binding parameters from
// the writer, and scanning the result into the reader
func (p *conn) Insert(ctx context.Context, reader Reader, writer Writer) error {
return insert(ctx, p.conn, p.bind, reader, writer)
}
// Perform an update, selecting using the selector, binding parameters from
// the writer, and scanning the result into the reader
func (p *conn) Update(ctx context.Context, reader Reader, sel Selector, writer Writer) error {
return update(ctx, p.conn, p.bind, reader, sel, writer)
}
// Perform a delete, binding parameters with the selector and scanning the
// deleted data into the reader
func (p *conn) Delete(ctx context.Context, reader Reader, sel Selector) error {
return del(ctx, p.conn, p.bind, reader, sel)
}
// Perform a get, binding parameters with the selector and scanning a single
// row into the reader
func (p *conn) Get(ctx context.Context, reader Reader, sel Selector) error {
return get(ctx, p.conn, p.bind, reader, sel)
}
// Perform a list, binding parameters with the selector and scanning rows
// into the reader
func (p *conn) List(ctx context.Context, reader Reader, sel Selector) error {
return list(ctx, p.conn, p.bind, reader, sel)
}
////////////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
func tx(ctx context.Context, tx pgx.Tx, bind *Bind, fn func(Conn) error) error {
tx, err := tx.Begin(ctx)
if err != nil {
return err
}
tx_ := &conn{tx, bind.Copy()}
if err := fn(tx_); err != nil {
return errors.Join(pgerror(err), tx.Rollback(ctx))
} else {
return errors.Join(pgerror(err), tx.Commit(ctx))
}
}
func insert(ctx context.Context, conn pgx.Tx, bind *Bind, reader Reader, writer Writer) error {
query, err := writer.Insert(bind)
if err != nil {
return err
}
return exec(ctx, conn, bind, query, reader)
}
func update(ctx context.Context, conn pgx.Tx, bind *Bind, reader Reader, sel Selector, writer Writer) error {
query, err := sel.Select(bind, Update)
if err != nil {
return err
}
if writer != nil {
if err := writer.Update(bind); err != nil {
return err
}
}
return exec(ctx, conn, bind, query, reader)
}
func del(ctx context.Context, conn pgx.Tx, bind *Bind, reader Reader, sel Selector) error {
query, err := sel.Select(bind, Delete)
if err != nil {
return err
}
return exec(ctx, conn, bind, query, reader)
}
func get(ctx context.Context, conn pgx.Tx, bind *Bind, reader Reader, sel Selector) error {
query, err := sel.Select(bind, Get)
if err != nil {
return err
}
return exec(ctx, conn, bind, query, reader)
}
func list(ctx context.Context, conn pgx.Tx, bind *Bind, reader Reader, sel Selector) error {
bind.Set("offsetlimit", "")
query, err := sel.Select(bind, List)
if err != nil {
return pgerror(err)
}
// Count the number of rows if the reader is a ListReader
if counter, ok := reader.(ListReader); ok {
if err := count(ctx, conn, query, bind, counter); err != nil {
return pgerror(err)
}
}
// Execute the query. In the case of a list, we return success even if there aren't any
// rows returned
if err := exec(ctx, conn, bind, query+` ${offsetlimit}`, reader); errors.Is(err, ErrNotFound) {
return nil
} else {
return pgerror(err)
}
}
func count(ctx context.Context, conn pgx.Tx, query string, bind *Bind, reader ListReader) error {
// Make a subquery
return pgerror(reader.ScanCount(bind.Copy("as", "t (count BIGINT)").queryRow(ctx, conn, `WITH sq AS (`+query+`) SELECT COUNT(*) AS "count" FROM sq`)))
}
func exec(ctx context.Context, conn pgx.Tx, bind *Bind, query string, reader Reader) error {
// Without a reader, just execute the query
if reader == nil {
return pgerror(bind.exec(ctx, conn, query))
}
// Execute the query
rows, err := bind.query(ctx, conn, query)
if err != nil {
return pgerror(err)
}
defer rows.Close()
// Read rows
var scanned bool
for rows.Next() {
if err := reader.Scan(rows); err != nil {
return pgerror(err)
}
scanned = true
}
if err := rows.Err(); err != nil {
return err
} else if !scanned {
return pgerror(pgx.ErrNoRows)
}
// Return success
return nil
}