Limit value context to sequences in value positions

Fix #198
This commit is contained in:
Tianxiang Xiong 2018-01-09 23:35:22 -08:00
parent faba02f2b4
commit ccff2d2c23
2 changed files with 16 additions and 2 deletions

View file

@ -98,7 +98,7 @@
(to-sql [x]))
(defn to-sql-value [x]
(binding [*value-context?* true]
(binding [*value-context?* (sequential? x)]
(to-sql x)))
(defmulti fn-handler (fn [op & args] op))

View file

@ -120,7 +120,21 @@
(is (= ["SELECT foo FROM bar WHERE (col1 mod ?) = (col2 + ?)" 4 4]
(format {:select [:foo]
:from [:bar]
:where [:= [:mod :col1 4] [:+ :col2 4]]})))))
:where [:= [:mod :col1 4] [:+ :col2 4]]}))))
(testing "Value context only applies to sequences in value/comparison spots"
(let [sub {:select [:%sum.amount]
:from [:bar]
:where [:in :id ["id-1" "id-2"]]}]
(is (= ["SELECT total FROM foo WHERE (SELECT sum(amount) FROM bar WHERE (id in (?, ?))) = total" "id-1" "id-2"]
(format {:select [:total]
:from [:foo]
:where [:= sub :total]})))
(is (= ["WITH t AS (SELECT sum(amount) FROM bar WHERE (id in (?, ?))) SELECT total FROM foo WHERE total = t" "id-1" "id-2"]
(format {:with [[:t sub]]
:select [:total]
:from [:foo]
:where [:= :total :t]}))))))
(deftest union-with-cte
(is (= (format {:union [{:select [:foo] :from [:bar1]}