Scala squeryl查询中的模式匹配

Scala squeryl查询中的模式匹配,scala,orm,squeryl,Scala,Orm,Squeryl,我刚刚开始使用squeryl,对于如何编写这样的查询,我没有答案 SELECT ref label FROM x_table WHERE ref like x% or lable like x% 其中,x是用户提供的一些值,特别是我还没有在squeryl中找到类似符号%,或者我如何使用它 我的版本: val products = from(AppDB.productTable) ( s => where ((s.label like value) or (s.ref like

我刚刚开始使用squeryl,对于如何编写这样的查询,我没有答案

SELECT ref label 
FROM x_table 
WHERE ref like x% or lable like x%
其中,
x
是用户提供的一些值,特别是我还没有在squeryl中找到类似符号
%
,或者我如何使用它

我的版本:

val products = from(AppDB.productTable) (
    s => where ((s.label like value) or (s.ref like value))  select(s)
)
不正常


val-value:Option[String]
我从用户那里获得

您可以尝试对字段进行优化,如下所示:

val products = from(AppDB.productTable) (s => 
  where ((Some(s.label) like value) or (Some(s.ref) like value)) 
  select(s))
当查询将选项[String]与选项[String]进行比较时,它将编译。Squeryl将在内部处理选项状态

如果您只是想将通配符添加到正在比较的内容中,假设like子句的两边都是
Option[String]
类型,则可以执行以下操作:

s.label like value.map(_ + "%")

您可以尝试对字段进行优化,如下所示:

val products = from(AppDB.productTable) (s => 
  where ((Some(s.label) like value) or (Some(s.ref) like value)) 
  select(s))
当查询将选项[String]与选项[String]进行比较时,它将编译。Squeryl将在内部处理选项状态

如果您只是想将通配符添加到正在比较的内容中,假设like子句的两边都是
Option[String]
类型,则可以执行以下操作:

s.label like value.map(_ + "%")

您可以使用较短的版本:

AppDB.productTable.where ((s.label like value) or (s.ref like value))

选项字段用于可为空的列

您可以使用较短的版本:

AppDB.productTable.where ((s.label like value) or (s.ref like value))

选项字段用于可为空的列

不,在这种情况下,我们得到的erorr不是某个[option[String]]的成员……
s.ref
是一个
选项[String]
是一个
选项[String]
?您最初收到的错误是什么?是的,我正在查看如何添加通配符,这样就可以了,谢谢,但它仅适用于选项[String],而不适用于字符串,例如,对吗?对于答案顶部建议的代码@jcern,如果值是字符串,它会给出一个错误“like is not a member of Some[Option[String]”,您可以简单地将右侧设置为
(value+“%”
。不,在这种情况下,我们得到的erorr不是某个[Option[String]]的成员……
s.ref
选项[String]
value
选项[String]
?您最初收到的错误是什么?是的,我正在查看如何添加通配符,这样就可以了,谢谢,但它仅适用于选项[String],而不适用于字符串,例如,对吗?对于答案顶部建议的代码@jcern,如果值是字符串,它会给出一个错误“like is not a member of Some[Option[String]”,您可以简单地将右侧设置为
(value+“%”
。不,这样它就没有必要的行为,如果需要获取label或ref包含值的所有行,则必须使用通配符,这样它就没有必要的行为,如果需要获取label或ref包含值的所有行,则必须使用通配符。您是在询问如何将
%
字符附加到字符串的结尾吗?您是在询问如何将
%
字符附加到字符串的结尾吗?