Add RowScanner interface

non-blocking
Jack Christensen 2022-04-30 12:49:12 -05:00
parent 01190e5d78
commit c1495aace0
3 changed files with 47 additions and 0 deletions

View File

@ -120,6 +120,10 @@ See documentation for `QueryExecMode`.
pgx now supports named arguments with the NamedArgs type. This is implemented via the new QueryRewriter interface which
allows arbitrary rewriting of query SQL and arguments.
## RowScanner Interface
The `RowScanner` interface allows a single argument to Rows.Scan to scan the entire row.
## 3rd Party Logger Integration
All integrations with 3rd party loggers have been extracted to separate repositories. This trims the pgx dependency

13
rows.go
View File

@ -69,6 +69,12 @@ type Row interface {
Scan(dest ...any) error
}
// RowScanner scans an entire row at a time into the RowScanner.
type RowScanner interface {
// ScanRows scans the row.
ScanRow(rows Rows) error
}
// connRow implements the Row interface for Conn.QueryRow.
type connRow connRows
@ -212,6 +218,13 @@ func (rows *connRows) Scan(dest ...any) error {
rows.fatal(err)
return err
}
if len(dest) == 1 {
if rc, ok := dest[0].(RowScanner); ok {
return rc.ScanRow(rows)
}
}
if len(fieldDescriptions) != len(dest) {
err := fmt.Errorf("number of field descriptions must equal number of destinations, got %d and %d", len(fieldDescriptions), len(dest))
rows.fatal(err)

30
rows_test.go Normal file
View File

@ -0,0 +1,30 @@
package pgx_test
import (
"context"
"testing"
"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/require"
)
type testRowScanner struct {
name string
age int32
}
func (rs *testRowScanner) ScanRow(rows pgx.Rows) error {
return rows.Scan(&rs.name, &rs.age)
}
func TestRowScanner(t *testing.T) {
t.Parallel()
defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
var s testRowScanner
err := conn.QueryRow(ctx, "select 'Adam' as name, 72 as height").Scan(&s)
require.NoError(t, err)
require.Equal(t, "Adam", s.name)
require.Equal(t, int32(72), s.age)
})
}