Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding update! function #89

Merged
merged 3 commits into from Apr 6, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/clojureql/core.clj
Expand Up @@ -164,9 +164,18 @@

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! [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)
to updated values.

Ex. (update-in! (table :one) (where (= :id 5))
{:age 22})")
Expand Down Expand Up @@ -341,13 +350,18 @@
(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))
(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))
(if (map? records)
(update-or-insert-vals tname predicate records)
(apply update-or-insert-vals tname predicate records)))]
(update-vals tname predicate record))]
(with-meta this (meta retr))))

(grouped [this field]
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
14 changes: 13 additions & 1 deletion 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
Expand Down Expand Up @@ -112,6 +119,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 +225,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]))))