diff --git a/CHANGELOG.md b/CHANGELOG.md index 4704bb8..2fa2df4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/next/jdbc.clj b/src/next/jdbc.clj index 4a74a9d..afe2105 100644 --- a/src/next/jdbc.clj +++ b/src/next/jdbc.clj @@ -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)] diff --git a/src/next/jdbc/default_options.clj b/src/next/jdbc/default_options.clj index 111f87e..89f9d82 100644 --- a/src/next/jdbc/default_options.clj +++ b/src/next/jdbc/default_options.clj @@ -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] diff --git a/test/next/jdbc_test.clj b/test/next/jdbc_test.clj index a4d9f8e..0bb69fc 100644 --- a/test/next/jdbc_test.clj +++ b/test/next/jdbc_test.clj @@ -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