diff options
author | scgilardi <scgilardi@gmail.com> | 2008-06-15 23:03:27 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2008-06-15 23:03:27 +0000 |
commit | 7552bf10ffc56c1595b7ff92a4c87f1af45253fb (patch) | |
tree | 05716bc17c5c713d1d1f5422b3339765de288ff7 | |
parent | 01c4b7746bf5de045a98a73837e0dacb6f4be06e (diff) |
updates to lib.clj and sql.clj to adopt lispy syntax for interop after 20080612 release of Clojure, Derby database example.
-rw-r--r-- | lib.clj | 63 | ||||
-rw-r--r-- | sql.clj | 112 |
2 files changed, 69 insertions, 106 deletions
@@ -99,16 +99,6 @@ ;; ;; (use (genclass :ns clojure)) ;; -;; Loading -;; -;; lib.clj provides these functions for loading arbitrary Clojure source -;; files: -;; -;; 'load-uri' loads Clojure source from a location -;; -;; 'load-system-resource' loads Clojure source from a resource in -;; classpath -;; ;; scgilardi (gmail) ;; 06 May 2008 ;; @@ -119,6 +109,7 @@ (clojure/refer 'clojure) (import '(java.io BufferedReader InputStreamReader)) +(import '(clojure.lang RT)) ;; Private @@ -127,8 +118,8 @@ {:private true} [var init] `(let [v# (resolve '~var)] - (when-not (. v# (isBound)) - (. v# (bindRoot ~init))))) + (when-not (.isBound v#) + (.bindRoot v# ~init)))) (def #^{:private true :doc @@ -142,18 +133,16 @@ *verbose*) (init-once *verbose* false) -(def load-system-resource) - (defn- load-lib - "Loads a lib from <classpath>/in/ and ensures the namespace - named by ns (if any) exists" + "Loads a lib from <classpath>/in/ and ensures that namespace + ns (if specified) exists" [sym in ns] (let [res (str sym ".clj")] - (load-system-resource res in) + (.loadResourceScript RT (if in (str in \/ res) res)) (when (and ns (not (find-ns ns))) - (throw (new Exception - (str "namespace '" ns "' not found after " - "loading resource '" res "'"))))) + (throw (Exception. + (str "namespace '" ns "' not found after " + "loading resource '" res "'"))))) (dosync (commute *libs* conj sym)) (when *verbose* @@ -256,37 +245,3 @@ are filters for clojure/refer." [& args] `(apply load-libs :require :use '~args)) - -;; Loading - -(defn load-uri - "Loads Clojure source from a URI, which may be a java.net.URI - java.net.URL, or String. Accepts any URI scheme supported by - java.net.URLConnection (http and jar), plus file URIs." - [uri] - (let [url (cond ; coerce argument into java.net.URL - (instance? java.net.URL uri) uri - (instance? java.net.URI uri) (. uri (toURL)) - (string? uri) (new java.net.URL uri) - :else (throw (new Exception - (str "Cannot coerce " - (class uri) - " into java.net.URL."))))] - (if (= "file" (. url (getProtocol))) - (load-file (. url (getFile))) - (with-open reader - (new BufferedReader - (new InputStreamReader - (. url (openStream)))) - (load reader))))) - -(defn load-system-resource - "Loads Clojure source from a resource within classpath" - ([res] - (let [url (. ClassLoader (getSystemResource res))] - (when-not url - (throw (new Exception (str "resource '" res - "' not found in classpath")))) - (load-uri url))) - ([res in] - (load-system-resource (if in (str in \/ res) res)))) @@ -22,7 +22,7 @@ "Attempts to get a connection to a database via a jdbc URL" [subprotocol db-name] (let [url (str "jdbc:" subprotocol ":" db-name)] - (. DriverManager (getConnection url)))) + (.getConnection DriverManager url))) (defmacro with-connection "Evaluates body in the context of a connection to a database. Any updates @@ -31,37 +31,37 @@ [con init & body] `(with-open ~con ~init (try - (. ~con (setAutoCommit false)) + (.setAutoCommit ~con false)) ~@body - (. ~con (commit)) + (.commit ~con) (catch Exception e# - (. ~con (rollback)) - (throw (new Exception "transaction rolled back" e#)))))) + (.rollback ~con) + (throw (Exception. "transaction rolled back" e#))))) (defn execute-commands "Executes a sequence of SQL commands that do not return results" [con commands] - (with-open stmt (. con (createStatement)) + (with-open stmt (.createStatement con) (doseq cmd commands - (. stmt (addBatch cmd))) - (. stmt (executeBatch)))) + (.addBatch stmt cmd)) + (.executeBatch stmt))) (defn execute-prepared-statement "Executes a prepared statement with a sequence of parameter sets" [con sql sets] - (with-open stmt (. con (prepareStatement sql)) + (with-open stmt (.prepareStatement con sql) (doseq set sets - (doseq arg (map vector (iterate inc 1) set) - (. stmt (setObject (arg 0) (arg 1)))) - (. stmt (addBatch))) - (. stmt (executeBatch)))) + (doseq [index value] (map vector (iterate inc 1) set) + (.setObject stmt index value)) + (.addBatch stmt )) + (.executeBatch stmt))) (defmacro with-query-results "Executes a query and then evaluates body repeatedly with rec bound to each of the generated results in turn" [rec con sql & body] - `(with-open stmt# (. ~con (prepareStatement ~sql)) - (with-open rset# (. stmt# (executeQuery)) + `(with-open stmt# (.prepareStatement ~con ~sql) + (with-open rset# (.executeQuery stmt#) (doseq ~rec (resultset-seq rset#) ~@body)))) @@ -69,50 +69,58 @@ ;; Examples - ;; Simple tests of sql.clj using sqlite as a JDBC provider. Note that - ;; unlike sql.clj itself, these tests require that java be able to - ;; access sqlite via something like: http://zentus.com/sqlitejdbc . + ;; 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). - (require sql) +(clojure/in-ns 'sql-test) +(clojure/refer 'clojure) + +(lib/use sql) - (. Class (forName "org.sqlite.JDBC")) +(.forName Class "org.apache.derby.jdbc.EmbeddedDriver") - (defn db [] - (get-connection "sqlite" "test.db")) +(defn db [] + (get-connection "derby" "/tmp/test-derby.db;create=true")) - (defn db-write [] - (with-connection con (db) +(defn db-drop [] + (with-connection con (db) + (try (execute-commands con - ["drop table if exists fruit" - "create table fruit (name, appearance, 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 (new Exception "an exception")))) + ["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")))) ) |