Skip to main content

diesel/query_dsl/
combine_dsl.rs

1use crate::dsl;
2use crate::query_builder::combination_clause::{
3    All, CombinationClause, Distinct, Except, Intersect, Union,
4};
5use crate::query_builder::{AsQuery, Query};
6use crate::query_source::QueryRelation;
7
8/// Extension trait to combine queries using a combinator like `UNION`, `INTERSECT` or `EXCEPT`
9/// with or without `ALL` rule for duplicates
10pub trait CombineDsl {
11    /// What kind of query does this type represent?
12    type Query: Query;
13
14    /// Combine two queries using a SQL `UNION`
15    ///
16    /// # Examples
17    /// ```rust
18    /// # extern crate diesel;
19    /// # include!("../doctest_setup.rs");
20    /// # use schema::{users, animals};
21    /// # use crate::diesel::query_dsl::positional_order_dsl::PositionalOrderDsl;
22    /// #
23    /// # // Reading the query result requires reading memory allocated by the
24    /// # // native sqlite library, which miri cannot track provenance for.
25    /// # #[cfg(miri)]
26    /// # fn main() {}
27    /// # #[cfg(not(miri))]
28    /// # fn main() {
29    /// #     use self::users::dsl::{users, name as user_name};
30    /// #     use self::animals::dsl::{animals, name as animal_name};
31    /// #     let connection = &mut establish_connection();
32    /// let data = users
33    ///     .select(user_name.nullable())
34    ///     .union(
35    ///         animals
36    ///             .select(animal_name)
37    ///             .filter(animal_name.is_not_null()),
38    ///     )
39    /// #   .positional_order_by(1)
40    ///     .load(connection);
41    ///
42    /// let expected_data = vec![
43    ///     Some(String::from("Jack")),
44    ///     Some(String::from("Sean")),
45    ///     Some(String::from("Tess")),
46    /// ];
47    /// assert_eq!(Ok(expected_data), data);
48    /// # }
49    /// ```
50    fn union<Rhs>(self, rhs: Rhs) -> dsl::Union<Self, Rhs>
51    where
52        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>;
53
54    /// Combine two queries using a SQL `UNION ALL`
55    fn union_all<Rhs>(self, rhs: Rhs) -> dsl::UnionAll<Self, Rhs>
56    where
57        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>;
58
59    /// Combine two queries using a SQL `INTERSECT`
60    fn intersect<Rhs>(self, rhs: Rhs) -> dsl::Intersect<Self, Rhs>
61    where
62        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>;
63
64    /// Combine two queries using a SQL `INTERSECT ALL`
65    fn intersect_all<Rhs>(self, rhs: Rhs) -> dsl::IntersectAll<Self, Rhs>
66    where
67        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>;
68
69    /// Combine two queries using a SQL `EXCEPT`
70    fn except<Rhs>(self, rhs: Rhs) -> dsl::Except<Self, Rhs>
71    where
72        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>;
73
74    /// Combine two queries using a SQL `EXCEPT ALL`
75    fn except_all<Rhs>(self, rhs: Rhs) -> dsl::ExceptAll<Self, Rhs>
76    where
77        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>;
78}
79
80impl<T: QueryRelation> CombineDsl for T {
81    type Query = T::Query;
82
83    fn union<Rhs>(self, rhs: Rhs) -> dsl::Union<Self, Rhs>
84    where
85        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
86    {
87        CombinationClause::new(Union, Distinct, self.as_query(), rhs.as_query())
88    }
89
90    fn union_all<Rhs>(self, rhs: Rhs) -> dsl::UnionAll<Self, Rhs>
91    where
92        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
93    {
94        CombinationClause::new(Union, All, self.as_query(), rhs.as_query())
95    }
96
97    fn intersect<Rhs>(self, rhs: Rhs) -> dsl::Intersect<Self, Rhs>
98    where
99        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
100    {
101        CombinationClause::new(Intersect, Distinct, self.as_query(), rhs.as_query())
102    }
103
104    fn intersect_all<Rhs>(self, rhs: Rhs) -> dsl::IntersectAll<Self, Rhs>
105    where
106        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
107    {
108        CombinationClause::new(Intersect, All, self.as_query(), rhs.as_query())
109    }
110
111    fn except<Rhs>(self, rhs: Rhs) -> dsl::Except<Self, Rhs>
112    where
113        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
114    {
115        CombinationClause::new(Except, Distinct, self.as_query(), rhs.as_query())
116    }
117
118    fn except_all<Rhs>(self, rhs: Rhs) -> dsl::ExceptAll<Self, Rhs>
119    where
120        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
121    {
122        CombinationClause::new(Except, All, self.as_query(), rhs.as_query())
123    }
124}