From ddeed3b0dd0864fdf364809397da96d7b9123a23 Mon Sep 17 00:00:00 2001 From: misfo Date: Wed, 4 Jan 2012 22:45:53 -0600 Subject: [PATCH] Make where-params optional for update-values function This prevents having to pass a wonky always-true clause (e.g. ["1=1"]) as the where-params when updating all the rows in a table. --- src/main/clojure/clojure/java/jdbc.clj | 27 +++++++++++++++------------ src/test/clojure/clojure/java/test_jdbc.clj | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/main/clojure/clojure/java/jdbc.clj b/src/main/clojure/clojure/java/jdbc.clj index add1f0c..d8cde1c 100644 --- a/src/main/clojure/clojure/java/jdbc.clj +++ b/src/main/clojure/clojure/java/jdbc.clj @@ -298,18 +298,21 @@ generated keys are returned (as a map)." } params))) (defn update-values - "Updates values on selected rows in a table. where-params is a vector - containing a string providing the (optionally parameterized) selection - criteria followed by values for any parameters. record is a map from - strings or keywords (identifying columns) to updated values." - [table where-params record] - (let [[where & params] where-params - column-strs (map as-identifier (keys record)) - columns (apply str (concat (interpose "=?, " column-strs) "=?"))] - (do-prepared* - (format "UPDATE %s SET %s WHERE %s" - (as-identifier table) columns where) - (concat (vals record) params)))) + "Updates values on selected rows in a table. where-params is an optional + vector containing a string providing the (optionally parameterized) + selection criteria followed by values for any parameters. record is a map + from strings or keywords (identifying columns) to updated values." + ([table record] + (update-values table nil record)) + ([table where-params record] + (let [[where & params] where-params + column-strs (map as-identifier (keys record)) + columns (apply str (concat (interpose "=?, " column-strs) "=?"))] + (do-prepared* + (str "UPDATE " (as-identifier table) + " SET " columns + (when where (str " WHERE " where))) + (concat (vals record) params))))) (defn update-or-insert-values "Updates values on selected rows in a table, or inserts a new row when no diff --git a/src/test/clojure/clojure/java/test_jdbc.clj b/src/test/clojure/clojure/java/test_jdbc.clj index ff99ebf..1b51aaf 100644 --- a/src/test/clojure/clojure/java/test_jdbc.clj +++ b/src/test/clojure/clojure/java/test_jdbc.clj @@ -198,6 +198,20 @@ (is (= "Banana" (sql/with-query-results res ["SELECT * FROM fruit WHERE appearance = ?" "bruised"] (:name (first res))))) (is (= 14 (sql/with-query-results res ["SELECT * FROM fruit WHERE name = ?" "Banana"] (:cost (first res)))))))) +(deftest test-update-values2 + (doseq [db (test-specs)] + (sql/with-connection db + (create-test-table :fruit db) + (let [r (sql/insert-rows + :fruit + [1 "Apple" "red" 59 87] + [2 "Banana" "yellow" 29 92.2])] + (is (= '(1 1) r))) + (sql/update-values + :fruit + {:appearance "ripe" :cost 11}) + (is (= 2 (sql/with-query-results res ["SELECT * FROM fruit WHERE appearance = ? AND cost = ?" "ripe" 11] (count res))))))) + (deftest test-update-or-insert-values (doseq [db (test-specs)] (sql/with-connection db -- 1.7.7.3