1

I am continuing my journey with Play, Scala & Anorm, and I faced a following problem:

One of my repository classes holds a logic to fetch list of email from DB for a user, as follows:

val sql =
      """
        |select * from user_email where user_id = {user_id}
        |;--
      """.stripMargin

    SQL(sql).on(
      'user_id -> user.id
    ).as(UserEmail.simpleParser *)

While a parser is something like this:

val simpleParser: RowParser[UserEmail] = (
      SqlParser.get[Muid](qualifiedColumnNameOf("", Identifiable.Column.Id)) ~
      AuditMetadata.generateParser("") ~
      SqlParser.get[Muid]("user_id") ~
      SqlParser.get[String]("email") ~
      SqlParser.get[Boolean]("verified") ~
      SqlParser.get[Muid]("verification_code_id") ~
      SqlParser.get[Option[DateTime]]("verification_sent_date")
    ) map {
    case id ~ audit ~ userId ~ email ~ emailVerified ~ emailVerificationCode ~ emailVerificationSentDate =>
      UserEmail(
        id,
        audit,
        userId,
        email,
        emailVerified,
        emailVerificationCode,
        emailVerificationSentDate
      )
  }

When I execute this in test, I am getting the following error:

[error]    Multiple ResultSets were returned by the query. (AbstractJdbc2Statement.java:354)
...

It is expected, that there are more than a simple result; however, I am puzzled about how to parse this case correctly.

I was under assumption, that: UserEmail.simpleParser single is for simple row and UserEmail.simpleParser * is to handle multiple rows

I could not figure this put based on documentation, and, at least for now, didn't find anything useful anywhere else.

How do I parse multiple rows from the result set?

Update: I just found this gist (https://gist.github.com/davegurnell/4b432066b39949850b04) with pretty good explanation and created a ResultSetParser like so:

val multipleParser: ResultSetParser[List[UserEmail]] = UserEmail.simpleParser.*

And... that didn't help!

Thanks,

cchantep
  • 9,118
  • 3
  • 30
  • 41
Shurik Agulyansky
  • 2,607
  • 2
  • 34
  • 76
  • The error is not about having multiple rows in the result set, but about having multiple result sets, which is possible according JDBC but should not happen in such case. – cchantep Dec 24 '15 at 10:57
  • I don't think you can do this with anorm. If I try to put in multiple statements like you have (seperated by `;--` then I get an exception about "No operations allowed after statement closed".), if you could get that running then you could grab the statement from the result of `executeQuery()` and drop to the JDBC layer to loop over the result sets (see http://stackoverflow.com/a/33281387/1808164), but if the query never runs no dice. The BatchSQL in anorm is only for updates it looks like, and not for multiple different statements like you can do in regular JDBC. So looks impossible. – EdgeCaseBerg Dec 02 '16 at 20:40

0 Answers0