forked from jschaf/pggen
/
query.sql.go
691 lines (607 loc) · 26.9 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
// Code generated by pggen. DO NOT EDIT.
package author
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v5"
)
// 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 {
// FindAuthorById finds one (or zero) authors by ID.
FindAuthorByID(ctx context.Context, authorID int32) (FindAuthorByIDRow, error)
// FindAuthorByIDBatch enqueues a FindAuthorByID query into batch to be executed
// later by the batch.
FindAuthorByIDBatch(batch genericBatch, authorID int32)
// FindAuthorByIDScan scans the result of an executed FindAuthorByIDBatch query.
FindAuthorByIDScan(results pgx.BatchResults) (FindAuthorByIDRow, error)
// FindAuthors finds authors by first name.
FindAuthors(ctx context.Context, firstName string) ([]FindAuthorsRow, error)
// FindAuthorsBatch enqueues a FindAuthors query into batch to be executed
// later by the batch.
FindAuthorsBatch(batch genericBatch, firstName string)
// FindAuthorsScan scans the result of an executed FindAuthorsBatch query.
FindAuthorsScan(results pgx.BatchResults) ([]FindAuthorsRow, error)
// FindAuthorNames finds one (or zero) authors by ID.
FindAuthorNames(ctx context.Context, authorID int32) ([]FindAuthorNamesRow, error)
// FindAuthorNamesBatch enqueues a FindAuthorNames query into batch to be executed
// later by the batch.
FindAuthorNamesBatch(batch genericBatch, authorID int32)
// FindAuthorNamesScan scans the result of an executed FindAuthorNamesBatch query.
FindAuthorNamesScan(results pgx.BatchResults) ([]FindAuthorNamesRow, error)
// FindFirstNames finds one (or zero) authors by ID.
FindFirstNames(ctx context.Context, authorID int32) ([]*string, error)
// FindFirstNamesBatch enqueues a FindFirstNames query into batch to be executed
// later by the batch.
FindFirstNamesBatch(batch genericBatch, authorID int32)
// FindFirstNamesScan scans the result of an executed FindFirstNamesBatch query.
FindFirstNamesScan(results pgx.BatchResults) ([]*string, error)
// DeleteAuthors deletes authors with a first name of "joe".
DeleteAuthors(ctx context.Context) (pgconn.CommandTag, error)
// DeleteAuthorsBatch enqueues a DeleteAuthors query into batch to be executed
// later by the batch.
DeleteAuthorsBatch(batch genericBatch)
// DeleteAuthorsScan scans the result of an executed DeleteAuthorsBatch query.
DeleteAuthorsScan(results pgx.BatchResults) (pgconn.CommandTag, error)
// DeleteAuthorsByFirstName deletes authors by first name.
DeleteAuthorsByFirstName(ctx context.Context, firstName string) (pgconn.CommandTag, error)
// DeleteAuthorsByFirstNameBatch enqueues a DeleteAuthorsByFirstName query into batch to be executed
// later by the batch.
DeleteAuthorsByFirstNameBatch(batch genericBatch, firstName string)
// DeleteAuthorsByFirstNameScan scans the result of an executed DeleteAuthorsByFirstNameBatch query.
DeleteAuthorsByFirstNameScan(results pgx.BatchResults) (pgconn.CommandTag, error)
// DeleteAuthorsByFullName deletes authors by the full name.
DeleteAuthorsByFullName(ctx context.Context, params DeleteAuthorsByFullNameParams) (pgconn.CommandTag, error)
// DeleteAuthorsByFullNameBatch enqueues a DeleteAuthorsByFullName query into batch to be executed
// later by the batch.
DeleteAuthorsByFullNameBatch(batch genericBatch, params DeleteAuthorsByFullNameParams)
// DeleteAuthorsByFullNameScan scans the result of an executed DeleteAuthorsByFullNameBatch query.
DeleteAuthorsByFullNameScan(results pgx.BatchResults) (pgconn.CommandTag, error)
// InsertAuthor inserts an author by name and returns the ID.
InsertAuthor(ctx context.Context, firstName string, lastName string) (int32, error)
// InsertAuthorBatch enqueues a InsertAuthor query into batch to be executed
// later by the batch.
InsertAuthorBatch(batch genericBatch, firstName string, lastName string)
// InsertAuthorScan scans the result of an executed InsertAuthorBatch query.
InsertAuthorScan(results pgx.BatchResults) (int32, error)
// InsertAuthorSuffix inserts an author by name and suffix and returns the
// entire row.
InsertAuthorSuffix(ctx context.Context, params InsertAuthorSuffixParams) (InsertAuthorSuffixRow, error)
// InsertAuthorSuffixBatch enqueues a InsertAuthorSuffix query into batch to be executed
// later by the batch.
InsertAuthorSuffixBatch(batch genericBatch, params InsertAuthorSuffixParams)
// InsertAuthorSuffixScan scans the result of an executed InsertAuthorSuffixBatch query.
InsertAuthorSuffixScan(results pgx.BatchResults) (InsertAuthorSuffixRow, error)
StringAggFirstName(ctx context.Context, authorID int32) (*string, error)
// StringAggFirstNameBatch enqueues a StringAggFirstName query into batch to be executed
// later by the batch.
StringAggFirstNameBatch(batch genericBatch, authorID int32)
// StringAggFirstNameScan scans the result of an executed StringAggFirstNameBatch query.
StringAggFirstNameScan(results pgx.BatchResults) (*string, error)
ArrayAggFirstName(ctx context.Context, authorID int32) ([]string, error)
// ArrayAggFirstNameBatch enqueues a ArrayAggFirstName query into batch to be executed
// later by the batch.
ArrayAggFirstNameBatch(batch genericBatch, authorID int32)
// ArrayAggFirstNameScan scans the result of an executed ArrayAggFirstNameBatch query.
ArrayAggFirstNameScan(results pgx.BatchResults) ([]string, error)
}
type DBQuerier struct {
conn genericConn // underlying Postgres transport to use
types *typeResolver // resolve types by name
}
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 ...any) (pgconn.CommandTag, error)
}
// genericBatch batches queries to send in a single network request to a
// Postgres server. This is usually backed by *pgx.Batch.
type genericBatch interface {
// Queue queues a query to batch b. query can be an SQL query or the name of a
// prepared statement. See Queue on *pgx.Batch.
Queue(query string, arguments ...any) *pgx.QueuedQuery
}
// NewQuerier creates a DBQuerier that implements Querier. conn is typically
// *pgx.Conn, pgx.Tx, or *pgxpool.Pool.
func NewQuerier(conn genericConn) *DBQuerier {
return NewQuerierConfig(conn, QuerierConfig{})
}
type QuerierConfig struct {
// DataTypes contains pgtype.Value to use for encoding and decoding instead
// of pggen-generated pgtype.ValueTranscoder.
//
// If OIDs are available for an input parameter type and all of its
// transitive dependencies, pggen will use the binary encoding format for
// the input parameter.
DataTypes []pgtype.DataType
}
// NewQuerierConfig creates a DBQuerier that implements Querier with the given
// config. conn is typically *pgx.Conn, pgx.Tx, or *pgxpool.Pool.
func NewQuerierConfig(conn genericConn, cfg QuerierConfig) *DBQuerier {
return &DBQuerier{conn: conn, types: newTypeResolver(cfg.DataTypes)}
}
// 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
}
// preparer is any Postgres connection transport that provides a way to prepare
// a statement, most commonly *pgx.Conn.
type preparer interface {
Prepare(ctx context.Context, name, sql string) (sd *pgconn.StatementDescription, err error)
}
// PrepareAllQueries executes a PREPARE statement for all pggen generated SQL
// queries in querier files. Typical usage is as the AfterConnect callback
// for pgxpool.Config
//
// pgx will use the prepared statement if available. Calling PrepareAllQueries
// is an optional optimization to avoid a network round-trip the first time pgx
// runs a query if pgx statement caching is enabled.
func PrepareAllQueries(ctx context.Context, p preparer) error {
if _, err := p.Prepare(ctx, findAuthorByIDSQL, findAuthorByIDSQL); err != nil {
return fmt.Errorf("prepare query 'FindAuthorByID': %w", err)
}
if _, err := p.Prepare(ctx, findAuthorsSQL, findAuthorsSQL); err != nil {
return fmt.Errorf("prepare query 'FindAuthors': %w", err)
}
if _, err := p.Prepare(ctx, findAuthorNamesSQL, findAuthorNamesSQL); err != nil {
return fmt.Errorf("prepare query 'FindAuthorNames': %w", err)
}
if _, err := p.Prepare(ctx, findFirstNamesSQL, findFirstNamesSQL); err != nil {
return fmt.Errorf("prepare query 'FindFirstNames': %w", err)
}
if _, err := p.Prepare(ctx, deleteAuthorsSQL, deleteAuthorsSQL); err != nil {
return fmt.Errorf("prepare query 'DeleteAuthors': %w", err)
}
if _, err := p.Prepare(ctx, deleteAuthorsByFirstNameSQL, deleteAuthorsByFirstNameSQL); err != nil {
return fmt.Errorf("prepare query 'DeleteAuthorsByFirstName': %w", err)
}
if _, err := p.Prepare(ctx, deleteAuthorsByFullNameSQL, deleteAuthorsByFullNameSQL); err != nil {
return fmt.Errorf("prepare query 'DeleteAuthorsByFullName': %w", err)
}
if _, err := p.Prepare(ctx, insertAuthorSQL, insertAuthorSQL); err != nil {
return fmt.Errorf("prepare query 'InsertAuthor': %w", err)
}
if _, err := p.Prepare(ctx, insertAuthorSuffixSQL, insertAuthorSuffixSQL); err != nil {
return fmt.Errorf("prepare query 'InsertAuthorSuffix': %w", err)
}
if _, err := p.Prepare(ctx, stringAggFirstNameSQL, stringAggFirstNameSQL); err != nil {
return fmt.Errorf("prepare query 'StringAggFirstName': %w", err)
}
if _, err := p.Prepare(ctx, arrayAggFirstNameSQL, arrayAggFirstNameSQL); err != nil {
return fmt.Errorf("prepare query 'ArrayAggFirstName': %w", err)
}
return nil
}
// typeResolver looks up the pgtype.ValueTranscoder by Postgres type name.
type typeResolver struct {
connInfo *pgtype.ConnInfo // types by Postgres type name
}
func newTypeResolver(types []pgtype.DataType) *typeResolver {
ci := pgtype.NewConnInfo()
for _, typ := range types {
if txt, ok := typ.Value.(textPreferrer); ok && typ.OID != unknownOID {
typ.Value = txt.ValueTranscoder
}
ci.RegisterDataType(typ)
}
return &typeResolver{connInfo: ci}
}
// findValue find the OID, and pgtype.ValueTranscoder for a Postgres type name.
func (tr *typeResolver) findValue(name string) (uint32, pgtype.ValueTranscoder, bool) {
typ, ok := tr.connInfo.DataTypeForName(name)
if !ok {
return 0, nil, false
}
v := pgtype.NewValue(typ.Value)
return typ.OID, v.(pgtype.ValueTranscoder), true
}
// setValue sets the value of a ValueTranscoder to a value that should always
// work and panics if it fails.
func (tr *typeResolver) setValue(vt pgtype.ValueTranscoder, val interface{}) pgtype.ValueTranscoder {
if err := vt.Set(val); err != nil {
panic(fmt.Sprintf("set ValueTranscoder %T to %+v: %s", vt, val, err))
}
return vt
}
const findAuthorByIDSQL = `SELECT * FROM author WHERE author_id = $1;`
type FindAuthorByIDRow struct {
AuthorID int32 `json:"author_id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Suffix *string `json:"suffix"`
}
// FindAuthorByID implements Querier.FindAuthorByID.
func (q *DBQuerier) FindAuthorByID(ctx context.Context, authorID int32) (FindAuthorByIDRow, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "FindAuthorByID")
row := q.conn.QueryRow(ctx, findAuthorByIDSQL, authorID)
var item FindAuthorByIDRow
if err := row.Scan(&item.AuthorID, &item.FirstName, &item.LastName, &item.Suffix); err != nil {
return item, fmt.Errorf("query FindAuthorByID: %w", err)
}
return item, nil
}
// FindAuthorByIDBatch implements Querier.FindAuthorByIDBatch.
func (q *DBQuerier) FindAuthorByIDBatch(batch genericBatch, authorID int32) {
batch.Queue(findAuthorByIDSQL, authorID)
}
// FindAuthorByIDScan implements Querier.FindAuthorByIDScan.
func (q *DBQuerier) FindAuthorByIDScan(results pgx.BatchResults) (FindAuthorByIDRow, error) {
row := results.QueryRow()
var item FindAuthorByIDRow
if err := row.Scan(&item.AuthorID, &item.FirstName, &item.LastName, &item.Suffix); err != nil {
return item, fmt.Errorf("scan FindAuthorByIDBatch row: %w", err)
}
return item, nil
}
const findAuthorsSQL = `SELECT * FROM author WHERE first_name = $1;`
type FindAuthorsRow struct {
AuthorID int32 `json:"author_id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Suffix *string `json:"suffix"`
}
// FindAuthors implements Querier.FindAuthors.
func (q *DBQuerier) FindAuthors(ctx context.Context, firstName string) ([]FindAuthorsRow, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "FindAuthors")
rows, err := q.conn.Query(ctx, findAuthorsSQL, firstName)
if err != nil {
return nil, fmt.Errorf("query FindAuthors: %w", err)
}
defer rows.Close()
items := []FindAuthorsRow{}
for rows.Next() {
var item FindAuthorsRow
if err := rows.Scan(&item.AuthorID, &item.FirstName, &item.LastName, &item.Suffix); err != nil {
return nil, fmt.Errorf("scan FindAuthors row: %w", err)
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close FindAuthors rows: %w", err)
}
return items, err
}
// FindAuthorsBatch implements Querier.FindAuthorsBatch.
func (q *DBQuerier) FindAuthorsBatch(batch genericBatch, firstName string) {
batch.Queue(findAuthorsSQL, firstName)
}
// FindAuthorsScan implements Querier.FindAuthorsScan.
func (q *DBQuerier) FindAuthorsScan(results pgx.BatchResults) ([]FindAuthorsRow, error) {
rows, err := results.Query()
if err != nil {
return nil, fmt.Errorf("query FindAuthorsBatch: %w", err)
}
defer rows.Close()
items := []FindAuthorsRow{}
for rows.Next() {
var item FindAuthorsRow
if err := rows.Scan(&item.AuthorID, &item.FirstName, &item.LastName, &item.Suffix); err != nil {
return nil, fmt.Errorf("scan FindAuthorsBatch row: %w", err)
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close FindAuthorsBatch rows: %w", err)
}
return items, err
}
const findAuthorNamesSQL = `SELECT first_name, last_name FROM author ORDER BY author_id = $1;`
type FindAuthorNamesRow struct {
FirstName *string `json:"first_name"`
LastName *string `json:"last_name"`
}
// FindAuthorNames implements Querier.FindAuthorNames.
func (q *DBQuerier) FindAuthorNames(ctx context.Context, authorID int32) ([]FindAuthorNamesRow, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "FindAuthorNames")
rows, err := q.conn.Query(ctx, findAuthorNamesSQL, authorID)
if err != nil {
return nil, fmt.Errorf("query FindAuthorNames: %w", err)
}
defer rows.Close()
items := []FindAuthorNamesRow{}
for rows.Next() {
var item FindAuthorNamesRow
if err := rows.Scan(&item.FirstName, &item.LastName); err != nil {
return nil, fmt.Errorf("scan FindAuthorNames row: %w", err)
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close FindAuthorNames rows: %w", err)
}
return items, err
}
// FindAuthorNamesBatch implements Querier.FindAuthorNamesBatch.
func (q *DBQuerier) FindAuthorNamesBatch(batch genericBatch, authorID int32) {
batch.Queue(findAuthorNamesSQL, authorID)
}
// FindAuthorNamesScan implements Querier.FindAuthorNamesScan.
func (q *DBQuerier) FindAuthorNamesScan(results pgx.BatchResults) ([]FindAuthorNamesRow, error) {
rows, err := results.Query()
if err != nil {
return nil, fmt.Errorf("query FindAuthorNamesBatch: %w", err)
}
defer rows.Close()
items := []FindAuthorNamesRow{}
for rows.Next() {
var item FindAuthorNamesRow
if err := rows.Scan(&item.FirstName, &item.LastName); err != nil {
return nil, fmt.Errorf("scan FindAuthorNamesBatch row: %w", err)
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close FindAuthorNamesBatch rows: %w", err)
}
return items, err
}
const findFirstNamesSQL = `SELECT first_name FROM author ORDER BY author_id = $1;`
// FindFirstNames implements Querier.FindFirstNames.
func (q *DBQuerier) FindFirstNames(ctx context.Context, authorID int32) ([]*string, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "FindFirstNames")
rows, err := q.conn.Query(ctx, findFirstNamesSQL, authorID)
if err != nil {
return nil, fmt.Errorf("query FindFirstNames: %w", err)
}
defer rows.Close()
items := []*string{}
for rows.Next() {
var item string
if err := rows.Scan(&item); err != nil {
return nil, fmt.Errorf("scan FindFirstNames row: %w", err)
}
items = append(items, &item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close FindFirstNames rows: %w", err)
}
return items, err
}
// FindFirstNamesBatch implements Querier.FindFirstNamesBatch.
func (q *DBQuerier) FindFirstNamesBatch(batch genericBatch, authorID int32) {
batch.Queue(findFirstNamesSQL, authorID)
}
// FindFirstNamesScan implements Querier.FindFirstNamesScan.
func (q *DBQuerier) FindFirstNamesScan(results pgx.BatchResults) ([]*string, error) {
rows, err := results.Query()
if err != nil {
return nil, fmt.Errorf("query FindFirstNamesBatch: %w", err)
}
defer rows.Close()
items := []*string{}
for rows.Next() {
var item string
if err := rows.Scan(&item); err != nil {
return nil, fmt.Errorf("scan FindFirstNamesBatch row: %w", err)
}
items = append(items, &item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close FindFirstNamesBatch rows: %w", err)
}
return items, err
}
const deleteAuthorsSQL = `DELETE FROM author WHERE first_name = 'joe';`
// DeleteAuthors implements Querier.DeleteAuthors.
func (q *DBQuerier) DeleteAuthors(ctx context.Context) (pgconn.CommandTag, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "DeleteAuthors")
cmdTag, err := q.conn.Exec(ctx, deleteAuthorsSQL)
if err != nil {
return cmdTag, fmt.Errorf("exec query DeleteAuthors: %w", err)
}
return cmdTag, err
}
// DeleteAuthorsBatch implements Querier.DeleteAuthorsBatch.
func (q *DBQuerier) DeleteAuthorsBatch(batch genericBatch) {
batch.Queue(deleteAuthorsSQL)
}
// DeleteAuthorsScan implements Querier.DeleteAuthorsScan.
func (q *DBQuerier) DeleteAuthorsScan(results pgx.BatchResults) (pgconn.CommandTag, error) {
cmdTag, err := results.Exec()
if err != nil {
return cmdTag, fmt.Errorf("exec DeleteAuthorsBatch: %w", err)
}
return cmdTag, err
}
const deleteAuthorsByFirstNameSQL = `DELETE FROM author WHERE first_name = $1;`
// DeleteAuthorsByFirstName implements Querier.DeleteAuthorsByFirstName.
func (q *DBQuerier) DeleteAuthorsByFirstName(ctx context.Context, firstName string) (pgconn.CommandTag, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "DeleteAuthorsByFirstName")
cmdTag, err := q.conn.Exec(ctx, deleteAuthorsByFirstNameSQL, firstName)
if err != nil {
return cmdTag, fmt.Errorf("exec query DeleteAuthorsByFirstName: %w", err)
}
return cmdTag, err
}
// DeleteAuthorsByFirstNameBatch implements Querier.DeleteAuthorsByFirstNameBatch.
func (q *DBQuerier) DeleteAuthorsByFirstNameBatch(batch genericBatch, firstName string) {
batch.Queue(deleteAuthorsByFirstNameSQL, firstName)
}
// DeleteAuthorsByFirstNameScan implements Querier.DeleteAuthorsByFirstNameScan.
func (q *DBQuerier) DeleteAuthorsByFirstNameScan(results pgx.BatchResults) (pgconn.CommandTag, error) {
cmdTag, err := results.Exec()
if err != nil {
return cmdTag, fmt.Errorf("exec DeleteAuthorsByFirstNameBatch: %w", err)
}
return cmdTag, err
}
const deleteAuthorsByFullNameSQL = `DELETE
FROM author
WHERE first_name = $1
AND last_name = $2
AND suffix = $3;`
type DeleteAuthorsByFullNameParams struct {
FirstName string
LastName string
Suffix string
}
// DeleteAuthorsByFullName implements Querier.DeleteAuthorsByFullName.
func (q *DBQuerier) DeleteAuthorsByFullName(ctx context.Context, params DeleteAuthorsByFullNameParams) (pgconn.CommandTag, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "DeleteAuthorsByFullName")
cmdTag, err := q.conn.Exec(ctx, deleteAuthorsByFullNameSQL, params.FirstName, params.LastName, params.Suffix)
if err != nil {
return cmdTag, fmt.Errorf("exec query DeleteAuthorsByFullName: %w", err)
}
return cmdTag, err
}
// DeleteAuthorsByFullNameBatch implements Querier.DeleteAuthorsByFullNameBatch.
func (q *DBQuerier) DeleteAuthorsByFullNameBatch(batch genericBatch, params DeleteAuthorsByFullNameParams) {
batch.Queue(deleteAuthorsByFullNameSQL, params.FirstName, params.LastName, params.Suffix)
}
// DeleteAuthorsByFullNameScan implements Querier.DeleteAuthorsByFullNameScan.
func (q *DBQuerier) DeleteAuthorsByFullNameScan(results pgx.BatchResults) (pgconn.CommandTag, error) {
cmdTag, err := results.Exec()
if err != nil {
return cmdTag, fmt.Errorf("exec DeleteAuthorsByFullNameBatch: %w", err)
}
return cmdTag, err
}
const insertAuthorSQL = `INSERT INTO author (first_name, last_name)
VALUES ($1, $2)
RETURNING author_id;`
// InsertAuthor implements Querier.InsertAuthor.
func (q *DBQuerier) InsertAuthor(ctx context.Context, firstName string, lastName string) (int32, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "InsertAuthor")
row := q.conn.QueryRow(ctx, insertAuthorSQL, firstName, lastName)
var item int32
if err := row.Scan(&item); err != nil {
return item, fmt.Errorf("query InsertAuthor: %w", err)
}
return item, nil
}
// InsertAuthorBatch implements Querier.InsertAuthorBatch.
func (q *DBQuerier) InsertAuthorBatch(batch genericBatch, firstName string, lastName string) {
batch.Queue(insertAuthorSQL, firstName, lastName)
}
// InsertAuthorScan implements Querier.InsertAuthorScan.
func (q *DBQuerier) InsertAuthorScan(results pgx.BatchResults) (int32, error) {
row := results.QueryRow()
var item int32
if err := row.Scan(&item); err != nil {
return item, fmt.Errorf("scan InsertAuthorBatch row: %w", err)
}
return item, nil
}
const insertAuthorSuffixSQL = `INSERT INTO author (first_name, last_name, suffix)
VALUES ($1, $2, $3)
RETURNING author_id, first_name, last_name, suffix;`
type InsertAuthorSuffixParams struct {
FirstName string
LastName string
Suffix string
}
type InsertAuthorSuffixRow struct {
AuthorID int32 `json:"author_id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Suffix *string `json:"suffix"`
}
// InsertAuthorSuffix implements Querier.InsertAuthorSuffix.
func (q *DBQuerier) InsertAuthorSuffix(ctx context.Context, params InsertAuthorSuffixParams) (InsertAuthorSuffixRow, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "InsertAuthorSuffix")
row := q.conn.QueryRow(ctx, insertAuthorSuffixSQL, params.FirstName, params.LastName, params.Suffix)
var item InsertAuthorSuffixRow
if err := row.Scan(&item.AuthorID, &item.FirstName, &item.LastName, &item.Suffix); err != nil {
return item, fmt.Errorf("query InsertAuthorSuffix: %w", err)
}
return item, nil
}
// InsertAuthorSuffixBatch implements Querier.InsertAuthorSuffixBatch.
func (q *DBQuerier) InsertAuthorSuffixBatch(batch genericBatch, params InsertAuthorSuffixParams) {
batch.Queue(insertAuthorSuffixSQL, params.FirstName, params.LastName, params.Suffix)
}
// InsertAuthorSuffixScan implements Querier.InsertAuthorSuffixScan.
func (q *DBQuerier) InsertAuthorSuffixScan(results pgx.BatchResults) (InsertAuthorSuffixRow, error) {
row := results.QueryRow()
var item InsertAuthorSuffixRow
if err := row.Scan(&item.AuthorID, &item.FirstName, &item.LastName, &item.Suffix); err != nil {
return item, fmt.Errorf("scan InsertAuthorSuffixBatch row: %w", err)
}
return item, nil
}
const stringAggFirstNameSQL = `SELECT string_agg(first_name, ',') AS names FROM author WHERE author_id = $1;`
// StringAggFirstName implements Querier.StringAggFirstName.
func (q *DBQuerier) StringAggFirstName(ctx context.Context, authorID int32) (*string, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "StringAggFirstName")
row := q.conn.QueryRow(ctx, stringAggFirstNameSQL, authorID)
var item *string
if err := row.Scan(&item); err != nil {
return item, fmt.Errorf("query StringAggFirstName: %w", err)
}
return item, nil
}
// StringAggFirstNameBatch implements Querier.StringAggFirstNameBatch.
func (q *DBQuerier) StringAggFirstNameBatch(batch genericBatch, authorID int32) {
batch.Queue(stringAggFirstNameSQL, authorID)
}
// StringAggFirstNameScan implements Querier.StringAggFirstNameScan.
func (q *DBQuerier) StringAggFirstNameScan(results pgx.BatchResults) (*string, error) {
row := results.QueryRow()
var item *string
if err := row.Scan(&item); err != nil {
return item, fmt.Errorf("scan StringAggFirstNameBatch row: %w", err)
}
return item, nil
}
const arrayAggFirstNameSQL = `SELECT array_agg(first_name) AS names FROM author WHERE author_id = $1;`
// ArrayAggFirstName implements Querier.ArrayAggFirstName.
func (q *DBQuerier) ArrayAggFirstName(ctx context.Context, authorID int32) ([]string, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "ArrayAggFirstName")
row := q.conn.QueryRow(ctx, arrayAggFirstNameSQL, authorID)
item := []string{}
if err := row.Scan(&item); err != nil {
return item, fmt.Errorf("query ArrayAggFirstName: %w", err)
}
return item, nil
}
// ArrayAggFirstNameBatch implements Querier.ArrayAggFirstNameBatch.
func (q *DBQuerier) ArrayAggFirstNameBatch(batch genericBatch, authorID int32) {
batch.Queue(arrayAggFirstNameSQL, authorID)
}
// ArrayAggFirstNameScan implements Querier.ArrayAggFirstNameScan.
func (q *DBQuerier) ArrayAggFirstNameScan(results pgx.BatchResults) ([]string, error) {
row := results.QueryRow()
item := []string{}
if err := row.Scan(&item); err != nil {
return item, fmt.Errorf("scan ArrayAggFirstNameBatch row: %w", err)
}
return item, nil
}
// textPreferrer wraps a pgtype.ValueTranscoder and sets the preferred encoding
// format to text instead binary (the default). pggen uses the text format
// when the OID is unknownOID because the binary format requires the OID.
// Typically occurs if the results from QueryAllDataTypes aren't passed to
// NewQuerierConfig.
type textPreferrer struct {
pgtype.ValueTranscoder
typeName string
}
// PreferredParamFormat implements pgtype.ParamFormatPreferrer.
func (t textPreferrer) PreferredParamFormat() int16 { return pgtype.TextFormatCode }
func (t textPreferrer) NewTypeValue() pgtype.Value {
return textPreferrer{ValueTranscoder: pgtype.NewValue(t.ValueTranscoder).(pgtype.ValueTranscoder), typeName: t.typeName}
}
func (t textPreferrer) TypeName() string {
return t.typeName
}
// unknownOID means we don't know the OID for a type. This is okay for decoding
// because pgx call DecodeText or DecodeBinary without requiring the OID. For
// encoding parameters, pggen uses textPreferrer if the OID is unknown.
const unknownOID = 0