I've been using clojure for prototyping in JBoss and I found that clojure.contrib.sql does not currently support opening JDBC connections via a javax.sql.DataSource.
So I propose the patch below. Cheers, Juergen diff --git a/src/clojure/contrib/sql.clj b/src/clojure/contrib/sql.clj index 75242b5..0141398 100644 --- a/src/clojure/contrib/sql.clj +++ b/src/clojure/contrib/sql.clj @@ -24,8 +24,8 @@ (defmacro with-connection "Evaluates body in the context of a new connection to a database then - closes the connection. db-spec is a map containing string values for - these required keys: + closes the connection. db-spec is either a javax.sql.DataSource + or a map containing string values for these required keys: :classname the jdbc driver class name :subprotocol the jdbc subprotocol :subname the jdbc subname diff --git a/src/clojure/contrib/sql/internal.clj b/src/clojure/ contrib/sql/internal.clj index 9f1aca7..29c7791 100644 --- a/src/clojure/contrib/sql/internal.clj +++ b/src/clojure/contrib/sql/internal.clj @@ -50,26 +50,37 @@ ([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 string values for - these required keys: - :classname the jdbc driver class name - :subprotocol the jdbc subprotocol - :subname the jdbc subname - If db-spec contains additional keys (such as :user, :password, etc.) and - associated values, they will be passed along to the driver as properties." - [{:keys [classname subprotocol subname] :as db-spec} func] - (clojure.lang.RT/loadClassForName classname) - (with-open - [con - (java.sql.DriverManager/getConnection - (format "jdbc:%s:%s" subprotocol subname) - (properties (dissoc db- spec :classname :subprotocol :subname)))] +(defn- with-open-connection [con func] + (with-open [con con] (binding [*db* (assoc *db* :connection con :level 0 :rollback-only (atom false))] (func)))) +(defmulti + #^{:doc + "Evaluates body in the context of a new connection to a database then + closes the connection. db-spec is either a javax.sql.DataSource + or a map containing string values for these required keys: + :classname the jdbc driver class name + :subprotocol the jdbc subprotocol + :subname the jdbc subname + If db-spec contains additional keys (such as :user, :password, etc.) and + associated values, they will be passed along to the driver as properties."} + with-connection* (fn [spec func] (class spec))) + +(defmethod with-connection* javax.sql.DataSource + [ds func] + (with-open-connection (.getConnection ds) func)) + +(defmethod with-connection* :default + [{:keys [classname subprotocol subname] :as db-spec} func] + (clojure.lang.RT/loadClassForName classname) + (with-open-connection + (java.sql.DriverManager/getConnection + (format "jdbc:%s:%s" subprotocol subname) + (properties (dissoc db- spec :classname :subprotocol :subname))) + func)) + (defn transaction* "Evaluates func as a transaction on the open database connection. Any nested transactions are absorbed into the outermost transaction. All --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---