address #548 by splitting format-var

Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2024-10-11 09:01:19 -07:00
parent ba78dc2d27
commit 203e923f99
No known key found for this signature in database
3 changed files with 28 additions and 7 deletions

View file

@ -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).

View file

@ -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)

View file

@ -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]})