问题
I have a Slick query with two left joins (which end up as a Rep[Option[...]]
) and column maps. I need to filter (using like
) on the results. A row has to be included in the results if any of three columns match the like
criteria, so I can't pre-filter the tables - I have to filter the resulting join, Rep[Option]
s and all.
I can't figure how to filter the Rep[Option[String]]
columns. The below code does not compile due to "cannot resolve symbol ||" - if compiles perfectly when I remove the Rep[Option]
columns.
val joinedTable = Sites.map(s=>(s.id, s.gisId))
.joinLeft(SiteText.filter(_.lang==="jp").map(l=>(l.name, l.siteId))).on{ case(s,t)=>s._1===t._2 }
.joinLeft(SiteText.filter(_.lang==="en").map(l=>(l.name, l.siteId))).on{ case(st,t)=>st._1._1===t._2 }
val searchedTable = joinedTable.filter { row =>
List(
searchStr.map( t => row._1._1._2 like t ),
searchStr.map( t => row._1._2.map(_._1 like t) ),
searchStr.map( t => row._2.map(_._1 like t) )
).collect({case Some(criteria) => criteria}).reduceLeftOption(_ || _).getOrElse(true: Rep[Boolean])
}
回答1:
The following seems to work for me:
joinedTable
.filter({ case ((a, b), c) => List(
searchStr.map(t => (a._2 like t)),
searchStr.map(t => b.filter(_._1 like t).isDefined),
searchStr.map(t => c.filter(_._1 like t).isDefined)
)
.flatten
.reduceLeftOption(_ || _)
.getOrElse(false: Rep[Boolean])
})
来源:https://stackoverflow.com/questions/43572566/slick-3-1-left-joins-and-filters