From 06f25ed2e3f96abce4920feca6034f954e138277 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Thu, 1 Apr 2021 12:50:09 -0700 Subject: [PATCH] Fixes #317 by dropping qualifier in :set clause --- CHANGELOG.md | 1 + src/honey/sql.cljc | 2 +- test/honey/sql_test.cljc | 40 ++++++++++++++++++++++++++++++++-------- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6297ab1..0d335a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changes * 2.0.next in progress + * Fix #317 by dropping qualifiers in `:set` clauses (just like we do with `:insert` columns). Note that you can still use explicit _dotted_ names if you want table qualification. * Fix #312 by adding `:raw` as a clause. There is no helper function equivalent (because it would be ambiguous whether you meant a function form -- `[:raw ..]` -- or a clause form -- `{:raw ..}`; and for the same reason, there is no `nest` helper function since that also works as a clause and as a function/special syntax). * 2.0.0-alpha3 (for early testing; 2021-03-13) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 2b64011..f79ef19 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -559,7 +559,7 @@ (let [[sqls params] (reduce-kv (fn [[sql params] v e] (let [[sql' & params'] (format-expr e)] - [(conj sql (str (format-entity v) " = " sql')) + [(conj sql (str (format-entity v {:drop-ns true}) " = " sql')) (if params' (into params params') params)])) [[] []] xs)] diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index dfdb7e8..e774603 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -337,17 +337,41 @@ #_{:parameterizer :mysql-fill}) ["WHERE (foo = ?) AND (bar = ?) AND (quux = ?)" "foo" "bar" "quux"])))) -(deftest set-before-from ; issue 235 +(deftest set-before-from + ;; issue 235 (is (= ["UPDATE \"films\" \"f\" SET \"kind\" = \"c\".\"test\" FROM (SELECT \"b\".\"test\" FROM \"bar\" AS \"b\" WHERE \"b\".\"id\" = ?) AS \"c\" WHERE \"f\".\"kind\" = ?" 1 "drama"] (-> - {:update [:films :f] - :set {:kind :c.test} - :from [[{:select [:b.test] - :from [[:bar :b]] - :where [:= :b.id 1]} :c]] - :where [:= :f.kind "drama"]} - (format {:quoted true}))))) + {:update [:films :f] + :set {:kind :c.test} + :from [[{:select [:b.test] + :from [[:bar :b]] + :where [:= :b.id 1]} :c]] + :where [:= :f.kind "drama"]} + (format {:quoted true})))) + ;; issue 317 + (is (= + ["UPDATE \"films\" \"f\" SET \"kind\" = \"c\".\"test\" FROM (SELECT \"b\".\"test\" FROM \"bar\" AS \"b\" WHERE \"b\".\"id\" = ?) AS \"c\" WHERE \"f\".\"kind\" = ?" 1 "drama"] + (-> + {:update [:films :f] + ;; drop ns in set clause... + :set {:f/kind :c.test} + :from [[{:select [:b.test] + :from [[:bar :b]] + :where [:= :b.id 1]} :c]] + :where [:= :f.kind "drama"]} + (format {:quoted true})))) + (is (= + ["UPDATE \"films\" \"f\" SET \"f\".\"kind\" = \"c\".\"test\" FROM (SELECT \"b\".\"test\" FROM \"bar\" AS \"b\" WHERE \"b\".\"id\" = ?) AS \"c\" WHERE \"f\".\"kind\" = ?" 1 "drama"] + (-> + {:update [:films :f] + ;; ...but keep literal dotted name + :set {:f.kind :c.test} + :from [[{:select [:b.test] + :from [[:bar :b]] + :where [:= :b.id 1]} :c]] + :where [:= :f.kind "drama"]} + (format {:quoted true}))))) (deftest set-after-join (is (=