diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2009-02-20 11:17:24 +0100 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2009-02-20 11:17:24 +0100 |
commit | ef6ea9ef55ae44d8c7e97b32b89d3ede4c718055 (patch) | |
tree | f86093e36bff7be9627a2d0d67144d1fd937a7c5 | |
parent | da44c7246490e3e3afc9c4e3607d6ea2813b0fbb (diff) |
subscribe-to-feed: Stylistic improvements.
-rw-r--r-- | cljssss-g.clj | 49 |
1 files changed, 21 insertions, 28 deletions
diff --git a/cljssss-g.clj b/cljssss-g.clj index bdb8c17..5d56365 100644 --- a/cljssss-g.clj +++ b/cljssss-g.clj @@ -38,6 +38,11 @@ (defn trim-nil [thing] (and thing (.trim thing))) +(defn maximum-id [table-name] + (sql/with-query-results [max-id-map] + [(str "SELECT MAX(id) FROM " table-name)] + (or (second (first max-id-map)) -1))) + (defn fetch-feed [id] (with-db (let [uri (sql/with-query-results [{uri :uri}] @@ -59,11 +64,7 @@ (sql/with-query-results [{potential-entry-id :id}] ["SELECT id FROM entry WHERE iri = ?" (.getUri entry)] (let [entry-id - (or potential-entry-id - (+ 1 - (sql/with-query-results [max-id-map] - ["SELECT MAX(id) FROM entry"] - (or (second (first max-id-map)) -1))))] + (or potential-entry-id (+ 1 (maximum-id "entry")))] (sql/update-or-insert-values :entry ["id = ?" entry-id] {:id entry-id @@ -92,30 +93,22 @@ :entry entry-id})))))))) ;; system-wide subscription -;; ******* FIXME: Doesnt work when no feed was subscribed yet (defn subscribe-to-feed [url] - (let [maybe-ret - (with-dbt - (if (sql/with-query-results - [exi] - ["SELECT id FROM feed WHERE uri=?" url] - (if exi - (second (first exi)) - nil)) - nil ;; do nothing, return nil - (let [free-id - (sql/with-query-results - [max-id] - ["SELECT MAX(id) FROM feed"] - (if max-id (+ (second (first max-id)) 1) 0))] - (sql/update-or-insert-values :feed - ["id = ?" free-id] - {:id free-id - :uri url}) - free-id)))] - (if maybe-ret - (fetch-feed maybe-ret)) - maybe-ret)) + (let [maybe-id + (with-dbt + (when-not + (sql/with-query-results [{id :id}] + ["SELECT id FROM feed WHERE uri=?" url] + id) + (let [free-id (+ 1 (maximum-id "feed"))] + (sql/update-or-insert-values :feed + ["id = ?" free-id] + {:id free-id + :uri url}) + free-id)))] + (when maybe-id + (fetch-feed maybe-id)) + maybe-id)) (run-server {:port 8080} |