2021-12-03

Kotlin - how to pass a list of Pair values as tuples to an SQL select statement?

I have an SQL statement where I would like to pass a list of tuplets idSubCustomerPairs so that I can fetch all the rows matching the where clause from the table customers:

This is the SQL for it:

@Language("PostgreSQL")
private const val getExistingRowsSql = """
  select id, subcustomer
  from customer
  where (uuid IN (:uuids) AND is_deleted = false)
  union
  select id, subcustomer
  from customer
  where (id, subcustomer) IN (:idSubCustomerPairs) AND is_deleted = false
"""

I have a list of customers from which I am building a list of Pair values for idSubCustomerPairs. It looks like this:

val existingCustomers = fetchRows(
      ctx, getExistingRowsSql, mapOf(
      "uuids" to customers.map { it["uuid"] },
      "idSubCustomerPairs" to customers.map { Pair(it["id"], it["subCustomer"]) }
  ))

And I have a function where I convert values to jdbcValues:

fun jdbcConvertValue(session: Session, v: Any?): Any? =
    when (v) {
      is Collection<*> -> {
        when (val type = v.firstOrNull()) {
          null -> null
          is String -> session.connection.underlying.createArrayOf("text", v.toTypedArray())
          is Long -> session.connection.underlying.createArrayOf("bigint", v.toTypedArray())
          is Int -> session.connection.underlying.createArrayOf("bigint", v.toTypedArray())
          is UUID -> session.connection.underlying.createArrayOf("uuid", v.toTypedArray())
          is Pair<*, *> -> session.connection.underlying.createArrayOf("pair", v.toTypedArray())
          else -> throw Exception("You need to map your array type $type ${type.javaClass}")
        }
      }
      else -> v
    }

But, when I try to fetch rows like that I get an error:

org.postgresql.util.PSQLException: Unable to find server array type for provided name pair.

I am not sure how to pass values as tuples into a SQL statement?



from Recent Questions - Stack Overflow https://ift.tt/3DhVwLK
https://ift.tt/eA8V8J

No comments:

Post a Comment