0
votes

I am trying to use pattern matching when querying a mysql database from a play2 module using anorm. Code looks like this:

def test= Action {
    DB.withConnection { implicit c =>
    val entities = SQL("SELECT entity.idEntity, entity.name FROM entity")().collect {
        case Row(id:Int, name:String) => Entity(id, name)
  }
  printList(entities.toList)

}

But the name:String is not matching anything (already tried to match just the integer and it works fine). On my db the entity table "name" column type is varchar(45).

Anything I am missing?

2
Try with 'name: Option[String]' since the column is nullable. - maba
same error with Option[String] or even Some[String] - jdrm
You are mentioning an error. What error is that? - maba
No error...it just doesn't matches anything if I put the String type there. - jdrm

2 Answers

0
votes

You can try to match Row on an Int and a named wild card.

scala> new Row(0,"foo")
res0: Row = Row(0,foo)


scala> res0 match{
     | case Row(i: Int,s @ _) => println(i + " " + s)
     | }
0 foo
0
votes

If name is nullable then this matching should work:

case Row(id:Int, Some(name:String))