diff options
author | scgilardi <scgilardi@gmail.com> | 2008-10-04 02:25:51 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2008-10-04 02:25:51 +0000 |
commit | 7d2785200afe9ae79cb5b2056d9f4d93877c8ee4 (patch) | |
tree | 0c0fe34d36e9f3568fd3587cd60385a5e1f02f5b | |
parent | f3c6fc60e81e503e57053053684ee1b0e8855e94 (diff) |
allow keywords for table names, column names, and types, move internal code into its own file
-rw-r--r-- | src/clojure/contrib/sql/internal.clj | 39 | ||||
-rw-r--r-- | src/clojure/contrib/sql/sql.clj | 45 | ||||
-rw-r--r-- | src/clojure/contrib/sql/test/test.clj | 20 |
3 files changed, 68 insertions, 36 deletions
diff --git a/src/clojure/contrib/sql/internal.clj b/src/clojure/contrib/sql/internal.clj new file mode 100644 index 00000000..ad1d7550 --- /dev/null +++ b/src/clojure/contrib/sql/internal.clj @@ -0,0 +1,39 @@ +;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and +;; distribution terms for this software are covered by the Common Public +;; License 1.0 (http://opensource.org/licenses/cpl.php) which can be found +;; in the file CPL.TXT at the root of this distribution. By using this +;; software in any fashion, you are agreeing to be bound by the terms of +;; this license. You must not remove this notice, or any other, from this +;; software. +;; +;; internal definitions for 'clojure.contrib.sql +;; +;; scgilardi (gmail) +;; Created 3 October 2008 + +(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- the-str + "Returns the String represented by the String, Keyword, or Symbol x" + [x] + (if (instance? String x) + x + (name x))) + +(defn- the-strs + "Returns a seq of the Strings represented by the Strings, Keywords, or + Symbols in the seq x" + [x] + (map the-str x)) diff --git a/src/clojure/contrib/sql/sql.clj b/src/clojure/contrib/sql/sql.clj index 73e7b037..590dcd4c 100644 --- a/src/clojure/contrib/sql/sql.clj +++ b/src/clojure/contrib/sql/sql.clj @@ -18,21 +18,8 @@ (ns clojure.contrib.sql (:import (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)) + (java.util Properties)) + (:load "internal.clj")) (defn connection "Returns a connection to a database via a jdbc URL. Additional options @@ -74,43 +61,49 @@ (doseq set sets (doseq [index value] (map vector (iterate inc 1) set) (.setObject stmt index value)) - (.addBatch stmt )) + (.addBatch stmt)) (.executeBatch stmt))) (defn create-table - "Creates a table given a name and column specifications" + "Creates a table given a name (a string or keyword) and column specs. A + column spec is a name (a string or keyword) followed by a type (a string + or keyword naming a data type understood by the database)." [con name & cols] (do-commands con (format "create table %s (%s)" - name - (apply str (interpose "," cols))))) + (the-str name) + (apply str + (apply concat + (interpose ", " + (map (partial interpose " ") + (partition 2 (the-strs cols))))))))) (defn drop-table - "Drops a table give its name" + "Drops a table give its name (a string or keyword)" [con name] (do-commands con - (format "drop table %s" name))) + (format "drop table %s" (the-str name)))) (defn insert-values "Inserts values into columns of a table. Columns is a seq of column - names (as strings) and each value is a seq of values for those - columns. To insert complete rows (all columns), use insert-rows" + names (strings or keywords) and each value is a seq of values for those + columns. To insert complete rows (all columns), use insert-rows." [con table columns & values] (let [count (count (first values)) template (apply str (interpose "," (replicate count "?"))) cols (if (seq columns) - (format "(%s)" (apply str (interpose "," columns))) + (format "(%s)" (apply str (interpose "," (the-strs columns)))) "")] (apply do-prepared con - (format "insert into %s %s values (%s)" table cols template) + (format "insert into %s %s values (%s)" (the-str table) cols template) values))) (defn insert-rows "Inserts complete rows into a table. Each row is a seq of values for each of the table's columns in order." [con table & rows] - (apply insert-values con table nil rows)) + (apply insert-values con (the-str table) nil rows)) (defmacro with-results "Executes a query and then evaluates body repeatedly with rec bound to diff --git a/src/clojure/contrib/sql/test/test.clj b/src/clojure/contrib/sql/test/test.clj index 83df028f..13a0d09f 100644 --- a/src/clojure/contrib/sql/test/test.clj +++ b/src/clojure/contrib/sql/test/test.clj @@ -16,7 +16,7 @@ (ns clojure.contrib.sql.test (:use clojure.contrib.sql)) -(Class/forName "org.apache.derby.jdbc.EmbeddedDriver") +(clojure.lang.RT/classForName "org.apache.derby.jdbc.EmbeddedDriver") (defn db [] (connection "derby" "/tmp/clojure.contrib.sql.test.db" :create true)) @@ -24,19 +24,19 @@ (defn db-write [] (with-connection con (db) (try - (drop-table con "fruit") + (drop-table con :fruit) (catch Exception e)) - (create-table con "fruit" - "name varchar(32)" - "appearance varchar(32)" - "cost int" - "grade real") - (insert-rows con "fruit" + (create-table con :fruit + :name "varchar(32)" + :appearance "varchar(32)" + :cost :int + :grade :real) + (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")))) |