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
Does pgx support scanning into structs? #760
Comments
|
pgx does not have anything like sqlx. But you can use sqlx with pgx when pgx is used as a database/sql driver. |
|
Also the sqlc in next version will do what you need albeit in a bit different way that you expect. |
While this works, it is a somewhat annoying to have to drop down from sqlx.DB to pgx.Conn by AcquireConn() in order to use the very much faster bulk-copy (CopyFrom()). In libpq, you could feed a sqlx Prepare statement with the output of pq.CopyIn() in order do do bulk-inserts. |
|
@mrdulin here is the library exactly for what you asked: https://github.com/georgysavva/scany Disclaimer: I am the author of this library. |
|
Well, well..this should really be part of the core, since its fundamentals. Yet another dependecy to a private person. |
|
maybe there ... https://github.com/randallmlough/pgxscan |
|
See also |
|
In addition to the API @bfontaine pointed out, pgx.RowToStructByName is available in pgx v5. |
|
@elllot How do I apply |
|
It's been a while since I was looking into this, but you could use a combination of You can check the API definitions to see what the differences are between the API variants. I might be misremembering, but I remember there wasn't a way to use |
|
@elllot yup that's how I'm using it now, tried looking for a way to use a single row |
|
As mentioned by @elllot // Supplied from somewhere.
var dbPool *pgxpool.Pool
rows, _ := dbPool.Query(context.Background(), someQuery, ...someQueryArgs)
// Assumes the returned row only has a single hit. StructToFill is the target struct.
p, err := pgx.CollectOneRow(rows, pgx.RowToAddrOfStructByName[StructToFill])
if err != nil {
return nil, err
}Also, for an even higher level interface, check out SelectRow from my personal pgx utility library. |
|
in that pgx utility lib, I miss something like this: It is useful in when inserting "structs": |
something like: https://godoc.org/github.com/jmoiron/sqlx#StructScan
And struct tags like this:
Or this:
I didn't find any information on https://github.com/jackc/pgx/blob/master/query_test.go file and documentation.
There are only examples which scan into the basic data type like
int,floatPlease give an example of how to scan rows into a struct. Thanks
The text was updated successfully, but these errors were encountered: