Slick 3.1, Left Joins and Filters

别说谁变了你拦得住时间么 提交于 2021-01-23 06:33:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!