How to create a query dynamically? #4381
-
Is there a way to create a query dynamically, meaning to construct different blocks of the query following certain logic? A simple case: I may or may not want a filter to be applied to the query, and I want to avoid writing two separate queries for this. Here's what I naively did; however, it doesn't compile because I don't know what to put in let table = nodes::table;
let query = if let Some(filter) = filter {
table.filter(nodes::name.eq("Node2"))
} else {
table.filter(??)
};
query.load::<Node>(conn); Undoubtedly my method is not correct, do you have any suggestions? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
See the documentation of |
Beta Was this translation helpful? Give feedback.
See the documentation of
QueryDsl::into_boxed
for a solution.