aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib
diff options
context:
space:
mode:
authorscgilardi <scgilardi@gmail.com>2008-09-13 21:18:56 +0000
committerscgilardi <scgilardi@gmail.com>2008-09-13 21:18:56 +0000
commit218c24ec8e03ccfedab320ba24ece868debc0af7 (patch)
tree2411c54ebc60992629aa12767f4e4deb1e4fe8ce /src/clojure/contrib
parent25926242b3307e9b4681b44a64a65fe06756ed8f (diff)
sql: move test to sql.test
Diffstat (limited to 'src/clojure/contrib')
-rw-r--r--src/clojure/contrib/sql/sql.clj64
-rw-r--r--src/clojure/contrib/sql/test/test.clj47
2 files changed, 50 insertions, 61 deletions
diff --git a/src/clojure/contrib/sql/sql.clj b/src/clojure/contrib/sql/sql.clj
index 9c595258..467e6e16 100644
--- a/src/clojure/contrib/sql/sql.clj
+++ b/src/clojure/contrib/sql/sql.clj
@@ -10,6 +10,8 @@
;;
;; A Clojure interface to sql databases via jdbc
;;
+;; See clojure.contrib.sql.test for an example
+;;
;; scgilardi (gmail)
;; 23 April 2008
@@ -21,7 +23,7 @@
"Attempts to get a connection to a database via a jdbc URL"
[subprotocol db-name]
(let [url (str "jdbc:" subprotocol ":" db-name)]
- (.getConnection DriverManager url)))
+ (DriverManager/getConnection url)))
(defmacro with-connection
"Evaluates body in the context of a connection to a database. Any updates
@@ -63,63 +65,3 @@
(with-open rset# (.executeQuery stmt#)
(doseq ~rec (resultset-seq rset#)
~@body))))
-
-(comment
-
- ;; Examples
-
- ;; Simple tests of sql.clj using derby as a JDBC provider.
- ;;
- ;; Substituting a different database should only affect the definition
- ;; of 'db' below (and perhaps suggest the need for more variations of
- ;; get-connection).
-
-(clojure/in-ns 'sql-test)
-(clojure/refer 'clojure)
-
-(lib/use sql)
-
-(.forName Class "org.apache.derby.jdbc.EmbeddedDriver")
-
-(defn db []
- (get-connection "derby" "/tmp/test-derby.db;create=true"))
-
-(defn db-drop []
- (with-connection con (db)
- (try
- (execute-commands con
- ["drop table fruit"])
- (catch Exception e))))
-
-(defn db-write []
- (db-drop)
- (with-connection con (db)
- (execute-commands con
- ["create table fruit (name varchar(32), appearance varchar(32), cost int, grade real)"])
- (execute-prepared-statement con
- "insert into fruit values (?, ?, ?, ?)"
- [["Apple" "red" 59 87]
- ["Banana" "yellow" 29 92.2]
- ["Peach" "fuzzy" 139 90.0]
- ["Orange" "juicy" 89 88.6]])))
-
-(defn db-read []
- (with-connection con (db)
- (with-query-results rec con
- "select * from fruit"
- (println rec))))
-
-(defn db-grade-a []
- (with-connection con (db)
- (with-query-results rec con
- "select name, cost from fruit where grade >= 90"
- (println rec))))
-
-(defn db-exception []
- (with-connection con (db)
- (execute-prepared-statement con
- "insert into fruit (name, appearance) values (?, ?)"
- [["Grape" "yummy"]
- ["Pear" "bruised"]])
- (throw (Exception. "an exception"))))
-)
diff --git a/src/clojure/contrib/sql/test/test.clj b/src/clojure/contrib/sql/test/test.clj
new file mode 100644
index 00000000..f098c9aa
--- /dev/null
+++ b/src/clojure/contrib/sql/test/test.clj
@@ -0,0 +1,47 @@
+(ns clojure.contrib.sql.test
+ (:use clojure.contrib.sql))
+
+(Class/forName "org.apache.derby.jdbc.EmbeddedDriver")
+
+(defn db []
+ (get-connection "derby" "/tmp/clojure.contrib.sql.test.db;create=true"))
+
+(defn db-drop []
+ (with-connection con (db)
+ (try
+ (execute-commands con
+ ["drop table fruit"])
+ (catch Exception e))))
+
+(defn db-write []
+ (db-drop)
+ (with-connection con (db)
+ (execute-commands con
+ ["create table fruit (name varchar(32), appearance varchar(32), cost int, grade real)"])
+ (seq
+ (execute-prepared-statement con
+ "insert into fruit values (?, ?, ?, ?)"
+ [["Apple" "red" 59 87]
+ ["Banana" "yellow" 29 92.2]
+ ["Peach" "fuzzy" 139 90.0]
+ ["Orange" "juicy" 89 88.6]]))))
+
+(defn db-read []
+ (with-connection con (db)
+ (with-query-results rec con
+ "select * from fruit"
+ (println rec))))
+
+(defn db-grade-a []
+ (with-connection con (db)
+ (with-query-results rec con
+ "select name, cost from fruit where grade >= 90"
+ (println rec))))
+
+(defn db-exception []
+ (with-connection con (db)
+ (execute-prepared-statement con
+ "insert into fruit (name, appearance) values (?, ?)"
+ [["Grape" "yummy"]
+ ["Pear" "bruised"]])
+ (throw (Exception. "an exception"))))