Consider wrapping union sub-expressions in parenthesis

This commit is contained in:
Andy Chambers 2018-08-13 17:16:10 +01:00
parent 2b238c5a47
commit a58e5edcf2
3 changed files with 14 additions and 11 deletions

View file

@ -235,7 +235,7 @@ Queries may be united within a :union or :union-all keyword:
```clojure ```clojure
(sql/format {:union [(-> (select :*) (from :foo)) (sql/format {:union [(-> (select :*) (from :foo))
(-> (select :*) (from :bar))]}) (-> (select :*) (from :bar))]})
=> ["SELECT * FROM foo UNION SELECT * FROM bar"] => ["(SELECT * FROM foo) UNION (SELECT * FROM bar)"]
``` ```
Keywords that begin with `%` are interpreted as SQL function calls: Keywords that begin with `%` are interpreted as SQL function calls:

View file

@ -614,15 +614,18 @@
(defmethod format-clause :union [[_ maps] _] (defmethod format-clause :union [[_ maps] _]
(binding [*subquery?* false] (binding [*subquery?* false]
(string/join " UNION " (map to-sql maps)))) (string/join " UNION " (map (fn [x]
(str "(" (to-sql x) ")")) maps))))
(defmethod format-clause :union-all [[_ maps] _] (defmethod format-clause :union-all [[_ maps] _]
(binding [*subquery?* false] (binding [*subquery?* false]
(string/join " UNION ALL " (map to-sql maps)))) (string/join " UNION ALL " (map (fn [x]
(str "(" (to-sql x) ")")) maps))))
(defmethod format-clause :intersect [[_ maps] _] (defmethod format-clause :intersect [[_ maps] _]
(binding [*subquery?* false] (binding [*subquery?* false]
(string/join " INTERSECT " (map to-sql maps)))) (string/join " INTERSECT " (map (fn [x]
(str "(" (to-sql x) ")")) maps))))
(defmethod fn-handler "case" [_ & clauses] (defmethod fn-handler "case" [_ & clauses]
(str "CASE " (str "CASE "

View file

@ -92,17 +92,17 @@
;; ORDER BY foo ASC ;; 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)"])))
(deftest inner-parts-test (deftest inner-parts-test
(testing "The correct way to apply ORDER BY to various parts of a UNION" (testing "The correct way to apply ORDER BY to various parts of a UNION"
@ -116,7 +116,7 @@
:order-by [[:amount :desc]] :order-by [[:amount :desc]]
:limit 5}]}] :limit 5}]}]
:order-by [[:amount :asc]]}) :order-by [[:amount :asc]]})
["SELECT amount, id, created_on FROM transactions UNION SELECT amount, id, created_on FROM (SELECT amount, id, created_on FROM other_transactions ORDER BY amount DESC LIMIT ?) ORDER BY amount ASC" 5])))) ["(SELECT amount, id, created_on FROM transactions) UNION (SELECT amount, id, created_on FROM (SELECT amount, id, created_on FROM other_transactions ORDER BY amount DESC LIMIT ?)) ORDER BY amount ASC" 5]))))
(deftest compare-expressions-test (deftest compare-expressions-test
(testing "Sequences should be fns when in value/comparison spots" (testing "Sequences should be fns when in value/comparison spots"
@ -144,7 +144,7 @@
{:select [:foo] :from [:bar2]}] {:select [:foo] :from [:bar2]}]
:with [[[:bar {:columns [:spam :eggs]}] :with [[[:bar {:columns [:spam :eggs]}]
{:values [[1 2] [3 4] [5 6]]}]]}) {:values [[1 2] [3 4] [5 6]]}]]})
["WITH bar (spam, eggs) AS (VALUES (?, ?), (?, ?), (?, ?)) SELECT foo FROM bar1 UNION SELECT foo FROM bar2" 1 2 3 4 5 6]))) ["WITH bar (spam, eggs) AS (VALUES (?, ?), (?, ?), (?, ?)) (SELECT foo FROM bar1) UNION (SELECT foo FROM bar2)" 1 2 3 4 5 6])))
(deftest union-all-with-cte (deftest union-all-with-cte
@ -152,7 +152,7 @@
{:select [:foo] :from [:bar2]}] {:select [:foo] :from [:bar2]}]
:with [[[:bar {:columns [:spam :eggs]}] :with [[[:bar {:columns [:spam :eggs]}]
{:values [[1 2] [3 4] [5 6]]}]]}) {:values [[1 2] [3 4] [5 6]]}]]})
["WITH bar (spam, eggs) AS (VALUES (?, ?), (?, ?), (?, ?)) SELECT foo FROM bar1 UNION ALL SELECT foo FROM bar2" 1 2 3 4 5 6]))) ["WITH bar (spam, eggs) AS (VALUES (?, ?), (?, ?), (?, ?)) (SELECT foo FROM bar1) UNION ALL (SELECT foo FROM bar2)" 1 2 3 4 5 6])))
(deftest parameterizer-none (deftest parameterizer-none
(testing "array parameter" (testing "array parameter"
@ -168,7 +168,7 @@
:with [[[:bar {:columns [:spam :eggs]}] :with [[[:bar {:columns [:spam :eggs]}]
{:values [[1 2] [3 4] [5 6]]}]]} {:values [[1 2] [3 4] [5 6]]}]]}
:parameterizer :none) :parameterizer :none)
["WITH bar (spam, eggs) AS (VALUES (1, 2), (3, 4), (5, 6)) SELECT foo FROM bar1 UNION SELECT foo FROM bar2"])))) ["WITH bar (spam, eggs) AS (VALUES (1, 2), (3, 4), (5, 6)) (SELECT foo FROM bar1) UNION (SELECT foo FROM bar2)"]))))
(deftest where-and (deftest where-and
(testing "should ignore a nil predicate" (testing "should ignore a nil predicate"