diff --git a/src/honeysql/format.cljc b/src/honeysql/format.cljc index 2f3766f..9a543e8 100644 --- a/src/honeysql/format.cljc +++ b/src/honeysql/format.cljc @@ -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)) diff --git a/test/honeysql/format_test.cljc b/test/honeysql/format_test.cljc index e75901d..f40f386 100644 --- a/test/honeysql/format_test.cljc +++ b/test/honeysql/format_test.cljc @@ -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]}