Skip to content

Instantly share code, notes, and snippets.

@ghadishayban
Created September 8, 2014 18:10
Show Gist options
  • Save ghadishayban/d2f31961deba98ee4595 to your computer and use it in GitHub Desktop.
Save ghadishayban/d2f31961deba98ee4595 to your computer and use it in GitHub Desktop.
result sets
(defn result-set
[^ResultSet rs & {:keys [identifiers]
:or {identifiers str/lower-case}}]
(reify clojure.lang.IReduce
(reduce [this f]
(reduce [this f (f)]))
(reduce [this f init]
(let [rsmeta (.getMetaData rs)
idxs (range 1 (inc (.getColumnCount rsmeta)))
col (fn [^Integer i]
(.getColumnLabel rsmeta i))
val (fn [^Integer i]
(-> rs
(.getObject i)
(result-set-read-column rsmeta i)))
keys (->> (map col idxs)
make-cols-unique
(map (comp keyword identifiers)))]
(loop [ret init]
(if (.next rs)
(let [ret (f ret (zipmap keys (map val idxs)))]
(if (reduced? ret)
@ret
(recur ret)))
ret))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment