【问题标题】:What is the replacement for the deprecated "with-query-results" function in clojure/java.jdbc 0.3.0?clojure/java.jdbc 0.3.0 中已弃用的“with-query-results”函数的替代品是什么?
【发布时间】:2013-12-02 19:54:40
【问题描述】:

我正在将当前使用 clojure/java.jdbc 0.2.0 的代码迁移到新的 0.3.0 API。

0.3.0 API 中有很多新弃用的函数。

我一直在广泛使用“with-query-results”,但现在已弃用,那么用于获得与此等效行为的直接替换函数是什么?

(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)))))

我知道不再需要“with-connection”,但在那之后可用的文档对我来说非常不清楚。

【问题讨论】:

    标签: jdbc clojure


    【解决方案1】:

    现在可以在顶级 java.jdbc 函数的范围内通过两个新的命名参数处理结果集::row-fn:result-set-fn。第一个转换每一行,第二个转换行的集合。如果:result-set-fn 返回一个惰性序列,那么稍后使用它时会出现连接或结果集关闭异常。默认:result-set-fndoall。使用您自己的时,请确保已实现。

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

    【讨论】:

      【解决方案2】:

      我假设你想要这样的东西:

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

      herein the respective documentation

      【讨论】:

        猜你喜欢
        • 2016-05-25
        • 2019-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-17
        • 2023-03-30
        • 2014-05-14
        • 2021-05-26
        相关资源
        最近更新 更多