对于已弃用的“带有查询结果”的替换是什么?函数在clojure / java.jdbc 0.3.0? [英] What is the replacement for the deprecated "with-query-results" function in clojure/java.jdbc 0.3.0?

查看:156
本文介绍了对于已弃用的“带有查询结果”的替换是什么?函数在clojure / java.jdbc 0.3.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

我一直在使用 with-query-results,但是现在已经过时了,那么什么是直接替换函数来获得与此相同的行为呢?

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

我知道不再需要连接,但之后,可用的文档对我来说是不清楚的。 p>

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

推荐答案

现在,通过两个新的命名参数,可以在顶级java.jdbc函数的范围内处理结果集::row-fn :result-set-fn 。第一个转换每行,第二个转换行的集合。如果:result-set-fn 返回延迟序列,那么在以后使用它时,将会获得connection或resultset关闭异常。默认的:result-set-fn doall 。当使用自己的,确保它实现。

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)

这篇关于对于已弃用的“带有查询结果”的替换是什么?函数在clojure / java.jdbc 0.3.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆