From 403fb33fc4f9bb049f42fba9252825ba73f9b757 Mon Sep 17 00:00:00 2001 From: Erik Price Date: Thu, 8 Jan 2015 20:57:14 -0500 Subject: [PATCH 1/2] Support for `RETURNING` keyword --- src/honeysql/core.clj | 3 ++- src/honeysql/format.clj | 5 ++++- src/honeysql/helpers.clj | 8 ++++++++ test/honeysql/core_test.clj | 16 ++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/honeysql/core.clj b/src/honeysql/core.clj index d25415c..cae0be6 100644 --- a/src/honeysql/core.clj +++ b/src/honeysql/core.clj @@ -49,7 +49,8 @@ :query-values :update :set - :delete-from" + :delete-from + :returning" [& clauses] (let [[base clauses] (if (map? (first clauses)) [(first clauses) (rest clauses)] diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 004a640..269c8bb 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -158,7 +158,7 @@ "Determines the order that clauses will be placed within generated SQL" [:select :insert-into :update :delete-from :columns :set :from :join :left-join :right-join :where :group-by :having :order-by :limit :offset - :values :query-values]) + :values :query-values :returning]) (def known-clauses (set clause-order)) @@ -359,6 +359,9 @@ (defmethod format-clause :offset [[_ offset] _] (str "OFFSET " (to-sql offset))) +(defmethod format-clause :returning [[_ returning] _] + (str "RETURNING " (comma-join (map to-sql returning)))) + (defmethod format-clause :insert-into [[_ table] _] (str "INSERT INTO " (to-sql table))) diff --git a/src/honeysql/helpers.clj b/src/honeysql/helpers.clj index 0646928..817a6ff 100644 --- a/src/honeysql/helpers.clj +++ b/src/honeysql/helpers.clj @@ -37,6 +37,14 @@ (defhelper merge-from [m tables] (update-in m [:from] concat (collify tables))) +(defmethod build-clause :returning [_ m columns] + (if (empty? columns) + m + (assoc m :returning columns))) + +(defhelper returning [m columns] + (assoc m :returning (collify columns))) + (defmethod build-clause :where [_ m pred] (if (nil? pred) m diff --git a/test/honeysql/core_test.clj b/test/honeysql/core_test.clj index 161b52f..815a5f1 100644 --- a/test/honeysql/core_test.clj +++ b/test/honeysql/core_test.clj @@ -6,6 +6,22 @@ ;; TODO: more tests +(deftest test-insert + (let [m1 (-> (insert-into :t) + (columns :column_one :column_two) + (values [["one"] ["two"]]) + (returning :column_one :column_two)) + m2 {:insert-into :t + :columns [:column_one :column_two] + :values [["one"] ["two"]] + :returning [:column_one :column_two]} + m3 (sql/build m2)] + (testing "Various construction methods are consistent" + (is (= m1 m3))) + (testing "SQL data formats correctly" + (is (= (sql/format m1) + ["INSERT INTO t (column_one, column_two) VALUES (?), (?) RETURNING column_one, column_two" "one" "two"]))))) + (deftest test-select (let [m1 (-> (select :f.* :b.baz :c.quux [:b.bla :bla-bla] :%now (sql/raw "@x := 10")) From 8841c2900f955e578698ca2c7385b63153439435 Mon Sep 17 00:00:00 2001 From: Erik Price Date: Wed, 25 Feb 2015 09:15:17 -0500 Subject: [PATCH 2/2] Remove redundant `defmethod build-clause :returning` The `defhelper returning` that is immediately below it is all we needed. --- src/honeysql/helpers.clj | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/honeysql/helpers.clj b/src/honeysql/helpers.clj index 817a6ff..c84c42e 100644 --- a/src/honeysql/helpers.clj +++ b/src/honeysql/helpers.clj @@ -37,11 +37,6 @@ (defhelper merge-from [m tables] (update-in m [:from] concat (collify tables))) -(defmethod build-clause :returning [_ m columns] - (if (empty? columns) - m - (assoc m :returning columns))) - (defhelper returning [m columns] (assoc m :returning (collify columns)))