Suma
Suma

Reputation: 34393

Slick 2 - combine queries

I would like to combine queries programatically to create a query resulting in all requirements satisfied. I can see there is a union and ++ operator, but I see no "intersection" or **.

Assuming Slick FirstExample, let us have a code:

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

How can create notCheap, notExpensive and midprice so that midprice is created from notCheap and notExpensive to avoid code repetition?

Upvotes: 1

Views: 662

Answers (1)

cvogt
cvogt

Reputation: 11270

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

There is also info about this in our Scala Days 2013 talk and the Slick patterns talk at Scala eXchange 2013. http://slick.typesafe.com/docs/

Upvotes: 4

Related Questions