Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Add Factory as a method of obtaining open database connections, fixes…
Browse files Browse the repository at this point in the history
… #50
  • Loading branch information
scgilardi committed Dec 15, 2009
1 parent e5b7819 commit be33acd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
8 changes: 6 additions & 2 deletions src/clojure/contrib/sql.clj
Expand Up @@ -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
Expand All @@ -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
Expand Down
43 changes: 25 additions & 18 deletions src/clojure/contrib/sql/internal.clj
Expand Up @@ -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
Expand All @@ -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."
Expand Down

0 comments on commit be33acd

Please sign in to comment.