1

Is there a way in Slick 2.0 to create a method on a table class,that generates a SQL JOIN syntax rather than just a WHERE clause?

Using an example similar to the documentation:

class Suppliers(tag: Tag) extends Table[(Int, String)](tag, "SUPPLIER") {
    ...
    def suppliedCoffees = coffees.filter(_.supplierId == id)
    def * = (id, name)
}

class Coffees(tag: Tag) extends Table[(Int, Int, String)](tag, "COFFEES") {
    ...
    def * = (id, supplierId, name)
}

I can create the suppliedCoffees method and use it as such:

for {
    s <- suppliers
    c <- s.suppliedCoffees
} yield (s.name, c.name) ...

Nice and neat - easy to read, and the join criteria are nicely hidden. But this generates an SQL WHERE clause. How can I do something similar to the following, but using a method like suppliedCoffees so that it's encapsulated in the class?

for { 
    (s, c) <- suppliers innerJoin coffees on (_.id === _.supplierId)
} yield (s, c) ...

Or better yet, how can I do an outer join? (but as a method like suppliedCoffees)

for { 
    (s, c) <- suppliers leftJoin coffees on (_.id === _.supplierId)
} yield (s.name, c.name.?) ...

Thanks in advance.

Cheers, Rob.

p.s. Slick is awesome! It sure makes using an SQL database fun again :)

headexplodes
  • 125
  • 1
  • 5

1 Answers1

0

You can make them method or functions. We explain how to do this in our Scala Days 2013 and Scala eXchange 2013 talks. Slides and videos are available here: http://slick.typesafe.com/docs/

cvogt
  • 11,260
  • 30
  • 46
  • Thanks for the reply. So I'd have to use implicit classes? Is there a way to make it as simple as the `suppliedCoffees` method in my example (ie, just using `filter`) – headexplodes Feb 25 '14 at 09:35
  • This appears to be currently broken. I added a test case: `https://github.com/slick/slick/pull/689`. Why do you want the SQL join syntax over the where syntax? – cvogt Feb 25 '14 at 14:26
  • That's a really good question :) So that I can do outer joins, and for consistency with other DB queries I guess. I'm assuming that in the case of inner join it would perform the same in most DBs as a WHERE clause. The Query(this).join ... is what I was after (although I may have to wait), and the implicit class approach seems to be working for me at the moment too. Thanks for your help! – headexplodes Feb 25 '14 at 21:28