diff options
author | Steve Gilardi <scgilardi@gmail.com> | 2009-12-14 11:40:42 -0500 |
---|---|---|
committer | Steve Gilardi <scgilardi@gmail.com> | 2009-12-14 21:39:13 -0500 |
commit | be33acd87f190d9ec2ad756d8cb31c88abca7e5f (patch) | |
tree | 42e377592082f4befe81c3f083d4a89a4065067b /src | |
parent | e5b7819ef66410a87b8072417dc2f09ac4ddb03f (diff) |
Add Factory as a method of obtaining open database connections, fixes #50
Diffstat (limited to 'src')
-rw-r--r-- | src/clojure/contrib/sql.clj | 8 | ||||
-rw-r--r-- | src/clojure/contrib/sql/internal.clj | 43 |
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." |