|
29 | 29 | (:import [java.sql PreparedStatement SQLException]) |
30 | 30 | (:gen-class)) |
31 | 31 |
|
| 32 | +(def DB_EXEC_MODE_QUERY "query") |
| 33 | +(def DB_EXEC_MODE_EXEC "execute") |
32 | 34 |
|
33 | 35 | ;; Error handling ---------------------------------------------------------------------------- |
34 | 36 |
|
|
174 | 176 | "only table or database owner can vacuum it" |
175 | 177 | "only table or database owner can analyze it" |
176 | 178 | "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)) |
179 | 181 | :retry-pause-millis (millis/<-seconds 5) |
180 | 182 | ; Must be overridden later on: |
181 | 183 | :sql-fn nothing |
|
653 | 655 | col-value-pairs (partition 2 (rest key-string-parts)) |
654 | 656 | where-conditions (map (fn [[col value]] (str col "='" value "'")) col-value-pairs)] |
655 | 657 | (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