make execute-batch! work for with-logging

This commit is contained in:
Sean Corfield 2021-12-23 22:00:09 -08:00
parent c4df28bfe8
commit 21a520a8d8
4 changed files with 41 additions and 8 deletions

View file

@ -4,6 +4,7 @@ Only accretive/fixative changes will be made from now on.
* 1.2.next in progress
* Address #193 by expanding the argument specs for `get-datasource` and `get-connection`.
* Streamline `execute-batch!` for `with-options` and `with-logging` (and this should generalize to any wrapper that satisfies `Connectable` and stores the actual `Connection` under the `:connectable` key).
* Update log4j2 test dependency.
* Update `build-clj` to v0.6.5.

View file

@ -332,7 +332,8 @@
params)))
([connectable sql param-groups opts]
(if (or (instance? java.sql.Connection connectable)
(opts/wrapped-connection? connectable))
(and (satisfies? p/Connectable connectable)
(instance? java.sql.Connection (:connectable connectable))))
(with-open [ps (prepare connectable [sql] opts)]
(execute-batch! ps param-groups opts))
(with-open [con (get-connection connectable)]

View file

@ -8,13 +8,6 @@
(defrecord DefaultOptions [connectable options])
(defn wrapped-connection?
"Used internally to detect that a connectable is wrapped
in options and contains a Connection object."
[connectable]
(and (instance? DefaultOptions connectable)
(instance? java.sql.Connection (:connectable connectable))))
(extend-protocol p/Sourceable
DefaultOptions
(get-datasource [this]

View file

@ -614,6 +614,44 @@ INSERT INTO fruit (name, appearance) VALUES (?,?)
(finally
(jdbc/execute-one! (ds) ["delete from fruit where id > 4"])))))
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))
(testing "batch with-options"
(is (= [1 1 1 1 1 1 1 1 1 13]
(try
(let [result (jdbc/execute-batch! (jdbc/with-options (ds) {})
"INSERT INTO fruit (name, appearance) VALUES (?,?)"
[["fruit1" "one"]
["fruit2" "two"]
["fruit3" "three"]
["fruit4" "four"]
["fruit5" "five"]
["fruit6" "six"]
["fruit7" "seven"]
["fruit8" "eight"]
["fruit9" "nine"]]
{})]
(conj result (count (jdbc/execute! (ds) ["select * from fruit"]))))
(finally
(jdbc/execute-one! (ds) ["delete from fruit where id > 4"])))))
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))
(testing "batch with-logging"
(is (= [1 1 1 1 1 1 1 1 1 13]
(try
(let [result (jdbc/execute-batch! (jdbc/with-logging (ds) println println)
"INSERT INTO fruit (name, appearance) VALUES (?,?)"
[["fruit1" "one"]
["fruit2" "two"]
["fruit3" "three"]
["fruit4" "four"]
["fruit5" "five"]
["fruit6" "six"]
["fruit7" "seven"]
["fruit8" "eight"]
["fruit9" "nine"]]
{})]
(conj result (count (jdbc/execute! (ds) ["select * from fruit"]))))
(finally
(jdbc/execute-one! (ds) ["delete from fruit where id > 4"])))))
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))
(testing "small batch insert"
(is (= [1 1 1 1 1 1 1 1 1 13]
(try