Frank Adrian
Frank Adrian

Reputation: 11

Using :row-fn failing in clojure.jdbc query call

This code works, printing the rows in the given table:

(defn count-extreferences-subset [config]
  (let [emr-dbs (:emr-databases config)]
    (println "Counting external references: " emr-dbs)
    (jdbc/with-db-connection [dbconn (:db-spec (first emr-dbs))]
                             (let [q "SELECT * FROM LOCREG"
                                    rs (jdbc/query dbconn [q])]
                               (dorun (map println rs))))))

According to the documentation in clojure.jdbc, this should also work, but should print the rows as the result set is realized (preventing memory overflow for large result sets):

(defn count-extreferences-subset [config]
  (let [emr-dbs (:emr-databases config)]
    (println "Counting external references: " emr-dbs)
    (jdbc/with-db-connection [dbconn (:db-spec (first emr-dbs))]
                             (let [q "SELECT * FROM LOCREG"
                                   _ (jdbc/query dbconn [q] {:row-fn println})]))))

However this fails at run-time with the following exception:

java.lang.IllegalArgumentException: No value supplied for key: {:row-fn #object[clojure.core$println 0x46ed7a0e "clojure.core$println@46ed7a0e"]}

Any idea why the use of the :row-fn option is failing?

Upvotes: 0

Views: 460

Answers (1)

Alan Thompson
Alan Thompson

Reputation: 29958

I believe the curly braces are the problem. Your code should follow the following pattern:

(jdbc/query db-spec 
  ["select name, cost from fruit where cost = 12"]
  :row-fn add-tax)

You can fine more information in The Clojure Cookbook. I highly recommend buying a copy!

Upvotes: 0

Related Questions