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

Make it possible to pass *interface{} to Rows.Scan() or what is a workaround? #769

Closed
georgysavva opened this issue Jun 3, 2020 · 2 comments

Comments

@georgysavva
Copy link
Contributor

Hi. I am writing some kind of a wrapper on top of Rows.Scan().
In some cases I need to scan into destination with type *interface{}. For example to fill map[string]interface{} or to skip current column value and don't scan it at all.
The call can look like that:

     var s string
     var i interface{}
     err := rows.Scan(&s, &i)

Of course I am simplifying it, and in the real case I would use the reflection.
Nonetheless, currently pgx.Rows can't scan into destination with type *interface{}. I checked how sql.Rows behaves in this situation (with pgx as a driver), and it scan into *interface{} perfectly. I think it's worth implementing it in pgx.Rows too, for consistency.

If I am wrong and this was your intentional restriction, how can I implement what I need?
I can get all row values via Rows.Values() and for *inteface{} destinations assign corresponding row value from the list, but I still need to call pgx.Scan() for non *interface{} destinations and I need some kind of placeholder in place of *interface{} destinations.
Looking at the example above after I assign i from Rows.Values() I need to put something instead of &i to rows.Scan() call in order it to succeed. What would be the most efficient and elegant way to do that?

@jackc
Copy link
Owner

jackc commented Jun 6, 2020

It's not an intentional restriction, it's just something that has never come up before.

As pgx is now you could use nil for the placeholder in Scan. That no-ops the field. Then you could use Values to get those values.

Adding support to Scan directly would probably involve a new pgtype.ScanPlan for empty interfaces that would call Get on the underlying pgtype.Value.

@georgysavva
Copy link
Contributor Author

Thanks for the hint with nil :)
BTW I will try to add support for that feature in pgx the way you recommended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants