summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/mulk/benki/db.clj41
1 files changed, 39 insertions, 2 deletions
diff --git a/src/mulk/benki/db.clj b/src/mulk/benki/db.clj
index b45ec30..95bceab 100644
--- a/src/mulk/benki/db.clj
+++ b/src/mulk/benki/db.clj
@@ -1,11 +1,48 @@
(ns mulk.benki.db
(:refer-clojure)
- (:use [mulk.benki config])
+ (: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
+ :pool 1}))
+ {:datasource @+immutant-db+})))))
+
(defn ^:private db []
- (:database @benki-config))
+ (or (immutant-db) (classic-db)))
+
(defn call-with-db [thunk]
(sql/with-connection (db)