diff --git a/CHANGELOG.md b/CHANGELOG.md index 976f1fa..0eacb53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changes +* 2.6.next in progress + * Fix [#548](https://github.com/seancorfield/honeysql/issues/548) which was a regression introduced in [#526](https://github.com/seancorfield/honeysql/issues/526). + * 2.6.1196 -- 2024-10-06 * Address [#547](https://github.com/seancorfield/honeysql/issues/547) by adding examples of conditional SQL building with the helpers to the README and the `honey.sql.helpers` ns docstring. * Performance optimizations via PRs [#545](https://github.com/seancorfield/honeysql/pull/545) and [#546](https://github.com/seancorfield/honeysql/pull/546) [@alexander-yakushev](https://github.com/alexander-yakushev). diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 1aec57a..a49bd9e 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -415,6 +415,18 @@ [x] (upper-case (str/replace (name x) "-" "_"))) +(defn- format-simple-var [x & [c opts]] + (let [c (or c + (if (keyword? x) + #?(:clj (str (.sym ^clojure.lang.Keyword x)) ;; Omits leading colon + :default (subs (str x) 1)) + (str x)))] + (if (str/starts-with? c "'") + (do + (reset! *formatted-column* true) + [(subs c 1)]) + [(format-entity x opts)]))) + (defn- format-var [x & [opts]] ;; rather than name/namespace, we want to allow ;; for multiple / in the %fun.call case so that @@ -436,12 +448,8 @@ (->numbered-param k) :else ["?" (->param k)])) - (str/starts-with? c "'") - (do - (reset! *formatted-column* true) - [(subs c 1)]) :else - [(format-entity x opts)]))) + (format-simple-var x c opts)))) (defn- format-entity-alias [x] (cond (sequential? x) @@ -1277,7 +1285,7 @@ [(butlast coll) (last coll) nil]))] (into [(join " " (map sql-kw) prequel) (when table - (let [[v & more] (format-var table)] + (let [[v & more] (format-simple-var table)] (when (seq more) (throw (ex-info (str "DDL syntax error at: " (pr-str table) diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index 08df655..8e7cb95 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -6,7 +6,8 @@ [clojure.test :refer [deftest is testing]] [honey.sql :as sut :refer [format]] [honey.sql.helpers :as h]) - #?(:clj (:import (clojure.lang ExceptionInfo)))) + #?(:clj (:import (clojure.lang ExceptionInfo) + (java.net URLEncoder)))) (deftest mysql-tests (is (= ["SELECT * FROM `table` WHERE `id` = ?" 1] @@ -1387,6 +1388,15 @@ ORDER BY id = ? DESC (is (= ["SELECT * FROM `t1` INNER JOIN `t2` USING (`id`) WHERE `t1`.`id` = ?" 1] (sut/format '{select * from t1 join (t2 (:using id)) where (= t1/id 1)} {:dialect :mysql}))))) +(deftest issue-548-format-var-encoding + (is (= ["CREATE TABLE \"With%20Space\""] + (sut/format {:create-table "With%20Space"}))) + (is (= ["CREATE TABLE \"%20WithLeadingSpace\""] + (sut/format {:create-table "%20WithLeadingSpace"}))) + #?(:clj (let [table (URLEncoder/encode "привіт")] + (is (= [(str "CREATE TABLE \"" table "\"")] + (sut/format {:create-table table})))))) + (comment ;; partial (incorrect!) workaround for #407: (sut/format {:select :f.* :from [[:foo [:f :for :system-time]]] :where [:= :f.id 1]})