主动脉动态滤器 [英] anorm dynamic filters

查看:44
本文介绍了主动脉动态滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm studying a little the anorm documentation (from play framework) and is not clear if it supports a common query use case: dynamic filters, that is the user fills in 2 or 3 search criteria on a 10 fields search form.

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

In this case how can I dynamically construct the query without the classic string manipulation?

推荐答案

是的,我认为Robin Green引用的问题中包含了答案.只需使用占位符(例如{criterion1})使用所有可能的条件定义查询,然后对查询调用on()方法,并按照接受的答案中的说明传递Option参数的实际Seq.

Yes, I think the question referenced by Robin Green contains the answer. Just define your query with all the possible criteria using placeholders (e.g. {criterion1}) and call the on() method on the query, passing the actual Seq of Option parameters as described in the accepted answer.

Anorm文档中的修改示例,假设您有两个条件,但只希望查询根据国家(地区)代码而不是大写字母进行过滤:

Modified example from the Anorm doc, assuming you have two criteria but only want your query to filter on the country code and not on the capital:

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)

应该可以解决问题.

这篇关于主动脉动态滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆