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?
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?
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.
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 PSQLI'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))))I'll try to check the metadata on v, is that right?
read-column-by-index is passed rsmeta as the second argument (the index is the third argument).
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).
However, if you want all java.sql.Array types to return as vectors (of something) then what you have may be enough.
(`rsmeta` is a ResultSetMetaData object, in case the name isn't obvious enough)
It worked 🙂
thanks @seancorfield
(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))))
something like this is helpful
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.