Slick 3.1,左连接和过滤器 [英] Slick 3.1, Left Joins and Filters

查看:50
本文介绍了Slick 3.1,左连接和过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个左连接(最终作为 Rep[Option[...]])和列映射的 Slick 查询.我需要对结果进行过滤(使用 like).如果三列中的任何一列符合 like 条件,则结果中必须包含一行,因此我无法预过滤表 - 我必须过滤结果连接,Rep[选项]s 和所有.

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.

我不知道如何过滤 Rep[Option[String]] 列.由于无法解析符号 ||",以下代码无法编译- 如果在删除 Rep[Option] 列时编译完美.

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])
}

推荐答案

以下似乎对我有用:

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])
  })

这篇关于Slick 3.1,左连接和过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆