diff options
author | scgilardi <scgilardi@gmail.com> | 2008-10-03 23:26:10 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2008-10-03 23:26:10 +0000 |
commit | f3c6fc60e81e503e57053053684ee1b0e8855e94 (patch) | |
tree | 2261937021005671466d99259749f0e8da6af225 /src/clojure/contrib/sql/sql.clj | |
parent | fcbdeb6e5b07994b5880f5706d05ae5a619b5456 (diff) |
improved support for additional options in sql/connection. Based on a suggestion from Tom Emerson.
Diffstat (limited to 'src/clojure/contrib/sql/sql.clj')
-rw-r--r-- | src/clojure/contrib/sql/sql.clj | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/src/clojure/contrib/sql/sql.clj b/src/clojure/contrib/sql/sql.clj index 313df7de..73e7b037 100644 --- a/src/clojure/contrib/sql/sql.clj +++ b/src/clojure/contrib/sql/sql.clj @@ -17,12 +17,33 @@ (ns clojure.contrib.sql (:import - (java.sql DriverManager Connection PreparedStatement ResultSet))) + (java.sql DriverManager Connection PreparedStatement ResultSet) + (java.util Properties))) + +(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 + symbols to the String representation of the values" + [m] + (let [p (Properties.)] + (when m + (loop [[key & keys] (keys m) + [val & vals] (vals m)] + (.setProperty p (name key) (str val)) + (when keys + (recur keys vals)))) + p)) (defn connection - "Attempts to get a connection to a database via a jdbc URL" - [subprotocol db-name] - (DriverManager/getConnection (format "jdbc:%s:%s" subprotocol db-name))) + "Returns a connection to a database via a jdbc URL. Additional options + for the connection may be specified either as a map from keywords to + values or as inline keywords and values after db-name" + [subprotocol db-name & opts] + (DriverManager/getConnection + (format "jdbc:%s:%s" subprotocol db-name) + (properties (if (keyword? (first opts)) + (apply hash-map opts) + (first opts))))) (defmacro with-connection "Evaluates body in the context of a connection to a database. Any updates |