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