-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Direct conversion from row-data into concrete struct via conversion functions #1089
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
Comments
Optimally, things like optionals could be handled magically, too: |
Unfortunately this is completely impossible until Rust has stronger constant execution facilities. Notably we need const functions in traits. See #514 to track the status of this. |
We did make a recent large improvement to Given: query!("SELECT * from whatever where stuff")
.fetch_one(&mut conn)
.await
.map(|x| MyData {
foo: Foo(x.foo as i32),
bar: Bar(x.bar),
thing: match x.thing {
"x": Thing::X,
"y": Thing::Y,
_ => panic!("This is just for demo, please don't actually panic :/")
})
.other_stuff() You can now do: query!("SELECT * from whatever where stuff")
.map(|x| MyData {
foo: Foo(x.foo as i32),
bar: Bar(x.bar),
thing: match x.thing {
"x": Thing::X,
"y": Thing::Y,
_ => panic!("This is just for demo, please don't actually panic :/")
})
.fetch_one(&mut conn)
// other fetch_ variants can be used with the above map as well
// .fetch_all(&mut conn)
.await
.other_stuff() |
I want to note that I don't intend to dismiss your specific ideas for If you want to push some of those ideas (in a separate issue) into extending |
Ahh, well, sad i guess,.... I may do a separate issue to extend FromRow for now, yea! Thanks anyways. |
Currently, query_as! gives us a way to map the queried fields into a very basic representation of the row-data. In most cases, this is not exactly what we need, thus we either manually
.map
the anonymous record into our business-datatype, or we change our data representation - or create two structs with a.into
.Optimally, my proposal would be to allow for
#[sqlx::convert = some_func]
attributes on each fields of a struct, such thatquery_as!
(or something equivalent) could then convert the values into our business-data directly.Example:
Currently, we'd have to do something like this:
Given the same types, my proposal would be to have:
Implementation wise i'd say that this could be done by generating a trait implementation that either pretty much does
FromRow
,or by basing on
query_as
- first generating a named struct with fields of the basic types, then generating a conversion from that raw data struct into the business object.The text was updated successfully, but these errors were encountered: