summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--resources/config.sexp.sample7
-rw-r--r--src/mulk/benki/db.clj41
2 files changed, 43 insertions, 5 deletions
diff --git a/resources/config.sexp.sample b/resources/config.sexp.sample
index 804117d..a1e3a36 100644
--- a/resources/config.sexp.sample
+++ b/resources/config.sexp.sample
@@ -1,8 +1,9 @@
;;;; -*- mode: clojure; coding: utf-8 -*-
-{:database {:classname "org.postgresql.Driver"
- :subprotocol "postgresql"
- :subname "//localhost:5432/benki"
+{:database {;; NOTE: PostgreSQL is the only supported RDBMS.
+ :host "localhost"
+ :port 5432
+ :database "benki"
:user "benki"
:password ""}
:websocket-base "ws://localhost:3001"
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)