sql

2024-07-20T23:46:08.487139Z

I have created some custom enum types on my PSQL, I want to parse only them with it values as Keywords, is there any way to do using extend-protocol rs/ReadableColumn for an java.sql.Array of my type?

seancorfield 2024-07-21T00:40:45.916159Z

ReadableColumn is how you turn SQL types into Java (or Clojure) types. You could extend it to java.sql.Array if that's how your array of category value comes back from the database. You'd need to check the rs metadata, I think, to determine whether it was the type you wanted?

seancorfield 2024-07-21T00:41:28.236429Z

Also, can you try to post a single message in-channel with more detail in threads (so it's obvious where the discussion is and where folks should answer). Thanks.

👍 1
2024-07-21T00:42:49.679489Z

CREATE TYPE category AS ENUM ('a', 'b', 'c');

(defn ks->category
  [db-conn ks]
  (.createArrayOf db-conn
                "category"
                (into-array String (map name ks))))
I'm using this to serialize my keys to PSQL

2024-07-21T00:44:05.103629Z

I've extended the protocol using:

(extend-protocol rs/ReadableColumn
  Array
  (read-column-by-label [^Array v _]
    (vec (.getArray v)))
  (read-column-by-index [^Array v _ _]
    (vec (.getArray v))))

2024-07-21T00:44:19.557639Z

I'll try to check the metadata on v, is that right?

seancorfield 2024-07-21T01:14:04.364489Z

read-column-by-index is passed rsmeta as the second argument (the index is the third argument).

seancorfield 2024-07-21T01:15:03.067959Z

With read-column-by-label, there is no rsmeta available so you'd have to rely on checking the label (second argument) to see if it matches the column name (but of course other things could have the same label, via aliases).

seancorfield 2024-07-21T01:15:31.976649Z

However, if you want all java.sql.Array types to return as vectors (of something) then what you have may be enough.

seancorfield 2024-07-21T01:16:51.942279Z

(`rsmeta` is a ResultSetMetaData object, in case the name isn't obvious enough)

2024-07-21T01:37:01.830449Z

It worked 🙂

2024-07-21T01:37:04.602499Z

thanks @seancorfield

2024-07-21T01:37:44.452899Z

(read-column-by-index [^Array v rs idx]
  (cond (= "a" (.getTableName rs idx))
        (condp = (.getColumnName rs idx)
          "a" (mapv keyword (vec (.getArray v))))
        :else (vec (.getArray v))))

2024-07-21T01:37:51.933749Z

something like this is helpful

👍🏻 1
seancorfield 2024-07-21T03:17:25.105779Z

I'd have probably gone with:

(read-column-by-index [^Array v rs idx]
  (cond->> (vec (.getArray v))
    (and (= "a" (.getTableName rs idx)) (= "a" (.getColumnName rs idx)))
    (mapv keyword)))
but, yeah, that works.