1

我对 Rust 和 Diesel 很陌生。我现在正在尝试为这样的查询实现 Diesel 过滤:

-- @param1 is duration in years
-- @param2 is duration in months

SELECT columns FROM a
WHERE 
(...dynamic AND clauses)
AND (((a.min_unit = "Years") AND (a.min_duration <= @param1))
  OR ((a.min_unit = "Months") AND (a.min_duration <= @param2)))
(...dynamic AND clauses)

在文档和网络上进行一些搜索后,我仍然找不到如何执行此操作。我最接近的猜测是:

let param1 = ...;
let param2 = ...;

let mut query = a::table.select(a::all_columns).into_boxed();
// dynamic clauses: query = query.filter(...) ...
query = query.filter(a::min_unit.eq(Some("Years")).and(a::min_duration.le(Some(param1))))
  .or_filter(a::min_unit.eq(Some("Months")).and(a::min_duration.le(Some(param2))));
// dynamic clauses: query = query.filter(...) ...

let results = a::table.load<A>(&*conn);

有人有想法吗?

谢谢!

4

2 回答 2

2

您的代码实际上看起来是正确的(我没有运行它)。这里的文档指出了它是如何完成的:https foo.filter(bar).or_filter(baz) is like foo.filter(bar.or(baz)) ://docs.diesel.rs/diesel/query_dsl/trait.QueryDsl.html#method.or_filter但第二个是“嵌套的”。所以,得到 (id = 5 OR other = 6) AND foo=7 一个就可以了 .filter(id.eq(5).or(other.eq(6))).filter(foo.eq(7))。希望有帮助!

于 2020-05-14T04:19:45.657 回答
0

Well, here is my latest try:

let min_months_predicate = a::min_unit.eq(Some("Months"))
    .and(a::min_duration.le(Some(param1)));
let min_years_predicate = a::min_unit.eq(Some("Years"))
    .and(a::min_duration.le(Some(param1)));

query = query.filter(min_months_predicate.or(min_years_predicate))
    .filter(a::max_duration.ge(Some(param2)));

debug!("TEST QUERY: {:?}", debug_query(&query));

which yields this query:

"SELECT \"a\".\"id\", \"a\".\"code\", .... WHERE (\"a\".\"min_unit\" = $1 AND \"a\".\"min_duration\" <= $2 OR \"a\".\"min_unit\" = $3 AND \"a\".\"min_duration\" <= $4) AND \"a\".\"max_duration\" >= $5"

And when I use sql EXPLAIN with this query, I got this clause:

  Filter: ((max_duration >= 60) AND (((min_unit = 'Months'::text) AND (min_duration <= 3)) OR ((min_unit = 'Years'::text) AND (min_duration <= 5))))

which seems correct.

于 2020-06-05T03:55:49.677 回答