Add minor internal db api improvements

This commit is contained in:
Andrey Antukh 2023-11-08 10:44:44 +01:00
parent b32c8e2a83
commit 47e877d6c3

View file

@ -422,29 +422,30 @@
(.rollback conn sp))) (.rollback conn sp)))
(defn tx-run! (defn tx-run!
[cfg f] [system f & params]
(cond (cond
(connection? cfg) (connection? system)
(tx-run! {::conn cfg} f) (tx-run! {::conn system} f)
(pool? cfg) (pool? system)
(tx-run! {::pool cfg} f) (tx-run! {::pool system} f)
(::conn cfg) (::conn system)
(let [conn (::conn cfg) (let [conn (::conn system)
sp (savepoint conn)] sp (savepoint conn)]
(try (try
(let [result (f cfg)] (let [result (apply f system params)]
(release! conn sp) (release! conn sp)
result) result)
(catch Throwable cause (catch Throwable cause
(rollback! conn sp) (rollback! conn sp)
(throw cause)))) (throw cause))))
(::pool cfg) (::pool system)
(with-atomic [conn (::pool cfg)] (with-atomic [conn (::pool system)]
(let [result (f (assoc cfg ::conn conn))] (let [system (assoc system ::conn conn)
(when (::rollback cfg) result (apply f system params)]
(when (::rollback system)
(l/dbg :hint "explicit rollback requested") (l/dbg :hint "explicit rollback requested")
(rollback! conn)) (rollback! conn))
result)) result))
@ -453,20 +454,20 @@
(throw (IllegalArgumentException. "invalid arguments")))) (throw (IllegalArgumentException. "invalid arguments"))))
(defn run! (defn run!
[cfg f] [system f & params]
(cond (cond
(connection? cfg) (connection? system)
(run! {::conn cfg} f) (run! {::conn system} f)
(pool? cfg) (pool? system)
(run! {::pool cfg} f) (run! {::pool system} f)
(::conn cfg) (::conn system)
(f cfg) (apply f system params)
(::pool cfg) (::pool system)
(with-open [^Connection conn (open (::pool cfg))] (with-open [^Connection conn (open (::pool system))]
(f (assoc cfg ::conn conn))) (apply f (assoc system ::conn conn) params))
:else :else
(throw (IllegalArgumentException. "invalid arguments")))) (throw (IllegalArgumentException. "invalid arguments"))))