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

Scan into struct for db.QueryRow #5

Closed
matrixik opened this issue Jan 21, 2015 · 12 comments
Closed

Scan into struct for db.QueryRow #5

matrixik opened this issue Jan 21, 2015 · 12 comments

Comments

@matrixik
Copy link
Contributor

Hello,
Thank you for really useful package.

Maybe you can consider adding function to scan only one row into struct (for db.QueryRow).

Cheers

@kisielk
Copy link
Owner

kisielk commented Jan 21, 2015

I'm glad you find the package useful. Unfortunately I don't think we can support sql.QueryRow because the sql.Row type doesn't have a Columns() method which we need to decide which struct members to scan the result in to. If you have any suggestions for a possible implementation I'd be glad to hear them.

@matrixik
Copy link
Contributor Author

Hmm, https://github.com/jmoiron/sqlx have something like that:

// You can also get a single result, a la QueryRow
jason = Person{}
err = db.Get(&jason, "SELECT * FROM person WHERE first_name=$1", "Jason")
fmt.Printf("%#v\n", jason)
// Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}

But I don't have time now to investigate how it works and personally I think sqlx is a little overkill.

@kisielk
Copy link
Owner

kisielk commented Jan 21, 2015

db.Get in sqlx actually uses sql.Query under the hood, not sql.QueryRow, so it has access to Columns() to do the scanning. But yeah, sqlx is a more full-featured complete wrapper around database/sql whereas this package is just a few convenience functions that work with the existing API.

@matrixik
Copy link
Contributor Author

Maybe something like this:
https://github.com/matrixik/sqlstruct/commit/f5a8e2e385f6304fdf194eacf0906ecccc9499ec

But I don't know if I can copy some of Go database/sql source here:
http://golang.org/src/database/sql/sql.go?s=44347:44803#L1703

@kisielk
Copy link
Owner

kisielk commented Jan 22, 2015

Yeah that works, but that's wrapping query which isn't what I want to do in this library. It doesn't really work analogous to how the Scan function works.

@matrixik
Copy link
Contributor Author

So, any suggestions how to change it or you don't want something like that in you package?

@matrixik
Copy link
Contributor Author

that's wrapping query which isn't what I want to do in this library

That's exactly what QueryRow is doing:
http://golang.org/src/database/sql/sql.go#L1001

@kisielk
Copy link
Owner

kisielk commented Jan 22, 2015

I don't want to wrap any sort of DB object and run queries within this package, that's not the intent of the design. At that point sqlx or similar would be better choice. The only way I could see it working is if we could get columns from a sql.Row object, so if the Go developers could be convinced to add a Columns() method to it like sql.Rows has. The chances of that happening are pretty slim I think.

@kisielk
Copy link
Owner

kisielk commented Jan 22, 2015

To put it another way, the package is built to function around anything that implements sqlstruct.Rows, which sql.Row does not. If it did, then it would work :)

@matrixik
Copy link
Contributor Author

Would that be sufficient: golang/go#5606 ?
Or should I create new issue?

@kisielk
Copy link
Owner

kisielk commented Jan 22, 2015

That wouldn't help here, that issue is for an API for database driver authors. You would need to file a new issue for adding the Columns() method to sql.Row. To be honest I think it's probably not worth the effort, you could just use a simple utility function in your code base, basically what you had here: #5 (comment)

@matrixik
Copy link
Contributor Author

Thank you for your time.

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