Skip to content

database/sql: allow drivers to override Scan behavior #67648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/next/67546.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pkg database/sql/driver, type RowsColumnScanner interface { Close, Columns, Next, ScanColumn } #67546
pkg database/sql/driver, type RowsColumnScanner interface, Close() error #67546
pkg database/sql/driver, type RowsColumnScanner interface, Columns() []string #67546
pkg database/sql/driver, type RowsColumnScanner interface, Next([]Value) error #67546
pkg database/sql/driver, type RowsColumnScanner interface, ScanColumn(interface{}, int) error #67546
1 change: 1 addition & 0 deletions doc/next/6-stdlib/99-minor/database/sql/driver/67546.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A database driver may implement [RowsColumnScanner] to entirely override `Scan` behavior.
12 changes: 12 additions & 0 deletions src/database/sql/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,18 @@ type RowsColumnTypePrecisionScale interface {
ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool)
}

// RowsColumnScanner may be implemented by [Rows]. It allows the driver to completely
// take responsibility for how values are scanned and replace the normal [database/sql].
// scanning path. This allows drivers to directly support types that do not implement
// [database/sql.Scanner].
type RowsColumnScanner interface {
Rows

// ScanColumn copies the column in the current row into the value pointed at by
// dest. It returns [ErrSkip] to fall back to the normal [database/sql] scanning path.
ScanColumn(dest any, index int) error
}

// Tx is a transaction.
type Tx interface {
Commit() error
Expand Down
11 changes: 10 additions & 1 deletion src/database/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3397,7 +3397,16 @@ func (rs *Rows) Scan(dest ...any) error {
}

for i, sv := range rs.lastcols {
err := convertAssignRows(dest[i], sv, rs)
err := driver.ErrSkip

if rcs, ok := rs.rowsi.(driver.RowsColumnScanner); ok {
err = rcs.ScanColumn(dest[i], i)
}

if err == driver.ErrSkip {
err = convertAssignRows(dest[i], sv, rs)
}

if err != nil {
rs.closemuRUnlockIfHeldByScan()
return fmt.Errorf(`sql: Scan error on column index %d, name %q: %w`, i, rs.rowsi.Columns()[i], err)
Expand Down
96 changes: 96 additions & 0 deletions src/database/sql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4200,6 +4200,102 @@ func TestNamedValueCheckerSkip(t *testing.T) {
}
}

type rcsDriver struct {
fakeDriver
}

func (d *rcsDriver) Open(dsn string) (driver.Conn, error) {
c, err := d.fakeDriver.Open(dsn)
fc := c.(*fakeConn)
fc.db.allowAny = true
return &rcsConn{fc}, err
}

type rcsConn struct {
*fakeConn
}

func (c *rcsConn) PrepareContext(ctx context.Context, q string) (driver.Stmt, error) {
stmt, err := c.fakeConn.PrepareContext(ctx, q)
if err != nil {
return stmt, err
}
return &rcsStmt{stmt.(*fakeStmt)}, nil
}

type rcsStmt struct {
*fakeStmt
}

func (s *rcsStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
rows, err := s.fakeStmt.QueryContext(ctx, args)
if err != nil {
return rows, err
}
return &rcsRows{rows.(*rowsCursor)}, nil
}

type rcsRows struct {
*rowsCursor
}

func (r *rcsRows) ScanColumn(dest any, index int) error {
switch d := dest.(type) {
case *int64:
*d = 42
return nil
}

return driver.ErrSkip
}

func TestRowsColumnScanner(t *testing.T) {
Register("RowsColumnScanner", &rcsDriver{})
db, err := Open("RowsColumnScanner", "")
if err != nil {
t.Fatal(err)
}
defer db.Close()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

_, err = db.ExecContext(ctx, "CREATE|t|str=string,n=int64")
if err != nil {
t.Fatal("exec create", err)
}

_, err = db.ExecContext(ctx, "INSERT|t|str=?,n=?", "foo", int64(1))
if err != nil {
t.Fatal("exec insert", err)
}
var (
str string
i64 int64
i int
f64 float64
ui uint
)
err = db.QueryRowContext(ctx, "SELECT|t|str,n,n,n,n|").Scan(&str, &i64, &i, &f64, &ui)
if err != nil {
t.Fatal("select", err)
}

list := []struct{ got, want any }{
{str, "foo"},
{i64, int64(42)},
{i, int(1)},
{f64, float64(1)},
{ui, uint(1)},
}

for index, item := range list {
if !reflect.DeepEqual(item.got, item.want) {
t.Errorf("got %#v wanted %#v for index %d", item.got, item.want, index)
}
}
}

func TestOpenConnector(t *testing.T) {
Register("testctx", &fakeDriverCtx{})
db, err := Open("testctx", "people")
Expand Down