diff options
author | scgilardi <scgilardi@gmail.com> | 2009-01-04 18:47:18 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2009-01-04 18:47:18 +0000 |
commit | 10762826a3862bd32d33a3e248a9fbd4569b5f5a (patch) | |
tree | 75f029796ed74510c7beb4a26ce3f4ed6cbacc6d /src | |
parent | a2fc8fb7c7dd0f376246b8db452a58436cbc57c8 (diff) |
sql: make 'connection' public, a function that returns the current database connection or throws if there is none
Diffstat (limited to 'src')
-rw-r--r-- | src/clojure/contrib/sql.clj | 10 | ||||
-rw-r--r-- | src/clojure/contrib/sql/internal.clj | 14 |
2 files changed, 9 insertions, 15 deletions
diff --git a/src/clojure/contrib/sql.clj b/src/clojure/contrib/sql.clj index 172cd190..d569ead5 100644 --- a/src/clojure/contrib/sql.clj +++ b/src/clojure/contrib/sql.clj @@ -16,8 +16,12 @@ ;; Created 2 April 2008 (ns clojure.contrib.sql + (:use [clojure.contrib.def :only (defvar)]) (:use clojure.contrib.sql.internal)) +(defvar connection connection* + "Returns the current database connection (or throws if there is none)") + (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 @@ -42,7 +46,7 @@ "Executes SQL commands that don't return results on the open database connection" [& commands] - (with-open [stmt (create-statement)] + (with-open [stmt (.createStatement (connection))] (doseq [cmd commands] (.addBatch stmt cmd)) (.executeBatch stmt))) @@ -51,7 +55,7 @@ "Executes a prepared statement on the open database connection with parameter sets" [sql & sets] - (with-open [stmt (prepare-statement sql)] + (with-open [stmt (.prepareStatement (connection) sql)] (doseq [set sets] (doseq [[index value] (map vector (iterate inc 1) set)] (.setObject stmt index value)) @@ -107,7 +111,7 @@ "Executes a query and then evaluates body with results bound to a seq of the results" [results sql & body] - `(with-open [stmt# (prepare-statement ~sql) + `(with-open [stmt# (.prepareStatement (connection) ~sql) rset# (.executeQuery stmt#)] (let [~results (resultset-seq rset#)] ~@body))) diff --git a/src/clojure/contrib/sql/internal.clj b/src/clojure/contrib/sql/internal.clj index 8d6e8414..8d17552c 100644 --- a/src/clojure/contrib/sql/internal.clj +++ b/src/clojure/contrib/sql/internal.clj @@ -15,22 +15,12 @@ (def *db* {:connection nil :level 0}) -(defn connection +(defn connection* "Returns the current database connection or throws" [] (or (:connection *db*) (throw (Exception. "no current database connection")))) -(defn create-statement - "Creates a statement object on the current connection" - [] - (.createStatement (connection))) - -(defn prepare-statement - "Creates a prepared statement object on the current connection" - [sql] - (.prepareStatement (connection) sql)) - (defn the-str "Returns the name or string representation of x" [x] @@ -73,7 +63,7 @@ or rolled back on any uncaught exception. Any nested transactions are absorbed into the outermost transaction." [thunk] - (let [con (connection) + (let [con (connection*) outermost (zero? (:level *db*)) auto-commit (when outermost (.getAutoCommit con))] (binding [*db* (update-in *db* [:level] inc)] |