Adds support for except set operation #254
This commit is contained in:
parent
5bc1fafddd
commit
e7dce6b347
4 changed files with 15 additions and 2 deletions
|
|
@ -295,9 +295,9 @@ If you want to delete everything from a table, you can use `truncate`:
|
||||||
=> ["TRUNCATE films"]
|
=> ["TRUNCATE films"]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Unions
|
### Set operations
|
||||||
|
|
||||||
Queries may be united within a :union or :union-all keyword:
|
Queries may be combined within a :union, :union-all, :intersect or :except keyword:
|
||||||
|
|
||||||
```clojure
|
```clojure
|
||||||
(sql/format {:union [(-> (select :*) (from :foo))
|
(sql/format {:union [(-> (select :*) (from :foo))
|
||||||
|
|
|
||||||
|
|
@ -226,6 +226,7 @@
|
||||||
:intersect 35
|
:intersect 35
|
||||||
:union 40
|
:union 40
|
||||||
:union-all 45
|
:union-all 45
|
||||||
|
:except 47
|
||||||
:select 50
|
:select 50
|
||||||
:insert-into 60
|
:insert-into 60
|
||||||
:update 70
|
:update 70
|
||||||
|
|
@ -686,6 +687,10 @@
|
||||||
(binding [*subquery?* false]
|
(binding [*subquery?* false]
|
||||||
(string/join " INTERSECT " (map to-sql maps))))
|
(string/join " INTERSECT " (map to-sql maps))))
|
||||||
|
|
||||||
|
(defmethod format-clause :except [[_ maps] _]
|
||||||
|
(binding [*subquery?* false]
|
||||||
|
(string/join " EXCEPT " (map to-sql maps))))
|
||||||
|
|
||||||
(defmethod fn-handler "case" [_ & clauses]
|
(defmethod fn-handler "case" [_ & clauses]
|
||||||
(str "CASE "
|
(str "CASE "
|
||||||
(space-join
|
(space-join
|
||||||
|
|
|
||||||
|
|
@ -345,3 +345,6 @@
|
||||||
|
|
||||||
(defmethod build-clause :intersect [_ m maps]
|
(defmethod build-clause :intersect [_ m maps]
|
||||||
(assoc m :intersect maps))
|
(assoc m :intersect maps))
|
||||||
|
|
||||||
|
(defmethod build-clause :except [_ m maps]
|
||||||
|
(assoc m :except maps))
|
||||||
|
|
|
||||||
|
|
@ -126,6 +126,11 @@
|
||||||
{: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
|
||||||
|
(is (= (format {:except [{:select [:foo] :from [:bar1]}
|
||||||
|
{: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"
|
||||||
(is (= (format
|
(is (= (format
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue