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::Table;
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    /// # fn main() {
24    /// #     use self::users::dsl::{users, name as user_name};
25    /// #     use self::animals::dsl::{animals, name as animal_name};
26    /// #     let connection = &mut establish_connection();
27    /// let data = users
28    ///     .select(user_name.nullable())
29    ///     .union(
30    ///         animals
31    ///             .select(animal_name)
32    ///             .filter(animal_name.is_not_null()),
33    ///     )
34    /// #   .positional_order_by(1)
35    ///     .load(connection);
36    ///
37    /// let expected_data = vec![
38    ///     Some(String::from("Jack")),
39    ///     Some(String::from("Sean")),
40    ///     Some(String::from("Tess")),
41    /// ];
42    /// assert_eq!(Ok(expected_data), data);
43    /// # }
44    /// ```
45    fn union<Rhs>(self, rhs: Rhs) -> dsl::Union<Self, Rhs>
46    where
47        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>;
48
49    /// Combine two queries using a SQL `UNION ALL`
50    fn union_all<Rhs>(self, rhs: Rhs) -> dsl::UnionAll<Self, Rhs>
51    where
52        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>;
53
54    /// Combine two queries using a SQL `INTERSECT`
55    fn intersect<Rhs>(self, rhs: Rhs) -> dsl::Intersect<Self, Rhs>
56    where
57        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>;
58
59    /// Combine two queries using a SQL `INTERSECT ALL`
60    fn intersect_all<Rhs>(self, rhs: Rhs) -> dsl::IntersectAll<Self, Rhs>
61    where
62        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>;
63
64    /// Combine two queries using a SQL `EXCEPT`
65    fn except<Rhs>(self, rhs: Rhs) -> dsl::Except<Self, Rhs>
66    where
67        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>;
68
69    /// Combine two queries using a SQL `EXCEPT ALL`
70    fn except_all<Rhs>(self, rhs: Rhs) -> dsl::ExceptAll<Self, Rhs>
71    where
72        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>;
73}
74
75impl<T: Table> CombineDsl for T {
76    type Query = T::Query;
77
78    fn union<Rhs>(self, rhs: Rhs) -> dsl::Union<Self, Rhs>
79    where
80        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
81    {
82        CombinationClause::new(Union, Distinct, self.as_query(), rhs.as_query())
83    }
84
85    fn union_all<Rhs>(self, rhs: Rhs) -> dsl::UnionAll<Self, Rhs>
86    where
87        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
88    {
89        CombinationClause::new(Union, All, self.as_query(), rhs.as_query())
90    }
91
92    fn intersect<Rhs>(self, rhs: Rhs) -> dsl::Intersect<Self, Rhs>
93    where
94        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
95    {
96        CombinationClause::new(Intersect, Distinct, self.as_query(), rhs.as_query())
97    }
98
99    fn intersect_all<Rhs>(self, rhs: Rhs) -> dsl::IntersectAll<Self, Rhs>
100    where
101        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
102    {
103        CombinationClause::new(Intersect, All, self.as_query(), rhs.as_query())
104    }
105
106    fn except<Rhs>(self, rhs: Rhs) -> dsl::Except<Self, Rhs>
107    where
108        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
109    {
110        CombinationClause::new(Except, Distinct, self.as_query(), rhs.as_query())
111    }
112
113    fn except_all<Rhs>(self, rhs: Rhs) -> dsl::ExceptAll<Self, Rhs>
114    where
115        Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
116    {
117        CombinationClause::new(Except, All, self.as_query(), rhs.as_query())
118    }
119}