As far as I can tell, contrib.sql's functions do not use database
cursors (at least for PostgreSQL, again as far as I can tell). For
result sets that are larger than you'd like to load into the running
process, but rather step through the results and have them fetched on
demand from the server, I've tried creating a version of
with-query-results that does this:
(ns db-utils
(:require [clojure.contrib.sql :as sql]))
(def *default-fetch-size* 50)
(defn with-query-results-cursor [[sql & params :as sql-params] func]
(sql/transaction
(with-open [stmt (.prepareStatement (sql/connection) sql)]
(doseq [[index value] (map vector (iterate inc 1) params)]
(.setObject stmt index value))
(.setFetchSize stmt *default-fetch-size*)
(with-open [rset (.executeQuery stmt)]
(func (resultset-seq rset))))))
;; example usage:
(sql/with-connection db-conn-info
(with-query-results-cursor ["SELECT * FROM table_name"]
(fn [rs]
(doseq [rec rs]
(printf "rec: %s\n" rec)))))
The Postgres documentation states that auto-commit must be off and the
fetch size must be non-zero for the cursor to work correctly. Is
there any interest in folding this into contrib.sql?
Best Regards,
Kyle Burton
--
Twitter: @kyleburton
Blog: http://asymmetrical-view.com/
Fun: http://snapclean.me/
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en