Clean up stmt-sql return-keys again

This commit is contained in:
Sean Corfield 2020-06-26 23:04:27 -07:00
parent d3b51f9cc6
commit c4430abe49

View file

@ -642,13 +642,12 @@
"Given a `Statement`, a SQL command, and options, execute it and return a "Given a `Statement`, a SQL command, and options, execute it and return a
`ResultSet` if possible." `ResultSet` if possible."
^ResultSet ^ResultSet
[^Statement stmt ^String sql opts] [^Statement stmt ^String sql]
(if (.execute stmt sql) (if (.execute stmt sql)
(.getResultSet stmt) (.getResultSet stmt)
(when (:return-keys opts) (try
(try (.getGeneratedKeys stmt)
(.getGeneratedKeys stmt) (catch Exception _))))
(catch Exception _)))))
(defn- reduce-stmt-sql (defn- reduce-stmt-sql
"Execute the SQL command on the given `Statement`, attempt to get either "Execute the SQL command on the given `Statement`, attempt to get either
@ -659,7 +658,7 @@
a hash map containing `:next.jdbc/update-count` and the number of rows a hash map containing `:next.jdbc/update-count` and the number of rows
updated, with the supplied function and initial value applied." updated, with the supplied function and initial value applied."
[^Statement stmt sql f init opts] [^Statement stmt sql f init opts]
(if-let [rs (stmt-sql->result-set stmt sql opts)] (if-let [rs (stmt-sql->result-set stmt sql)]
(let [rs-map (mapify-result-set rs opts)] (let [rs-map (mapify-result-set rs opts)]
(loop [init' init] (loop [init' init]
(if (.next rs) (if (.next rs)
@ -679,7 +678,7 @@
a hash map containing `:next.jdbc/update-count` and the number of rows a hash map containing `:next.jdbc/update-count` and the number of rows
updated, and fold that as a single element collection." updated, and fold that as a single element collection."
[^Statement stmt sql n combinef reducef connectable opts] [^Statement stmt sql n combinef reducef connectable opts]
(if-let [rs (stmt-sql->result-set stmt sql opts)] (if-let [rs (stmt-sql->result-set stmt sql)]
(let [rs-map (mapify-result-set rs opts) (let [rs-map (mapify-result-set rs opts)
chunk (fn [batch] (#'r/fjtask #(r/reduce reducef (combinef) batch))) chunk (fn [batch] (#'r/fjtask #(r/reduce reducef (combinef) batch)))
realize (fn [row] (datafiable-row row connectable opts))] realize (fn [row] (datafiable-row row connectable opts))]
@ -814,27 +813,22 @@
[{:next.jdbc/update-count (.getUpdateCount this)}])) [{:next.jdbc/update-count (.getUpdateCount this)}]))
java.sql.Statement java.sql.Statement
;; we can't tell if this Statement will return generated
;; keys so we pass a truthy value to at least attempt it if we
;; do not get a ResultSet back from the execute call
(-execute [this sql-params opts] (-execute [this sql-params opts]
(assert (= 1 (count sql-params)) (assert (= 1 (count sql-params))
"Parameters cannot be provided when executing a non-prepared Statement") "Parameters cannot be provided when executing a non-prepared Statement")
(reify (reify
clojure.lang.IReduceInit clojure.lang.IReduceInit
(reduce [_ f init] (reduce [_ f init]
(reduce-stmt-sql this (first sql-params) f init (reduce-stmt-sql this (first sql-params) f init opts))
(assoc opts :return-keys true)))
r/CollFold r/CollFold
(coll-fold [_ n combinef reducef] (coll-fold [_ n combinef reducef]
(fold-stmt-sql this (first sql-params) n combinef reducef (fold-stmt-sql this (first sql-params) n combinef reducef
(.getConnection this) (.getConnection this) opts))
(assoc opts :return-keys true)))
(toString [_] "`IReduceInit` from `plan` -- missing reduction?"))) (toString [_] "`IReduceInit` from `plan` -- missing reduction?")))
(-execute-one [this sql-params opts] (-execute-one [this sql-params opts]
(assert (= 1 (count sql-params)) (assert (= 1 (count sql-params))
"Parameters cannot be provided when executing a non-prepared Statement") "Parameters cannot be provided when executing a non-prepared Statement")
(if-let [rs (stmt-sql->result-set this (first sql-params) (assoc opts :return-keys true))] (if-let [rs (stmt-sql->result-set this (first sql-params))]
(let [builder-fn (get opts :builder-fn as-maps) (let [builder-fn (get opts :builder-fn as-maps)
builder (builder-fn rs opts)] builder (builder-fn rs opts)]
(when (.next rs) (when (.next rs)
@ -844,7 +838,7 @@
(-execute-all [this sql-params opts] (-execute-all [this sql-params opts]
(assert (= 1 (count sql-params)) (assert (= 1 (count sql-params))
"Parameters cannot be provided when executing a non-prepared Statement") "Parameters cannot be provided when executing a non-prepared Statement")
(if-let [rs (stmt-sql->result-set this (first sql-params) opts)] (if-let [rs (stmt-sql->result-set this (first sql-params))]
(datafiable-result-set rs (.getConnection this) opts) (datafiable-result-set rs (.getConnection this) opts)
[{:next.jdbc/update-count (.getUpdateCount this)}])) [{:next.jdbc/update-count (.getUpdateCount this)}]))