From f489eda4d028055e3ce0be7fca7aacb8f2834084 Mon Sep 17 00:00:00 2001 From: Dave Della Costa Date: Wed, 5 Mar 2014 21:16:05 +0900 Subject: [PATCH 1/5] Fixes formatting for values, adds tests. --- src/honeysql/format.clj | 14 ++++++++------ test/honeysql/core_test.clj | 7 +++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 004a640..d43303b 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -366,13 +366,15 @@ (str "(" (comma-join (map to-sql fields)) ")")) (defmethod format-clause :values [[_ values] _] - (if (sequential? (first values)) - (str "VALUES " (comma-join (for [x values] - (str "(" (comma-join (map to-sql x)) ")")))) + (if (map? values) (str - "(" (comma-join (map to-sql (keys (first values)))) ") VALUES " - (comma-join (for [x values] - (str "(" (comma-join (map to-sql (vals x))) ")")))))) + "(" (comma-join (map to-sql (keys values))) ") " + "VALUES " + (str "(" (comma-join (map to-sql (vals values))) ")")) + (->> values + (reduce #(conj %1 (str "(" (comma-join (map to-sql %2)) ")")) []) + comma-join + (str "VALUES ")))) (defmethod format-clause :query-values [[_ query-values] _] (to-sql query-values)) diff --git a/test/honeysql/core_test.clj b/test/honeysql/core_test.clj index 161b52f..d79e1e4 100644 --- a/test/honeysql/core_test.clj +++ b/test/honeysql/core_test.clj @@ -55,3 +55,10 @@ "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 (?, ?)" "foo" "bar"] + (sql/format (values {:foo "foo" :bar "bar"}))))) From 290cc4cee04169079be16d42bdeb486202bdf429 Mon Sep 17 00:00:00 2001 From: Dave Della Costa Date: Wed, 5 Mar 2014 21:16:24 +0900 Subject: [PATCH 2/5] Updates README with insert/update/delete examples. --- README.md | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9eb5736..eaca363 100644 --- a/README.md +++ b/README.md @@ -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 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`: @@ -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. From 73caa251b8dd5ba65f31fd8bcbb02932d8ee8bac Mon Sep 17 00:00:00 2001 From: Dave Della Costa Date: Wed, 5 Mar 2014 21:34:16 +0900 Subject: [PATCH 3/5] fixes formatting of insert/update/delete header, fixes version # in that section --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eaca363..1f800e2 100644 --- a/README.md +++ b/README.md @@ -188,9 +188,9 @@ Here's a big, complicated query. Note that Honey SQL makes no attempt to verify => true ``` -## Insert, Update, Delete +### Insert, Update, Delete -Since version 4.3 HoneySQL supports insert, update, and delete. +Since version 0.4.3 HoneySQL supports insert, update, and delete. ```clj ;; Insert From ab991b2dd86648884b374e3b2bf4849a8037e206 Mon Sep 17 00:00:00 2001 From: Dave Della Costa Date: Sat, 8 Mar 2014 17:21:56 +0900 Subject: [PATCH 4/5] Whoops, realized I had misunderstood intended format for insert-into's values helper. Reverts patch, updates README to suit. --- src/honeysql/format.clj | 14 ++++++-------- test/honeysql/core_test.clj | 5 ++++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index d43303b..004a640 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -366,15 +366,13 @@ (str "(" (comma-join (map to-sql fields)) ")")) (defmethod format-clause :values [[_ values] _] - (if (map? values) + (if (sequential? (first values)) + (str "VALUES " (comma-join (for [x values] + (str "(" (comma-join (map to-sql x)) ")")))) (str - "(" (comma-join (map to-sql (keys values))) ") " - "VALUES " - (str "(" (comma-join (map to-sql (vals values))) ")")) - (->> values - (reduce #(conj %1 (str "(" (comma-join (map to-sql %2)) ")")) []) - comma-join - (str "VALUES ")))) + "(" (comma-join (map to-sql (keys (first values)))) ") VALUES " + (comma-join (for [x values] + (str "(" (comma-join (map to-sql (vals x))) ")")))))) (defmethod format-clause :query-values [[_ query-values] _] (to-sql query-values)) diff --git a/test/honeysql/core_test.clj b/test/honeysql/core_test.clj index d79e1e4..f2da41e 100644 --- a/test/honeysql/core_test.clj +++ b/test/honeysql/core_test.clj @@ -60,5 +60,8 @@ (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"}))))) + (sql/format (values [{:foo "foo" :bar "bar"}]))))) From 17b6af58ec0c7223b6be7ac8c62aa96c30d982c9 Mon Sep 17 00:00:00 2001 From: Dave Della Costa Date: Sat, 8 Mar 2014 17:25:08 +0900 Subject: [PATCH 5/5] adds another test for values helper format --- test/honeysql/core_test.clj | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/honeysql/core_test.clj b/test/honeysql/core_test.clj index f2da41e..1c7a48e 100644 --- a/test/honeysql/core_test.clj +++ b/test/honeysql/core_test.clj @@ -64,4 +64,7 @@ (sql/format (values [{:foo 1 :bar "bar"}])))) (is (= ["(foo, bar) VALUES (?, ?)" "foo" "bar"] - (sql/format (values [{:foo "foo" :bar "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"}])))))