aboutsummaryrefslogtreecommitdiff
path: root/src/clojure
diff options
context:
space:
mode:
authorscgilardi <scgilardi@gmail.com>2008-10-03 23:26:10 +0000
committerscgilardi <scgilardi@gmail.com>2008-10-03 23:26:10 +0000
commitf3c6fc60e81e503e57053053684ee1b0e8855e94 (patch)
tree2261937021005671466d99259749f0e8da6af225 /src/clojure
parentfcbdeb6e5b07994b5880f5706d05ae5a619b5456 (diff)
improved support for additional options in sql/connection. Based on a suggestion from Tom Emerson.
Diffstat (limited to 'src/clojure')
-rw-r--r--src/clojure/contrib/sql/sql.clj29
-rw-r--r--src/clojure/contrib/sql/test/test.clj8
2 files changed, 29 insertions, 8 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
diff --git a/src/clojure/contrib/sql/test/test.clj b/src/clojure/contrib/sql/test/test.clj
index 4937d96f..83df028f 100644
--- a/src/clojure/contrib/sql/test/test.clj
+++ b/src/clojure/contrib/sql/test/test.clj
@@ -19,7 +19,7 @@
(Class/forName "org.apache.derby.jdbc.EmbeddedDriver")
(defn db []
- (connection "derby" "/tmp/clojure.contrib.sql.test.db;create=true"))
+ (connection "derby" "/tmp/clojure.contrib.sql.test.db" :create true))
(defn db-write []
(with-connection con (db)
@@ -31,12 +31,12 @@
"appearance varchar(32)"
"cost int"
"grade real")
- (insert-rows con "fruit"
+ (insert-rows con "fruit"
["Apple" "red" 59 87]
["Banana" "yellow" 29 92.2]
["Peach" "fuzzy" 139 90.0]
["Orange" "juicy" 89 88.6])
- (insert-values con "fruit" ["name" "cost"]
+ (insert-values con "fruit" ["name" "cost"]
["Mango" 722]
["Feijoa" 441])))
@@ -54,7 +54,7 @@
(defn db-exception []
(with-connection con (db)
- (insert-values con "fruit" ["name" "appearance"]
+ (insert-values con "fruit" ["name" "appearance"]
["Grape" "yummy"]
["Pear" "bruised"])
(throw (Exception. "an exception"))))