format with opts, hyphen to under in function names, improve and relocate tests
This commit is contained in:
parent
6ebc017969
commit
9c38554154
3 changed files with 28 additions and 32 deletions
|
|
@ -153,6 +153,7 @@
|
||||||
|
|
||||||
(defn- namespace-_ [x] (some-> (namespace x) (str/replace "-" "_")))
|
(defn- namespace-_ [x] (some-> (namespace x) (str/replace "-" "_")))
|
||||||
(defn- name-_ [x] (str/replace (name x) "-" "_"))
|
(defn- name-_ [x] (str/replace (name x) "-" "_"))
|
||||||
|
(defn- -_ [x] (str/replace x "-" "_"))
|
||||||
|
|
||||||
(defn- sqlize-value [x]
|
(defn- sqlize-value [x]
|
||||||
(cond
|
(cond
|
||||||
|
|
@ -217,12 +218,10 @@
|
||||||
(let [c (name x)]
|
(let [c (name x)]
|
||||||
(cond (= \% (first c))
|
(cond (= \% (first c))
|
||||||
(let [[f & args] (str/split (subs c 1) #"\.")
|
(let [[f & args] (str/split (subs c 1) #"\.")
|
||||||
quoted-args (->> args
|
quoted-args (map #(format-entity (keyword %) opts) args)]
|
||||||
(map keyword)
|
[(str (upper-case (-_ f)) "(" (str/join ", " quoted-args) ")")])
|
||||||
(map format-entity))]
|
|
||||||
[(str (upper-case f) "(" (str/join "," quoted-args) ")")])
|
|
||||||
(= \? (first c))
|
(= \? (first c))
|
||||||
(let [k (keyword (subs c 1))]
|
(let [k (keyword (subs (-_ c) 1))]
|
||||||
(if *inline*
|
(if *inline*
|
||||||
[(sqlize-value (param-value k))]
|
[(sqlize-value (param-value k))]
|
||||||
["?" (->param k)]))
|
["?" (->param k)]))
|
||||||
|
|
|
||||||
|
|
@ -866,27 +866,3 @@
|
||||||
{:with [[:a]],
|
{:with [[:a]],
|
||||||
:insert-into [[:quux [:x :y]]
|
:insert-into [[:quux [:x :y]]
|
||||||
{:select [:id], :from [:table]}]}))))
|
{:select [:id], :from [:table]}]}))))
|
||||||
|
|
||||||
(deftest quoting-:%-syntax
|
|
||||||
(testing "quoting of expressions in functions shouldn't depend on syntax"
|
|
||||||
(is (= [(str "SELECT `foo-bar`,"
|
|
||||||
" COUNT(*),"
|
|
||||||
" COUNT(*),"
|
|
||||||
" SYSDATE(),"
|
|
||||||
" SYSDATE(),"
|
|
||||||
" AVG(`bar-bar`),"
|
|
||||||
" AVG(`bar-bar`,`bar-foo`),"
|
|
||||||
" SUM(`foo-foo`)"
|
|
||||||
" FROM `employee`"
|
|
||||||
" GROUP BY `foo-bar`")]
|
|
||||||
(-> (select :foo-bar
|
|
||||||
[[:count :*]]
|
|
||||||
:%count.*
|
|
||||||
:%sysdate
|
|
||||||
:%sysdate.
|
|
||||||
:%avg.bar-bar
|
|
||||||
:%avg.bar-bar.bar-foo
|
|
||||||
[[:sum :foo-foo]])
|
|
||||||
(from :employee)
|
|
||||||
(group-by :foo-bar)
|
|
||||||
(sql/format :dialect :mysql))))))
|
|
||||||
|
|
|
||||||
|
|
@ -731,3 +731,24 @@ ORDER BY id = ? DESC
|
||||||
(is (thrown-with-msg? ExceptionInfo #"does not match"
|
(is (thrown-with-msg? ExceptionInfo #"does not match"
|
||||||
(format {:where [:in :x :?y]}
|
(format {:where [:in :x :?y]}
|
||||||
{:params {:y [nil]} :checking :strict})))))
|
{: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)))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue