-
-
Save toby/113814480a4e79d186c9 to your computer and use it in GitHub Desktop.
This file contains 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
(defn with-lazy-results* [chunksize query func] | |
(let [sql (:sql-str query) | |
params (:params query)] | |
(with-open [conn (.getConnection (:datasource (or (when-let [db (:db query)] | |
(get-connection db)) | |
(jdbc/find-connection) | |
(get-connection @_default))))] | |
(let [initial-autocommit (.getAutoCommit conn)] | |
(try | |
(.setAutoCommit conn false) | |
(let [statement (jdbc/prepare-statement conn | |
sql | |
:fetch-size chunksize)] | |
(jdbc/with-query-results* (apply vector statement params) func)) | |
(catch Exception e (throw e)) | |
(finally (.setAutoCommit conn initial-autocommit))))))) | |
(defmacro with-lazy-results [results chunksize query & body] | |
`(with-lazy-results* ~chunksize (kc/query-only ~query) (fn [~results] ~@body))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment