5

Using Slick's plain SQL, I am pulling out case classes using getResult.

case class Foo(a: String, b: String)

def foos(): Future[Vector[Foo]] ={
  val getFoo = GetResult(r => Foo(r.<<, r.<<))
  sql"SELECT a, b FROM foo"
    .as(GetFoo)   // could also use getFoo as an implicit and use .as(Foo)
}

For long case-classes this quickly becomes unwieldy... Can I pass a name of the columns rather than use r.>> or r.nextString etc?


With Anorm I would do something like this:

val parser: RowParser[Foo] = Macro.parser[Foo]("a", "b")
/* Generated as:
get[String]("a") ~ get[String]("b") map {
  case a ~ b => Foo(a, b)
}
*/

def foos(): List[Foo] ={
  SQL("SELECT a, b FROM foo")
    .as(parser.*)
}

This question from 2013 suggests you can use r.rs.getString("a"). This seems slightly strange and is very verbose (but it's what we're currently doing in a few places as a workaround).

Is there a better way? Or a macro that can expand into .rs solution?

Community
  • 1
  • 1
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535

0 Answers0