IssueRaiser

proposal: database/sql: add methods to scan an entire row into one value

Edited: struct field names are matched to columns case-sensitively. Edited: untouched storage is ignored rather than zeroed. --------------- I propose adding the method ``` ScanRow(dest any) error ``` to the `Row` and `Rows` types. `ScanRow` attempts to populate `dest` with all the columns of the current row. `dest` can be a pointer to an array, slice, map or struct. ## Motivation `ScanRow` makes it more convenient to populate a row from a struct. Evidence that this is a desirable feature comes from the [github.com/jmoiron/sqlx](https://pkg.go.dev/github.com/jmoiron/sqlx) package, which has 9,800 importers, about 1,000 forks and about 14,000 GitHub stars. (To be fair, `sqlx` provides several other enhancements to `database/sql` as well.) The `ScanRow` method brings `database/sql` closer to parity with `encoding/json`, whose ability to unmarshal into structs and other data types is widely used. Another motivation comes from the likely addition of [iterators](https://go.dev/issue/61405) to the language. Without something like `ScanRow`, an iterator over a DB query would have to return a `Row` or `Rows`, since the `Scan` method is variadic. An iterator like that still improves on using a `Rows` directly because it makes error-handling more explicit and always calls `Close`. But we could do better with `ScanRow`, because the iterator could deliver a single value holding the entire row: ``` type Product struct { Name string Quantity int } func processProducts() {

Posted by jba on Jul 28, 2023, 2:10 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.2,500 issues0 comments

github (Software)

proposal: database/sql: add methods to scan an entire row into one value

jbaJul 28, 2023, 2:10 PM

Edited: struct field names are matched to columns case-sensitively. Edited: untouched storage is ignored rather than zeroed. --------------- I propose adding the method ``` ScanRow(dest any) error ``` to the `Row` and `Rows` types. `ScanRow` attempts to populate `dest` with all the columns of the current row. `dest` can be a pointer to an array, slice, map or struct. ## Motivation `ScanRow` makes it more convenient to populate a row from a struct. Evidence that this is a desirable feature comes from the [github.com/jmoiron/sqlx](https://pkg.go.dev/github.com/jmoiron/sqlx) package, which has 9,800 importers, about 1,000 forks and about 14,000 GitHub stars. (To be fair, `sqlx` provides several other enhancements to `database/sql` as well.) The `ScanRow` method brings `database/sql` closer to parity with `encoding/json`, whose ability to unmarshal into structs and other data types is widely used. Another motivation comes from the likely addition of [iterators](https://go.dev/issue/61405) to the language. Without something like `ScanRow`, an iterator over a DB query would have to return a `Row` or `Rows`, since the `Scan` method is variadic. An iterator like that still improves on using a `Rows` directly because it makes error-handling more explicit and always calls `Close`. But we could do better with `ScanRow`, because the iterator could deliver a single value holding the entire row: ``` type Product struct { Name string Quantity int } func processProducts() {

Comments (0)

No comments yet. Start the discussion.

Comment on this issue