georgysavva / scany

Library for scanning data from a database into Go structs and more
MIT License
1.24k stars 67 forks source link

Scanning into a slice #49

Closed ccp-norbert closed 3 years ago

ccp-norbert commented 3 years ago

Hi,

The scany documentation states "Apart from structs, support for other destination types: maps, slices, etc." but I have not been able to find anything related to this. I have a SQL procedure which returns an array of UUIDs, and I'm having trouble figuring out how to use scany to scan that result into a slice (or an array, doesn't really matter).

Would you have any pointers to put me on the right path? Note: I use pgx as my postgres driver.

Thanks in advance.

georgysavva commented 3 years ago

Hello. And Thank you for your issue! If you have an SQL query that selects multiple rows with a single UUID column your can scan it like that:

var uuids []string
err := pgxscan.Select(ctx, db, &uuids, `SELECT uuid_column FROM my_table`) 

More details in this docs section: https://pkg.go.dev/github.com/georgysavva/scany@v0.2.8/dbscan#hdr-Scanning_into_other_types

Note that, for now it only works with Go primitive types like string you can't use pgtype.UUID or google.UUID instead, more about that here https://github.com/georgysavva/scany/issues/33#issuecomment-813221294

I hope it helps!

ccp-norbert commented 3 years ago

It does, thanks!