Replies: 5 comments 14 replies
-
Important note: there is not known at the compile time which option will have both |
Beta Was this translation helpful? Give feedback.
-
You can do that with SQLx itself: SELECT c
FROM table1
WHERE
1 = 1
AND (
a IS NULL
OR a = $1
)
AND (
b IS NULL
OR b = $2
) The one downside is that PostgreSQL doesn't have the the opportunity to optimize the query as well, but in a lot of cases that's good enough, and I would generally suggest going with the simpler option. But you can certainly do this with let row: Vec<Rws> = sqlx_conditional_queries::conditional_query_as!(
Rws,
r#"
SELECT c
FROM table1
WHERE
1 = 1
{#a}
{#b}
"#,
#a = match a {
Some(_) => "AND a = {a}",
None => "",
},
#b = match b {
Some(_) => "AND b = {b}",
None => "",
},
) or let row: Vec<Rws> = sqlx_conditional_queries::conditional_query_as!(
Rws,
r#"
SELECT c
FROM table1
{#wc}
"#,
#wc = match (a, b) {
(Some(_), None) => r#"
WHERE a = {a}
"#,
(Some(_), Some(_)) => r#"
WHERE
a = {a}
AND b = {b}
"#,
(None, Some(_)) => r#"
WHERE b = {b}
"#,
(None, None) => "",
},
) Both of those options are makes for harder-to-read queries though, so I would recommend avoiding them where possible. |
Beta Was this translation helpful? Give feedback.
-
Isn't is possible to bind variables using tuples? I mean, such code:
written in this manner:
I got the error:
|
Beta Was this translation helpful? Give feedback.
-
It looks I found a infinite recursion of something like this The code: #[derive(Debug)]
#[derive(sqlx::FromRow)]
struct Rws {
c: Option<String>
}
#[derive(Clone, Deserialize, Default, PartialEq, Debug, sqlx::FromRow)]
pub struct Infrastructure {
has_parking: bool,
is_kindergarden_nearby: bool,
is_school_nearby: bool,
}
...
let row: Vec<Rws> = sqlx_conditional_queries::conditional_query_as!(
Rws,
r#"
SELECT "some text"
FROM table1
INNER JOIN infrastructure USING (infrastructure.id)
WHERE
1 = 1
{#a}
{#b}
{#infrastructure_filter}
"#,
#a = match a {
Some(_) => "AND a = {a}",
None => "",
},
#b = match b {
Some(_) => "AND b = {b}",
None => "",
},
#infrastructure_filter = match infrastructure.has_parking {
true => "AND has_parking = true",
_ => "",
},
#infrastructure_filter = match infrastructure.is_kindergarden_nearby {
true => "{#infrastructure_filter} AND is_kindergarden_nearby = true",
_ => "",
},
) So, what I wanted to do above to do. I have a single logical step
rather, I wanted use the only one compile time bound
I suppose it will be better to throw an error when encounter such statement |
Beta Was this translation helpful? Give feedback.
-
Isn't it possible to use nested blocks like a match inside another match? let infrastructure = Some(
Infrastructure {
has_parking: false,
is_kindergarden_nearby: true,
is_school_nearby: false,
}
);
let row: Vec<Rws> = sqlx_conditional_queries::conditional_query_as!(
Rws,
r#"
SELECT "some text"
FROM table1
INNER JOIN infrastructure USING (infrastructure.id)
WHERE
1 = 1
{#a}
{#b}
{#infrastructure_filter}
"#,
#a = match a {
Some(_) => "AND a = {a}",
None => "",
},
#b = match b {
Some(_) => "AND b = {b}",
None => "",
},
#infrastructure_filter = match infrastructure {
Some(infrastructure) => match infrastructure.has_parking{
true => "AND has_parking = true",
false => "",
},
None => "",
},
... Error: expected string literal, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have the code using vanila sqlx (not the sqlx-conditional):
and then
I need the query above to be executed the next way:
a
isSome(1)
andb
isNone
, then the DB connection crate should to send the next query:a
isSome(2)
andb
is `Some(3), then the DB connection crate should to send the next query:a
andb
areNone
, then the DB connection crate should to send the next query (perhaps without thewhere
part):So, I have the questions:
conditional_query_as!
macros (or any other function in your crate) have a such feature?Beta Was this translation helpful? Give feedback.
All reactions