diff options
author | scgilardi <scgilardi@gmail.com> | 2009-01-21 14:08:34 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2009-01-21 14:08:34 +0000 |
commit | ddadff672034102f41234c5c933d6e189059a8f8 (patch) | |
tree | 51d20187944b5351dd3a82ebbc3c975e901b15dd /src/clojure/contrib | |
parent | 348519cd1a9d37c45d429943510a41043edecbde (diff) |
sql.internal: extract get-connection code into its own function
Diffstat (limited to 'src/clojure/contrib')
-rw-r--r-- | src/clojure/contrib/sql/internal.clj | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/src/clojure/contrib/sql/internal.clj b/src/clojure/contrib/sql/internal.clj index 341a926e..34278694 100644 --- a/src/clojure/contrib/sql/internal.clj +++ b/src/clojure/contrib/sql/internal.clj @@ -50,10 +50,9 @@ ([val] (swap! (:rollback-only *db*) (fn [_] val)))) -(defn with-connection* - "Evaluates func in the context of a new connection to a database then - closes the connection. db-spec is a map containing values for one of the - following parameter sets: +(defn get-connection + "Creates a connection to a database. db-spec is a map containing values + for one of the following parameter sets: DataSource: :datasource (required) a javax.sql.DataSource @@ -66,21 +65,25 @@ :subname (required) a String, the jdbc subname (others) (optional) passed to the driver as properties." [{:keys [datasource username password classname subprotocol subname] - :as db-spec} func] + :as db-spec}] (when classname (clojure.lang.RT/loadClassForName classname)) - (let [con - (if datasource - (if username - (.getConnection datasource username password) - (.getConnection datasource)) - (java.sql.DriverManager/getConnection - (format "jdbc:%s:%s" subprotocol subname) - (properties (dissoc db-spec :classname :subprotocol :subname))))] - (with-open [con con] - (binding [*db* (assoc *db* :connection con :level 0 - :rollback-only (atom false))] - (func))))) + (if datasource + (if username + (.getConnection datasource username password) + (.getConnection datasource)) + (java.sql.DriverManager/getConnection + (format "jdbc:%s:%s" subprotocol subname) + (properties (dissoc db-spec :classname :subprotocol :subname))))) + +(defn with-connection* + "Evaluates func in the context of a new connection to a database then + closes the connection." + [db-spec func] + (with-open [con (get-connection db-spec)] + (binding [*db* (assoc *db* :connection con :level 0 + :rollback-only (atom false))] + (func)))) (defn transaction* "Evaluates func as a transaction on the open database connection. Any |