summaryrefslogtreecommitdiff
path: root/src/mulk/benki/db.clj
blob: 4adf30d0dd8fd96902b01d038b65c8a882f3193c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(ns mulk.benki.db
  (:refer-clojure)
  (:use [mulk.benki config]
        [clojure.pprint :only [cl-format]])
  (:require [clojure.java.jdbc :as sql]))


(defn ^:private classic-db []
  (let [{host :host, port :port, database :database,
         user :user, password :password}
        (:database @benki-config)]
    {:classname "org.postgresql.Driver"
     :subprotocol "postgresql"
     :subname (cl-format nil "//~A:~D/~A" host port database)
     :user user
     :password password}))

(defonce ^:private +immutant-db+ (atom nil))

(defn ^:private immutant-db []
  (if @+immutant-db+
    {:datasource @+immutant-db+}
    (let [{host :host, port :port, database :database,
           user :user, password :password}
          (:database @benki-config)]
      (when (try
              (require 'immutant.xa)
              true
              (catch Exception e
                false))
        (let [datasource (ns-resolve 'immutant.xa 'datasource)]
          (reset! +immutant-db+
                  (datasource "benkidb"
                              {:adapter  "postgres"
                               :host     host
                               :port     port
                               :database database
                               :username user
                               :password password
                               }))
          {:datasource @+immutant-db+})))))

(defn ^:private db []
  (or (immutant-db) (classic-db)))


(defn call-with-db [thunk]
  (sql/with-connection (db)
    (thunk)))

(defmacro with-db [& body]
  `(call-with-db (fn [] ~@body)))

(defmacro with-dbt [& body]
  `(call-with-db (fn [] (sql/transaction ~@body))))

(defmacro query [query-string & params]
  `(sql/with-query-results results# ~(into [] (concat [query-string] params))
     (into '() results#)))

(defmacro query1 [query-string & params]
  `(first (query ~query-string ~@params)))