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

Adds documentation for insert/update/delete in README.md, and adds a few tests #20

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 36 additions & 2 deletions README.md
Expand Up @@ -188,6 +188,41 @@ Here's a big, complicated query. Note that Honey SQL makes no attempt to verify
=> true
```

### Insert, Update, Delete

Since version 0.4.3 HoneySQL supports insert, update, and delete.

```clj
;; Insert
(-> (insert-into :foo)
(values [{:foo "foo"
:bar "bar"}])
sql/format)

=> ["INSERT INTO foo (foo, bar) VALUES (?, ?)" "foo" "bar"]

;; Alternatively, you can enter multiple rows at a time:
(-> (insert-into :foo)
(columns :foo :bar)
(values [["foo1" "bar1"] ["foo2" "bar2"]])
sql/format)

=> ["INSERT INTO foo (foo, bar) VALUES (?, ?), (?, ?)" "foo1" "bar1" "foo2" "bar2"]

(-> (update :foo)
(sset {:foo "foo"}) ;; (not a typo!)
(where [:= :id 1])
sql/format)

=> ["UPDATE foo SET foo = ? WHERE id = 1" "foo"]

(-> (delete-from :foo)
(where [:= :id 1])
sql/format)

=> ["DELETE FROM foo WHERE id = 1"]
```

## Extensibility

You can define your own function handlers for use in `where`:
Expand Down Expand Up @@ -230,11 +265,10 @@ If you do implement a clause or function handler, consider submitting a pull req

## TODO

* Insert, update, delete
* Create table, etc.

## License

Copyright © 2012 Justin Kramer
Copyright © 2014 Justin Kramer

Distributed under the Eclipse Public License, the same as Clojure.
13 changes: 13 additions & 0 deletions test/honeysql/core_test.clj
Expand Up @@ -55,3 +55,16 @@
"bort" "gabba" 2])))
(testing "SQL data prints and reads correctly"
(is (= m1 (read-string (pr-str m1)))))))

(deftest can-format-values-properly
(is (= ["VALUES (?, ?), (?, ?)" "foo1" "bar1" "foo2" "bar2"]
(sql/format (values [["foo1" "bar1"] ["foo2" "bar2"]]))))

(is (= ["(foo, bar) VALUES (1, ?)" "bar"]
(sql/format (values [{:foo 1 :bar "bar"}]))))

(is (= ["(foo, bar) VALUES (?, ?)" "foo" "bar"]
(sql/format (values [{:foo "foo" :bar "bar"}]))))

(is (= ["(foo, bar) VALUES (?, ?), (?, ?)" "foo1" "bar1" "foo2" "bar2"]
(sql/format (values [{:foo "foo1" :bar "bar1"} {:foo "foo2" :bar "bar2"}])))))