我正在制作一个按照以下方式过滤查询的功能:
class Foos(tag: Tag) extends RichTable[Foo](tag, "t_foo") {
// foo slick mapping
}
工作职能
object Foos extends PostgresGeneric[Foos, Foo] {
private def filterQuery(query :Query[Foos,Foos#TableElementType,Seq], searchFilter: FooFilter) = {
var filteredQuery = query
if(searchFilter.searchEntity.isDefined) {
val searchEntity = searchFilter.searchEntity.get
if(searchEntity.cancelled.isDefined) {
filteredQuery = filteredQuery.filter(p => p.cancelled === searchEntity.cancelled.get)
}
if(searchEntity.nome.isDefined) {
filteredQuery = filteredQuery.filter(p => p.nome.toLowerCase like "%"+searchEntity.nome.get.toLowerCase()+"%")
}
if(searchEntity.localidade.isDefined) {
filteredQuery = filteredQuery.filter(_.localidade === searchEntity.localidade)
}
}
filteredQuery
}
}
无效功能
object Foos extends PostgresGeneric[Foos, Foo] {
private def filterQuery(query :Query[Foos,Foos#TableElementType,Seq], searchFilter: FooFilter) = {
var filteredQuery = query
if(searchFilter.searchEntity.isDefined) {
val searchEntity = searchFilter.searchEntity.get
if(searchEntity.cancelled.isDefined) {
filteredQuery = filteredQuery.filter(p => p.cancelled === searchEntity.cancelled.get)
}
if(searchEntity.nome.isDefined) {
filteredQuery = filteredQuery.filter(p => p.nome.toLowerCase like "%"+searchEntity.nome.get.toLowerCase()+"%")
}
if(searchEntity.localidade.isDefined) {
filteredQuery = filteredQuery.filter(_.localidade === searchEntity.localidade)
filteredQuery = filteredQuery.join(tableReferenceFoo2).on(_.foo2 === _.id)
}
}
filteredQuery
}
}
错误消息
Error:(165, 76) type mismatch;
found : scala.slick.lifted.Query[(model.db.entity.Foos, model.db.entity.Foos2),(model.db.entity.Foo, model.db.entity.Foos2#TableElementType),Seq]
(which expands to) scala.slick.lifted.Query[(model.db.entity.Foos, model.db.entity.Foos2),(model.db.entity.Foo, model.db.entity.Foo2),Seq]
required: slick.driver.PostgresDriver.simple.Query[model.db.entity.Foos,model.db.entity.Foo,Seq]
(which expands to) scala.slick.lifted.Query[model.db.entity.Foos,model.db.entity.Foo,Seq]
filteredQuery = filteredQuery.join(tableReferenceFoo2).on(_.Foo2 === _.id)
第一个代码有效,因为生成的查询在过滤后总是具有相同的类型。它在连接后失败,因为类型改变了。
Porblem:
此函数正常工作,直到我需要与另一个表执行连接。
那么,我如何以一种接受可以挂载任何类型查询的类型的方式键入此Query实例?有可能吗?