Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for except set operation #254 #255

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions src/honeysql/format.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
:intersect 35
:union 40
:union-all 45
:except 47
:select 50
:insert-into 60
:update 70
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/honeysql/helpers.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,6 @@

(defmethod build-clause :intersect [_ m maps]
(assoc m :intersect maps))

(defmethod build-clause :except [_ m maps]
(assoc m :except maps))
5 changes: 5 additions & 0 deletions test/honeysql/format_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down