diesel/query_source/aliasing/
dsl_impls.rs1use super::field_alias_mapper::FieldAliasMapper;
2use super::{Alias, AliasSource};
3
4use crate::dsl;
5#[cfg(feature = "postgres_backend")]
6use crate::expression::SelectableExpression;
7use crate::expression::{Expression, TypedExpressionType, ValidGrouping};
8use crate::expression_methods::EqAll;
9use crate::query_builder::{AsQuery, FromClause, Query, SelectStatement, combination_clause};
10use crate::query_dsl::methods::*;
11use crate::query_dsl::{CombineDsl, QueryDsl, RunQueryDslSupport};
12use crate::query_source::{QuerySource, Table};
13
14impl<S: AliasSource> QueryDsl for Alias<S> {}
15
16impl<S, Predicate> FilterDsl<Predicate> for Alias<S>
17where
18 Self: AsQuery,
19 <Self as AsQuery>::Query: FilterDsl<Predicate>,
20{
21 type Output = dsl::Filter<<Self as AsQuery>::Query, Predicate>;
22
23 fn filter(self, predicate: Predicate) -> Self::Output {
24 self.as_query().filter(predicate)
25 }
26}
27
28impl<S, Selection> SelectDsl<Selection> for Alias<S>
29where
30 Selection: Expression,
31 Self: AsQuery,
32 <Self as AsQuery>::Query: SelectDsl<Selection>,
33{
34 type Output = dsl::Select<<Self as AsQuery>::Query, Selection>;
35
36 fn select(self, selection: Selection) -> Self::Output {
37 self.as_query().select(selection)
38 }
39}
40
41impl<S, PK> FindDsl<PK> for Alias<S>
42where
43 S: AliasSource,
44 S::Target: Table,
45 <S::Target as Table>::PrimaryKey: FieldAliasMapper<S>,
46 <<S::Target as Table>::PrimaryKey as FieldAliasMapper<S>>::Out: EqAll<PK>,
47 Self: FilterDsl<
48 <<<S::Target as Table>::PrimaryKey as FieldAliasMapper<S>>::Out as EqAll<PK>>::Output,
49 >,
50{
51 type Output = dsl::Filter<
52 Self,
53 <<<S::Target as Table>::PrimaryKey as FieldAliasMapper<S>>::Out as EqAll<PK>>::Output,
54 >;
55
56 fn find(self, id: PK) -> Self::Output {
57 let primary_key = self.source.target().primary_key();
58 let predicate = self.fields(primary_key).eq_all(id);
59 QueryDsl::filter(self, predicate)
60 }
61}
62
63impl<'a, S, DB> BoxedDsl<'a, DB> for Alias<S>
64where
65 Alias<S>: QuerySource + AsQuery<Query = SelectStatement<FromClause<Alias<S>>>>,
66 SelectStatement<FromClause<Alias<S>>>: BoxedDsl<'a, DB>,
67 <Alias<S> as QuerySource>::DefaultSelection:
68 Expression<SqlType = <Alias<S> as AsQuery>::SqlType> + ValidGrouping<()>,
69 <Alias<S> as AsQuery>::SqlType: TypedExpressionType,
70{
71 type Output = dsl::IntoBoxed<'a, SelectStatement<FromClause<Alias<S>>>, DB>;
72
73 fn internal_into_boxed(self) -> Self::Output {
74 self.as_query().internal_into_boxed()
75 }
76}
77
78impl<'a, S, DB> BoxedCloneDsl<'a, DB> for Alias<S>
79where
80 Alias<S>: QuerySource + AsQuery<Query = SelectStatement<FromClause<Alias<S>>>>,
81 SelectStatement<FromClause<Alias<S>>>: BoxedCloneDsl<'a, DB>,
82 <Alias<S> as QuerySource>::DefaultSelection:
83 Expression<SqlType = <Alias<S> as AsQuery>::SqlType> + ValidGrouping<()>,
84 <Alias<S> as AsQuery>::SqlType: TypedExpressionType,
85{
86 type Output = dsl::IntoBoxedClone<'a, SelectStatement<FromClause<Alias<S>>>, DB>;
87
88 fn internal_into_boxed_clone(self) -> Self::Output {
89 self.as_query().internal_into_boxed_clone()
90 }
91}
92
93impl<S> CombineDsl for Alias<S>
94where
95 S: AliasSource,
96 S::Target: Table,
97 Self: AsQuery,
98{
99 type Query = <Self as AsQuery>::Query;
100
101 fn union<Rhs>(self, rhs: Rhs) -> dsl::Union<Self, Rhs>
102 where
103 Rhs: AsQuery<SqlType = <<Self as AsQuery>::Query as Query>::SqlType>,
104 {
105 combination_clause::CombinationClause::new(
106 combination_clause::Union,
107 combination_clause::Distinct,
108 self.as_query(),
109 rhs.as_query(),
110 )
111 }
112
113 fn union_all<Rhs>(self, rhs: Rhs) -> dsl::UnionAll<Self, Rhs>
114 where
115 Rhs: AsQuery<SqlType = <<Self as AsQuery>::Query as Query>::SqlType>,
116 {
117 combination_clause::CombinationClause::new(
118 combination_clause::Union,
119 combination_clause::All,
120 self.as_query(),
121 rhs.as_query(),
122 )
123 }
124
125 fn intersect<Rhs>(self, rhs: Rhs) -> dsl::Intersect<Self, Rhs>
126 where
127 Rhs: AsQuery<SqlType = <<Self as AsQuery>::Query as Query>::SqlType>,
128 {
129 combination_clause::CombinationClause::new(
130 combination_clause::Intersect,
131 combination_clause::Distinct,
132 self.as_query(),
133 rhs.as_query(),
134 )
135 }
136
137 fn intersect_all<Rhs>(self, rhs: Rhs) -> dsl::IntersectAll<Self, Rhs>
138 where
139 Rhs: AsQuery<SqlType = <<Self as AsQuery>::Query as Query>::SqlType>,
140 {
141 combination_clause::CombinationClause::new(
142 combination_clause::Intersect,
143 combination_clause::All,
144 self.as_query(),
145 rhs.as_query(),
146 )
147 }
148
149 fn except<Rhs>(self, rhs: Rhs) -> dsl::Except<Self, Rhs>
150 where
151 Rhs: AsQuery<SqlType = <<Self as AsQuery>::Query as Query>::SqlType>,
152 {
153 combination_clause::CombinationClause::new(
154 combination_clause::Except,
155 combination_clause::Distinct,
156 self.as_query(),
157 rhs.as_query(),
158 )
159 }
160
161 fn except_all<Rhs>(self, rhs: Rhs) -> dsl::ExceptAll<Self, Rhs>
162 where
163 Rhs: AsQuery<SqlType = <<Self as AsQuery>::Query as Query>::SqlType>,
164 {
165 combination_clause::CombinationClause::new(
166 combination_clause::Except,
167 combination_clause::All,
168 self.as_query(),
169 rhs.as_query(),
170 )
171 }
172}
173
174#[cfg(feature = "postgres_backend")]
175impl<S, Selection> DistinctOnDsl<Selection> for Alias<S>
176where
177 S: AliasSource,
178 Selection: SelectableExpression<Self>,
179 Self: QuerySource + AsQuery<Query = SelectStatement<FromClause<Self>>>,
180 SelectStatement<FromClause<Self>>: DistinctOnDsl<Selection>,
181 <Self as QuerySource>::DefaultSelection:
182 Expression<SqlType = <Self as AsQuery>::SqlType> + ValidGrouping<()>,
183 <Self as AsQuery>::SqlType: TypedExpressionType,
184{
185 type Output = dsl::DistinctOn<SelectStatement<FromClause<Self>>, Selection>;
186
187 fn distinct_on(self, selection: Selection) -> dsl::DistinctOn<Self, Selection> {
188 DistinctOnDsl::distinct_on(self.as_query(), selection)
189 }
190}
191
192impl<S, Predicate> OrFilterDsl<Predicate> for Alias<S>
193where
194 Self: AsQuery,
195 <Self as AsQuery>::Query: OrFilterDsl<Predicate>,
196{
197 type Output = dsl::OrFilter<<Self as AsQuery>::Query, Predicate>;
198
199 fn or_filter(self, predicate: Predicate) -> Self::Output {
200 self.as_query().or_filter(predicate)
201 }
202}
203
204impl<S, Expr> GroupByDsl<Expr> for Alias<S>
205where
206 Expr: Expression,
207 Self: QuerySource + AsQuery<Query = SelectStatement<FromClause<Self>>>,
208 <Self as QuerySource>::DefaultSelection:
209 Expression<SqlType = <Self as AsQuery>::SqlType> + ValidGrouping<()>,
210 <Self as AsQuery>::SqlType: TypedExpressionType,
211 <Self as AsQuery>::Query: GroupByDsl<Expr>,
212{
213 type Output = dsl::GroupBy<SelectStatement<FromClause<Self>>, Expr>;
214
215 fn group_by(self, expr: Expr) -> dsl::GroupBy<Self, Expr> {
216 GroupByDsl::group_by(self.as_query(), expr)
217 }
218}
219
220impl<S> LimitDsl for Alias<S>
221where
222 Self: AsQuery,
223 <Self as AsQuery>::Query: LimitDsl,
224{
225 type Output = <<Self as AsQuery>::Query as LimitDsl>::Output;
226
227 fn limit(self, limit: i64) -> Self::Output {
228 self.as_query().limit(limit)
229 }
230}
231
232impl<S, Lock> LockingDsl<Lock> for Alias<S>
233where
234 Self: QuerySource + AsQuery<Query = SelectStatement<FromClause<Self>>>,
235 <Self as QuerySource>::DefaultSelection:
236 Expression<SqlType = <Self as AsQuery>::SqlType> + ValidGrouping<()>,
237 <Self as AsQuery>::SqlType: TypedExpressionType,
238{
239 type Output = <SelectStatement<FromClause<Self>> as LockingDsl<Lock>>::Output;
240
241 fn with_lock(self, lock: Lock) -> Self::Output {
242 self.as_query().with_lock(lock)
243 }
244}
245
246impl<S: AliasSource> RunQueryDslSupport for Alias<S> {}
247
248impl<S> OffsetDsl for Alias<S>
249where
250 Self: AsQuery,
251 <Self as AsQuery>::Query: OffsetDsl,
252{
253 type Output = <<Self as AsQuery>::Query as OffsetDsl>::Output;
254
255 fn offset(self, offset: i64) -> Self::Output {
256 self.as_query().offset(offset)
257 }
258}
259
260impl<S, Expr> OrderDsl<Expr> for Alias<S>
261where
262 Expr: Expression,
263 Self: AsQuery,
264 <Self as AsQuery>::Query: OrderDsl<Expr>,
265{
266 type Output = <<Self as AsQuery>::Query as OrderDsl<Expr>>::Output;
267
268 fn order(self, expr: Expr) -> Self::Output {
269 self.as_query().order(expr)
270 }
271}
272
273impl<S, Expr> ThenOrderDsl<Expr> for Alias<S>
274where
275 Expr: Expression,
276 Self: AsQuery,
277 <Self as AsQuery>::Query: ThenOrderDsl<Expr>,
278{
279 type Output = <<Self as AsQuery>::Query as ThenOrderDsl<Expr>>::Output;
280
281 fn then_order_by(self, expr: Expr) -> Self::Output {
282 self.as_query().then_order_by(expr)
283 }
284}