summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2009-04-12 13:42:11 +0000
committerRich Hickey <richhickey@gmail.com>2009-04-12 13:42:11 +0000
commit0a8ac6b185348290c5af15c4bfdeda84a5a98592 (patch)
tree8b69ca7c1420f1c1725f7969612d08609e76bdd7
parent166982205692046dae7d14200d85bfeb80e6517c (diff)
use column labels instead of names in resultset-seq, and validate unique [issue 33]
-rw-r--r--src/clj/clojure/core.clj35
1 files changed, 19 insertions, 16 deletions
diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj
index 91dfbf62..fceded48 100644
--- a/src/clj/clojure/core.clj
+++ b/src/clj/clojure/core.clj
@@ -2344,22 +2344,6 @@
(clojure.lang.LineNumberingPushbackReader.))]
(load-reader rdr)))
-(defn resultset-seq
- "Creates and returns a lazy sequence of structmaps corresponding to
- the rows in the java.sql.ResultSet rs"
- [#^java.sql.ResultSet rs]
- (let [rsmeta (. rs (getMetaData))
- idxs (range 1 (inc (. rsmeta (getColumnCount))))
- keys (map (comp keyword #(.toLowerCase #^String %))
- (map (fn [i] (. rsmeta (getColumnName i))) idxs))
- row-struct (apply create-struct keys)
- row-values (fn [] (map (fn [#^Integer i] (. rs (getObject i))) idxs))
- rows (fn thisfn []
- (lazy-seq
- (when (. rs (next))
- (cons (apply struct row-struct (row-values)) (thisfn)))))]
- (rows)))
-
(defn set
"Returns a set of the distinct elements of coll."
[coll] (apply hash-set coll))
@@ -3437,6 +3421,25 @@
true))
false)))
+(defn resultset-seq
+ "Creates and returns a lazy sequence of structmaps corresponding to
+ the rows in the java.sql.ResultSet rs"
+ [#^java.sql.ResultSet rs]
+ (let [rsmeta (. rs (getMetaData))
+ idxs (range 1 (inc (. rsmeta (getColumnCount))))
+ keys (map (comp keyword #(.toLowerCase #^String %))
+ (map (fn [i] (. rsmeta (getColumnLabel i))) idxs))
+ check-keys
+ (or (apply distinct? keys)
+ (throw (Exception. "ResultSet must have unique column labels")))
+ row-struct (apply create-struct keys)
+ row-values (fn [] (map (fn [#^Integer i] (. rs (getObject i))) idxs))
+ rows (fn thisfn []
+ (lazy-seq
+ (when (. rs (next))
+ (cons (apply struct row-struct (row-values)) (thisfn)))))]
+ (rows)))
+
(defn iterator-seq
"Returns a seq on a java.util.Iterator. Note that most collections
providing iterators implement Iterable and thus support seq directly."