Skip to content

Commit

Permalink
fixes #543
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Corfield <sean@corfield.org>
  • Loading branch information
seancorfield committed Sep 23, 2024
1 parent 150fcda commit 084c1ec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changes

* 2.6.next in progress
* Fix [#543](https://github.com/seancorfield/honeysql/issues/543) by supporting both symbols and keywords in named parameters.
* Getting Started updated based on feedback from Los Angeles Clojure meetup walkthrough [#539](https://github.com/seancorfield/honeysql/issues/539).
* Update Clojure version to 1.12.0.

Expand Down
18 changes: 11 additions & 7 deletions src/honey/sql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1920,12 +1920,13 @@
(into [(str/join ", " sqls)] params)))
:param
(fn [_ [k]]
(cond *inline*
[(sqlize-value (param-value k))]
*numbered*
(->numbered-param k)
:else
["?" (->param k)]))
(let [k (sym->kw k)]
(cond *inline*
[(sqlize-value (param-value k))]
*numbered*
(->numbered-param k)
:else
["?" (->param k)])))
:raw
(fn [_ [& xs]]
;; #476 : preserve existing single-argument behavior...
Expand Down Expand Up @@ -2124,7 +2125,10 @@
*quoted-snake* (if (contains? opts :quoted-snake)
(:quoted-snake opts)
@default-quoted-snake)
*params* (:params opts)
*params* (reduce-kv (fn [m k v]
(assoc m (sym->kw k) v))
{}
(:params opts))
*values-default-columns* (:values-default-columns opts)]
(if cache
(->> (through-opts opts cache data (fn [_] (formatter data (dissoc opts :cache))))
Expand Down
21 changes: 21 additions & 0 deletions test/honey/sql_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,27 @@ ORDER BY id = ? DESC
:from [[{:values [[1 2 3] [4 5 6]]}
[:t [:composite :a :b :c]]]]}))))

(deftest issue-543-param
(testing "quoted param with symbol param"
(is (= ["SELECT a FROM table WHERE x = ?" 42]
(sut/format '{select a from table where (= x (param y))}
{:params {'y 42}})))
(is (= ["SELECT a FROM table WHERE x = ?" 42]
(sut/format '{select a from table where (= x ?y)}
{:params {'y 42}}))))
(testing "quoted param with keyword param"
(is (= ["SELECT a FROM table WHERE x = ?" 42]
(sut/format '{select a from table where (= x (param y))}
{:params {:y 42}})))
(is (= ["SELECT a FROM table WHERE x = ?" 42]
(sut/format '{select a from table where (= x :?y)}
{:params {:y 42}}))))
(testing "all combinations"
(doseq [p1 [:y 'y] p2 [:y 'y]]
(is (= ["SELECT a FROM table WHERE x = ?" 42]
(sut/format {:select :a :from :table :where [:= :x [:param p1]]}
{:params {p2 42}}))))))

(comment
;; partial (incorrect!) workaround for #407:
(sut/format {:select :f.* :from [[:foo [:f :for :system-time]]] :where [:= :f.id 1]})
Expand Down

0 comments on commit 084c1ec

Please sign in to comment.