Merge pull request #325 from zugnush/quoting_fix

quoting for :%fun.col(s) syntax to match with [[:fun :col]]
This commit is contained in:
Sean Corfield 2021-05-10 12:24:06 -07:00 committed by GitHub
commit e38bb73295
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 8 deletions

View file

@ -153,6 +153,7 @@
(defn- namespace-_ [x] (some-> (namespace x) (str/replace "-" "_")))
(defn- name-_ [x] (str/replace (name x) "-" "_"))
(defn- -_ [x] (str/replace x "-" "_"))
(defn- sqlize-value [x]
(cond
@ -214,16 +215,16 @@
(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 (map #(format-entity (keyword %) opts) args)]
[(str (upper-case (-_ f)) "(" (str/join ", " quoted-args) ")")])
(= \? (first c))
(let [k (keyword (subs c 1))]
(if *inline*
[(sqlize-value (param-value k))]
["?" (->param k)]))
(let [k (keyword (subs (-_ c) 1))]
(if *inline*
[(sqlize-value (param-value k))]
["?" (->param k)]))
:else
[(format-entity x opts)])))

View file

@ -731,3 +731,24 @@ ORDER BY id = ? DESC
(is (thrown-with-msg? ExceptionInfo #"does not match"
(format {:where [:in :x :?y]}
{:params {:y [nil]} :checking :strict})))))
(deftest quoting-:%-syntax
(testing "quoting of expressions in functions shouldn't depend on syntax"
(is (= ["SELECT SYSDATE()"]
(format {:select [[[:sysdate]]]})
(format {:select :%sysdate})))
(is (= ["SELECT COUNT(*)"]
(format {:select [[[:count :*]]]})
(format {:select :%count.*})))
(is (= ["SELECT AVERAGE(`foo-foo`)"]
(format {:select [[[:average :foo-foo]]]} :dialect :mysql)
(format {:select :%average.foo-foo} :dialect :mysql)))
(is (= ["SELECT GREATER(`foo-foo`, `bar-bar`)"]
(format {:select [[[:greater :foo-foo :bar-bar]]]} :dialect :mysql)
(format {:select :%greater.foo-foo.bar-bar} :dialect :mysql)))
(is (= ["SELECT MIXED_KEBAB(`yum-yum`)"]
#_(format {:select [[[:mixed-kebab :yum-yum]]]} :dialect :mysql)
(format {:select :%mixed-kebab.yum-yum} :dialect :mysql)))
(is (= ["SELECT RANSOM(`NoTe`)"]
(format {:select [[[:ransom :NoTe]]]} :dialect :mysql)
(format {:select :%ransom.NoTe} :dialect :mysql)))))