summaryrefslogtreecommitdiff
path: root/src/mulk/benki/db.clj
blob: d2349e9e6623cd1b9d655bd7b1d15dad88616a56 (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
(ns mulk.benki.db
  (:refer-clojure)
  (:use mulk.benki.util)
  (:require [clojure.java.jdbc :as sql]))


(def ^:private db
  {:classname "org.postgresql.Driver"
   :subprotocol "postgresql"
   :subname "//localhost:5432/benki"
   :user "benki"
   :password (slurp "dbpassword.txt")})

(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)))