Adds support for except set operation #254

This commit is contained in:
Ted Coakley 2020-03-06 17:34:06 +00:00
parent 5bc1fafddd
commit e7dce6b347
4 changed files with 15 additions and 2 deletions

View file

@ -295,9 +295,9 @@ If you want to delete everything from a table, you can use `truncate`:
=> ["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
(sql/format {:union [(-> (select :*) (from :foo))

View file

@ -226,6 +226,7 @@
:intersect 35
:union 40
:union-all 45
:except 47
:select 50
:insert-into 60
:update 70
@ -686,6 +687,10 @@
(binding [*subquery?* false]
(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]
(str "CASE "
(space-join

View file

@ -345,3 +345,6 @@
(defmethod build-clause :intersect [_ m maps]
(assoc m :intersect maps))
(defmethod build-clause :except [_ m maps]
(assoc m :except maps))

View file

@ -126,6 +126,11 @@
{: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
(testing "The correct way to apply ORDER BY to various parts of a UNION"
(is (= (format