fixes #365
This commit is contained in:
parent
3d0a9ba79c
commit
6aee04e25c
6 changed files with 19 additions and 18 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
* 2.0.next in progress
|
* 2.0.next in progress
|
||||||
* Fix #367 by supporting parameters in subexpressions around `IS NULL` / `IS NOT NULL` tests.
|
* Fix #367 by supporting parameters in subexpressions around `IS NULL` / `IS NOT NULL` tests.
|
||||||
|
* Fix #365 -- a regression from 1.x -- where subclauses for `UNION`, `EXCEPT`, etc were incorrectly parenthesized.
|
||||||
* Update `build-clj` to v0.5.0.
|
* Update `build-clj` to v0.5.0.
|
||||||
|
|
||||||
* 2.0.813 -- 2021-09-25
|
* 2.0.813 -- 2021-09-25
|
||||||
|
|
|
||||||
|
|
@ -471,7 +471,7 @@ Queries may be combined with a `:union`, `:union-all`, `:intersect` or `:except`
|
||||||
```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"]
|
||||||
```
|
```
|
||||||
|
|
||||||
There are also helpers for each of those:
|
There are also helpers for each of those:
|
||||||
|
|
@ -479,7 +479,7 @@ There are also helpers for each of those:
|
||||||
```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"]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -298,7 +298,7 @@ between those clauses.
|
||||||
```clojure
|
```clojure
|
||||||
user=> (sql/format '{union [{select (id,status) from (table-a)}
|
user=> (sql/format '{union [{select (id,status) from (table-a)}
|
||||||
{select (id,(event status) from (table-b))}]})
|
{select (id,(event status) from (table-b))}]})
|
||||||
["(SELECT id, status FROM table_a) UNION (SELECT id, event AS status, from, table_b)"]
|
["SELECT id, status FROM table_a UNION SELECT id, event AS status, from, table_b"]
|
||||||
```
|
```
|
||||||
|
|
||||||
## select, select-distinct
|
## select, select-distinct
|
||||||
|
|
|
||||||
|
|
@ -338,7 +338,7 @@
|
||||||
(reduce (fn [[sql params] [sql' & params']]
|
(reduce (fn [[sql params] [sql' & params']]
|
||||||
[(conj sql sql') (if params' (into params params') params)])
|
[(conj sql sql') (if params' (into params params') params)])
|
||||||
[[] []]
|
[[] []]
|
||||||
(map #(format-dsl % {:nested true}) xs))]
|
(map #(format-dsl %) xs))]
|
||||||
(into [(str/join (str " " (sql-kw k) " ") sqls)] params)))
|
(into [(str/join (str " " (sql-kw k) " ") sqls)] params)))
|
||||||
|
|
||||||
(defn format-expr-list
|
(defn format-expr-list
|
||||||
|
|
|
||||||
|
|
@ -319,7 +319,7 @@
|
||||||
|
|
||||||
(deftest values-except-select
|
(deftest values-except-select
|
||||||
(testing "select which values are not not present in a table"
|
(testing "select which values are not not present in a table"
|
||||||
(is (= ["(VALUES (?), (?), (?)) EXCEPT (SELECT id FROM images)" 4 5 6]
|
(is (= ["VALUES (?), (?), (?) EXCEPT SELECT id FROM images" 4 5 6]
|
||||||
(sql/format
|
(sql/format
|
||||||
{:except
|
{:except
|
||||||
[{:values [[4] [5] [6]]}
|
[{:values [[4] [5] [6]]}
|
||||||
|
|
@ -327,7 +327,7 @@
|
||||||
|
|
||||||
(deftest select-except-select
|
(deftest select-except-select
|
||||||
(testing "select which rows are not present in another table"
|
(testing "select which rows are not present in another table"
|
||||||
(is (= ["(SELECT ip) EXCEPT (SELECT ip FROM ip_location)"]
|
(is (= ["SELECT ip EXCEPT SELECT ip FROM ip_location"]
|
||||||
(sql/format
|
(sql/format
|
||||||
{:except
|
{:except
|
||||||
[{:select [:ip]}
|
[{:select [:ip]}
|
||||||
|
|
@ -335,7 +335,7 @@
|
||||||
|
|
||||||
(deftest values-except-all-select
|
(deftest values-except-all-select
|
||||||
(testing "select which values are not not present in a table"
|
(testing "select which values are not not present in a table"
|
||||||
(is (= ["(VALUES (?), (?), (?)) EXCEPT ALL (SELECT id FROM images)" 4 5 6]
|
(is (= ["VALUES (?), (?), (?) EXCEPT ALL SELECT id FROM images" 4 5 6]
|
||||||
(sql/format
|
(sql/format
|
||||||
{:except-all
|
{:except-all
|
||||||
[{:values [[4] [5] [6]]}
|
[{:values [[4] [5] [6]]}
|
||||||
|
|
@ -343,7 +343,7 @@
|
||||||
|
|
||||||
(deftest select-except-all-select
|
(deftest select-except-all-select
|
||||||
(testing "select which rows are not present in another table"
|
(testing "select which rows are not present in another table"
|
||||||
(is (= ["(SELECT ip) EXCEPT ALL (SELECT ip FROM ip_location)"]
|
(is (= ["SELECT ip EXCEPT ALL SELECT ip FROM ip_location"]
|
||||||
(sql/format
|
(sql/format
|
||||||
{:except-all
|
{:except-all
|
||||||
[{:select [:ip]}
|
[{:select [:ip]}
|
||||||
|
|
|
||||||
|
|
@ -184,30 +184,30 @@
|
||||||
;; 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"]))
|
||||||
|
|
||||||
(testing "union complex values"
|
(testing "union complex values"
|
||||||
(is (= (format {:union [{:select [:foo] :from [:bar1]}
|
(is (= (format {:union [{:select [:foo] :from [:bar1]}
|
||||||
{: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)"
|
["WITH bar (spam, eggs) AS (VALUES (?, ?), (?, ?), (?, ?)) SELECT foo FROM bar1 UNION SELECT foo FROM bar2"
|
||||||
1 2 3 4 5 6]))))
|
1 2 3 4 5 6]))))
|
||||||
|
|
||||||
(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 except-test
|
(deftest except-test
|
||||||
(is (= (format {:except [{:select [:foo] :from [:bar1]}
|
(is (= (format {:except [{:select [:foo] :from [:bar1]}
|
||||||
{:select [:foo] :from [:bar2]}]})
|
{:select [:foo] :from [:bar2]}]})
|
||||||
["(SELECT foo FROM bar1) EXCEPT (SELECT foo FROM bar2)"])))
|
["SELECT foo FROM bar1 EXCEPT 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"
|
||||||
|
|
@ -221,7 +221,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"
|
||||||
|
|
@ -253,14 +253,14 @@
|
||||||
{: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
|
||||||
(is (= (format {:union-all [{:select [:foo] :from [:bar1]}
|
(is (= (format {:union-all [{:select [:foo] :from [:bar1]}
|
||||||
{: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"
|
||||||
|
|
@ -276,7 +276,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]]}]]}
|
||||||
{:inline true})
|
{:inline true})
|
||||||
["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 inline-was-parameterizer-none
|
(deftest inline-was-parameterizer-none
|
||||||
(testing "array parameter"
|
(testing "array parameter"
|
||||||
|
|
@ -293,7 +293,7 @@
|
||||||
:with [[[:bar {:columns [:spam :eggs]}]
|
:with [[[:bar {:columns [:spam :eggs]}]
|
||||||
{:values (mapv #(mapv vector (repeat :inline) %)
|
{:values (mapv #(mapv vector (repeat :inline) %)
|
||||||
[[1 2] [3 4] [5 6]])}]]})
|
[[1 2] [3 4] [5 6]])}]]})
|
||||||
["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 similar-regex-tests
|
(deftest similar-regex-tests
|
||||||
(testing "basic similar to"
|
(testing "basic similar to"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue