问题
I want to do something like this (this is a made-up example to simplify my actual problem):
def findByGender(isMale: Option[Boolean]) = {
People.filter(row => row.name.isNotNull && isMale match {
case Some(true) => row.wife.isNotNull // find rows where wife column is not null
case Some(false) => row.wife.isNull // find rows where wife column is null
case None => true // select everything
})
}
This does not compile because of the last "true". Any better way to do this?
回答1:
You have to make it a Column[Boolean]
:
def findByGender(isMale: Option[Boolean]) = {
People.filter(row => row.name.isNotNull && isMale match {
case Some(true) => row.wife.isNotNull // find rows where wife column is not null
case Some(false) => row.wife.isNull // find rows where wife column is null
case None => LiteralColumn(true) // select everything
})
}
回答2:
If you're using Slick 3.3.x you can use the following solution:
def findByGender(isMale: Option[Boolean]) =
People
.filter(_.name.isDefined)
.filterIf(isMale == Some(true))(_.wife.isDefined)
.filterIf(isMale == Some(false))(_.wife.isEmpty)
or
def findByGender(isMale: Option[Boolean]) =
People
.filter(_.name.isDefined)
.filterOpt(isMale) {
case (row, true) => row.wife.isDefined
case (row, false) => row.wife.isEmpty
}
There are 3 cases:
isMale
is defined and equalSome(true)
Result SQL:
select * from people when (name is not null) and (wife is not null);
isMale
is defined and equalSome(false)
:Result SQL:
select * from people when (name is not null) and (wife is null);
isMale
is emptyResult SQL:
select * from people when name is not null;
来源:https://stackoverflow.com/questions/26554224/slick-option-column-filtering