Skip to content

Commit

Permalink
Added update! function
Browse files Browse the repository at this point in the history
(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})
  • Loading branch information
paraseba committed Mar 30, 2011
1 parent 6916c91 commit 5c6b32d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/clojureql/core.clj
Expand Up @@ -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)
Expand Down Expand Up @@ -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})]
Expand Down
10 changes: 9 additions & 1 deletion 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
Expand Down Expand Up @@ -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)]
Expand Down
7 changes: 6 additions & 1 deletion test/clojureql/test/integration.clj
Expand Up @@ -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))))
Expand Down Expand Up @@ -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]))))
'(["Lau Jensen" 100] ["Christophe" 200] ["sthuebner" 300] ["Frank" 400]))))

0 comments on commit 5c6b32d

Please sign in to comment.