@Dickon, if you just want an object returned do something like:
case class Foo(id: Int, bar: String)
object Foos extends Table[Foo]("foo") {
def id = column[Int]("id", O PrimaryKey)
def bar column[String]("bar")
}
def getFoos(id: Int): List[Foo] {
val q = (for {f <- Foos; if
f.id is id.bind } yield f)
asList(q)
}
def asList[T](q: Query[_,_]): List[T] = {
db withSession { implicit ss: Session=>
q.list map { case t:T => t }
}
}
This approach is of course not fully type safe (if you chose to do,
"yield (f.bar)" for example, then the query would fail at runtime as
you are missing the id field to perform the query result to case class
conversion). Anyway, highly convenient, IMO.
Projection case classes (a view in DBMS terms) are the same, but
instead of returning object tuple(s) from your query (that's what SQ
returns with "yield f", a tuple of fields representing the mapping),
you return the tuple that represents your projection case class.
Should note that if you intend to use Parameters binding (suggested,
caches query statements) then asList method above either needs
signature changed from "q: Query[_,_]" to "q: MutatingUnitInvoker[_]"
or have asList versions for both query types if for some reason you
don't want to cache query generation (which is what happens when you
only go with fooparam.bind). asOption versions would be the same but
with q.firstOption in place of q.list and of course, return Option[T]
Take all this with a grain of salt, am new-ish to ScalaQuery, far from
an expert, but am absolutely loving SQ, functional queries, LINQ-to-
SQL goodness in Scala, totally awesome ;-)