Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing to PostgreSQL array via ScalikeJDBC

I try use ScalikeJDBC for access to array in PostgreSQL 9.4. DDL:

create table itab (
        code varchar primary key,
        group_list varchar[]
);

A simple case class and loader are defined in Scala application.

case class Item(code: String, groupSet: List[String])

trait loader {
  def loadAllItems: List[Item] = {
      insideReadOnly { implicit session =>
                       sql"select CODE, GROUP_LIST from ITAB"
                       .map(e => Item(
                           e.string("code"),
                           e.array("group_list").asInstanceOf[Buffer[String]]
                        )).list.apply()
                     }
  }
}

When I run an application I get runtime exception

java.lang.ClassCastException: org.postgresql.jdbc4.Jdbc4Array cannot be cast to scala.collection.mutable.Buffer

How can I resolve it? Thanks. Hoviman.

like image 347
hoviman Avatar asked Sep 04 '15 09:09

hoviman


1 Answers

Use rs.array("group_list").getArray.asInstanceOf[Array[String]]

It's just java.sql.Array underneath

like image 177
Tyth Avatar answered Sep 21 '22 16:09

Tyth