我对 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);
有人有想法吗?
谢谢!