Skip to content
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

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

Open
wants to merge 2 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
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(index int, dest any) 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 @@ -3389,7 +3389,16 @@ func (rs *Rows) Scan(dest ...any) error {
}

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

if rowsColumnScanner, ok := rs.rowsi.(driver.RowsColumnScanner); ok {
err = rowsColumnScanner.ScanColumn(i, dest[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