0

I'm trying to use scalikejdbc to access schema that contains array field, i.e.

CREATE TABLE foo (
  id         INTEGER      PRIMARY KEY,
  events     TEXT[]       NOT NULL
);

I can't find anything about arrays in documentation or examples, I can only guess by looking at the scalikejdbc sources.

My model looks like

case class Foo(id: Long, events: Array[String])

object Foo extends SQLSyntaxSupport[Foo] with ShortenedNames {
  override val columnNames = Seq("id", "events")
  private val s = syntax("s")

  private def apply(sp: SyntaxProvider[Foo])(rs: WrappedResultSet): Foo = apply(sp.resultName)(rs)

  private def apply(rn: ResultName[Foo])(rs: WrappedResultSet): Foo = Foo(
    id          = rs.get(rn.id),
    events      = rs.get(rn.events)
  )

  // ...
}

And compiler complains

erroneous or inaccessible type events      = rs.get(rn.events)
                                                   ^

If I change extractor code to

events      = rs.array(rn.events).getArray.asInstanceOf[Array[String]]

It compiles fine, but I get runtime exception

Execution exception[[UnsupportedOperationException: null]]

So how do I access array field using scalikejdbc?

lambdas
  • 3,990
  • 2
  • 29
  • 54

2 Answers2

1

I've found out that result should be casted to Buffer

events      = rs.any(rn.events).asInstanceOf[Buffer[String]]
lambdas
  • 3,990
  • 2
  • 29
  • 54
1

Which RDBMS are you using? I guess UnsupportedOperationException is thrown by your JDBC driver implementation. I just tried a simple example to extract Array value from java.sql.Array value by simply using java.sql.Array#getArray() with H2 database and it works fine.

Kazuhiro Sera
  • 1,822
  • 12
  • 15
  • Hi, thanks for response. I'm using Postgresql 9.3 with postgresql-async 0.2.13 driver. – lambdas Aug 27 '14 at 08:34
  • I see. scalikejdbc-async is different from JDBC specification. Basically postgresql-async doesn't support all the types in JDBC, so scalikejdbc-async also doesn't support all the types. https://github.com/scalikejdbc/scalikejdbc-async/blob/develop/core/src/main/scala/scalikejdbc/async/internal/AsyncResultSetImpl.scala – Kazuhiro Sera Aug 28 '14 at 00:45
  • I should have mention that I'm using scailkejbc-async, sorry. What's interesting, `array` still resolves and compiles, event it is not implemented in `AsyncResultSetImpl`. – lambdas Aug 28 '14 at 02:36
  • *-async are still in the alpha stage. We're waiting for your contributions. – Kazuhiro Sera Aug 28 '14 at 08:33