| 1 |
use super::BoxedSelectStatement; |
| 2 |
use crate::associations::HasTable; |
| 3 |
use crate::backend::Backend; |
| 4 |
use crate::dsl::AsExprOf; |
| 5 |
use crate::expression::nullable::Nullable; |
| 6 |
use crate::expression::*; |
| 7 |
use crate::insertable::Insertable; |
| 8 |
use crate::query_builder::combination_clause::*; |
| 9 |
use crate::query_builder::distinct_clause::*; |
| 10 |
use crate::query_builder::from_clause::AsQuerySource; |
| 11 |
use crate::query_builder::from_clause::FromClause; |
| 12 |
use crate::query_builder::group_by_clause::*; |
| 13 |
use crate::query_builder::insert_statement::InsertFromSelect; |
| 14 |
use crate::query_builder::limit_clause::*; |
| 15 |
use crate::query_builder::limit_offset_clause::{BoxedLimitOffsetClause, LimitOffsetClause}; |
| 16 |
use crate::query_builder::locking_clause::*; |
| 17 |
use crate::query_builder::offset_clause::*; |
| 18 |
use crate::query_builder::order_clause::*; |
| 19 |
use crate::query_builder::select_clause::*; |
| 20 |
use crate::query_builder::update_statement::target::*; |
| 21 |
use crate::query_builder::where_clause::*; |
| 22 |
use crate::query_builder::NoFromClause; |
| 23 |
use crate::query_builder::{ |
| 24 |
AsQuery, IntoBoxedClause, Query, QueryFragment, SelectQuery, SelectStatement, |
| 25 |
}; |
| 26 |
use crate::query_dsl::group_by_dsl::ValidDistinctForGroupBy; |
| 27 |
use crate::query_dsl::methods::*; |
| 28 |
use crate::query_dsl::order_dsl::ValidOrderingForDistinct; |
| 29 |
use crate::query_dsl::*; |
| 30 |
use crate::query_source::joins::{Join, JoinOn, JoinTo}; |
| 31 |
use crate::query_source::QuerySource; |
| 32 |
use crate::sql_types::{BigInt, BoolOrNullableBool}; |
| 33 |
|
| 34 |
impl<F, D, W, O, LOf, G, H, LC, Rhs, Kind, On> InternalJoinDsl<Rhs, Kind, On> |
| 35 |
for SelectStatement<FromClause<F>, DefaultSelectClause<FromClause<F>>, D, W, O, LOf, G, H, LC> |
| 36 |
where |
| 37 |
F: QuerySource, |
| 38 |
Rhs: QuerySource, |
| 39 |
JoinOn<Join<F, Rhs, Kind>, On>: QuerySource, |
| 40 |
SelectStatement< |
| 41 |
FromClause<JoinOn<Join<F, Rhs, Kind>, On>>, |
| 42 |
DefaultSelectClause<FromClause<JoinOn<Join<F, Rhs, Kind>, On>>>, |
| 43 |
D, |
| 44 |
W, |
| 45 |
O, |
| 46 |
LOf, |
| 47 |
G, |
| 48 |
H, |
| 49 |
LC, |
| 50 |
>: AsQuery, |
| 51 |
{ |
| 52 |
type Output = SelectStatement< |
| 53 |
FromClause<JoinOn<Join<F, Rhs, Kind>, On>>, |
| 54 |
DefaultSelectClause<FromClause<JoinOn<Join<F, Rhs, Kind>, On>>>, |
| 55 |
D, |
| 56 |
W, |
| 57 |
O, |
| 58 |
LOf, |
| 59 |
G, |
| 60 |
H, |
| 61 |
LC, |
| 62 |
>; |
| 63 |
|
| 64 |
fn join(self, rhs: Rhs, kind: Kind, on: On) -> Self::Output { |
| 65 |
let from = FromClause::new(Join::new(self.from.source, rhs, kind).on(on)); |
| 66 |
SelectStatement::new( |
| 67 |
DefaultSelectClause::new(&from), |
| 68 |
from, |
| 69 |
self.distinct, |
| 70 |
self.where_clause, |
| 71 |
self.order, |
| 72 |
self.limit_offset, |
| 73 |
self.group_by, |
| 74 |
self.having, |
| 75 |
self.locking, |
| 76 |
) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
impl<F, S, D, W, O, LOf, G, H, LC, Rhs, Kind, On> InternalJoinDsl<Rhs, Kind, On> |
| 81 |
for SelectStatement<FromClause<F>, SelectClause<S>, D, W, O, LOf, G, H, LC> |
| 82 |
where |
| 83 |
F: QuerySource, |
| 84 |
Rhs: QuerySource, |
| 85 |
JoinOn<Join<F, Rhs, Kind>, On>: QuerySource, |
| 86 |
SelectStatement< |
| 87 |
FromClause<JoinOn<Join<F, Rhs, Kind>, On>>, |
| 88 |
SelectClause<S>, |
| 89 |
D, |
| 90 |
W, |
| 91 |
O, |
| 92 |
LOf, |
| 93 |
G, |
| 94 |
H, |
| 95 |
LC, |
| 96 |
>: AsQuery, |
| 97 |
{ |
| 98 |
type Output = SelectStatement< |
| 99 |
FromClause<JoinOn<Join<F, Rhs, Kind>, On>>, |
| 100 |
SelectClause<S>, |
| 101 |
D, |
| 102 |
W, |
| 103 |
O, |
| 104 |
LOf, |
| 105 |
G, |
| 106 |
H, |
| 107 |
LC, |
| 108 |
>; |
| 109 |
|
| 110 |
fn join(self, rhs: Rhs, kind: Kind, on: On) -> Self::Output { |
| 111 |
SelectStatement::new( |
| 112 |
self.select, |
| 113 |
FromClause::new(Join::new(self.from.source, rhs, kind).on(on)), |
| 114 |
self.distinct, |
| 115 |
self.where_clause, |
| 116 |
self.order, |
| 117 |
self.limit_offset, |
| 118 |
self.group_by, |
| 119 |
self.having, |
| 120 |
self.locking, |
| 121 |
) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
impl<F, S, D, W, O, LOf, G, H, LC, Selection> SelectDsl<Selection> |
| 126 |
for SelectStatement<FromClause<F>, S, D, W, O, LOf, G, H, LC> |
| 127 |
where |
| 128 |
G: ValidGroupByClause, |
| 129 |
F: QuerySource, |
| 130 |
Selection: SelectableExpression<F> + ValidGrouping<G::Expressions>, |
| 131 |
SelectStatement<FromClause<F>, SelectClause<Selection>, D, W, O, LOf, G, H, LC>: SelectQuery, |
| 132 |
D: ValidDistinctForGroupBy<Selection, G::Expressions>, |
| 133 |
O: ValidGrouping<G::Expressions>, |
| 134 |
<Selection as ValidGrouping<G::Expressions>>::IsAggregate: |
| 135 |
MixedAggregates<<O as ValidGrouping<G::Expressions>>::IsAggregate>, |
| 136 |
{ |
| 137 |
type Output = SelectStatement<FromClause<F>, SelectClause<Selection>, D, W, O, LOf, G, H, LC>; |
| 138 |
|
| 139 |
fn select(self, selection: Selection) -> Self::Output { |
| 140 |
SelectStatement::new( |
| 141 |
SelectClause(selection), |
| 142 |
self.from, |
| 143 |
self.distinct, |
| 144 |
self.where_clause, |
| 145 |
self.order, |
| 146 |
self.limit_offset, |
| 147 |
self.group_by, |
| 148 |
self.having, |
| 149 |
self.locking, |
| 150 |
) |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
impl<S, D, W, O, LOf, G, H, LC, Selection> SelectDsl<Selection> |
| 155 |
for SelectStatement<NoFromClause, S, D, W, O, LOf, G, H, LC> |
| 156 |
where |
| 157 |
G: ValidGroupByClause, |
| 158 |
Selection: SelectableExpression<NoFromClause> + ValidGrouping<G::Expressions>, |
| 159 |
SelectStatement<NoFromClause, SelectClause<Selection>, D, W, O, LOf, G, H, LC>: SelectQuery, |
| 160 |
D: ValidDistinctForGroupBy<Selection, G::Expressions>, |
| 161 |
{ |
| 162 |
type Output = SelectStatement<NoFromClause, SelectClause<Selection>, D, W, O, LOf, G, H, LC>; |
| 163 |
|
| 164 |
fn select(self, selection: Selection) -> Self::Output { |
| 165 |
SelectStatement::new( |
| 166 |
SelectClause(selection), |
| 167 |
self.from, |
| 168 |
self.distinct, |
| 169 |
self.where_clause, |
| 170 |
self.order, |
| 171 |
self.limit_offset, |
| 172 |
self.group_by, |
| 173 |
self.having, |
| 174 |
self.locking, |
| 175 |
) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
impl<ST, F, S, D, W, O, LOf, G, H> DistinctDsl for SelectStatement<F, S, D, W, O, LOf, G, H> |
| 180 |
where |
| 181 |
Self: SelectQuery<SqlType = ST>, |
| 182 |
SelectStatement<F, S, DistinctClause, W, O, LOf, G, H>: SelectQuery<SqlType = ST>, |
| 183 |
{ |
| 184 |
type Output = SelectStatement<F, S, DistinctClause, W, O, LOf, G, H>; |
| 185 |
|
| 186 |
fn distinct(self) -> Self::Output { |
| 187 |
SelectStatement::new( |
| 188 |
self.select, |
| 189 |
self.from, |
| 190 |
DistinctClause, |
| 191 |
self.where_clause, |
| 192 |
self.order, |
| 193 |
self.limit_offset, |
| 194 |
self.group_by, |
| 195 |
self.having, |
| 196 |
self.locking, |
| 197 |
) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
impl<F, S, D, W, O, LOf, G, H, LC, Predicate> FilterDsl<Predicate> |
| 202 |
for SelectStatement<F, S, D, W, O, LOf, G, H, LC> |
| 203 |
where |
| 204 |
Predicate: Expression + NonAggregate, |
| 205 |
Predicate::SqlType: BoolOrNullableBool, |
| 206 |
W: WhereAnd<Predicate>, |
| 207 |
{ |
| 208 |
type Output = SelectStatement<F, S, D, W::Output, O, LOf, G, H, LC>; |
| 209 |
|
| 210 |
fn filter(self, predicate: Predicate) -> Self::Output { |
| 211 |
SelectStatement::new( |
| 212 |
self.select, |
| 213 |
self.from, |
| 214 |
self.distinct, |
| 215 |
self.where_clause.and(predicate), |
| 216 |
self.order, |
| 217 |
self.limit_offset, |
| 218 |
self.group_by, |
| 219 |
self.having, |
| 220 |
self.locking, |
| 221 |
) |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
impl<F, S, D, W, O, LOf, G, H, LC, Predicate> OrFilterDsl<Predicate> |
| 226 |
for SelectStatement<F, S, D, W, O, LOf, G, H, LC> |
| 227 |
where |
| 228 |
Predicate: Expression + NonAggregate, |
| 229 |
Predicate::SqlType: BoolOrNullableBool, |
| 230 |
W: WhereOr<Predicate>, |
| 231 |
{ |
| 232 |
type Output = SelectStatement<F, S, D, W::Output, O, LOf, G, H, LC>; |
| 233 |
|
| 234 |
fn or_filter(self, predicate: Predicate) -> Self::Output { |
| 235 |
SelectStatement::new( |
| 236 |
self.select, |
| 237 |
self.from, |
| 238 |
self.distinct, |
| 239 |
self.where_clause.or(predicate), |
| 240 |
self.order, |
| 241 |
self.limit_offset, |
| 242 |
self.group_by, |
| 243 |
self.having, |
| 244 |
self.locking, |
| 245 |
) |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
use crate::dsl::Filter; |
| 250 |
use crate::expression_methods::EqAll; |
| 251 |
use crate::query_builder::having_clause::{HavingClause, NoHavingClause}; |
| 252 |
use crate::query_source::Table; |
| 253 |
|
| 254 |
impl<F, S, D, W, O, LOf, G, H, LC, PK> FindDsl<PK> |
| 255 |
for SelectStatement<FromClause<F>, S, D, W, O, LOf, G, H, LC> |
| 256 |
where |
| 257 |
F: Table, |
| 258 |
F::PrimaryKey: EqAll<PK>, |
| 259 |
Self: FilterDsl<<F::PrimaryKey as EqAll<PK>>::Output>, |
| 260 |
{ |
| 261 |
type Output = Filter<Self, <F::PrimaryKey as EqAll<PK>>::Output>; |
| 262 |
|
| 263 |
fn find(self, id: PK) -> Self::Output { |
| 264 |
let primary_key = self.from.source.primary_key(); |
| 265 |
FilterDsl::filter(self, primary_key.eq_all(id)) |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
// no impls for `NoFromClause` here because order is not really supported there yet |
| 270 |
|
| 271 |
// Without GROUP BY: validate that SELECT and ORDER BY have matching aggregate nature. |
| 272 |
impl<ST, F, S, D, W, O, LOf, H, LC, Expr> OrderDsl<Expr> |
| 273 |
for SelectStatement<FromClause<F>, S, D, W, O, LOf, NoGroupByClause, H, LC> |
| 274 |
where |
| 275 |
F: QuerySource, |
| 276 |
Expr: AppearsOnTable<F>, |
| 277 |
Self: SelectQuery<SqlType = ST>, |
| 278 |
SelectStatement<FromClause<F>, S, D, W, OrderClause<Expr>, LOf, NoGroupByClause, H, LC>: |
| 279 |
SelectQuery<SqlType = ST>, |
| 280 |
OrderClause<Expr>: ValidOrderingForDistinct<D>, |
| 281 |
S: SelectClauseExpression<FromClause<F>>, |
| 282 |
Expr: ValidGrouping<()>, |
| 283 |
S::Selection: ValidGrouping<()>, |
| 284 |
<S::Selection as ValidGrouping<()>>::IsAggregate: |
| 285 |
MixedAggregates<<Expr as ValidGrouping<()>>::IsAggregate>, |
| 286 |
{ |
| 287 |
type Output = |
| 288 |
SelectStatement<FromClause<F>, S, D, W, OrderClause<Expr>, LOf, NoGroupByClause, H, LC>; |
| 289 |
|
| 290 |
fn order(self, expr: Expr) -> Self::Output { |
| 291 |
let order = OrderClause(expr); |
| 292 |
SelectStatement::new( |
| 293 |
self.select, |
| 294 |
self.from, |
| 295 |
self.distinct, |
| 296 |
self.where_clause, |
| 297 |
order, |
| 298 |
self.limit_offset, |
| 299 |
self.group_by, |
| 300 |
self.having, |
| 301 |
self.locking, |
| 302 |
) |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
// With GROUP BY: only validate that the ORDER BY expression is valid for the group |
| 307 |
// (grouped column or aggregate). SELECT validity is enforced by the Query impl at |
| 308 |
// execution time, so checking it here would reject valid queries where order_by() |
| 309 |
// is called before select() with a non-trivial GROUP BY. |
| 310 |
impl<ST, F, S, D, W, O, LOf, GB, H, LC, Expr> OrderDsl<Expr> |
| 311 |
for SelectStatement<FromClause<F>, S, D, W, O, LOf, GroupByClause<GB>, H, LC> |
| 312 |
where |
| 313 |
F: QuerySource, |
| 314 |
Expr: AppearsOnTable<F>, |
| 315 |
Self: SelectQuery<SqlType = ST>, |
| 316 |
SelectStatement<FromClause<F>, S, D, W, OrderClause<Expr>, LOf, GroupByClause<GB>, H, LC>: |
| 317 |
SelectQuery<SqlType = ST>, |
| 318 |
OrderClause<Expr>: ValidOrderingForDistinct<D>, |
| 319 |
Expr: ValidGrouping<GB>, |
| 320 |
{ |
| 321 |
type Output = |
| 322 |
SelectStatement<FromClause<F>, S, D, W, OrderClause<Expr>, LOf, GroupByClause<GB>, H, LC>; |
| 323 |
|
| 324 |
fn order(self, expr: Expr) -> Self::Output { |
| 325 |
let order = OrderClause(expr); |
| 326 |
SelectStatement::new( |
| 327 |
self.select, |
| 328 |
self.from, |
| 329 |
self.distinct, |
| 330 |
self.where_clause, |
| 331 |
order, |
| 332 |
self.limit_offset, |
| 333 |
self.group_by, |
| 334 |
self.having, |
| 335 |
self.locking, |
| 336 |
) |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
// Without GROUP BY: validate that SELECT and the new ORDER BY term have matching |
| 341 |
// aggregate nature. |
| 342 |
impl<F, S, D, W, O, LOf, H, LC, Expr> ThenOrderDsl<Expr> |
| 343 |
for SelectStatement<FromClause<F>, S, D, W, OrderClause<O>, LOf, NoGroupByClause, H, LC> |
| 344 |
where |
| 345 |
F: QuerySource, |
| 346 |
Expr: AppearsOnTable<F>, |
| 347 |
S: SelectClauseExpression<FromClause<F>>, |
| 348 |
Expr: ValidGrouping<()>, |
| 349 |
S::Selection: ValidGrouping<()>, |
| 350 |
<S::Selection as ValidGrouping<()>>::IsAggregate: |
| 351 |
MixedAggregates<<Expr as ValidGrouping<()>>::IsAggregate>, |
| 352 |
{ |
| 353 |
type Output = SelectStatement< |
| 354 |
FromClause<F>, |
| 355 |
S, |
| 356 |
D, |
| 357 |
W, |
| 358 |
OrderClause<(O, Expr)>, |
| 359 |
LOf, |
| 360 |
NoGroupByClause, |
| 361 |
H, |
| 362 |
LC, |
| 363 |
>; |
| 364 |
|
| 365 |
fn then_order_by(self, expr: Expr) -> Self::Output { |
| 366 |
SelectStatement::new( |
| 367 |
self.select, |
| 368 |
self.from, |
| 369 |
self.distinct, |
| 370 |
self.where_clause, |
| 371 |
OrderClause((self.order.0, expr)), |
| 372 |
self.limit_offset, |
| 373 |
self.group_by, |
| 374 |
self.having, |
| 375 |
self.locking, |
| 376 |
) |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
// With GROUP BY: Validate the whole order expression against the given group by expression |
| 381 |
impl<ST, F, S, D, W, O, LOf, GB, H, LC, Expr> ThenOrderDsl<Expr> |
| 382 |
for SelectStatement<FromClause<F>, S, D, W, OrderClause<O>, LOf, GroupByClause<GB>, H, LC> |
| 383 |
where |
| 384 |
F: QuerySource, |
| 385 |
Expr: AppearsOnTable<F>, |
| 386 |
Self: SelectQuery<SqlType = ST>, |
| 387 |
SelectStatement<FromClause<F>, S, D, W, OrderClause<(O, Expr)>, LOf, GroupByClause<GB>, H, LC>: |
| 388 |
SelectQuery<SqlType = ST>, |
| 389 |
(O, Expr): ValidGrouping<GB>, |
| 390 |
{ |
| 391 |
type Output = SelectStatement< |
| 392 |
FromClause<F>, |
| 393 |
S, |
| 394 |
D, |
| 395 |
W, |
| 396 |
OrderClause<(O, Expr)>, |
| 397 |
LOf, |
| 398 |
GroupByClause<GB>, |
| 399 |
H, |
| 400 |
LC, |
| 401 |
>; |
| 402 |
|
| 403 |
fn then_order_by(self, expr: Expr) -> Self::Output { |
| 404 |
SelectStatement::new( |
| 405 |
self.select, |
| 406 |
self.from, |
| 407 |
self.distinct, |
| 408 |
self.where_clause, |
| 409 |
OrderClause((self.order.0, expr)), |
| 410 |
self.limit_offset, |
| 411 |
self.group_by, |
| 412 |
self.having, |
| 413 |
self.locking, |
| 414 |
) |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
impl<F, S, D, W, LOf, G, LC, Expr> ThenOrderDsl<Expr> |
| 419 |
for SelectStatement<F, S, D, W, NoOrderClause, LOf, G, LC> |
| 420 |
where |
| 421 |
Expr: Expression, |
| 422 |
Self: OrderDsl<Expr>, |
| 423 |
{ |
| 424 |
type Output = crate::dsl::Order<Self, Expr>; |
| 425 |
|
| 426 |
fn then_order_by(self, expr: Expr) -> Self::Output { |
| 427 |
self.order_by(expr) |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
#[doc(hidden)] |
| 432 |
type Limit = AsExprOf<i64, BigInt>; |
| 433 |
|
| 434 |
impl<ST, F, S, D, W, O, L, Of, G, H, LC> LimitDsl |
| 435 |
for SelectStatement<F, S, D, W, O, LimitOffsetClause<L, Of>, G, H, LC> |
| 436 |
where |
| 437 |
Self: SelectQuery<SqlType = ST>, |
| 438 |
SelectStatement<F, S, D, W, O, LimitOffsetClause<LimitClause<Limit>, Of>, G, H, LC>: |
| 439 |
SelectQuery<SqlType = ST>, |
| 440 |
{ |
| 441 |
type Output = |
| 442 |
SelectStatement<F, S, D, W, O, LimitOffsetClause<LimitClause<Limit>, Of>, G, H, LC>; |
| 443 |
|
| 444 |
fn limit(self, limit: i64) -> Self::Output { |
| 445 |
let limit_clause = LimitClause(limit.into_sql::<BigInt>()); |
| 446 |
SelectStatement::new( |
| 447 |
self.select, |
| 448 |
self.from, |
| 449 |
self.distinct, |
| 450 |
self.where_clause, |
| 451 |
self.order, |
| 452 |
LimitOffsetClause { |
| 453 |
limit_clause, |
| 454 |
offset_clause: self.limit_offset.offset_clause, |
| 455 |
}, |
| 456 |
self.group_by, |
| 457 |
self.having, |
| 458 |
self.locking, |
| 459 |
) |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
#[doc(hidden)] |
| 464 |
type Offset = Limit; |
| 465 |
|
| 466 |
impl<ST, F, S, D, W, O, L, Of, G, H, LC> OffsetDsl |
| 467 |
for SelectStatement<F, S, D, W, O, LimitOffsetClause<L, Of>, G, H, LC> |
| 468 |
where |
| 469 |
Self: SelectQuery<SqlType = ST>, |
| 470 |
SelectStatement<F, S, D, W, O, LimitOffsetClause<L, OffsetClause<Offset>>, G, H, LC>: |
| 471 |
SelectQuery<SqlType = ST>, |
| 472 |
{ |
| 473 |
type Output = |
| 474 |
SelectStatement<F, S, D, W, O, LimitOffsetClause<L, OffsetClause<Offset>>, G, H, LC>; |
| 475 |
|
| 476 |
fn offset(self, offset: i64) -> Self::Output { |
| 477 |
let offset_clause = OffsetClause(offset.into_sql::<BigInt>()); |
| 478 |
SelectStatement::new( |
| 479 |
self.select, |
| 480 |
self.from, |
| 481 |
self.distinct, |
| 482 |
self.where_clause, |
| 483 |
self.order, |
| 484 |
LimitOffsetClause { |
| 485 |
limit_clause: self.limit_offset.limit_clause, |
| 486 |
offset_clause, |
| 487 |
}, |
| 488 |
self.group_by, |
| 489 |
self.having, |
| 490 |
self.locking, |
| 491 |
) |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
impl<F, S, D, W, O, LOf, G, H, Expr> GroupByDsl<Expr> for SelectStatement<F, S, D, W, O, LOf, G, H> |
| 496 |
where |
| 497 |
SelectStatement<F, S, D, W, O, LOf, GroupByClause<Expr>, H>: SelectQuery, |
| 498 |
Expr: Expression + AppearsOnTable<F>, |
| 499 |
{ |
| 500 |
type Output = SelectStatement<F, S, D, W, O, LOf, GroupByClause<Expr>, H>; |
| 501 |
|
| 502 |
fn group_by(self, expr: Expr) -> Self::Output { |
| 503 |
let group_by = GroupByClause(expr); |
| 504 |
SelectStatement::new( |
| 505 |
self.select, |
| 506 |
self.from, |
| 507 |
self.distinct, |
| 508 |
self.where_clause, |
| 509 |
self.order, |
| 510 |
self.limit_offset, |
| 511 |
group_by, |
| 512 |
self.having, |
| 513 |
self.locking, |
| 514 |
) |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
impl<F, S, W, O, LOf, Lock> LockingDsl<Lock> |
| 519 |
for SelectStatement<F, S, NoDistinctClause, W, O, LOf> |
| 520 |
{ |
| 521 |
type Output = SelectStatement< |
| 522 |
F, |
| 523 |
S, |
| 524 |
NoDistinctClause, |
| 525 |
W, |
| 526 |
O, |
| 527 |
LOf, |
| 528 |
NoGroupByClause, |
| 529 |
NoHavingClause, |
| 530 |
LockingClause<Lock, NoModifier>, |
| 531 |
>; |
| 532 |
|
| 533 |
fn with_lock(self, lock: Lock) -> Self::Output { |
| 534 |
SelectStatement::new( |
| 535 |
self.select, |
| 536 |
self.from, |
| 537 |
self.distinct, |
| 538 |
self.where_clause, |
| 539 |
self.order, |
| 540 |
self.limit_offset, |
| 541 |
self.group_by, |
| 542 |
self.having, |
| 543 |
LockingClause::new(lock, NoModifier), |
| 544 |
) |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
impl<F, S, D, W, O, LOf, G, H, LC, LM, Modifier> ModifyLockDsl<Modifier> |
| 549 |
for SelectStatement<F, S, D, W, O, LOf, G, H, LockingClause<LC, LM>> |
| 550 |
{ |
| 551 |
type Output = SelectStatement<F, S, D, W, O, LOf, G, H, LockingClause<LC, Modifier>>; |
| 552 |
|
| 553 |
fn modify_lock(self, modifier: Modifier) -> Self::Output { |
| 554 |
SelectStatement::new( |
| 555 |
self.select, |
| 556 |
self.from, |
| 557 |
self.distinct, |
| 558 |
self.where_clause, |
| 559 |
self.order, |
| 560 |
self.limit_offset, |
| 561 |
self.group_by, |
| 562 |
self.having, |
| 563 |
LockingClause::new(self.locking.lock_mode, modifier), |
| 564 |
) |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
impl<'a, F, S, D, W, O, LOf, G, H, DB> BoxedDsl<'a, DB> |
| 569 |
for SelectStatement<FromClause<F>, S, D, W, O, LOf, G, H> |
| 570 |
where |
| 571 |
Self: AsQuery, |
| 572 |
DB: Backend, |
| 573 |
F: QuerySource, |
| 574 |
S: SelectClauseExpression<FromClause<F>> + QueryFragment<DB> + Send + 'a, |
| 575 |
S::Selection: ValidGrouping<G::Expressions>, |
| 576 |
D: QueryFragment<DB> + Send + 'a, |
| 577 |
W: Into<BoxedWhereClause<'a, DB>>, |
| 578 |
O: Into<Option<Box<dyn QueryFragment<DB> + Send + 'a>>>, |
| 579 |
LOf: IntoBoxedClause<'a, DB, BoxedClause = BoxedLimitOffsetClause<'a, DB>>, |
| 580 |
G: ValidGroupByClause + QueryFragment<DB> + Send + 'a, |
| 581 |
H: QueryFragment<DB> + Send + 'a, |
| 582 |
{ |
| 583 |
type Output = |
| 584 |
BoxedSelectStatement<'a, S::SelectClauseSqlType, FromClause<F>, DB, G::Expressions>; |
| 585 |
|
| 586 |
fn internal_into_boxed(self) -> Self::Output { |
| 587 |
BoxedSelectStatement::new( |
| 588 |
self.select, |
| 589 |
self.from, |
| 590 |
Box::new(self.distinct), |
| 591 |
self.where_clause.into(), |
| 592 |
self.order.into(), |
| 593 |
self.limit_offset.into_boxed(), |
| 594 |
self.group_by, |
| 595 |
Box::new(self.having), |
| 596 |
) |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
impl<'a, S, D, W, O, LOf, G, H, DB> BoxedDsl<'a, DB> |
| 601 |
for SelectStatement<NoFromClause, S, D, W, O, LOf, G, H> |
| 602 |
where |
| 603 |
Self: AsQuery, |
| 604 |
DB: Backend, |
| 605 |
S: SelectClauseExpression<NoFromClause> + QueryFragment<DB> + Send + 'a, |
| 606 |
S::Selection: ValidGrouping<G::Expressions>, |
| 607 |
D: QueryFragment<DB> + Send + 'a, |
| 608 |
W: Into<BoxedWhereClause<'a, DB>>, |
| 609 |
O: Into<Option<Box<dyn QueryFragment<DB> + Send + 'a>>>, |
| 610 |
LOf: IntoBoxedClause<'a, DB, BoxedClause = BoxedLimitOffsetClause<'a, DB>>, |
| 611 |
G: ValidGroupByClause + QueryFragment<DB> + Send + 'a, |
| 612 |
H: QueryFragment<DB> + Send + 'a, |
| 613 |
{ |
| 614 |
type Output = |
| 615 |
BoxedSelectStatement<'a, S::SelectClauseSqlType, NoFromClause, DB, G::Expressions>; |
| 616 |
|
| 617 |
fn internal_into_boxed(self) -> Self::Output { |
| 618 |
BoxedSelectStatement::new_no_from_clause( |
| 619 |
self.select, |
| 620 |
self.from, |
| 621 |
Box::new(self.distinct), |
| 622 |
self.where_clause.into(), |
| 623 |
self.order.into(), |
| 624 |
self.limit_offset.into_boxed(), |
| 625 |
self.group_by, |
| 626 |
Box::new(self.having), |
| 627 |
) |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
impl<F, S, D, W, O, LOf, G, H, LC> HasTable |
| 632 |
for SelectStatement<FromClause<F>, S, D, W, O, LOf, G, H, LC> |
| 633 |
where |
| 634 |
F: HasTable + QuerySource, |
| 635 |
{ |
| 636 |
type Table = F::Table; |
| 637 |
|
| 638 |
fn table() -> Self::Table { |
| 639 |
F::table() |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
impl<F, W> IntoUpdateTarget |
| 644 |
for SelectStatement<FromClause<F>, DefaultSelectClause<FromClause<F>>, NoDistinctClause, W> |
| 645 |
where |
| 646 |
F: QuerySource, |
| 647 |
Self: HasTable, |
| 648 |
W: ValidWhereClause<F>, |
| 649 |
{ |
| 650 |
type WhereClause = W; |
| 651 |
|
| 652 |
fn into_update_target(self) -> UpdateTarget<Self::Table, Self::WhereClause> { |
| 653 |
UpdateTarget { |
| 654 |
table: Self::table(), |
| 655 |
where_clause: self.where_clause, |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
// FIXME: Should we disable joining when `.group_by` has been called? Are there |
| 661 |
// any other query methods where a join no longer has the same semantics as |
| 662 |
// joining on just the table? |
| 663 |
impl<F, S, D, W, O, LOf, G, H, LC, Rhs> JoinTo<Rhs> |
| 664 |
for SelectStatement<FromClause<F>, S, D, W, O, LOf, G, H, LC> |
| 665 |
where |
| 666 |
F: JoinTo<Rhs> + QuerySource, |
| 667 |
{ |
| 668 |
type FromClause = <F as JoinTo<Rhs>>::FromClause; |
| 669 |
type OnClause = F::OnClause; |
| 670 |
|
| 671 |
fn join_target(rhs: Rhs) -> (Self::FromClause, Self::OnClause) { |
| 672 |
F::join_target(rhs) |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
impl<F, S, D, W, O, LOf, G, H, LC> QueryDsl for SelectStatement<F, S, D, W, O, LOf, G, H, LC> {} |
| 677 |
|
| 678 |
impl<F, S, D, W, O, LOf, G, H, LC, Conn> RunQueryDsl<Conn> |
| 679 |
for SelectStatement<F, S, D, W, O, LOf, G, H, LC> |
| 680 |
{ |
| 681 |
} |
| 682 |
|
| 683 |
impl<F, S, D, W, O, LOf, G, H, LC, Tab> Insertable<Tab> |
| 684 |
for SelectStatement<F, S, D, W, O, LOf, G, H, LC> |
| 685 |
where |
| 686 |
Tab: Table, |
| 687 |
Self: Query, |
| 688 |
<Tab::AllColumns as ValidGrouping<()>>::IsAggregate: |
| 689 |
MixedAggregates<is_aggregate::No, Output = is_aggregate::No>, |
| 690 |
{ |
| 691 |
type Values = InsertFromSelect<Self, Tab::AllColumns>; |
| 692 |
|
| 693 |
fn values(self) -> Self::Values { |
| 694 |
InsertFromSelect::new(self) |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
impl<F, S, D, W, O, LOf, G, H, LC, Tab> Insertable<Tab> |
| 699 |
for &SelectStatement<F, S, D, W, O, LOf, G, H, LC> |
| 700 |
where |
| 701 |
Tab: Table, |
| 702 |
Self: Query, |
| 703 |
<Tab::AllColumns as ValidGrouping<()>>::IsAggregate: |
| 704 |
MixedAggregates<is_aggregate::No, Output = is_aggregate::No>, |
| 705 |
{ |
| 706 |
type Values = InsertFromSelect<Self, Tab::AllColumns>; |
| 707 |
|
| 708 |
fn values(self) -> Self::Values { |
| 709 |
InsertFromSelect::new(self) |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
impl<F, S, D, W, O, LOf, G, H> SelectNullableDsl |
| 714 |
for SelectStatement<F, SelectClause<S>, D, W, O, LOf, G, H> |
| 715 |
{ |
| 716 |
type Output = SelectStatement<F, SelectClause<Nullable<S>>, D, W, O, LOf, G, H>; |
| 717 |
|
| 718 |
fn nullable(self) -> Self::Output { |
| 719 |
SelectStatement::new( |
| 720 |
SelectClause(Nullable::new(self.select.0)), |
| 721 |
self.from, |
| 722 |
self.distinct, |
| 723 |
self.where_clause, |
| 724 |
self.order, |
| 725 |
self.limit_offset, |
| 726 |
self.group_by, |
| 727 |
self.having, |
| 728 |
self.locking, |
| 729 |
) |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
impl<F, D, W, O, LOf, G, H> SelectNullableDsl |
| 734 |
for SelectStatement<F, DefaultSelectClause<F>, D, W, O, LOf, G, H> |
| 735 |
where |
| 736 |
F: AsQuerySource, |
| 737 |
{ |
| 738 |
type Output = SelectStatement< |
| 739 |
F, |
| 740 |
SelectClause<Nullable<<F::QuerySource as QuerySource>::DefaultSelection>>, |
| 741 |
D, |
| 742 |
W, |
| 743 |
O, |
| 744 |
LOf, |
| 745 |
G, |
| 746 |
H, |
| 747 |
>; |
| 748 |
|
| 749 |
fn nullable(self) -> Self::Output { |
| 750 |
SelectStatement::new( |
| 751 |
SelectClause(Nullable::new( |
| 752 |
self.from.as_query_source().default_selection(), |
| 753 |
)), |
| 754 |
self.from, |
| 755 |
self.distinct, |
| 756 |
self.where_clause, |
| 757 |
self.order, |
| 758 |
self.limit_offset, |
| 759 |
self.group_by, |
| 760 |
self.having, |
| 761 |
self.locking, |
| 762 |
) |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
impl<F, S, D, W, O, LOf, G, H, Predicate> HavingDsl<Predicate> |
| 767 |
for SelectStatement<FromClause<F>, S, D, W, O, LOf, GroupByClause<G>, H> |
| 768 |
where |
| 769 |
F: QuerySource, |
| 770 |
Predicate: AppearsOnTable<F>, |
| 771 |
Predicate: Expression, |
| 772 |
Predicate::SqlType: BoolOrNullableBool, |
| 773 |
{ |
| 774 |
type Output = |
| 775 |
SelectStatement<FromClause<F>, S, D, W, O, LOf, GroupByClause<G>, HavingClause<Predicate>>; |
| 776 |
|
| 777 |
fn having(self, predicate: Predicate) -> Self::Output { |
| 778 |
SelectStatement::new( |
| 779 |
self.select, |
| 780 |
self.from, |
| 781 |
self.distinct, |
| 782 |
self.where_clause, |
| 783 |
self.order, |
| 784 |
self.limit_offset, |
| 785 |
self.group_by, |
| 786 |
HavingClause(predicate), |
| 787 |
self.locking, |
| 788 |
) |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
impl<F, S, D, W, O, LOf, G, H, LC> CombineDsl for SelectStatement<F, S, D, W, O, LOf, G, H, LC> |
| 793 |
where |
| 794 |
Self: Query, |
| 795 |
{ |
| 796 |
type Query = Self; |
| 797 |
|
| 798 |
fn union<Rhs>(self, rhs: Rhs) -> crate::dsl::Union<Self, Rhs> |
| 799 |
where |
| 800 |
Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>, |
| 801 |
{ |
| 802 |
CombinationClause::new(Union, Distinct, self, rhs.as_query()) |
| 803 |
} |
| 804 |
|
| 805 |
fn union_all<Rhs>(self, rhs: Rhs) -> crate::dsl::UnionAll<Self, Rhs> |
| 806 |
where |
| 807 |
Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>, |
| 808 |
{ |
| 809 |
CombinationClause::new(Union, All, self, rhs.as_query()) |
| 810 |
} |
| 811 |
|
| 812 |
fn intersect<Rhs>(self, rhs: Rhs) -> crate::dsl::Intersect<Self, Rhs> |
| 813 |
where |
| 814 |
Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>, |
| 815 |
{ |
| 816 |
CombinationClause::new(Intersect, Distinct, self, rhs.as_query()) |
| 817 |
} |
| 818 |
|
| 819 |
fn intersect_all<Rhs>(self, rhs: Rhs) -> crate::dsl::IntersectAll<Self, Rhs> |
| 820 |
where |
| 821 |
Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>, |
| 822 |
{ |
| 823 |
CombinationClause::new(Intersect, All, self, rhs.as_query()) |
| 824 |
} |
| 825 |
|
| 826 |
fn except<Rhs>(self, rhs: Rhs) -> crate::dsl::Except<Self, Rhs> |
| 827 |
where |
| 828 |
Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>, |
| 829 |
{ |
| 830 |
CombinationClause::new(Except, Distinct, self, rhs.as_query()) |
| 831 |
} |
| 832 |
|
| 833 |
fn except_all<Rhs>(self, rhs: Rhs) -> crate::dsl::ExceptAll<Self, Rhs> |
| 834 |
where |
| 835 |
Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>, |
| 836 |
{ |
| 837 |
CombinationClause::new(Except, All, self, rhs.as_query()) |
| 838 |
} |
| 839 |
} |