aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/clojure/contrib/sql.clj8
-rw-r--r--src/clojure/contrib/sql/internal.clj43
2 files changed, 31 insertions, 20 deletions
diff --git a/src/clojure/contrib/sql.clj b/src/clojure/contrib/sql.clj
index 9bb809ba..5398c499 100644
--- a/src/clojure/contrib/sql.clj
+++ b/src/clojure/contrib/sql.clj
@@ -15,12 +15,12 @@
;; scgilardi (gmail)
;; Created 2 April 2008
-(ns
+(ns
#^{:author "Stephen C. Gilardi",
:doc "A Clojure interface to sql databases via jdbc
See clojure.contrib.sql.test for an example"
- :see-also [["http://code.google.com/p/clojure-contrib/source/browse/trunk/src/clojure/contrib/sql/test.clj"
+ :see-also [["http://code.google.com/p/clojure-contrib/source/browse/trunk/src/clojure/contrib/sql/test.clj"
"Example code"]]}
clojure.contrib.sql
(:use (clojure.contrib
@@ -36,6 +36,10 @@
closes the connection. db-spec is a map containing values for one of the
following parameter sets:
+ Factory:
+ :factory (required) a function of one argument, a map of params
+ (others) (optional) passed to the factory function in a map
+
DriverManager:
:classname (required) a String, the jdbc driver class name
:subprotocol (required) a String, the jdbc subprotocol
diff --git a/src/clojure/contrib/sql/internal.clj b/src/clojure/contrib/sql/internal.clj
index 03880ec5..290ebec3 100644
--- a/src/clojure/contrib/sql/internal.clj
+++ b/src/clojure/contrib/sql/internal.clj
@@ -52,6 +52,10 @@
"Creates a connection to a database. db-spec is a map containing values
for one of the following parameter sets:
+ Factory:
+ :factory (required) a function of one argument, a map of params
+ (others) (optional) passed to the factory function in a map
+
DriverManager:
:classname (required) a String, the jdbc driver class name
:subprotocol (required) a String, the jdbc subprotocol
@@ -66,28 +70,31 @@
JNDI:
:name (required) a String or javax.naming.Name
:environment (optional) a java.util.Map"
- [{:keys [classname subprotocol subname
+ [{:keys [factory
+ classname subprotocol subname
datasource username password
name environment]
:as db-spec}]
(cond
- (and classname subprotocol subname)
- (let [url (format "jdbc:%s:%s" subprotocol subname)
- etc (dissoc db-spec :classname :subprotocol :subname)]
- (RT/loadClassForName classname)
- (DriverManager/getConnection url (as-properties etc)))
- (and datasource username password)
- (.getConnection datasource username password)
- datasource
- (.getConnection datasource)
- name
- (let [env (and environment (Hashtable. environment))
- context (InitialContext. env)
- datasource (.lookup context name)]
- (.getConnection datasource))
- :else
- (throw-arg "db-spec %s is missing a required parameter" db-spec)))
-
+ factory
+ (factory (dissoc db-spec :factory))
+ (and classname subprotocol subname)
+ (let [url (format "jdbc:%s:%s" subprotocol subname)
+ etc (dissoc db-spec :classname :subprotocol :subname)]
+ (RT/loadClassForName classname)
+ (DriverManager/getConnection url (as-properties etc)))
+ (and datasource username password)
+ (.getConnection datasource username password)
+ datasource
+ (.getConnection datasource)
+ name
+ (let [env (and environment (Hashtable. environment))
+ context (InitialContext. env)
+ datasource (.lookup context name)]
+ (.getConnection datasource))
+ :else
+ (throw-arg "db-spec %s is missing a required parameter" db-spec)))
+
(defn with-connection*
"Evaluates func in the context of a new connection to a database then
closes the connection."