From 084c1ec5e542d4b0b7d5a81d18cfd4c14cfef1e7 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Sun, 22 Sep 2024 17:11:01 -0700 Subject: [PATCH] fixes #543 Signed-off-by: Sean Corfield --- CHANGELOG.md | 1 + src/honey/sql.cljc | 18 +++++++++++------- test/honey/sql_test.cljc | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9cf04d..4fa2271 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index a1543ca..9362fc8 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -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... @@ -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)))) diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index 18a3d58..6e579a2 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -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]})