You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, i've been spending quite time in trying to make basic examples to work, quite unsuccessfully (I'm a seasoned programmer but rust n00b).
I was able to make query_as! macro to work in a 1:1 relation.
Unfortunately because of #1031 it won't scale as it is necessary to have most of my structs to implement Type in order to be useful.
implementation of sqlx::Decode is not general enough
That requires me to go to query! macro approach instead.
For example
let row = query!(
r#"
SELECT
home.id AS "home_id",
address.id AS "address_id",
FROM home
JOIN address ON home.address_id = address.id
WHERE home.id = $1
"#,
home_id
)
.fetch_optional(pool)
.await?;
let result = row.map(|e| Home {
id: e.home_id,
address: Some(Address {
id: e.address_id,
}),
});
So far so good, however if i have more than 1 query to avoid lots of code duplication i want to:
Reuse the row.map between queries. Since it is an anonymous record, there is no type i can use and pass to functions. So the only way i know is to use my own macros to reuse code mapping.
Reuse query parts between queries. For example SELECT address.* code should come from another select in the address query mod. Because of "expected string literal" from the macro, i cannot format the macro and pass select fragments. (Prob there is a way to do this as it is all static code, but i cannot find how).
Conditional queries. To avoid to create 10 different query functions because of orderby permutations i will need to dynamically generate queries as it has been done here for query_as sqlx-conditional-queries. This actually would solve point 2 as well but afaik this is not for the query macro only
Workaround
use the query() function (as i'm doing now) and forget about compile time verification (the big plus of this library, imo).
make sqlx-conditional-queries to work forquery!`
I think every project more than a trivial TODO application will encounter pretty early the issues i mentioned above.
I'm sure you guys know a more idiomatic approach to this?
Note: i considered using tokio-postgres instead, but found out sqlx to be 30% faster in a stress test.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, i've been spending quite time in trying to make basic examples to work, quite unsuccessfully (I'm a seasoned programmer but rust n00b).
I was able to make query_as! macro to work in a 1:1 relation.
Unfortunately because of #1031 it won't scale as it is necessary to have most of my structs to implement Type in order to be useful.
For example
gives
implementation of sqlx::Decode is not general enough
That requires me to go to query! macro approach instead.
For example
So far so good, however if i have more than 1 query to avoid lots of code duplication i want to:
SELECT address.* code
should come from another select in the address query mod. Because of "expected string literal" from the macro, i cannot format the macro and pass select fragments. (Prob there is a way to do this as it is all static code, but i cannot find how).query
macro onlyWorkaround
query()
function (as i'm doing now) and forget about compile time verification (the big plus of this library, imo).to work for
query!`I think every project more than a trivial TODO application will encounter pretty early the issues i mentioned above.
I'm sure you guys know a more idiomatic approach to this?
Note: i considered using tokio-postgres instead, but found out sqlx to be 30% faster in a stress test.
Beta Was this translation helpful? Give feedback.
All reactions