First steps to multiple result sets #116

This commit is contained in:
Sean Corfield 2020-06-06 09:32:03 -07:00
parent f5f05ad537
commit 0a27e51f37
2 changed files with 33 additions and 4 deletions

View file

@ -575,6 +575,18 @@
(.getGeneratedKeys stmt)
(catch Exception _)))))
(defn- stmt->result-set'
"Given a `PreparedStatement` and options, execute it and return a `ResultSet`
if possible."
^ResultSet
[^PreparedStatement stmt go opts]
(if go
(.getResultSet stmt)
(when (:return-keys opts)
(try
(.getGeneratedKeys stmt)
(catch Exception _)))))
(defn- reduce-stmt
"Execute the `PreparedStatement`, attempt to get either its `ResultSet` or
its generated keys (as a `ResultSet`), and reduce that using the supplied
@ -687,9 +699,26 @@
(first sql-params)
(rest sql-params)
opts)]
(if (:multi-rs opts)
(loop [go (.execute stmt) acc nil rsn 0]
(let [rs (if-let [rs (stmt->result-set' stmt go opts)]
(datafiable-result-set rs this opts)
(let [n (.getUpdateCount stmt)]
(if (= -1 n)
nil
[{:next.jdbc/update-count (.getUpdateCount stmt)}])))]
(if-not rs
acc
(recur (.getMoreResults stmt)
(if acc
(-> acc
(conj {:next.jdbc/result-set rsn})
(into rs))
rs)
(inc rsn)))))
(if-let [rs (stmt->result-set stmt opts)]
(datafiable-result-set rs this opts)
[{:next.jdbc/update-count (.getUpdateCount stmt)}])))
[{:next.jdbc/update-count (.getUpdateCount stmt)}]))))
java.sql.PreparedStatement
;; we can't tell if this PreparedStatement will return generated

View file

@ -315,10 +315,10 @@ VALUES ('Pear', 'green', 49, 47)
(when (stored-proc?)
(testing "stored proc; multiple result sets"
(try
(println "====" (:dbtype (db)) "====")
(clojure.pprint/pprint
(jdbc/execute! (ds) [(if (mssql?)
"EXEC FRUITP"
"CALL FRUITP()")]))
(jdbc/execute! (ds) [(if (mssql?) "EXEC FRUITP" "CALL FRUITP()")]
{:multi-rs true}))
(catch Throwable t
(println 'call-proc (:dbtype (db)) (ex-message t) (some-> t (ex-cause) (ex-message))))))))