diff options
Diffstat (limited to 'src/clojure/contrib/sql/sql.clj')
-rw-r--r-- | src/clojure/contrib/sql/sql.clj | 64 |
1 files changed, 3 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")))) -) |