Skip to content

Commit

Permalink
Experimental: Added sugared lookup for single item resultsets
Browse files Browse the repository at this point in the history
user> ((select users (where (= :id 5))) :email)
"myemail@gmail.com"

There are many queries where you know by the predicate
that they will only return 1 item, so the above saves you this

(-> @(select users (where (= :id 5)))
    first
    :email)

Will throw an exception if you query returns more than 1 item
  • Loading branch information
Lau B. Jensen committed Dec 17, 2010
1 parent c8aee1d commit 1e7e8e9
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/clojureql/core.clj
Expand Up @@ -251,6 +251,13 @@
(with-open [rset (.executeQuery stmt)]
(f (resultset-seq rset)))))))

clojure.lang.IFn
(invoke [this kw]
(let [results @this]
(if (= 1 (count results))
(kw (first results))
(throw (Exception. "Multiple items in resultsetseq, keyword lookup not possible")))))

(select [this predicate]
(assoc this :restriction predicate)) ;TODO: Make this additive

Expand Down

0 comments on commit 1e7e8e9

Please sign in to comment.