Scala Play 2.3.0 with Anorm - Can't use Patter

2020-05-07 07:21发布

问题:

I am developing an application with Scala (2.11) and Play Framework (2.3.0) on IntelliJ IDEA. I'm using Anorm to retrieve data from my database (MySQL with MariaDB).

Here is my first test application (it works):

package controllers

import play.api.mvc._
import play.api.db._
import anorm._

case class Client(id: Int, nom: String, prenom: String)

object Application extends Controller {

  def index = Action {
    var result: List[(Int, String)] = List()
    val sqlQuery = SQL(
      """
        select idClient, nameClient from Clients
        where idClient = {idClient};
      """
    ).on("idClient" -> 1)

    DB.withConnection { implicit conn =>
      result = sqlQuery().map(row =>
        row[Int]("IDClient") -> row[String]("NameClient")
      ).toList

    }
    Ok(result.toString)
  }
}

This works fine. I get the name of my client correctly. However, when I try to use pattern matching, like this:

result = sqlQuery().collect {
  case Row(idClient: Int, nameClient: String) => idClient -> nameClient
}

IntelliJ gives me an error, stating that it "Cannot resolve Symbol Row". As far as I know, Row is defined in the Anorm library, and so is SQL. It doesn't make sense that SQL would be found and not Row...

What's happening?

回答1:

anorm.Row extractor is not there in Play 2.3 . As suggested you could use parser.