Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adding update! function #89

Merged
merged 3 commits into from

2 participants

@paraseba

I added a function to update records without inserting if the records can't be found

I also removed multiple arguments from update-in! as discussed in #clojureql with you

tests added and passing

I'm not sure update! is a good name, LauJensen suggeste set!, but that's a special form in clojure

paraseba added some commits
@paraseba paraseba 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.
6916c91
@paraseba paraseba 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})
5c6b32d
@paraseba

I added an explanation on how to run integration tests

@paraseba paraseba Added documentation for integration tests
Explain how integration tests are run
e94aa90
@LauJensen LauJensen merged commit e94aa90 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 30, 2011
  1. @paraseba

    update-in! accepts only one record

    paraseba authored
    It was accepting multiple maps, but the implementation was broken.
    It doesn't seem to have much sense to pass multiple maps to update.
  2. @paraseba

    Added update! function

    paraseba authored
    (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})
  3. @paraseba

    Added documentation for integration tests

    paraseba authored
    Explain how integration tests are run
This page is out of date. Refresh to see the latest.
View
28 src/clojureql/core.clj
@@ -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})")
@@ -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]
View
10 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)]
View
14 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
@@ -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))))
@@ -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]))))
Something went wrong with that request. Please try again.