fixes #181 for logging

Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2025-02-14 13:38:03 -08:00
parent 9ed335dc8d
commit 22e96dcb84
No known key found for this signature in database
3 changed files with 132 additions and 65 deletions

View file

@ -336,14 +336,11 @@
result)))) result))))
params))) params)))
([connectable sql param-groups opts] ([connectable sql param-groups opts]
(let [conn (p/unwrap connectable)] (if (instance? java.sql.Connection (p/unwrap connectable))
(if (instance? java.sql.Connection conn) (with-open [ps (prepare connectable [sql] opts)]
(with-open [ps (prepare conn [sql] (if-let [opts' (:options connectable)] (execute-batch! ps param-groups opts))
(merge opts' opts) (with-open [con (get-connection connectable)]
opts))] (execute-batch! con sql param-groups opts)))))
(execute-batch! ps param-groups opts))
(with-open [con (get-connection connectable)]
(execute-batch! con sql param-groups opts))))))
(defmacro on-connection (defmacro on-connection
"Given a connectable object, gets a connection and binds it to `sym`, "Given a connectable object, gets a connection and binds it to `sym`,

View file

@ -110,7 +110,7 @@
:opts (s/? ::opts-map))) :opts (s/? ::opts-map)))
(s/fdef jdbc/prepare (s/fdef jdbc/prepare
:args (s/cat :connection ::connection :args (s/cat :connection ::proto-connectable
:sql-params ::sql-params :sql-params ::sql-params
:opts (s/? ::opts-map))) :opts (s/? ::opts-map)))

View file

@ -732,62 +732,132 @@ INSERT INTO fruit (name, appearance) VALUES (?,?)
(jdbc/execute-one! (ds) [(str "delete from fruit where " (index) " > 4")]))))) (jdbc/execute-one! (ds) [(str "delete from fruit where " (index) " > 4")])))))
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))) (is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))
(testing "batch with-logging" (testing "batch with-logging"
(is (= [1 1 1 1 1 1 1 1 1 13] (let [tracker (atom {:opts 0, :log1 0 :log2 0})
(try ;; opts and log2 never get invoked with batch/prepare -- because
(let [result (jdbc/execute-batch! (jdbc/with-logging (ds) println println) ;; no fn opts are invoked on that path, and no post-logging is done:
"INSERT INTO fruit (name, appearance) VALUES (?,?)" track-fn (fn ([k] (fn [& _] (swap! tracker update k inc)))
[["fruit1" "one"] ([k f] (fn [& args] (swap! tracker update k inc) (apply f args))))]
["fruit2" "two"] (is (= {:opts 0, :log1 0 :log2 0} @tracker))
["fruit3" "three"] (is (= [1 1 1 1 1 1 1 1 1 13]
["fruit4" "four"] (try
["fruit5" "five"] ;; wrapping a non-connection will lose the wrapper:
["fruit6" "six"] (let [result (jdbc/execute-batch! (jdbc/with-options (ds) {:builder-fn (track-fn :opts rs/as-maps)})
["fruit7" "seven"] "INSERT INTO fruit (name, appearance) VALUES (?,?)"
["fruit8" "eight"] [["fruit1" "one"]
["fruit9" "nine"]] ["fruit2" "two"]
{})] ["fruit3" "three"]
(conj result (count (jdbc/execute! (ds) ["select * from fruit"])))) ["fruit4" "four"]
(finally ["fruit5" "five"]
(jdbc/execute-one! (ds) [(str "delete from fruit where " (index) " > 4")]))))) ["fruit6" "six"]
(is (= [1 1 1 1 1 1 1 1 1 13] ["fruit7" "seven"]
(try ["fruit8" "eight"]
(let [result (jdbc/execute-batch! (jdbc/with-options ["fruit9" "nine"]]
(jdbc/with-logging (ds) println println) {})]
{:ignore "me"}) (conj result (count (jdbc/execute! (ds) ["select * from fruit"]))))
"INSERT INTO fruit (name, appearance) VALUES (?,?)" (finally
[["fruit1" "one"] (jdbc/execute-one! (ds) [(str "delete from fruit where " (index) " > 4")])))))
["fruit2" "two"] (is (= {:opts 0, :log1 0 :log2 0} @tracker))
["fruit3" "three"] (is (= [1 1 1 1 1 1 1 1 1 13]
["fruit4" "four"] (try
["fruit5" "five"] ;; wrapping a non-connection will lose the wrapper:
["fruit6" "six"] (let [result (jdbc/execute-batch! (jdbc/with-logging (ds) {:builder-fn (track-fn :opts rs/as-maps)})
["fruit7" "seven"] "INSERT INTO fruit (name, appearance) VALUES (?,?)"
["fruit8" "eight"] [["fruit1" "one"]
["fruit9" "nine"]] ["fruit2" "two"]
{})] ["fruit3" "three"]
(conj result (count (jdbc/execute! (ds) ["select * from fruit"])))) ["fruit4" "four"]
(finally ["fruit5" "five"]
(jdbc/execute-one! (ds) [(str "delete from fruit where " (index) " > 4")]))))) ["fruit6" "six"]
(is (= [1 1 1 1 1 1 1 1 1 13] ["fruit7" "seven"]
(try ["fruit8" "eight"]
(let [result (jdbc/execute-batch! (jdbc/with-logging ["fruit9" "nine"]]
(jdbc/with-options (ds) {:ignore "me"}) {})]
println println) (conj result (count (jdbc/execute! (ds) ["select * from fruit"]))))
"INSERT INTO fruit (name, appearance) VALUES (?,?)" (finally
[["fruit1" "one"] (jdbc/execute-one! (ds) [(str "delete from fruit where " (index) " > 4")])))))
["fruit2" "two"] (is (= {:opts 0, :log1 0 :log2 0} @tracker))
["fruit3" "three"] (is (= [1 1 1 1 1 1 1 1 1 13]
["fruit4" "four"] (try
["fruit5" "five"] (with-open [con (jdbc/get-connection (ds))]
["fruit6" "six"] (let [result (jdbc/execute-batch! (jdbc/with-options con {:builder-fn (track-fn :opts rs/as-maps)})
["fruit7" "seven"] "INSERT INTO fruit (name, appearance) VALUES (?,?)"
["fruit8" "eight"] [["fruit1" "one"]
["fruit9" "nine"]] ["fruit2" "two"]
{})] ["fruit3" "three"]
(conj result (count (jdbc/execute! (ds) ["select * from fruit"])))) ["fruit4" "four"]
(finally ["fruit5" "five"]
(jdbc/execute-one! (ds) [(str "delete from fruit where " (index) " > 4")]))))) ["fruit6" "six"]
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))) ["fruit7" "seven"]
["fruit8" "eight"]
["fruit9" "nine"]]
{})]
(conj result (count (jdbc/execute! (ds) ["select * from fruit"])))))
(finally
(jdbc/execute-one! (ds) [(str "delete from fruit where " (index) " > 4")])))))
(is (= {:opts 0, :log1 0 :log2 0} @tracker))
(is (= [1 1 1 1 1 1 1 1 1 13]
(try
(with-open [con (jdbc/get-connection (ds))]
(let [result (jdbc/execute-batch! (jdbc/with-logging con (track-fn :log1) (track-fn :log2))
"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) [(str "delete from fruit where " (index) " > 4")])))))
(is (= {:opts 0, :log1 1 :log2 0} @tracker))
(is (= [1 1 1 1 1 1 1 1 1 13]
(try
(with-open [con (jdbc/get-connection (ds))]
(let [result (jdbc/execute-batch! (jdbc/with-options
(jdbc/with-logging con (track-fn :log1) (track-fn :log2))
{:builder-fn (track-fn :opts rs/as-maps)})
"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) [(str "delete from fruit where " (index) " > 4")])))))
(is (= {:opts 0, :log1 2 :log2 0} @tracker))
(is (= [1 1 1 1 1 1 1 1 1 13]
(try
(with-open [con (jdbc/get-connection (ds))]
(let [result (jdbc/execute-batch! (jdbc/with-logging
(jdbc/with-options con
{:builder-fn (track-fn :opts rs/as-maps)})
(track-fn :log1) (track-fn :log2))
"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) [(str "delete from fruit where " (index) " > 4")])))))
(is (= {:opts 0, :log1 3 :log2 0} @tracker))
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))))
(testing "small batch insert" (testing "small batch insert"
(is (= [1 1 1 1 1 1 1 1 1 13] (is (= [1 1 1 1 1 1 1 1 1 13]
(try (try