Skip to content
This repository was archived by the owner on Apr 17, 2021. It is now read-only.

Commit 3956a7b

Browse files
Merge pull request #20 from shopsmart/trans_mgmt_and_helpers_001
Add helper functions and functions for simple transaction wrapping
2 parents de8d48e + f9fdc74 commit 3956a7b

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject com.github.shopsmart/clj-infrastructure "0.1.14"
1+
(defproject com.github.shopsmart/clj-infrastructure "0.1.15"
22
:description "Infrastructure helpers for AWS, database, etc."
33
:url "https://github.com/shopsmart/clj-infrastructure"
44

src/clj_infrastructure/db.clj

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
(:import [java.sql PreparedStatement SQLException])
3030
(:gen-class))
3131

32+
(def DB_EXEC_MODE_QUERY "query")
33+
(def DB_EXEC_MODE_EXEC "execute")
3234

3335
;; Error handling ----------------------------------------------------------------------------
3436

@@ -174,8 +176,8 @@
174176
"only table or database owner can vacuum it"
175177
"only table or database owner can analyze it"
176178
"org.postgresql.util.PSQLException"]
177-
:max-retries 5
178-
:jdbc-timeout-millis (millis/<-minutes 30)
179+
:max-retries 0
180+
:jdbc-timeout-millis (millis/<-minutes (* 60 24))
179181
:retry-pause-millis (millis/<-seconds 5)
180182
; Must be overridden later on:
181183
:sql-fn nothing
@@ -653,3 +655,75 @@
653655
col-value-pairs (partition 2 (rest key-string-parts))
654656
where-conditions (map (fn [[col value]] (str col "='" value "'")) col-value-pairs)]
655657
(string/join " and " where-conditions)))
658+
659+
660+
(defn sql->stmt-detail-map [stmt-text & {:keys [exec-mode commit? binds opt-map op-comment] :as detail-kw}]
661+
(let [detail-map
662+
(merge
663+
{:stmt-text stmt-text
664+
:op-comment op-comment
665+
:exec-mode (or exec-mode dbc/DB_EXEC_MODE_QUERY)}
666+
(when binds {:binds binds})
667+
(when commit? {:commit? commit?})
668+
(when opt-map {:opt-map opt-map}))]
669+
detail-map))
670+
671+
672+
(defn run-sql-stmts-in-transaction
673+
[conn-or-spec stmt-detail-vec]
674+
675+
"""
676+
Runs the provided set of queries with the specified options.
677+
678+
sql-vec A vector of maps containing statement details. A statement detail map
679+
contains details such as sql text, execution mode, options, etc.
680+
681+
e.g.
682+
683+
[
684+
{ :stmt-text \"set random_page_cost = 1\"
685+
:exec-mode DB_EXEC_MODE_EXEC
686+
:op-comment \"Encourage index usage\"
687+
:binds nil
688+
:opt-map {:as-arrays? true}
689+
:commit? false}
690+
{ :stmt-text \"select 1\"
691+
:exec-mode DB_EXEC_MODE_QUERY
692+
:op-comment \"Extract new or modified user details\" } ]
693+
"""
694+
695+
(dbc/dbconfig-connection conn-or-spec
696+
(jdbc/with-db-transaction [conn (dbc/dbconfig {} "connection")]
697+
(doall
698+
(for [stmt-detail-map stmt-detail-vec]
699+
(run-statement conn stmt-detail-map))))))
700+
701+
702+
(defn run-statement
703+
[conn {:keys [stmt-text exec-mode op-comment binds opt-map commit?] :as stmt-detail-map}]
704+
705+
"""
706+
Runs the statement represented by the supplied statement detail
707+
map.
708+
709+
Returns: The statement detail map updated with {:result result-data}
710+
"""
711+
712+
(log/info (str "Running statement: [" stmt-text "] ..."))
713+
(let [sql-params (concat [stmt-text] binds)]
714+
(when op-comment
715+
(log/info (str "Operation comment: " op-comment)))
716+
(let [updated-map
717+
(assoc-in stmt-detail-map [:result]
718+
(cond
719+
(= exec-mode DB_EXEC_MODE_QUERY)
720+
(do
721+
(log/debug "Issuing statement as query (results expected) ...")
722+
(jdbc/query conn sql-params opt-map))
723+
(= exec-mode DB_EXEC_MODE_EXEC)
724+
(do
725+
(log/debug "Issuing statement as execution (no results expected) ...")
726+
(jdbc/execute! conn sql-params opt-map))))]
727+
(when commit?
728+
(.commit (:connection conn)))
729+
updated-map)))

0 commit comments

Comments
 (0)