Don't parenthesize the subclauses of a UNION, UNION ALL, or INTERSECT clause. Fixes #141.

This commit is contained in:
Richard Newman 2016-10-12 08:45:12 -07:00
parent 40fce0efc4
commit 9dd87d14a0
2 changed files with 15 additions and 6 deletions

View file

@ -560,13 +560,16 @@
(str "WITH RECURSIVE " (comma-join (map cte->sql ctes))))
(defmethod format-clause :union [[_ maps] _]
(string/join " UNION " (map to-sql maps)))
(binding [*subquery?* false]
(string/join " UNION " (map to-sql maps))))
(defmethod format-clause :union-all [[_ maps] _]
(string/join " UNION ALL " (map to-sql maps)))
(binding [*subquery?* false]
(string/join " UNION ALL " (map to-sql maps))))
(defmethod format-clause :intersect [[_ maps] _]
(string/join " INTERSECT " (map to-sql maps)))
(binding [*subquery?* false]
(string/join " INTERSECT " (map to-sql maps))))
(defmethod fn-handler "case" [_ & clauses]
(str "CASE "

View file

@ -77,16 +77,22 @@
["INSERT INTO foo (baz) VALUES (ARRAY[?, ?, ?])" "one" "two" "three"])))
(deftest union-test
;; UNION and INTERSECT subexpressions should not be parenthesized.
;; If you need to add more complex expressions, use a subquery like this:
;; SELECT foo FROM bar1
;; UNION
;; SELECT foo FROM (SELECT foo FROM bar2 ORDER BY baz LIMIT 2)
;; ORDER BY foo ASC
(is (= (format {:union [{:select [:foo] :from [:bar1]}
{:select [:foo] :from [:bar2]}]})
["(SELECT foo FROM bar1) UNION (SELECT foo FROM bar2)"])))
["SELECT foo FROM bar1 UNION SELECT foo FROM bar2"])))
(deftest union-all-test
(is (= (format {:union-all [{:select [:foo] :from [:bar1]}
{:select [:foo] :from [:bar2]}]})
["(SELECT foo FROM bar1) UNION ALL (SELECT foo FROM bar2)"])))
["SELECT foo FROM bar1 UNION ALL SELECT foo FROM bar2"])))
(deftest intersect-test
(is (= (format {:intersect [{:select [:foo] :from [:bar1]}
{:select [:foo] :from [:bar2]}]})
["(SELECT foo FROM bar1) INTERSECT (SELECT foo FROM bar2)"])))
["SELECT foo FROM bar1 INTERSECT SELECT foo FROM bar2"])))