From 6916c917cf8261b7767fc3f9b86903ac7253a00f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Bernardo=20Galkin?= Date: Wed, 30 Mar 2011 17:32:08 -0300 Subject: [PATCH 1/3] update-in! accepts only one record It was accepting multiple maps, but the implementation was broken. It doesn't seem to have much sense to pass multiple maps to update. --- src/clojureql/core.clj | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/clojureql/core.clj b/src/clojureql/core.clj index 4e80194..8bcd538 100644 --- a/src/clojureql/core.clj +++ b/src/clojureql/core.clj @@ -164,9 +164,10 @@ Ex. (disj! (table :one) (where (= :age 22)))") - (update-in! [this pred records] - "Inserts or updates record(s) where pred is true. Accepts records - as both maps and collections. + (update-in! [this pred record] + "Inserts or updates a record where pred is true. Record + is a map from strings or keywords (identifying columns) + to updated values. Ex. (update-in! (table :one) (where (= :id 5)) {:age 22})") @@ -341,13 +342,11 @@ (delete-rows tname (into [(str predicate)] (:env predicate)))) this) - (update-in! [this pred records] + (update-in! [this pred record] (let [predicate (into [(str pred)] (:env pred)) retr (with-cnx cnx (when *debug* (prn predicate)) - (if (map? records) - (update-or-insert-vals tname predicate records) - (apply update-or-insert-vals tname predicate records)))] + (update-or-insert-vals tname predicate record))] (with-meta this (meta retr)))) (grouped [this field] From 5c6b32d6f07b622d9d8227a4cc640aaf3a5796da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Bernardo=20Galkin?= Date: Wed, 30 Mar 2011 17:51:40 -0300 Subject: [PATCH 2/3] Added update! function (update! [this pred record]) Updates a record where pred is true. Record is a map from strings or keywords (identifying columns) to updated values. Ex. (update! (table :one) (where (= :id 5)) {:age 22}) --- src/clojureql/core.clj | 15 +++++++++++++++ test/clojureql/test/core.clj | 10 +++++++++- test/clojureql/test/integration.clj | 7 ++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/clojureql/core.clj b/src/clojureql/core.clj index 8bcd538..cc0f19b 100644 --- a/src/clojureql/core.clj +++ b/src/clojureql/core.clj @@ -164,6 +164,14 @@ Ex. (disj! (table :one) (where (= :age 22)))") + (update! [this pred record] + "Updates a record where pred is true. Record + is a map from strings or keywords (identifying columns) + to updated values. + + Ex. (update! (table :one) (where (= :id 5)) + {:age 22})") + (update-in! [this pred record] "Inserts or updates a record where pred is true. Record is a map from strings or keywords (identifying columns) @@ -349,6 +357,13 @@ (update-or-insert-vals tname predicate record))] (with-meta this (meta retr)))) + (update! [this pred record] + (let [predicate (into [(str pred)] (:env pred)) + retr (with-cnx cnx + (when *debug* (prn predicate)) + (update-vals tname predicate record))] + (with-meta this (meta retr)))) + (grouped [this field] ;TODO: We shouldn't call to-fieldlist here, first in the compiler (let [colname (with-meta [(to-fieldlist tname field)] {:prepend true})] diff --git a/test/clojureql/test/core.clj b/test/clojureql/test/core.clj index c33662d..fc2fa9b 100644 --- a/test/clojureql/test/core.clj +++ b/test/clojureql/test/core.clj @@ -1,7 +1,7 @@ (ns clojureql.test.core (:refer-clojure :exclude [compile take drop sort distinct conj! disj! case]) - (:use [clojureql.internal :only [update-or-insert-vals]] + (:use [clojureql.internal :only [update-or-insert-vals update-vals]] [clojure.contrib.sql :only [with-connection find-connection]] clojure.test clojureql.core @@ -338,6 +338,14 @@ "JOIN products ON (products.id = product_variants.product_id) " "WHERE (orders.status = 1)")))) + (testing "update!" + (expect [update-vals (has-args [:users ["(id = ?)" 1] {:name "Bob"}]) + find-connection (returns true)] + (update! (table :users) (where (= :id 1)) {:name "Bob"})) + (expect [update-vals (has-args [:users ["(salary IS NULL)"] {:salary 1000}]) + find-connection (returns true)] + (update! (table :users) (where (= :salary nil)) {:salary 1000}))) + (testing "update-in!" (expect [update-or-insert-vals (has-args [:users ["(id = ?)" 1] {:name "Bob"}]) find-connection (returns true)] diff --git a/test/clojureql/test/integration.clj b/test/clojureql/test/integration.clj index bd8148a..f18ad2e 100644 --- a/test/clojureql/test/integration.clj +++ b/test/clojureql/test/integration.clj @@ -112,6 +112,11 @@ (is (= @(select users (where (not (or (< :id 2) (not (< :id 3)))))) '({:title "Design Guru", :name "Christophe", :id 2})))) +(database-test test-update! + (is (= @(-> (update! users (where (= :id 2)) {:name "John"}) + (select (where (= :id 2)))) + '({:title "Design Guru", :name "John", :id 2})))) + (database-test test-update-in! (is (= @(-> (update-in! users (where (= :id 2)) {:name "John"}) (select (where (= :id 2)))) @@ -213,4 +218,4 @@ (transform #(map (juxt :name :wage) %)) (join (transform salary first) ; this transform will be ignored (where (= :users.id :salary.id)))) - '(["Lau Jensen" 100] ["Christophe" 200] ["sthuebner" 300] ["Frank" 400])))) \ No newline at end of file + '(["Lau Jensen" 100] ["Christophe" 200] ["sthuebner" 300] ["Frank" 400])))) From e94aa90ef9a44d6d40af4031eb6a26b4158358f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Bernardo=20Galkin?= Date: Wed, 30 Mar 2011 20:42:56 -0300 Subject: [PATCH 3/3] Added documentation for integration tests Explain how integration tests are run --- test/clojureql/test/integration.clj | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/clojureql/test/integration.clj b/test/clojureql/test/integration.clj index f18ad2e..ffa3026 100644 --- a/test/clojureql/test/integration.clj +++ b/test/clojureql/test/integration.clj @@ -1,4 +1,11 @@ (ns clojureql.test.integration + "To run integration tests, you can use cake command line arguments: + + cake test --integration=true + + You will need to create MySQL, PostgreSQL and Sqlite3 databases according + to the parameters you can find in test/clojureql/test.clj" + (:import java.sql.Timestamp) (:use clojure.test clojureql.core clojureql.test) (:refer-clojure