summaryrefslogtreecommitdiff
path: root/src/clojure_sql/dsl.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/clojure_sql/dsl.clj')
-rw-r--r--src/clojure_sql/dsl.clj24
1 files changed, 22 insertions, 2 deletions
diff --git a/src/clojure_sql/dsl.clj b/src/clojure_sql/dsl.clj
index 6ba81bc..0a05763 100644
--- a/src/clojure_sql/dsl.clj
+++ b/src/clojure_sql/dsl.clj
@@ -177,7 +177,8 @@
(and (nil? (:group query))
(nil? (:having query))
(nil? (:take query))
- (nil? (:drop query))))
+ (nil? (:drop query))
+ (nil? (:set-operation query))))
(defn ^:private convert-to-subquery [query]
(-> (table query)
@@ -195,7 +196,7 @@
(def ^:private valid-join-type? (comp boolean #{:cross :inner :outer :full-outer}))
(defn join
- "Join two queries into one query The fields of the resultant query
+ "Join two queries into one query. The fields of the resultant query
will be the union of the argument queries.
If `type` is not provided then the join type will be automatically
@@ -376,3 +377,22 @@
(if-let [old-drop (:drop query)]
(assoc query :drop (+ old-drop n))
(assoc query :drop n))))
+
+(defn ^:private union-compatible? [& queries]
+ (apply = (map (comp set keys :fields) queries)))
+
+(defn union
+ "Combine the results of two queries"
+ [& queries]
+ (assert (apply union-compatible? queries) "Unioned queries must expose the same fields.")
+ (assoc (q/->Query)
+ :set-operation :union
+ :queries queries))
+
+(defn intersection
+ "Take the common rows in two queries"
+ [& queries]
+ (assert (apply union-compatible? queries) "Unioned queries must expose the same fields.")
+ (assoc (q/->Query)
+ :set-operation :intersect
+ :queries queries))