diff options
author | scgilardi <scgilardi@gmail.com> | 2008-10-06 04:04:37 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2008-10-06 04:04:37 +0000 |
commit | add96b1de658f1ec51f46f22cf532a15d8b47c34 (patch) | |
tree | aab372358946f98a58346dba7c5d6d28e48e6182 /src/clojure/contrib/sql | |
parent | aaa467fd2eb6b2ca60b4bd7f2f1d6e0f7756a59c (diff) |
sql: move internal defs to their own proper namespace
Diffstat (limited to 'src/clojure/contrib/sql')
-rw-r--r-- | src/clojure/contrib/sql/internal/internal.clj (renamed from src/clojure/contrib/sql/sql_internal.clj) | 14 | ||||
-rw-r--r-- | src/clojure/contrib/sql/sql.clj | 17 |
2 files changed, 20 insertions, 11 deletions
diff --git a/src/clojure/contrib/sql/sql_internal.clj b/src/clojure/contrib/sql/internal/internal.clj index f58ad4be..e46e9108 100644 --- a/src/clojure/contrib/sql/sql_internal.clj +++ b/src/clojure/contrib/sql/internal/internal.clj @@ -11,6 +11,18 @@ ;; scgilardi (gmail) ;; Created 3 October 2008 +(ns clojure.contrib.sql.internal) + +(def *db* {:connection nil :level 0}) + +(defn connection + "Returns the current database connection. Throws an exception if there is + no curent connection." + [] + (if-let connection (:connection *db*) + connection + (throw (Exception. "no current database connection")))) + (defn properties "Converts a Clojure map from keywords or symbols to values into a java.util.Properties object that maps the names of the keywords or @@ -25,7 +37,7 @@ (recur keys vals)))) p)) -(defn- the-str +(defn the-str "Returns the String represented by the String, Keyword, or Symbol x" [x] (if (instance? String x) diff --git a/src/clojure/contrib/sql/sql.clj b/src/clojure/contrib/sql/sql.clj index 7f257e08..630a082b 100644 --- a/src/clojure/contrib/sql/sql.clj +++ b/src/clojure/contrib/sql/sql.clj @@ -16,10 +16,8 @@ ;; Created 2 April 2008 (ns clojure.contrib.sql - (:use clojure.contrib.except) - (:load "sql_internal.clj")) - -(def *db* {:con nil :level 0}) + (:use clojure.contrib.except + clojure.contrib.sql.internal)) (defmacro with-connection "Evaluates body in the context of a new connection to a database then @@ -37,7 +35,7 @@ (java.sql.DriverManager/getConnection (format "jdbc:%s:%s" (:subprotocol ~db-spec) (:subname ~db-spec)) (properties (dissoc ~db-spec :classname :subprotocol :subname))) - (binding [*db* (assoc *db* :con con# :level 0)] + (binding [*db* (assoc *db* :connection con# :level 0)] ~@body)))) (defmacro transaction @@ -46,9 +44,8 @@ any uncaught exception. Any nested transactions will be absorbed into the outermost transaction." [& body] - `(let [con# (:con *db*) + `(let [con# (connection) level# (:level *db*)] - (throw-if (not con#) "no database connection") (binding [*db* (assoc *db* :level (inc level#))] (let [auto-commit# (.getAutoCommit con#)] (when (zero? level#) @@ -71,7 +68,7 @@ "Executes SQL commands that don't return results on the open database connection" [& commands] - (with-open stmt (.createStatement (:con *db*)) + (with-open stmt (.createStatement (connection)) (doseq cmd commands (.addBatch stmt cmd)) (.executeBatch stmt))) @@ -80,7 +77,7 @@ "Executes a prepared statement on the open database connection with parameter sets" [sql & sets] - (with-open stmt (.prepareStatement (:con *db*) sql) + (with-open stmt (.prepareStatement (connection) sql) (doseq set sets (doseq [index value] (map vector (iterate inc 1) set) (.setObject stmt index value)) @@ -133,7 +130,7 @@ "Executes a query and then evaluates body with res bound to a seq of the results" [res sql & body] - `(with-open stmt# (.prepareStatement (:con *db*) ~sql) + `(with-open stmt# (.prepareStatement (connection) ~sql) (with-open rset# (.executeQuery stmt#) (let [~res (resultset-seq rset#)] ~@body)))) |