4

如何使用Clojure调用一个以字符串和日期为输入并返回一个refcursor作为输出的Oracle存储过程?

variable rc refcursor;
exec SOMEDB.PKG.GETPOSITIONS('ABC', to_date('2012-02-07','yyyy-mm-dd'), :rc)
print rc
4

1 回答 1

4

我就是这样做的。我已经包含了一些fn我用来将字符串转换为 SQL 日期的 helper。

(ns foo
  (:require [clojure.java.jdbc :as jdbc]
            [clojure.string :as cs]))

(def conn
  {:classname "oracle.jdbc.OracleDriver"
   :subprotocol "oracle:thin"
   :subname "@//host.name.here:port.here/db.here"
   :user "user"
   :password "pass"})

(defonce ORACLE_CURSOR oracle.jdbc.driver.OracleTypes/CURSOR)

(defn date-to-sql-date [date]
  (java.sql.Date. (.getTimeInMillis
                    (java.util.GregorianCalendar.
                      (+ (.getYear date) 1900) (.getMonth date) (.getDate date)))))

(defn string-to-sql-date [date]
  ;; assumes American date formats
  (if-not (empty? date)
    (date-to-sql-date (java.util.Date. (cs/replace date #"-" "/")))))

(jdbc/with-connection conn
  (with-open [stmt (.prepareCall (jdbc/connection) "{ call SOMEDB.PKG.GETPOSITIONS(?, ?, ?) }")]
    (doto stmt
      (.setString 1 "ABC")
      (.setDate 2 (string-to-sql-date "2012-02-07"))
      (.registerOutParameter 3 ORACLE_CURSOR)
      (.execute))
    (resultset-seq (. stmt getObject 3)))) ;; lazy-sequence

结果集序列

注意:如果您打算从 a 返回它,您可能需要消耗整个序列,fn因为连接将在fn返回后立即关闭。

于 2012-12-18T18:31:38.187 回答