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