fixes #181 for logging
Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
parent
9ed335dc8d
commit
22e96dcb84
3 changed files with 132 additions and 65 deletions
|
|
@ -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)]
|
|
||||||
(merge opts' opts)
|
|
||||||
opts))]
|
|
||||||
(execute-batch! ps param-groups opts))
|
(execute-batch! ps param-groups opts))
|
||||||
(with-open [con (get-connection connectable)]
|
(with-open [con (get-connection connectable)]
|
||||||
(execute-batch! con sql param-groups opts))))))
|
(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`,
|
||||||
|
|
|
||||||
|
|
@ -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)))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -732,9 +732,16 @@ 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"
|
||||||
|
(let [tracker (atom {:opts 0, :log1 0 :log2 0})
|
||||||
|
;; opts and log2 never get invoked with batch/prepare -- because
|
||||||
|
;; no fn opts are invoked on that path, and no post-logging is done:
|
||||||
|
track-fn (fn ([k] (fn [& _] (swap! tracker update k inc)))
|
||||||
|
([k f] (fn [& args] (swap! tracker update k inc) (apply f args))))]
|
||||||
|
(is (= {:opts 0, :log1 0 :log2 0} @tracker))
|
||||||
(is (= [1 1 1 1 1 1 1 1 1 13]
|
(is (= [1 1 1 1 1 1 1 1 1 13]
|
||||||
(try
|
(try
|
||||||
(let [result (jdbc/execute-batch! (jdbc/with-logging (ds) println println)
|
;; wrapping a non-connection will lose the wrapper:
|
||||||
|
(let [result (jdbc/execute-batch! (jdbc/with-options (ds) {:builder-fn (track-fn :opts rs/as-maps)})
|
||||||
"INSERT INTO fruit (name, appearance) VALUES (?,?)"
|
"INSERT INTO fruit (name, appearance) VALUES (?,?)"
|
||||||
[["fruit1" "one"]
|
[["fruit1" "one"]
|
||||||
["fruit2" "two"]
|
["fruit2" "two"]
|
||||||
|
|
@ -749,11 +756,70 @@ INSERT INTO fruit (name, appearance) VALUES (?,?)
|
||||||
(conj result (count (jdbc/execute! (ds) ["select * from fruit"]))))
|
(conj result (count (jdbc/execute! (ds) ["select * from fruit"]))))
|
||||||
(finally
|
(finally
|
||||||
(jdbc/execute-one! (ds) [(str "delete from fruit where " (index) " > 4")])))))
|
(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]
|
(is (= [1 1 1 1 1 1 1 1 1 13]
|
||||||
(try
|
(try
|
||||||
|
;; wrapping a non-connection will lose the wrapper:
|
||||||
|
(let [result (jdbc/execute-batch! (jdbc/with-logging (ds) {: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 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-options con {: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 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
|
(let [result (jdbc/execute-batch! (jdbc/with-options
|
||||||
(jdbc/with-logging (ds) println println)
|
(jdbc/with-logging con (track-fn :log1) (track-fn :log2))
|
||||||
{:ignore "me"})
|
{:builder-fn (track-fn :opts rs/as-maps)})
|
||||||
"INSERT INTO fruit (name, appearance) VALUES (?,?)"
|
"INSERT INTO fruit (name, appearance) VALUES (?,?)"
|
||||||
[["fruit1" "one"]
|
[["fruit1" "one"]
|
||||||
["fruit2" "two"]
|
["fruit2" "two"]
|
||||||
|
|
@ -765,14 +831,17 @@ INSERT INTO fruit (name, appearance) VALUES (?,?)
|
||||||
["fruit8" "eight"]
|
["fruit8" "eight"]
|
||||||
["fruit9" "nine"]]
|
["fruit9" "nine"]]
|
||||||
{})]
|
{})]
|
||||||
(conj result (count (jdbc/execute! (ds) ["select * from fruit"]))))
|
(conj result (count (jdbc/execute! (ds) ["select * from fruit"])))))
|
||||||
(finally
|
(finally
|
||||||
(jdbc/execute-one! (ds) [(str "delete from fruit where " (index) " > 4")])))))
|
(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]
|
(is (= [1 1 1 1 1 1 1 1 1 13]
|
||||||
(try
|
(try
|
||||||
|
(with-open [con (jdbc/get-connection (ds))]
|
||||||
(let [result (jdbc/execute-batch! (jdbc/with-logging
|
(let [result (jdbc/execute-batch! (jdbc/with-logging
|
||||||
(jdbc/with-options (ds) {:ignore "me"})
|
(jdbc/with-options con
|
||||||
println println)
|
{:builder-fn (track-fn :opts rs/as-maps)})
|
||||||
|
(track-fn :log1) (track-fn :log2))
|
||||||
"INSERT INTO fruit (name, appearance) VALUES (?,?)"
|
"INSERT INTO fruit (name, appearance) VALUES (?,?)"
|
||||||
[["fruit1" "one"]
|
[["fruit1" "one"]
|
||||||
["fruit2" "two"]
|
["fruit2" "two"]
|
||||||
|
|
@ -784,10 +853,11 @@ INSERT INTO fruit (name, appearance) VALUES (?,?)
|
||||||
["fruit8" "eight"]
|
["fruit8" "eight"]
|
||||||
["fruit9" "nine"]]
|
["fruit9" "nine"]]
|
||||||
{})]
|
{})]
|
||||||
(conj result (count (jdbc/execute! (ds) ["select * from fruit"]))))
|
(conj result (count (jdbc/execute! (ds) ["select * from fruit"])))))
|
||||||
(finally
|
(finally
|
||||||
(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 (= {: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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue