gpt4 book ai didi

scala - 异常动态过滤器

转载 作者:行者123 更新时间:2023-12-03 23:08:51 26 4
gpt4 key购买 nike

我正在研究一点 anorm 文档(来自 play framework),不清楚它是否支持常见的查询用例:动态过滤器,即用户在 10 个字段的搜索表单上填写 2 个或 3 个搜索条件。

在这种情况下,如何在没有经典字符串操作的情况下动态构造查询?

最佳答案

是的,我认为 Robin Green 提到的问题包含了答案。只需使用占位符(例如 {criterion1} )使用所有可能的条件定义您的查询,然后调用 on()方法上的查询,传递实际 SeqOption接受的答案中描述的参数。

来自 Anorm 文档的修改示例,假设您有两个条件,但只希望您的查询过滤国家代码而不是首都:

SQL(
"""
select * from Country c
join CountryLanguage l on l.CountryCode = c.Code
where ({countryCode} is null or c.code = {countryCode})
and ({capital} is null or c.capital = {capital});
"""
).on("countryCode" -> Some("FRA"), "capital" -> None)

这应该够了吧。

关于scala - 异常动态过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13516613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com