Skip to content

Instantly share code, notes, and snippets.

@jjwatt
Last active September 7, 2019 09:37
Show Gist options
  • Save jjwatt/ef731c51b6329d703215 to your computer and use it in GitHub Desktop.
Save jjwatt/ef731c51b6329d703215 to your computer and use it in GitHub Desktop.
core.async database query fn
(ns
#^{:author "Jesse Wattenbarger"
:doc "pulled out of a production program for gist."}
coolquery.core
(:gen-class)
(:require [clojure.java.io :as io]
[clojure.core.async :as a :refer
[chan go go-loop close! <!! <! >! >!!]]
[jdbc.core :as jdbc] ;; good jdbc interop
))
(defn q>!!
"Run a query in a future with a lazy result set and return a channel with the results.
Tries to use server-side cursors via 'with-query' to pull results lazily. Will block
after it fills up the channel and unblock once the channel has been drained so as not
to realize all of the result set in memory.
By default, politely closes the channel once [results] has been
exhausted. Leave the channel open if :close? is false. By default,
create and return an unbuffered channel.
It can also take an existing channel or buffer size, wherein it
will use that channel or a newly created buffered channel for results."
;; Yep, all this ^^^ functionality in 11 (really, 10) lines of clojure. Beat that, golang...
([dbspec query]
(q>!! dbspec query (chan)))
([dbspec query c-or-n & {:keys [close?] :or {close? true} :as opts}]
(let [channel (if (number? c-or-n) (chan c-or-n) c-or-n)]
(future
(jdbc/with-connection [conn dbspec]
(jdbc/with-query conn results query
(doseq [x results]
(>!! channel x))))
(when close? (close! channel)))
channel)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment