Skip to content

Instantly share code, notes, and snippets.

@toby
Created September 6, 2013 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toby/113814480a4e79d186c9 to your computer and use it in GitHub Desktop.
Save toby/113814480a4e79d186c9 to your computer and use it in GitHub Desktop.
(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