Skip to content

Commit 535ff43

Browse files
committed
feat: export PoolBatchResults, ErrBatchResults, ErrRows and ErrRow
1 parent fbd2416 commit 535ff43

5 files changed

Lines changed: 60 additions & 44 deletions

File tree

batch.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,27 @@ func invalidateCachesOnBatchResultsError(conn *Conn, b *Batch, err error) {
505505
}
506506
}
507507
}
508+
509+
type errBatchResults struct {
510+
err error
511+
}
512+
513+
func ErrBatchResults(err error) BatchResults {
514+
return errBatchResults{err: err}
515+
}
516+
517+
func (br errBatchResults) Exec() (pgconn.CommandTag, error) {
518+
return pgconn.CommandTag{}, br.err
519+
}
520+
521+
func (br errBatchResults) Query() (Rows, error) {
522+
return errRows{err: br.err}, br.err
523+
}
524+
525+
func (br errBatchResults) QueryRow() Row {
526+
return errRow{err: br.err}
527+
}
528+
529+
func (br errBatchResults) Close() error {
530+
return br.err
531+
}

pgxpool/batch_results.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,15 @@ import (
55
"github.com/jackc/pgx/v5/pgconn"
66
)
77

8-
type errBatchResults struct {
9-
err error
10-
}
11-
12-
func (br errBatchResults) Exec() (pgconn.CommandTag, error) {
13-
return pgconn.CommandTag{}, br.err
14-
}
15-
16-
func (br errBatchResults) Query() (pgx.Rows, error) {
17-
return errRows{err: br.err}, br.err
18-
}
19-
20-
func (br errBatchResults) QueryRow() pgx.Row {
21-
return errRow{err: br.err}
22-
}
23-
24-
func (br errBatchResults) Close() error {
25-
return br.err
26-
}
27-
288
type poolBatchResults struct {
299
br pgx.BatchResults
3010
c *Conn
3111
}
3212

13+
func PoolBatchResults(br pgx.BatchResults, c *Conn) pgx.BatchResults {
14+
return &poolBatchResults{br: br, c: c}
15+
}
16+
3317
func (br *poolBatchResults) Exec() (pgconn.CommandTag, error) {
3418
return br.br.Exec()
3519
}

pgxpool/pool.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -737,13 +737,13 @@ func (p *Pool) Exec(ctx context.Context, sql string, arguments ...any) (pgconn.C
737737
func (p *Pool) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) {
738738
c, err := p.Acquire(ctx)
739739
if err != nil {
740-
return errRows{err: err}, err
740+
return pgx.ErrRows(err), err
741741
}
742742

743743
rows, err := c.Query(ctx, sql, args...)
744744
if err != nil {
745745
c.Release()
746-
return errRows{err: err}, err
746+
return pgx.ErrRows(err), err
747747
}
748748

749749
return c.getPoolRows(rows), nil
@@ -764,7 +764,7 @@ func (p *Pool) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, er
764764
func (p *Pool) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row {
765765
c, err := p.Acquire(ctx)
766766
if err != nil {
767-
return errRow{err: err}
767+
return pgx.ErrRow(err)
768768
}
769769

770770
row := c.QueryRow(ctx, sql, args...)
@@ -774,7 +774,7 @@ func (p *Pool) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row {
774774
func (p *Pool) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults {
775775
c, err := p.Acquire(ctx)
776776
if err != nil {
777-
return errBatchResults{err: err}
777+
return pgx.ErrBatchResults(err)
778778
}
779779

780780
br := c.SendBatch(ctx, b)

pgxpool/rows.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,6 @@ import (
55
"github.com/jackc/pgx/v5/pgconn"
66
)
77

8-
type errRows struct {
9-
err error
10-
}
11-
12-
func (errRows) Close() {}
13-
func (e errRows) Err() error { return e.err }
14-
func (errRows) CommandTag() pgconn.CommandTag { return pgconn.CommandTag{} }
15-
func (errRows) FieldDescriptions() []pgconn.FieldDescription { return nil }
16-
func (errRows) Next() bool { return false }
17-
func (e errRows) Scan(dest ...any) error { return e.err }
18-
func (e errRows) Values() ([]any, error) { return nil, e.err }
19-
func (e errRows) RawValues() [][]byte { return nil }
20-
func (e errRows) Conn() *pgx.Conn { return nil }
21-
22-
type errRow struct {
23-
err error
24-
}
25-
26-
func (e errRow) Scan(dest ...any) error { return e.err }
27-
288
type poolRows struct {
299
r pgx.Rows
3010
c *Conn

rows.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ type Rows interface {
7070
Conn() *Conn
7171
}
7272

73+
type errRows struct {
74+
err error
75+
}
76+
77+
func ErrRows(err error) Rows {
78+
return errRows{err: err}
79+
}
80+
81+
func (errRows) Close() {}
82+
func (e errRows) Err() error { return e.err }
83+
func (errRows) CommandTag() pgconn.CommandTag { return pgconn.CommandTag{} }
84+
func (errRows) FieldDescriptions() []pgconn.FieldDescription { return nil }
85+
func (errRows) Next() bool { return false }
86+
func (e errRows) Scan(dest ...any) error { return e.err }
87+
func (e errRows) Values() ([]any, error) { return nil, e.err }
88+
func (e errRows) RawValues() [][]byte { return nil }
89+
func (e errRows) Conn() *Conn { return nil }
90+
7391
// Row is a convenience wrapper over Rows that is returned by QueryRow.
7492
//
7593
// Row is an interface instead of a struct to allow tests to mock QueryRow. However,
@@ -83,6 +101,16 @@ type Row interface {
83101
Scan(dest ...any) error
84102
}
85103

104+
type errRow struct {
105+
err error
106+
}
107+
108+
func ErrRow(err error) Row {
109+
return errRow{err: err}
110+
}
111+
112+
func (e errRow) Scan(dest ...any) error { return e.err }
113+
86114
// RowScanner scans an entire row at a time into the RowScanner.
87115
type RowScanner interface {
88116
// ScanRows scans the row.

0 commit comments

Comments
 (0)