Slick 2 - 结合查询

时间:2014-05-29 08:51:18

标签: scala slick slick-2.0

我想以编程方式组合查询以创建查询,从而满足所有需求。我可以看到有一个union和++运算符,但我看不到“交集”或**。

假设Slick FirstExample,我们有一个代码:

  val notCheap = coffees.filter(_.price>8.0)
  val notExpensive = coffees.filter(_.price<9.0)

  val midprice = coffees.filter(_.price>8.0).filter(_.price<9.0)

  println("Midprice coffees:")
  midprice foreach { case (name, supID, price, sales, total) =>
    println("  " + name + "\t" + supID + "\t" + price + "\t" + sales + "\t" + total)
  }

如何创建notCheapnotExpensivemidprice以便midpricenotCheap创建notExpensive以避免代码重复?< / p>

1 个答案:

答案 0 :(得分:4)

implicit class CoffeesExtensions(q: Query[Coffees, Coffee]){
  def notCheap = q.filter(_.price > 8.0)
  def notExpensive = q.filter(_.price < 8.0)
  def midprice = q.notCheap.notExpensive
}

coffees.midPrice

在我们的Scala Days 2013演讲和Slick patterns talk at Scala eXchange 2013中也有关于此的信息。 http://slick.typesafe.com/docs/