mysqlscalaanorm

Can I use anorm to call a MySQL stored procedure that returns two rowsets?


My friendly DB developer has given me a stored procedure that, given an ID, returns to me one (and only one) row of data of a particular format, and then 0-n rows of data in a different format.

That is, the first row will always contain columns a, b and c

Then I will possibly get more rows back, and if I do, there will be any number of them, and they will have columns d, e, f and g.

The stored procedure does two SELECT calls to accomplish this.

Using the mapping for anorm, it makes the call and reads in the data using the mapper for the case class that expects the first row - but then I'm lost. I don't see a way to then tell anorm, "Hey, there might be more data, and, if so, here's the mapper and case class for it..."

Is this even possible?

Here's my code that makes the call:

def sproc[P](name: String, params: Any*)(parser: RowParser[P]) = {
  DB.withConnection {
    implicit connection: Connection =>

    val query = createSprocString(name, params.length)
    val mapped = (for ((p, i) <- params.view.zipWithIndex) yield s"p$i" -> toParameterValue(p)).toArray

    SQL(query)
      .on(mapped: _*)
      .parse(parser *)
  }
}

and I call it thusly (StuffDTO.parse returning the parse mapping):

sproc("SPThatGetsStuffBasedOnAnID", parameters: _*)(StuffDTO.parse)

tl;dr: is there a way to parse the returned rows in anorm when the SQL call returns rows of different formats?


Solution

  • You can use Anorm streaming, parsing each row one after another (according your choosen discriminator; See personal tutorial).