What is the replacement for the deprecated “with-q

2019-05-13 05:12发布

问题:

I am migrating my code that currently uses clojure/java.jdbc 0.2.0 to the new 0.3.0 API.

There are a lot of newly deprecated functions in the 0.3.0 API.

I have been using "with-query-results" extensively but this is now deprecated, so what is the direct replacement function to use to get equivalent behaviour to this?

(defn get-user [username]
  (jdbc/with-connection db
    (jdbc/with-query-results results
      ["select username, password, roles from users where username = ?" username]
      (cond
        (empty? results)
          nil
        :else
          (first results)))))

I know that "with-connection" is no longer needed, but after that the available documentation is very unclear to me.

回答1:

Processing resultsets can now be done within the scope of the top level java.jdbc functions through two new named parameters: :row-fn and :result-set-fn. The first transforms each row, the second the collection of rows. If the :result-set-fn returns a lazy sequence, you will get a connection or resultset closed exception when using it later. The default :result-set-fn is doall. When using your own, make sure it is realized.

(query db
       ["select firstname, lastname from users where username = ?" username]
       :row-fn #(str (% :firstname) \space (% :lastname))
       :resultset-fn first)


回答2:

I assume you want something like:

(let [results (jdbc/query db ["select ..." ...] :as-arrays? true)]
  ...)

See here or in the respective documentation.



标签: jdbc clojure