diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 4514180..b3b69d4 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -214,11 +214,13 @@ (fn [fk _] (param-value (fk)))})) (defn- format-var [x & [opts]] - (let [c (name-_ x)] + (let [c (name x)] (cond (= \% (first c)) - (let [[f & args] (str/split (subs c 1) #"\.")] - ;; TODO: this does not quote arguments -- does that matter? - [(str (upper-case f) "(" (str/join "," args) ")")]) + (let [[f & args] (str/split (subs c 1) #"\.") + quoted-args (->> args + (map keyword) + (map format-entity))] + [(str (upper-case f) "(" (str/join "," quoted-args) ")")]) (= \? (first c)) (let [k (keyword (subs c 1))] (if *inline* diff --git a/test/honey/sql/helpers_test.cljc b/test/honey/sql/helpers_test.cljc index 33ea68c..60e9a4d 100644 --- a/test/honey/sql/helpers_test.cljc +++ b/test/honey/sql/helpers_test.cljc @@ -866,3 +866,27 @@ {:with [[:a]], :insert-into [[:quux [:x :y]] {:select [:id], :from [:table]}]})))) + +(deftest quoting-:%-syntax + (testing "quoting of expressions in functions shouldn't depend on syntax" + (is (= [(str "SELECT `foo-bar`," + " COUNT(*)," + " COUNT(*)," + " SYSDATE()," + " SYSDATE()," + " AVG(`bar-bar`)," + " AVG(`bar-bar`,`bar-foo`)," + " SUM(`foo-foo`)" + " FROM `employee`" + " GROUP BY `foo-bar`")] + (-> (select :foo-bar + [[:count :*]] + :%count.* + :%sysdate + :%sysdate. + :%avg.bar-bar + :%avg.bar-bar.bar-foo + [[:sum :foo-foo]]) + (from :employee) + (group-by :foo-bar) + (sql/format :dialect :mysql))))))