Address #2 by adding rs/as-arrays

This commit is contained in:
Sean Corfield 2019-04-01 22:19:02 -07:00
parent 81be787316
commit f217e86038
2 changed files with 28 additions and 2 deletions

View file

@ -31,6 +31,10 @@
(.getColumnLabel rsmeta i)))
idxs))))
(defprotocol ColumnarResultSet
(column-names [this])
(row-values [this]))
(defn- mapify-result-set
"Given a result set, return an object that wraps the current row as a hash
map. Note that a result set is mutable and the current row will change behind
@ -76,7 +80,27 @@
(seq (mapv (fn [^Integer i]
(clojure.lang.MapEntry. (nth @cols (dec i))
(.getObject rs i)))
(range 1 (inc (count @cols)))))))))
(range 1 (inc (count @cols))))))
ColumnarResultSet
(column-names [this] @cols)
(row-values [this]
(mapv (fn [^Integer i] (.getObject rs i))
(range 1 (inc (count @cols))))))))
(defn as-arrays
"A reducing function that can be used on a result set to produce an
array-based representation, where the first element is a vector of the
column names in the result set, and subsequent elements are vectors of
the rows from the result set.
It should be used with a nil initial value:
(reduce rs/as-arrays nil (reducible! con sql-params))"
[result rs-map]
(if result
(conj result (row-values rs-map))
(conj [(column-names rs-map)] (row-values rs-map))))
(defn- reduce-stmt
"Execute the PreparedStatement, attempt to get either its ResultSet or

View file

@ -149,4 +149,6 @@
(execute! con ["select * from fruit where name = ?" "Pear"])
(delete! con :fruit {:id 1})
(update! con :fruit {:appearance "Brown"} {:name "Banana"}))
(update! con :fruit {:appearance "Brown"} {:name "Banana"})
(reduce rs/as-arrays nil (reducible! con ["select * from fruit"])))