| 1 |
use super::field_alias_mapper::FieldAliasMapper; |
| 2 |
use super::{Alias, AliasSource}; |
| 3 |
|
| 4 |
use crate::dsl; |
| 5 |
#[cfg(feature = "postgres_backend")] |
| 6 |
use crate::expression::SelectableExpression; |
| 7 |
use crate::expression::{Expression, TypedExpressionType, ValidGrouping}; |
| 8 |
use crate::expression_methods::EqAll; |
| 9 |
use crate::query_builder::{combination_clause, AsQuery, FromClause, Query, SelectStatement}; |
| 10 |
use crate::query_dsl::methods::*; |
| 11 |
use crate::query_dsl::{CombineDsl, QueryDsl, RunQueryDsl}; |
| 12 |
use crate::query_source::{QuerySource, Table}; |
| 13 |
|
| 14 |
impl<S: AliasSource> QueryDsl for Alias<S> {} |
| 15 |
|
| 16 |
impl<S, Predicate> FilterDsl<Predicate> for Alias<S> |
| 17 |
where |
| 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 |
|
| 28 |
impl<S, Selection> SelectDsl<Selection> for Alias<S> |
| 29 |
where |
| 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 |
|
| 41 |
impl<S, PK> FindDsl<PK> for Alias<S> |
| 42 |
where |
| 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 |
|
| 63 |
impl<'a, S, DB> BoxedDsl<'a, DB> for Alias<S> |
| 64 |
where |
| 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 |
|
| 78 |
impl<S> CombineDsl for Alias<S> |
| 79 |
where |
| 80 |
S: AliasSource, |
| 81 |
S::Target: Table, |
| 82 |
Self: AsQuery, |
| 83 |
{ |
| 84 |
type Query = <Self as AsQuery>::Query; |
| 85 |
|
| 86 |
fn union<Rhs>(self, rhs: Rhs) -> dsl::Union<Self, Rhs> |
| 87 |
where |
| 88 |
Rhs: AsQuery<SqlType = <<Self as AsQuery>::Query as Query>::SqlType>, |
| 89 |
{ |
| 90 |
combination_clause::CombinationClause::new( |
| 91 |
combination_clause::Union, |
| 92 |
combination_clause::Distinct, |
| 93 |
self.as_query(), |
| 94 |
rhs.as_query(), |
| 95 |
) |
| 96 |
} |
| 97 |
|
| 98 |
fn union_all<Rhs>(self, rhs: Rhs) -> dsl::UnionAll<Self, Rhs> |
| 99 |
where |
| 100 |
Rhs: AsQuery<SqlType = <<Self as AsQuery>::Query as Query>::SqlType>, |
| 101 |
{ |
| 102 |
combination_clause::CombinationClause::new( |
| 103 |
combination_clause::Union, |
| 104 |
combination_clause::All, |
| 105 |
self.as_query(), |
| 106 |
rhs.as_query(), |
| 107 |
) |
| 108 |
} |
| 109 |
|
| 110 |
fn intersect<Rhs>(self, rhs: Rhs) -> dsl::Intersect<Self, Rhs> |
| 111 |
where |
| 112 |
Rhs: AsQuery<SqlType = <<Self as AsQuery>::Query as Query>::SqlType>, |
| 113 |
{ |
| 114 |
combination_clause::CombinationClause::new( |
| 115 |
combination_clause::Intersect, |
| 116 |
combination_clause::Distinct, |
| 117 |
self.as_query(), |
| 118 |
rhs.as_query(), |
| 119 |
) |
| 120 |
} |
| 121 |
|
| 122 |
fn intersect_all<Rhs>(self, rhs: Rhs) -> dsl::IntersectAll<Self, Rhs> |
| 123 |
where |
| 124 |
Rhs: AsQuery<SqlType = <<Self as AsQuery>::Query as Query>::SqlType>, |
| 125 |
{ |
| 126 |
combination_clause::CombinationClause::new( |
| 127 |
combination_clause::Intersect, |
| 128 |
combination_clause::All, |
| 129 |
self.as_query(), |
| 130 |
rhs.as_query(), |
| 131 |
) |
| 132 |
} |
| 133 |
|
| 134 |
fn except<Rhs>(self, rhs: Rhs) -> dsl::Except<Self, Rhs> |
| 135 |
where |
| 136 |
Rhs: AsQuery<SqlType = <<Self as AsQuery>::Query as Query>::SqlType>, |
| 137 |
{ |
| 138 |
combination_clause::CombinationClause::new( |
| 139 |
combination_clause::Except, |
| 140 |
combination_clause::Distinct, |
| 141 |
self.as_query(), |
| 142 |
rhs.as_query(), |
| 143 |
) |
| 144 |
} |
| 145 |
|
| 146 |
fn except_all<Rhs>(self, rhs: Rhs) -> dsl::ExceptAll<Self, Rhs> |
| 147 |
where |
| 148 |
Rhs: AsQuery<SqlType = <<Self as AsQuery>::Query as Query>::SqlType>, |
| 149 |
{ |
| 150 |
combination_clause::CombinationClause::new( |
| 151 |
combination_clause::Except, |
| 152 |
combination_clause::All, |
| 153 |
self.as_query(), |
| 154 |
rhs.as_query(), |
| 155 |
) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
#[cfg(feature = "postgres_backend")] |
| 160 |
impl<S, Selection> DistinctOnDsl<Selection> for Alias<S> |
| 161 |
where |
| 162 |
S: AliasSource, |
| 163 |
Selection: SelectableExpression<Self>, |
| 164 |
Self: QuerySource + AsQuery<Query = SelectStatement<FromClause<Self>>>, |
| 165 |
SelectStatement<FromClause<Self>>: DistinctOnDsl<Selection>, |
| 166 |
<Self as QuerySource>::DefaultSelection: |
| 167 |
Expression<SqlType = <Self as AsQuery>::SqlType> + ValidGrouping<()>, |
| 168 |
<Self as AsQuery>::SqlType: TypedExpressionType, |
| 169 |
{ |
| 170 |
type Output = dsl::DistinctOn<SelectStatement<FromClause<Self>>, Selection>; |
| 171 |
|
| 172 |
fn distinct_on(self, selection: Selection) -> dsl::DistinctOn<Self, Selection> { |
| 173 |
DistinctOnDsl::distinct_on(self.as_query(), selection) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
impl<S, Predicate> OrFilterDsl<Predicate> for Alias<S> |
| 178 |
where |
| 179 |
Self: AsQuery, |
| 180 |
<Self as AsQuery>::Query: OrFilterDsl<Predicate>, |
| 181 |
{ |
| 182 |
type Output = dsl::OrFilter<<Self as AsQuery>::Query, Predicate>; |
| 183 |
|
| 184 |
fn or_filter(self, predicate: Predicate) -> Self::Output { |
| 185 |
self.as_query().or_filter(predicate) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
impl<S, Expr> GroupByDsl<Expr> for Alias<S> |
| 190 |
where |
| 191 |
Expr: Expression, |
| 192 |
Self: QuerySource + AsQuery<Query = SelectStatement<FromClause<Self>>>, |
| 193 |
<Self as QuerySource>::DefaultSelection: |
| 194 |
Expression<SqlType = <Self as AsQuery>::SqlType> + ValidGrouping<()>, |
| 195 |
<Self as AsQuery>::SqlType: TypedExpressionType, |
| 196 |
<Self as AsQuery>::Query: GroupByDsl<Expr>, |
| 197 |
{ |
| 198 |
type Output = dsl::GroupBy<SelectStatement<FromClause<Self>>, Expr>; |
| 199 |
|
| 200 |
fn group_by(self, expr: Expr) -> dsl::GroupBy<Self, Expr> { |
| 201 |
GroupByDsl::group_by(self.as_query(), expr) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
impl<S> LimitDsl for Alias<S> |
| 206 |
where |
| 207 |
Self: AsQuery, |
| 208 |
<Self as AsQuery>::Query: LimitDsl, |
| 209 |
{ |
| 210 |
type Output = <<Self as AsQuery>::Query as LimitDsl>::Output; |
| 211 |
|
| 212 |
fn limit(self, limit: i64) -> Self::Output { |
| 213 |
self.as_query().limit(limit) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
impl<S, Lock> LockingDsl<Lock> for Alias<S> |
| 218 |
where |
| 219 |
Self: QuerySource + AsQuery<Query = SelectStatement<FromClause<Self>>>, |
| 220 |
<Self as QuerySource>::DefaultSelection: |
| 221 |
Expression<SqlType = <Self as AsQuery>::SqlType> + ValidGrouping<()>, |
| 222 |
<Self as AsQuery>::SqlType: TypedExpressionType, |
| 223 |
{ |
| 224 |
type Output = <SelectStatement<FromClause<Self>> as LockingDsl<Lock>>::Output; |
| 225 |
|
| 226 |
fn with_lock(self, lock: Lock) -> Self::Output { |
| 227 |
self.as_query().with_lock(lock) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
impl<S: AliasSource, Conn> RunQueryDsl<Conn> for Alias<S> {} |
| 232 |
|
| 233 |
impl<S> OffsetDsl for Alias<S> |
| 234 |
where |
| 235 |
Self: AsQuery, |
| 236 |
<Self as AsQuery>::Query: OffsetDsl, |
| 237 |
{ |
| 238 |
type Output = <<Self as AsQuery>::Query as OffsetDsl>::Output; |
| 239 |
|
| 240 |
fn offset(self, offset: i64) -> Self::Output { |
| 241 |
self.as_query().offset(offset) |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
impl<S, Expr> OrderDsl<Expr> for Alias<S> |
| 246 |
where |
| 247 |
Expr: Expression, |
| 248 |
Self: AsQuery, |
| 249 |
<Self as AsQuery>::Query: OrderDsl<Expr>, |
| 250 |
{ |
| 251 |
type Output = <<Self as AsQuery>::Query as OrderDsl<Expr>>::Output; |
| 252 |
|
| 253 |
fn order(self, expr: Expr) -> Self::Output { |
| 254 |
self.as_query().order(expr) |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
impl<S, Expr> ThenOrderDsl<Expr> for Alias<S> |
| 259 |
where |
| 260 |
Expr: Expression, |
| 261 |
Self: AsQuery, |
| 262 |
<Self as AsQuery>::Query: ThenOrderDsl<Expr>, |
| 263 |
{ |
| 264 |
type Output = <<Self as AsQuery>::Query as ThenOrderDsl<Expr>>::Output; |
| 265 |
|
| 266 |
fn then_order_by(self, expr: Expr) -> Self::Output { |
| 267 |
self.as_query().then_order_by(expr) |
| 268 |
} |
| 269 |
} |