Let's say that I have this method that runs a fairly basic query using Slick's plain SQL:
object Data {
case class User(user: String, password: String)
implicit val getUserResult = GetResult(r => User(r.<<, r.<<))
def getUser(user: String, password: String): Option[User] = DB.withSession {
sql"""
SELECT "user",
"password"
FROM "user"
WHERE "user" = $user AND
"password" = $password
""".as[User].firstOption
}
}
What if I have a different query from the same table that has over 100 columns:
SELECT * FROM "user"
In this case there would be a whole lot of typing concerning these two lines:
case class User(user: String, password: String, something: Int, ...)
implicit val getUserResult = GetResult(r => User(r.<<, r.<<, r.<<, ...))
Is it possible to somehow automate these two lines without manually mapping 100 columns? Auto inferring types or even if every column is returned as a string type would be a good alternative.
If specifics are required, my stack is Play Framework 2.2.1, Scala 2.10.3, Java 8 64Bit, PostgreSQL 9.3
The function you give to GetResult
receives a PositionedResult as its argument. Work with it as you like.
If you define
implicit val getListStringResult = scala.slick.jdbc.GetResult[List[String]](
prs => (1 to prs.numColumns).map(_ => prs.nextString).toList
)
you can then say
sql"...".as[List[String]].firstOption