Anorm Primer

Typically, you would read the results of an Anorm query, and map to a already defined type - case class, something like this:

val persons: List[Person] =
  SQL("select id from ....")

final case class Person(id: Int, name: String, weight: Float)

def toPerson(row: Row): Person =

There are times when you would want to read a single column. Or perhaps a set of columns for which you don’t have a pre-defined type (like Person above). In such cases, you will toil as follows:

val ids: List[Int] =
SQL("select id from ....")
    .map(row => row[Int]("id"))

// or

val ps: List[(Int, String)] =
  SQL("select id from ....")
    .map { row =>
        row => row[Int]("id"),
        row => row[String]("name")

Note that the following is different from what we are discussing here.

// NOTE: We are not talking about this !!!
val count: Long = SQL("select count(*) from users").as(scalar[Long].single)

Using core Anorm functions

import anorm.{SQL, SqlParser}, SqlParser.{int, str, to}

val result: List[Int] =
  SQL("select id from persons where ...")

val result: List[Int ~ String] =    // Anorm's type `~` for chanining types (much like shapeless HList)
  SQL("select id, name from ....")
    .as(int("id") ~ str("name"))

Anorm’s parser combinators 101

Extracting a reusable parser for your query

val resultSetParser: ResultSetParser[List[Int ~ String]] = int("id") ~ str("name")
val parser: ResultSetParser[List[(Int, String)]] = { case n ~ p => (n, p) }

// Shorthand of the above using built-in `flatten` function
val parser: ResultSetParser[List[(Int, String)]] =*

// The finishing touch!
val result: Map[Int, String] =
    .as(parser)  // List[(Int, String)]

A Bit More of Anorms' Parser Combinators

Anorm provides a bunch of other parser combinators. Check Anorm’s documentation for more details but here is a teaser:

Explicit control of how much to read from the ResultSet

val parser: ResultSetParser[List[(String, Int)]] =
  for {
    name <- str("name")
    age  <- int("age")
  } yield name -> age

val exactlyOne: (String, Int)         = SELECT("select ...").as(parser.single)
val zeroOneOrMore: List[(String, Int) = SQL("select ...").as(parser.*)
val oneOrMore: List[(String, Int)     = SQL("select ...").as(parser.+)

The to combinator to the rescue

import anorm.SqlParser.{ int, str, to }

def show(name: String, age: Int): String =  s"...."

val parser: ResultSetParser[List[(String, Int)]] =
  str("name") ~ int("age").map(to(show _))

Parser using Pattern matching

val parser: ResultSetParser[List[(String, Int, Boolean)]] =
  str("name") ~ int("age") ~ str("millionaire").map {
    case n ~ l ~ "T" => (n, l, true)
    case n ~ l ~ "F" => (n, l, false)