我有一个代码选择:
val requestId = 123
DB.withConnection { implicit con =>
SQL("SELECT my_field FROM my_table WHERE some_id = {request_id}").onParams(requestId)()
.collect {
case Row(myField: Option[String]) =>
myField match {
case Some(x) =>
// ???
case None => //there is no such a record?
}
}
}
my_field
可以是NULL
。因此有3种选择:
WHERE some_id = request_id
不存在WHERE some_id = request_id
存在并具有值WHERE some_id = request_id
已存在且为NULL
如何分开1和3?
答案 0 :(得分:1)
我对Anorm的经验很少,因为我已经加入了Slick的行列,但我认为你可以通过这种方式区分你的情况(按顺序):
Some
。None
。所以我猜你会这样匹配:
myField match {
case Seq() => //the record doesn't exist
case Some(x) => //unpack x because you found something
case None => //the record exists but some_id = NULL
}
查看Anorm文档,了解您获得的收集类型。 List
确实实现了Seq
特征。