1

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

假设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)
  }

如何 create notCheap, notExpensiveand midpriceso thatmidprice是从创建notCheapnotExpensive避免代码重复的?

4

1 回答 1

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 演讲和Scala eXchange 2013的 Slick 模式演讲中也有相关信息。http://slick.typesafe.com/docs/

于 2014-05-29T09:57:55.190 回答