Skip to content

Commit 9bffcc7

Browse files
committed
Add :unlogged-spec for queries without logging
1 parent 2b5bb03 commit 9bffcc7

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/duct/database/sql/hikaricp.clj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@
3939

4040
(defmethod ig/init-key :duct.database.sql/hikaricp
4141
[_ {:keys [logger connection-uri jdbc-url] :as options}]
42-
(sql/->Boundary {:datasource
43-
(-> (dissoc options :logger)
42+
(let [datasource (-> (dissoc options :logger)
4443
(assoc :jdbc-url (or jdbc-url connection-uri))
45-
(hikari-cp/make-datasource)
46-
(cond-> logger (wrap-logger logger)))}))
44+
(hikari-cp/make-datasource))]
45+
(if logger
46+
(-> (sql/->Boundary {:datasource (wrap-logger datasource logger)})
47+
(assoc :unlogged-spec {:datasource datasource}))
48+
(sql/->Boundary {:datasource datasource}))))
4749

4850
(defmethod ig/halt-key! :duct.database.sql/hikaricp [_ {:keys [spec]}]
4951
(let [ds (unwrap-logger (:datasource spec))]

test/duct/database/sql/hikaricp_test.clj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,19 @@
7979
(is (not (.isClosed (unwrap-logger (:datasource spec)))))
8080
(ig/halt-key! ::sql/hikaricp hikaricp)
8181
(is (.isClosed (unwrap-logger (:datasource spec))))))
82+
83+
(deftest unlogged-test
84+
(let [logs (atom [])
85+
logger (->AtomLogger logs)
86+
hikaricp (ig/init-key ::sql/hikaricp {:jdbc-url "jdbc:sqlite:" :logger logger})
87+
spec (:unlogged-spec hikaricp)]
88+
(jdbc/execute! spec ["CREATE TABLE foo (id INT, body TEXT)"])
89+
(jdbc/db-do-commands spec ["INSERT INTO foo VALUES (1, 'a')"
90+
"INSERT INTO foo VALUES (2, 'b')"])
91+
(is (= (jdbc/query spec ["SELECT * FROM foo"])
92+
[{:id 1, :body "a"} {:id 2, :body "b"}]))
93+
(is (= (jdbc/query spec ["SELECT * FROM foo WHERE id = ?" 1])
94+
[{:id 1, :body "a"}]))
95+
(is (= (jdbc/query spec ["SELECT * FROM foo WHERE id = ? AND body = ?" 1 "a"])
96+
[{:id 1, :body "a"}]))
97+
(is (empty? @logs))))

0 commit comments

Comments
 (0)