Skip to content

Commit 747ffde

Browse files
authored
Comma separate the column names provided to RETURNING (#48)
* Separate the column names provided to the returning clause by comma * Test for varying number of returning columns
1 parent 82efbb4 commit 747ffde

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/honeysql_postgres/format.cljc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@
130130
(sqlf/space-join (format-upsert-clause upsert)))
131131

132132
(defmethod format-clause :returning [[_ fields] _]
133-
(str "RETURNING " (sqlf/comma-join (map sqlf/to-sql fields))))
133+
(->> (flatten fields)
134+
(map sqlf/to-sql)
135+
(sqlf/comma-join)
136+
(str "RETURNING ")))
134137

135138
(defmethod format-clause :create-view [[_ viewname] _]
136139
(str "CREATE VIEW " (-> viewname

test/honeysql_postgres/postgres_test.cljc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from order-by update sset query-values
1212
modifiers]]
1313
[honeysql.core :as sql]
14-
[clojure.test :as test :refer [deftest is testing]]))
14+
[clojure.test :as test :refer [deftest is testing]]
15+
[clojure.string :as str]))
1516

1617
(deftest upsert-test
1718
(testing "upsert sql generation for postgresql"
@@ -69,14 +70,15 @@
6970
(testing "returning clause in sql generation for postgresql"
7071
(is (= ["DELETE FROM distributors WHERE did > 10 RETURNING *"]
7172
(sql/format {:delete-from :distributors
72-
:where [:> :did :10]
73-
:returning [:*]})))
74-
(is (= ["UPDATE distributors SET dname = ? WHERE did = 2 RETURNING did dname" "Foo Bar Designs"]
75-
(-> (update :distributors)
76-
(sset {:dname "Foo Bar Designs"})
77-
(where [:= :did :2])
78-
(returning [:did :dname])
79-
sql/format)))))
73+
:where [:> :did :10]
74+
:returning [:*]})))
75+
(doseq [returning-columns (reductions conj [] [:did :dname :nos])]
76+
(is (= [(str "UPDATE distributors SET dname = ? WHERE did = 2 RETURNING " (str/join ", " (map name returning-columns))) "Foo Bar Designs"]
77+
(-> (update :distributors)
78+
(sset {:dname "Foo Bar Designs"})
79+
(where [:= :did :2])
80+
(returning returning-columns)
81+
sql/format))))))
8082

8183
(deftest create-view-test
8284
(testing "creating a view from a table"

0 commit comments

Comments
 (0)