fix #394 by escaping quote chars

This matches the HoneySQL 1.x behavior now.
This commit is contained in:
Sean Corfield 2022-03-31 17:34:51 -07:00
parent 314f497417
commit a2e02c8a03
3 changed files with 20 additions and 5 deletions

View file

@ -3,7 +3,8 @@
* 2.2.next in progress * 2.2.next in progress
* Address [#400](https://github.com/seancorfield/honeysql/issues/400) by adding `:table` clause. * Address [#400](https://github.com/seancorfield/honeysql/issues/400) by adding `:table` clause.
* Address [#399](https://github.com/seancorfield/honeysql/issues/399) by correcting multi-column `RETURNING` clauses in docs and tests. * Address [#399](https://github.com/seancorfield/honeysql/issues/399) by correcting multi-column `RETURNING` clauses in docs and tests.
* Address [#398](https://github.com/seancorfield/honeysql/issues/398) by adding `honey.sql.pg-json` namespace that registers PostgreSQL JSON operators and provides symbolic names for "unwritable" operators (that contain `@`). * Address [#398](https://github.com/seancorfield/honeysql/issues/398) by adding `honey.sql.pg-json` namespace that registers PostgreSQL JSON operators and provides symbolic names for "unwritable" operators (that contain `@` or `#`).
* Fix [#394](https://github.com/seancorfield/honeysql/issues/394) by restoring HoneySQL 1.x's behavior when quoting.
* Fix [#387](https://github.com/seancorfield/honeysql/issues/387) again. * Fix [#387](https://github.com/seancorfield/honeysql/issues/387) again.
* Update CI to reflect Clojure 1.11 release (master -> 1.11; new master is 1.12). * Update CI to reflect Clojure 1.11 release (master -> 1.11; new master is 1.12).
* Update `build-clj` to v0.8.0. * Update `build-clj` to v0.8.0.

View file

@ -87,15 +87,20 @@
order)) order))
(conj order clause)))) (conj order clause))))
(defn- strop
"Escape any embedded closing strop characters."
[s x e]
(str s (str/replace x (str e) (str e e)) e))
(def ^:private dialects (def ^:private dialects
(reduce-kv (fn [m k v] (reduce-kv (fn [m k v]
(assoc m k (assoc v :dialect k))) (assoc m k (assoc v :dialect k)))
{} {}
{:ansi {:quote #(str \" % \")} {:ansi {:quote #(strop \" % \")}
:sqlserver {:quote #(str \[ % \])} :sqlserver {:quote #(strop \[ % \])}
:mysql {:quote #(str \` % \`) :mysql {:quote #(strop \` % \`)
:clause-order-fn #(add-clause-before % :set :where)} :clause-order-fn #(add-clause-before % :set :where)}
:oracle {:quote #(str \" % \") :as false}})) :oracle {:quote #(strop \" % \") :as false}}))
; should become defonce ; should become defonce
(def ^:private default-dialect (atom (:ansi dialects))) (def ^:private default-dialect (atom (:ansi dialects)))

View file

@ -883,3 +883,12 @@ ORDER BY id = ? DESC
(is (= "what_is_this" (sut/sql-kw :'what-is-this))) (is (= "what_is_this" (sut/sql-kw :'what-is-this)))
(is (= "fee_fie_foe_fum" (sut/sql-kw :'fee-fie-foe-fum))) (is (= "fee_fie_foe_fum" (sut/sql-kw :'fee-fie-foe-fum)))
(is (= "_what_the_" (sut/sql-kw :'-what-the-)))) (is (= "_what_the_" (sut/sql-kw :'-what-the-))))
(deftest issue-394-quoting
(is (= ["SELECT \"A\"\"B\""] (sut/format {:select (keyword "A\"B")} {:quoted true})))
(is (= ["SELECT \"A\"\"B\""] (sut/format {:select (keyword "A\"B")} {:dialect :ansi})))
(is (= ["SELECT [A\"B]"] (sut/format {:select (keyword "A\"B")} {:dialect :sqlserver})))
(is (= ["SELECT [A]]B]"] (sut/format {:select (keyword "A]B")} {:dialect :sqlserver})))
(is (= ["SELECT `A\"B`"] (sut/format {:select (keyword "A\"B")} {:dialect :mysql})))
(is (= ["SELECT `A``B`"] (sut/format {:select (keyword "A`B")} {:dialect :mysql})))
(is (= ["SELECT \"A\"\"B\""] (sut/format {:select (keyword "A\"B")} {:dialect :oracle}))))