address #548 by splitting format-var
Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
parent
ba78dc2d27
commit
203e923f99
3 changed files with 28 additions and 7 deletions
|
|
@ -1,5 +1,8 @@
|
||||||
# Changes
|
# 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
|
* 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.
|
* 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).
|
* 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).
|
||||||
|
|
|
||||||
|
|
@ -415,6 +415,18 @@
|
||||||
[x]
|
[x]
|
||||||
(upper-case (str/replace (name 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]]
|
(defn- format-var [x & [opts]]
|
||||||
;; rather than name/namespace, we want to allow
|
;; rather than name/namespace, we want to allow
|
||||||
;; for multiple / in the %fun.call case so that
|
;; for multiple / in the %fun.call case so that
|
||||||
|
|
@ -436,12 +448,8 @@
|
||||||
(->numbered-param k)
|
(->numbered-param k)
|
||||||
:else
|
:else
|
||||||
["?" (->param k)]))
|
["?" (->param k)]))
|
||||||
(str/starts-with? c "'")
|
|
||||||
(do
|
|
||||||
(reset! *formatted-column* true)
|
|
||||||
[(subs c 1)])
|
|
||||||
:else
|
:else
|
||||||
[(format-entity x opts)])))
|
(format-simple-var x c opts))))
|
||||||
|
|
||||||
(defn- format-entity-alias [x]
|
(defn- format-entity-alias [x]
|
||||||
(cond (sequential? x)
|
(cond (sequential? x)
|
||||||
|
|
@ -1277,7 +1285,7 @@
|
||||||
[(butlast coll) (last coll) nil]))]
|
[(butlast coll) (last coll) nil]))]
|
||||||
(into [(join " " (map sql-kw) prequel)
|
(into [(join " " (map sql-kw) prequel)
|
||||||
(when table
|
(when table
|
||||||
(let [[v & more] (format-var table)]
|
(let [[v & more] (format-simple-var table)]
|
||||||
(when (seq more)
|
(when (seq more)
|
||||||
(throw (ex-info (str "DDL syntax error at: "
|
(throw (ex-info (str "DDL syntax error at: "
|
||||||
(pr-str table)
|
(pr-str table)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@
|
||||||
[clojure.test :refer [deftest is testing]]
|
[clojure.test :refer [deftest is testing]]
|
||||||
[honey.sql :as sut :refer [format]]
|
[honey.sql :as sut :refer [format]]
|
||||||
[honey.sql.helpers :as h])
|
[honey.sql.helpers :as h])
|
||||||
#?(:clj (:import (clojure.lang ExceptionInfo))))
|
#?(:clj (:import (clojure.lang ExceptionInfo)
|
||||||
|
(java.net URLEncoder))))
|
||||||
|
|
||||||
(deftest mysql-tests
|
(deftest mysql-tests
|
||||||
(is (= ["SELECT * FROM `table` WHERE `id` = ?" 1]
|
(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]
|
(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})))))
|
(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
|
(comment
|
||||||
;; partial (incorrect!) workaround for #407:
|
;; partial (incorrect!) workaround for #407:
|
||||||
(sut/format {:select :f.* :from [[:foo [:f :for :system-time]]] :where [:= :f.id 1]})
|
(sut/format {:select :f.* :from [[:foo [:f :for :system-time]]] :where [:= :f.id 1]})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue