forked from jschaf/pggen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.sql.go
478 lines (425 loc) · 15.6 KB
/
query.sql.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// Code generated by pggen. DO NOT EDIT.
package device
import (
"context"
"fmt"
"github.com/jackc/pgconn"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
)
// Querier is a typesafe Go interface backed by SQL queries.
//
// Methods ending with Batch enqueue a query to run later in a pgx.Batch. After
// calling SendBatch on pgx.Conn, pgxpool.Pool, or pgx.Tx, use the Scan methods
// to parse the results.
type Querier interface {
FindDevicesByUser(ctx context.Context, id int) ([]FindDevicesByUserRow, error)
// FindDevicesByUserBatch enqueues a FindDevicesByUser query into batch to be executed
// later by the batch.
FindDevicesByUserBatch(batch *pgx.Batch, id int)
// FindDevicesByUserScan scans the result of an executed FindDevicesByUserBatch query.
FindDevicesByUserScan(results pgx.BatchResults) ([]FindDevicesByUserRow, error)
CompositeUser(ctx context.Context) ([]CompositeUserRow, error)
// CompositeUserBatch enqueues a CompositeUser query into batch to be executed
// later by the batch.
CompositeUserBatch(batch *pgx.Batch)
// CompositeUserScan scans the result of an executed CompositeUserBatch query.
CompositeUserScan(results pgx.BatchResults) ([]CompositeUserRow, error)
CompositeUserOne(ctx context.Context) (User, error)
// CompositeUserOneBatch enqueues a CompositeUserOne query into batch to be executed
// later by the batch.
CompositeUserOneBatch(batch *pgx.Batch)
// CompositeUserOneScan scans the result of an executed CompositeUserOneBatch query.
CompositeUserOneScan(results pgx.BatchResults) (User, error)
CompositeUserOneTwoCols(ctx context.Context) (CompositeUserOneTwoColsRow, error)
// CompositeUserOneTwoColsBatch enqueues a CompositeUserOneTwoCols query into batch to be executed
// later by the batch.
CompositeUserOneTwoColsBatch(batch *pgx.Batch)
// CompositeUserOneTwoColsScan scans the result of an executed CompositeUserOneTwoColsBatch query.
CompositeUserOneTwoColsScan(results pgx.BatchResults) (CompositeUserOneTwoColsRow, error)
CompositeUserMany(ctx context.Context) ([]User, error)
// CompositeUserManyBatch enqueues a CompositeUserMany query into batch to be executed
// later by the batch.
CompositeUserManyBatch(batch *pgx.Batch)
// CompositeUserManyScan scans the result of an executed CompositeUserManyBatch query.
CompositeUserManyScan(results pgx.BatchResults) ([]User, error)
InsertUser(ctx context.Context, userID int, name string) (pgconn.CommandTag, error)
// InsertUserBatch enqueues a InsertUser query into batch to be executed
// later by the batch.
InsertUserBatch(batch *pgx.Batch, userID int, name string)
// InsertUserScan scans the result of an executed InsertUserBatch query.
InsertUserScan(results pgx.BatchResults) (pgconn.CommandTag, error)
InsertDevice(ctx context.Context, mac pgtype.Macaddr, owner int) (pgconn.CommandTag, error)
// InsertDeviceBatch enqueues a InsertDevice query into batch to be executed
// later by the batch.
InsertDeviceBatch(batch *pgx.Batch, mac pgtype.Macaddr, owner int)
// InsertDeviceScan scans the result of an executed InsertDeviceBatch query.
InsertDeviceScan(results pgx.BatchResults) (pgconn.CommandTag, error)
}
type DBQuerier struct {
conn genericConn
}
var _ Querier = &DBQuerier{}
// genericConn is a connection to a Postgres database. This is usually backed by
// *pgx.Conn, pgx.Tx, or *pgxpool.Pool.
type genericConn interface {
// Query executes sql with args. If there is an error the returned Rows will
// be returned in an error state. So it is allowed to ignore the error
// returned from Query and handle it in Rows.
Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
// QueryRow is a convenience wrapper over Query. Any error that occurs while
// querying is deferred until calling Scan on the returned Row. That Row will
// error with pgx.ErrNoRows if no rows are returned.
QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
// Exec executes sql. sql can be either a prepared statement name or an SQL
// string. arguments should be referenced positionally from the sql string
// as $1, $2, etc.
Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
}
// NewQuerier creates a DBQuerier that implements Querier. conn is typically
// *pgx.Conn, pgx.Tx, or *pgxpool.Pool.
func NewQuerier(conn genericConn) *DBQuerier {
return &DBQuerier{
conn: conn,
}
}
// WithTx creates a new DBQuerier that uses the transaction to run all queries.
func (q *DBQuerier) WithTx(tx pgx.Tx) (*DBQuerier, error) {
return &DBQuerier{conn: tx}, nil
}
// User represents the Postgres composite type "user".
type User struct {
ID *int `json:"id"`
Name *string `json:"name"`
}
// ignoredOID means we don't know or care about the OID for a type. This is okay
// because pgx only uses the OID to encode values and lookup a decoder. We only
// use ignoredOID for decoding and we always specify a concrete decoder for scan
// methods.
const ignoredOID = 0
// DeviceType represents the Postgres enum "device_type".
type DeviceType string
const (
DeviceTypeUndefined DeviceType = "undefined"
DeviceTypePhone DeviceType = "phone"
DeviceTypeLaptop DeviceType = "laptop"
DeviceTypeIpad DeviceType = "ipad"
DeviceTypeDesktop DeviceType = "desktop"
DeviceTypeIot DeviceType = "iot"
)
func (d DeviceType) String() string { return string(d) }
func newCompositeType(name string, fieldNames []string, vals ...pgtype.ValueTranscoder) *pgtype.CompositeType {
fields := make([]pgtype.CompositeTypeField, len(fieldNames))
for i, name := range fieldNames {
fields[i] = pgtype.CompositeTypeField{Name: name, OID: ignoredOID}
}
// Okay to ignore error because it's only thrown when the number of field
// names does not equal the number of ValueTranscoders.
rowType, _ := pgtype.NewCompositeTypeValues(name, fields, vals)
return rowType
}
const findDevicesByUserSQL = `SELECT
id,
name,
(SELECT array_agg(mac) FROM device WHERE owner = id) AS mac_addrs
FROM "user"
WHERE id = $1;`
type FindDevicesByUserRow struct {
ID int `json:"id"`
Name string `json:"name"`
MacAddrs pgtype.MacaddrArray `json:"mac_addrs"`
}
// FindDevicesByUser implements Querier.FindDevicesByUser.
func (q *DBQuerier) FindDevicesByUser(ctx context.Context, id int) ([]FindDevicesByUserRow, error) {
rows, err := q.conn.Query(ctx, findDevicesByUserSQL, id)
if err != nil {
return nil, fmt.Errorf("query FindDevicesByUser: %w", err)
}
defer rows.Close()
items := []FindDevicesByUserRow{}
for rows.Next() {
var item FindDevicesByUserRow
if err := rows.Scan(&item.ID, &item.Name, &item.MacAddrs); err != nil {
return nil, fmt.Errorf("scan FindDevicesByUser row: %w", err)
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close FindDevicesByUser rows: %w", err)
}
return items, err
}
// FindDevicesByUserBatch implements Querier.FindDevicesByUserBatch.
func (q *DBQuerier) FindDevicesByUserBatch(batch *pgx.Batch, id int) {
batch.Queue(findDevicesByUserSQL, id)
}
// FindDevicesByUserScan implements Querier.FindDevicesByUserScan.
func (q *DBQuerier) FindDevicesByUserScan(results pgx.BatchResults) ([]FindDevicesByUserRow, error) {
rows, err := results.Query()
if err != nil {
return nil, fmt.Errorf("query FindDevicesByUserBatch: %w", err)
}
defer rows.Close()
items := []FindDevicesByUserRow{}
for rows.Next() {
var item FindDevicesByUserRow
if err := rows.Scan(&item.ID, &item.Name, &item.MacAddrs); err != nil {
return nil, fmt.Errorf("scan FindDevicesByUserBatch row: %w", err)
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close FindDevicesByUserBatch rows: %w", err)
}
return items, err
}
const compositeUserSQL = `SELECT
d.mac,
d.type,
ROW (u.id, u.name)::"user" AS "user"
FROM device d
LEFT JOIN "user" u ON u.id = d.owner;`
type CompositeUserRow struct {
Mac pgtype.Macaddr `json:"mac"`
Type DeviceType `json:"type"`
User User `json:"user"`
}
// CompositeUser implements Querier.CompositeUser.
func (q *DBQuerier) CompositeUser(ctx context.Context) ([]CompositeUserRow, error) {
rows, err := q.conn.Query(ctx, compositeUserSQL)
if err != nil {
return nil, fmt.Errorf("query CompositeUser: %w", err)
}
defer rows.Close()
items := []CompositeUserRow{}
userRow := newCompositeType(
"user",
[]string{"id", "name"},
&pgtype.Int8{},
&pgtype.Text{},
)
for rows.Next() {
var item CompositeUserRow
if err := rows.Scan(&item.Mac, &item.Type, userRow); err != nil {
return nil, fmt.Errorf("scan CompositeUser row: %w", err)
}
userRow.AssignTo(&item.User)
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close CompositeUser rows: %w", err)
}
return items, err
}
// CompositeUserBatch implements Querier.CompositeUserBatch.
func (q *DBQuerier) CompositeUserBatch(batch *pgx.Batch) {
batch.Queue(compositeUserSQL)
}
// CompositeUserScan implements Querier.CompositeUserScan.
func (q *DBQuerier) CompositeUserScan(results pgx.BatchResults) ([]CompositeUserRow, error) {
rows, err := results.Query()
if err != nil {
return nil, fmt.Errorf("query CompositeUserBatch: %w", err)
}
defer rows.Close()
items := []CompositeUserRow{}
userRow := newCompositeType(
"user",
[]string{"id", "name"},
&pgtype.Int8{},
&pgtype.Text{},
)
for rows.Next() {
var item CompositeUserRow
if err := rows.Scan(&item.Mac, &item.Type, userRow); err != nil {
return nil, fmt.Errorf("scan CompositeUserBatch row: %w", err)
}
userRow.AssignTo(&item.User)
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close CompositeUserBatch rows: %w", err)
}
return items, err
}
const compositeUserOneSQL = `SELECT ROW (15, 'qux')::"user" AS "user";`
// CompositeUserOne implements Querier.CompositeUserOne.
func (q *DBQuerier) CompositeUserOne(ctx context.Context) (User, error) {
row := q.conn.QueryRow(ctx, compositeUserOneSQL)
var item User
userRow := newCompositeType(
"user",
[]string{"id", "name"},
&pgtype.Int8{},
&pgtype.Text{},
)
if err := row.Scan(userRow); err != nil {
return item, fmt.Errorf("query CompositeUserOne: %w", err)
}
userRow.AssignTo(&item)
return item, nil
}
// CompositeUserOneBatch implements Querier.CompositeUserOneBatch.
func (q *DBQuerier) CompositeUserOneBatch(batch *pgx.Batch) {
batch.Queue(compositeUserOneSQL)
}
// CompositeUserOneScan implements Querier.CompositeUserOneScan.
func (q *DBQuerier) CompositeUserOneScan(results pgx.BatchResults) (User, error) {
row := results.QueryRow()
var item User
userRow := newCompositeType(
"user",
[]string{"id", "name"},
&pgtype.Int8{},
&pgtype.Text{},
)
if err := row.Scan(userRow); err != nil {
return item, fmt.Errorf("scan CompositeUserOneBatch row: %w", err)
}
userRow.AssignTo(&item)
return item, nil
}
const compositeUserOneTwoColsSQL = `SELECT 1 AS num, ROW (15, 'qux')::"user" AS "user";`
type CompositeUserOneTwoColsRow struct {
Num int32 `json:"num"`
User User `json:"user"`
}
// CompositeUserOneTwoCols implements Querier.CompositeUserOneTwoCols.
func (q *DBQuerier) CompositeUserOneTwoCols(ctx context.Context) (CompositeUserOneTwoColsRow, error) {
row := q.conn.QueryRow(ctx, compositeUserOneTwoColsSQL)
var item CompositeUserOneTwoColsRow
userRow := newCompositeType(
"user",
[]string{"id", "name"},
&pgtype.Int8{},
&pgtype.Text{},
)
if err := row.Scan(&item.Num, userRow); err != nil {
return item, fmt.Errorf("query CompositeUserOneTwoCols: %w", err)
}
userRow.AssignTo(&item.User)
return item, nil
}
// CompositeUserOneTwoColsBatch implements Querier.CompositeUserOneTwoColsBatch.
func (q *DBQuerier) CompositeUserOneTwoColsBatch(batch *pgx.Batch) {
batch.Queue(compositeUserOneTwoColsSQL)
}
// CompositeUserOneTwoColsScan implements Querier.CompositeUserOneTwoColsScan.
func (q *DBQuerier) CompositeUserOneTwoColsScan(results pgx.BatchResults) (CompositeUserOneTwoColsRow, error) {
row := results.QueryRow()
var item CompositeUserOneTwoColsRow
userRow := newCompositeType(
"user",
[]string{"id", "name"},
&pgtype.Int8{},
&pgtype.Text{},
)
if err := row.Scan(&item.Num, userRow); err != nil {
return item, fmt.Errorf("scan CompositeUserOneTwoColsBatch row: %w", err)
}
userRow.AssignTo(&item.User)
return item, nil
}
const compositeUserManySQL = `SELECT ROW (15, 'qux')::"user" AS "user";`
// CompositeUserMany implements Querier.CompositeUserMany.
func (q *DBQuerier) CompositeUserMany(ctx context.Context) ([]User, error) {
rows, err := q.conn.Query(ctx, compositeUserManySQL)
if err != nil {
return nil, fmt.Errorf("query CompositeUserMany: %w", err)
}
defer rows.Close()
items := []User{}
userRow := newCompositeType(
"user",
[]string{"id", "name"},
&pgtype.Int8{},
&pgtype.Text{},
)
for rows.Next() {
var item User
if err := rows.Scan(userRow); err != nil {
return nil, fmt.Errorf("scan CompositeUserMany row: %w", err)
}
userRow.AssignTo(&item)
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close CompositeUserMany rows: %w", err)
}
return items, err
}
// CompositeUserManyBatch implements Querier.CompositeUserManyBatch.
func (q *DBQuerier) CompositeUserManyBatch(batch *pgx.Batch) {
batch.Queue(compositeUserManySQL)
}
// CompositeUserManyScan implements Querier.CompositeUserManyScan.
func (q *DBQuerier) CompositeUserManyScan(results pgx.BatchResults) ([]User, error) {
rows, err := results.Query()
if err != nil {
return nil, fmt.Errorf("query CompositeUserManyBatch: %w", err)
}
defer rows.Close()
items := []User{}
userRow := newCompositeType(
"user",
[]string{"id", "name"},
&pgtype.Int8{},
&pgtype.Text{},
)
for rows.Next() {
var item User
if err := rows.Scan(userRow); err != nil {
return nil, fmt.Errorf("scan CompositeUserManyBatch row: %w", err)
}
userRow.AssignTo(&item)
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close CompositeUserManyBatch rows: %w", err)
}
return items, err
}
const insertUserSQL = `INSERT INTO "user" (id, name)
VALUES ($1, $2);`
// InsertUser implements Querier.InsertUser.
func (q *DBQuerier) InsertUser(ctx context.Context, userID int, name string) (pgconn.CommandTag, error) {
cmdTag, err := q.conn.Exec(ctx, insertUserSQL, userID, name)
if err != nil {
return cmdTag, fmt.Errorf("exec query InsertUser: %w", err)
}
return cmdTag, err
}
// InsertUserBatch implements Querier.InsertUserBatch.
func (q *DBQuerier) InsertUserBatch(batch *pgx.Batch, userID int, name string) {
batch.Queue(insertUserSQL, userID, name)
}
// InsertUserScan implements Querier.InsertUserScan.
func (q *DBQuerier) InsertUserScan(results pgx.BatchResults) (pgconn.CommandTag, error) {
cmdTag, err := results.Exec()
if err != nil {
return cmdTag, fmt.Errorf("exec InsertUserBatch: %w", err)
}
return cmdTag, err
}
const insertDeviceSQL = `INSERT INTO device (mac, owner)
VALUES ($1, $2);`
// InsertDevice implements Querier.InsertDevice.
func (q *DBQuerier) InsertDevice(ctx context.Context, mac pgtype.Macaddr, owner int) (pgconn.CommandTag, error) {
cmdTag, err := q.conn.Exec(ctx, insertDeviceSQL, mac, owner)
if err != nil {
return cmdTag, fmt.Errorf("exec query InsertDevice: %w", err)
}
return cmdTag, err
}
// InsertDeviceBatch implements Querier.InsertDeviceBatch.
func (q *DBQuerier) InsertDeviceBatch(batch *pgx.Batch, mac pgtype.Macaddr, owner int) {
batch.Queue(insertDeviceSQL, mac, owner)
}
// InsertDeviceScan implements Querier.InsertDeviceScan.
func (q *DBQuerier) InsertDeviceScan(results pgx.BatchResults) (pgconn.CommandTag, error) {
cmdTag, err := results.Exec()
if err != nil {
return cmdTag, fmt.Errorf("exec InsertDeviceBatch: %w", err)
}
return cmdTag, err
}