Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create anorm's parser with array?

I am using postgresql which supports array column field. To parse a row, I use this parser. It has error at the Array object. I guess I did it wrongly.

case class ServiceRequest(
  id: Pk[Long],
  firstname: String,
  lastname: String,
  images: Array[String])

val parser: RowParser[ServiceRequest] = {
    get[Pk[Long]]("id") ~
      get[String]("firstname") ~
      get[String]("lastname") ~
      Error here >>> get[Array[String]]("images") map {
        case id ~ firstname ~ lastname ~ images=>
          ServiceRequest(id, firstname, lastname, images)
      }
  }

Thanks

like image 852
mmdc Avatar asked Aug 22 '26 17:08

mmdc


1 Answers

The Array[T] type is now natively supported in play 2.4.x, your don't have to roll your own converters. But, it's still not nice to work with insert or update statements like:

def updateTags(id: Long, values: Seq[String]):Int = {
    DB.withConnection { implicit conn =>
      SQL("UPDATE entries SET tags = {value} WHERE id = {id}")
        .on('value -> values, 'id -> id).executeUpdate
} 

will give you an error

play - Cannot invoke the action, eventually got an error: org.postgresql.util.PSQLException: ERROR: syntax error at or near "$2"

Put it simply, you should create the PreparedStatement using java.sql.Array type:

on('value -> conn.createArrayOf("varchar", value.asInstanceOf[Array[AnyRef]]), ...

As of now (play 2.4-M1), java.sql.Array is not converted to ParameterValue by default, which means SQL(...).on(`arr -> values:java.sql.Array ) still gives a compilation error, you need another implicit conversion to make it compiling:

  implicit object sqlArrayToStatement extends ToStatement[java.sql.Array] {
    def set(s: PreparedStatement, i: Int, n: java.sql.Array) = s.setArray(i, n)
  }
like image 118
Zane XY Avatar answered Aug 25 '26 13:08

Zane XY