databasescalaplayframework-2.0anormscala-option

How to retrieve an Option[List[X]] instead of List[X] from a select statement in Play2!Scala Anorm?


In my Play2 app, I am trying to retrieve a list of users from one of my database table. The query responsible for this may potentially be empty if there is no row in the database matching the criteria (which is the firstName in our case). That is why I have managed to implement it like this :

    DB.withConnection { implicit connection =>
      SQL("""select u.* from users u
                where u.firstName like '%{firstName}%'
          """).on("firstName" -> firstName).as(userParser *)
    }

this query returns a List[User] but how can I return an Option[List[User]] since this query may not retrieve data corresponding to the provided param (firstName) ?

any help would be appreciated thanks...


Solution

  • You don't need to. If no user is found. The list will simply be empty.