Last active
September 7, 2019 09:37
-
-
Save jjwatt/ef731c51b6329d703215 to your computer and use it in GitHub Desktop.
core.async database query fn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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