Don't parenthesize the subclauses of a UNION, UNION ALL, or INTERSECT clause. Fixes #141.
This commit is contained in:
parent
40fce0efc4
commit
9dd87d14a0
2 changed files with 15 additions and 6 deletions
|
|
@ -560,13 +560,16 @@
|
||||||
(str "WITH RECURSIVE " (comma-join (map cte->sql ctes))))
|
(str "WITH RECURSIVE " (comma-join (map cte->sql ctes))))
|
||||||
|
|
||||||
(defmethod format-clause :union [[_ maps] _]
|
(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] _]
|
(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] _]
|
(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]
|
(defmethod fn-handler "case" [_ & clauses]
|
||||||
(str "CASE "
|
(str "CASE "
|
||||||
|
|
|
||||||
|
|
@ -77,16 +77,22 @@
|
||||||
["INSERT INTO foo (baz) VALUES (ARRAY[?, ?, ?])" "one" "two" "three"])))
|
["INSERT INTO foo (baz) VALUES (ARRAY[?, ?, ?])" "one" "two" "three"])))
|
||||||
|
|
||||||
(deftest union-test
|
(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]}
|
(is (= (format {:union [{:select [:foo] :from [:bar1]}
|
||||||
{:select [:foo] :from [:bar2]}]})
|
{: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
|
(deftest union-all-test
|
||||||
(is (= (format {:union-all [{:select [:foo] :from [:bar1]}
|
(is (= (format {:union-all [{:select [:foo] :from [:bar1]}
|
||||||
{:select [:foo] :from [:bar2]}]})
|
{: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
|
(deftest intersect-test
|
||||||
(is (= (format {:intersect [{:select [:foo] :from [:bar1]}
|
(is (= (format {:intersect [{:select [:foo] :from [:bar1]}
|
||||||
{:select [:foo] :from [:bar2]}]})
|
{:select [:foo] :from [:bar2]}]})
|
||||||
["(SELECT foo FROM bar1) INTERSECT (SELECT foo FROM bar2)"])))
|
["SELECT foo FROM bar1 INTERSECT SELECT foo FROM bar2"])))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue