Array in INSERT query

269 views
Skip to first unread message

Pavel Naydenov

unread,
Mar 7, 2017, 1:16:04 AM3/7/17
to ScalikeJDBC Users Group
Hello!
How can I use arrays with autosession? I found approach like this: https://github.com/nlinker/scalike-pg-arrays/blob/master/src/main/scala/nick/Foo.scala#L71 but it don't use JDBC array support:
  def create(name: String, barz: List[Int])(implicit session: DBSession = autoSession): Foo = {
    val generatedKey
=
      sql
"""
      INSERT INTO ${Foo.table} (
        ${column.name}
      , ${column.barz}
      ) VALUES (
        ${name}
      , ARRAY[${barz}]
      )
      """
.updateAndReturnGeneratedKey.apply()
   
Foo( id = generatedKey, name = name, barz = barz)
}
In pure JDBC I can create java.sql.Array using Connection object. But I don't know how can I do it using auto session in scalikejdbc

Kaz Sera

unread,
Mar 11, 2017, 9:15:15 PM3/11/17
to ScalikeJDBC Users Group
Hi Pavel,

Consider using ParameterBinder. That should work for your case.


-Kaz

2017年3月7日火曜日 15時16分04秒 UTC+9 Pavel Naydenov:

Pavel Naydenov

unread,
Mar 12, 2017, 10:43:50 AM3/12/17
to ScalikeJDBC Users Group
Hi Kaz, thanks for your answer!

Using ParameterBinder I can get access only to the PreparedStatement. But I think that I have to get access to the connection (I mean java.sql.Connection). As I know I can create instance of java.sql.Array using connection (using Connection#createArrayOf method).
Message has been deleted

Fares

unread,
Aug 8, 2018, 1:09:14 PM8/8/18
to ScalikeJDBC Users Group
Hey,

connection is a variable in the DBSession trait. You can access it like this (assuming you still have your implicit session in scope):

session.connection.createArrayOf
Reply all
Reply to author
Forward
0 new messages