Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array support? #11

Open
waywardmonkeys opened this issue Jul 20, 2011 · 1 comment
Open

Array support? #11

waywardmonkeys opened this issue Jul 20, 2011 · 1 comment

Comments

@waywardmonkeys
Copy link

I'm not sure what the best way to do this, but to work with arrays in Postgres, we need access to the connection object to call a method on it:

val sqlArray = connection.createArrayOf("uuid", products.toArray.asInstanceOf[Array[AnyRef]])

When products was an Iterable...

It'd be nice if this could be folded into prepare in Utils.scala somehow, but not sure how given the type name parameter...

Thoughts?

@lambdaknight
Copy link

Not a thought, per se, but I tried hacking on this briefly this morning. I tried changing the "prepare" method to:

@tailrec
private[jdub] def prepare(stmt: PreparedStatement, values: Seq[Any], index: Int = 1) {
  if (!values.isEmpty) {
    val v = values.head
    if (v == null) {
      stmt.setNull(index, Types.NULL)
    } else if(v.isInstanceOf[Array[_]]) {
      stmt.setObject(index, v.asInstanceOf[AnyRef], Types.ARRAY)
    } else {
      stmt.setObject(index, v.asInstanceOf[AnyRef])
    }
    prepare(stmt, values.tail, index + 1)
  }
}

Unfortunately, when I tried using this with the "org.postgresql.Driver" driver, I got:

org.postgresql.util.PSQLException: Cannot cast an instance of [I to type Types.ARRAY
    at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1736)
    at org.postgresql.jdbc3.AbstractJdbc3Statement.setObject(AbstractJdbc3Statement.java:1483)
    at org.postgresql.jdbc3g.AbstractJdbc3gStatement.setObject(AbstractJdbc3gStatement.java:47)
    at org.postgresql.jdbc4.AbstractJdbc4Statement.setObject(AbstractJdbc4Statement.java:69)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1751)
    at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:166)
    at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:166)
    at com.codahale.jdub.Utils$.prepare(Utils.scala:17)
    at com.codahale.jdub.Database$$anonfun$execute$1.apply$mcI$sp(Database.scala:145)
    at com.codahale.jdub.Database$$anonfun$execute$1.apply(Database.scala:139)
    at com.codahale.jdub.Database$$anonfun$execute$1.apply(Database.scala:139)
    at com.yammer.metrics.Timer.time(Timer.scala:20)
    at com.codahale.jdub.Database.execute(Database.scala:138)
    at .<init>(<console>:12)
    at .<clinit>(<console>)
    at .<init>(<console>:11)
    at .<clinit>(<console>)
    at $export(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:592)
    at scala.tools.nsc.interpreter.IMain$Request$$anonfun$10.apply(IMain.scala:828)
    at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
    at scala.tools.nsc.io.package$$anon$2.run(package.scala:31)
    at java.lang.Thread.run(Thread.java:680)

Unfortunately, I'm not seeing a way to get an Array in there easily (using only a PreparedStatement).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants