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
- [Meta] Feedback on granular control over Copilot changes
- unexpected status 401 Unauthorized issue
- [MODEL] Measurable quality regression in Claude generation 5 (Fable 5 / Opus 5 / Sonnet 5): worse nonsense detection, ~2x verbosity, under-disclosed model fallb
- [CRITICAL] Subagent spawning and subagent pattern bugs trigger infinite recursion, infinite token usage, grossly inefficient token usage, and lost accumulated s
- Video stops playing after some minutes
- [MODEL] Claude verbose code comments by default — ignores instructions to stop.
Raise real-world problems, find people facing the same issue, and discuss solutions.2,500 issues0 comments
github (Software)
[Meta] Feedback on granular control over Copilot changes
unexpected status 401 Unauthorized issue
[MODEL] Measurable quality regression in Claude generation 5 (Fable 5 / Opus 5 / Sonnet 5): worse nonsense detection, ~2x verbosity, under-disclosed model fallb
[CRITICAL] Subagent spawning and subagent pattern bugs trigger infinite recursion, infinite token usage, grossly inefficient token usage, and lost accumulated s
Video stops playing after some minutes
[MODEL] Claude verbose code comments by default — ignores instructions to stop.
Scope Codex VS Code chats to the current workspace/project
Update on "Co-authored-by: Copilot" in commit messages
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() {
Comments (0)
No comments yet. Start the discussion.