3

I have an SQL statement where I would like to pass a list of tuples customerIdSubCustomerPairs 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 customerId, subcustomer
  from customer
  where (uuid IN (:uuids) AND is_deleted = false)
  union
  select customerId, subcustomer
  from customer
  where (customerId, subcustomer) IN (:customerIdSubCustomerPairs) AND is_deleted = false
"""

I have a list of customers from which I am building a list of lists for customerIdSubCustomerPairs. It looks like this:

val existingCustomers = fetchRows(
      ctx, getExistingRowsSql, mapOf(
      "uuids" to customers.map { it["uuid"] },
      "customerIdSubCustomerPairs" to customers.map { listOf(it["customerId"] as String, it["subCustomer"] as String) }
  ))

And I have a function where I convert values to Oid types:

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 List<*> -> v.map { jdbcConvertValue(session, it) }.toTypedArray()
          else -> throw Exception("You need to map your array type $type ${type.javaClass}")
        }
      }
      else -> v
    }

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

org.postgresql.util.PSQLException: Cannot cast an instance of [Ljava.lang.Object; to type Types.ARRAY

I am not sure how to pass values as tuples into a SQL statement? The fields customerId and subcustomer are both text columns in the DB table.

Leff
  • 1,968
  • 24
  • 97
  • 201
  • 1
    (a) What are the data types of the columns `id` and `subcustomer` in table customer ? (b) Can you display a concrete example of `idSubCustomerPairs` and which leads to a postgres error ? – Edouard Dec 05 '21 at 22:21
  • a) they are both text fields, b) [["HGK", "Hong Kong"], ["BE", "Berlin"]]. Hope that helps @Edouard, let me know if something is unclear – Leff Dec 05 '21 at 22:41
  • Id was maybe be confusing, since it is not the primary key field, it was just to show it as an example, I have edited the question to reflect the situation as it is. The fields are customerId and subcustomer which are both text fields @Edouard – Leff Dec 05 '21 at 22:47
  • the problem may come from the format of `idSubCustomerPairs`. Can you test two solutions : (a) generate `idSubCustomerPairs = {{"HGK", "Hong Kong"}, {"BE", "Berlin"}}` and modify your sql query with `... where array[customerId, subcustomer] <@ (:customerIdSubCustomerPairs) AND ...` (b) generate `idSubCustomerPairs = (('HGK', 'Hong Kong'), ('BE', 'Berlin'))` and keep your sql query as it is. – Edouard Dec 05 '21 at 23:44
  • @Edouard how do you generate this kind of pairs, I used map to generate the list of lists, but how do you generate something like ```idSubCustomerPairs = {{"HGK", "Hong Kong"}, {"BE", "Berlin"}}``` or ```idSubCustomerPairs = (('HGK', 'Hong Kong'), ('BE', 'Berlin'))```, could you write an example? – Leff Dec 06 '21 at 07:17
  • Well, I'm like a newbie in java, so I don't have any relevant java solution yet. However for debug purpose, I can propose you a sql solution so that to check if the problem really comes from the idSubCustomerPairs format : `... WHERE array[customerId, subcustomer] <@ translate(:customerIdSubCustomerPairs,'[]','{}' ) :: text[] AND ...`. Could you set up a [dbfiddle](https://dbfiddle.uk/?rdbms=postgres_14) environment with a simplified version of your `customer` table and a sample of inserted rows, so that I can test some solutions on postgres side instead of providing you blind ideas ? – Edouard Dec 06 '21 at 08:54

0 Answers0