1use super::BoxedSelectStatement;
2use crate::associations::HasTable;
3use crate::backend::Backend;
4use crate::dsl::AsExprOf;
5use crate::expression::nullable::Nullable;
6use crate::expression::*;
7use crate::insertable::Insertable;
8use crate::query_builder::combination_clause::*;
9use crate::query_builder::distinct_clause::*;
10use crate::query_builder::from_clause::AsQuerySource;
11use crate::query_builder::from_clause::FromClause;
12use crate::query_builder::group_by_clause::*;
13use crate::query_builder::insert_statement::InsertFromSelect;
14use crate::query_builder::limit_clause::*;
15use crate::query_builder::limit_offset_clause::{BoxedLimitOffsetClause, LimitOffsetClause};
16use crate::query_builder::locking_clause::*;
17use crate::query_builder::offset_clause::*;
18use crate::query_builder::order_clause::*;
19use crate::query_builder::select_clause::*;
20use crate::query_builder::update_statement::target::*;
21use crate::query_builder::where_clause::*;
22use crate::query_builder::NoFromClause;
23use crate::query_builder::{
24 AsQuery, IntoBoxedClause, Query, QueryFragment, SelectQuery, SelectStatement,
25};
26use crate::query_dsl::group_by_dsl::ValidDistinctForGroupBy;
27use crate::query_dsl::methods::*;
28use crate::query_dsl::order_dsl::ValidOrderingForDistinct;
29use crate::query_dsl::*;
30use crate::query_source::joins::{Join, JoinOn, JoinTo};
31use crate::query_source::QuerySource;
32use crate::sql_types::{BigInt, BoolOrNullableBool};
33
34impl<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>
36where
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
80impl<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>
82where
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
125impl<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>
127where
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
154impl<S, D, W, O, LOf, G, H, LC, Selection> SelectDsl<Selection>
155 for SelectStatement<NoFromClause, S, D, W, O, LOf, G, H, LC>
156where
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
179impl<ST, F, S, D, W, O, LOf, G, H> DistinctDsl for SelectStatement<F, S, D, W, O, LOf, G, H>
180where
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
201impl<F, S, D, W, O, LOf, G, H, LC, Predicate> FilterDsl<Predicate>
202 for SelectStatement<F, S, D, W, O, LOf, G, H, LC>
203where
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
225impl<F, S, D, W, O, LOf, G, H, LC, Predicate> OrFilterDsl<Predicate>
226 for SelectStatement<F, S, D, W, O, LOf, G, H, LC>
227where
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
249use crate::dsl::Filter;
250use crate::expression_methods::EqAll;
251use crate::query_builder::having_clause::{HavingClause, NoHavingClause};
252use crate::query_source::Table;
253
254impl<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>
256where
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
269impl<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>
274where
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
306impl<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>
312where
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
340impl<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>
344where
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 OrderClause<(O, Expr)>: ValidOrderingForDistinct<D>,
353{
354 type Output = SelectStatement<
355 FromClause<F>,
356 S,
357 D,
358 W,
359 OrderClause<(O, Expr)>,
360 LOf,
361 NoGroupByClause,
362 H,
363 LC,
364 >;
365
366 fn then_order_by(self, expr: Expr) -> Self::Output {
367 SelectStatement::new(
368 self.select,
369 self.from,
370 self.distinct,
371 self.where_clause,
372 OrderClause((self.order.0, expr)),
373 self.limit_offset,
374 self.group_by,
375 self.having,
376 self.locking,
377 )
378 }
379}
380
381impl<ST, F, S, D, W, O, LOf, GB, H, LC, Expr> ThenOrderDsl<Expr>
383 for SelectStatement<FromClause<F>, S, D, W, OrderClause<O>, LOf, GroupByClause<GB>, H, LC>
384where
385 F: QuerySource,
386 Expr: AppearsOnTable<F>,
387 Self: SelectQuery<SqlType = ST>,
388 SelectStatement<FromClause<F>, S, D, W, OrderClause<(O, Expr)>, LOf, GroupByClause<GB>, H, LC>:
389 SelectQuery<SqlType = ST>,
390 (O, Expr): ValidGrouping<GB>,
391 OrderClause<(O, Expr)>: ValidOrderingForDistinct<D>,
392{
393 type Output = SelectStatement<
394 FromClause<F>,
395 S,
396 D,
397 W,
398 OrderClause<(O, Expr)>,
399 LOf,
400 GroupByClause<GB>,
401 H,
402 LC,
403 >;
404
405 fn then_order_by(self, expr: Expr) -> Self::Output {
406 SelectStatement::new(
407 self.select,
408 self.from,
409 self.distinct,
410 self.where_clause,
411 OrderClause((self.order.0, expr)),
412 self.limit_offset,
413 self.group_by,
414 self.having,
415 self.locking,
416 )
417 }
418}
419
420impl<F, S, D, W, LOf, G, LC, Expr> ThenOrderDsl<Expr>
421 for SelectStatement<F, S, D, W, NoOrderClause, LOf, G, LC>
422where
423 Expr: Expression,
424 Self: OrderDsl<Expr>,
425{
426 type Output = crate::dsl::Order<Self, Expr>;
427
428 fn then_order_by(self, expr: Expr) -> Self::Output {
429 self.order_by(expr)
430 }
431}
432
433#[doc(hidden)]
434type Limit = AsExprOf<i64, BigInt>;
435
436impl<ST, F, S, D, W, O, L, Of, G, H, LC> LimitDsl
437 for SelectStatement<F, S, D, W, O, LimitOffsetClause<L, Of>, G, H, LC>
438where
439 Self: SelectQuery<SqlType = ST>,
440 SelectStatement<F, S, D, W, O, LimitOffsetClause<LimitClause<Limit>, Of>, G, H, LC>:
441 SelectQuery<SqlType = ST>,
442{
443 type Output =
444 SelectStatement<F, S, D, W, O, LimitOffsetClause<LimitClause<Limit>, Of>, G, H, LC>;
445
446 fn limit(self, limit: i64) -> Self::Output {
447 let limit_clause = LimitClause(limit.into_sql::<BigInt>());
448 SelectStatement::new(
449 self.select,
450 self.from,
451 self.distinct,
452 self.where_clause,
453 self.order,
454 LimitOffsetClause {
455 limit_clause,
456 offset_clause: self.limit_offset.offset_clause,
457 },
458 self.group_by,
459 self.having,
460 self.locking,
461 )
462 }
463}
464
465#[doc(hidden)]
466type Offset = Limit;
467
468impl<ST, F, S, D, W, O, L, Of, G, H, LC> OffsetDsl
469 for SelectStatement<F, S, D, W, O, LimitOffsetClause<L, Of>, G, H, LC>
470where
471 Self: SelectQuery<SqlType = ST>,
472 SelectStatement<F, S, D, W, O, LimitOffsetClause<L, OffsetClause<Offset>>, G, H, LC>:
473 SelectQuery<SqlType = ST>,
474{
475 type Output =
476 SelectStatement<F, S, D, W, O, LimitOffsetClause<L, OffsetClause<Offset>>, G, H, LC>;
477
478 fn offset(self, offset: i64) -> Self::Output {
479 let offset_clause = OffsetClause(offset.into_sql::<BigInt>());
480 SelectStatement::new(
481 self.select,
482 self.from,
483 self.distinct,
484 self.where_clause,
485 self.order,
486 LimitOffsetClause {
487 limit_clause: self.limit_offset.limit_clause,
488 offset_clause,
489 },
490 self.group_by,
491 self.having,
492 self.locking,
493 )
494 }
495}
496
497impl<F, S, D, W, O, LOf, G, H, Expr> GroupByDsl<Expr> for SelectStatement<F, S, D, W, O, LOf, G, H>
498where
499 SelectStatement<F, S, D, W, O, LOf, GroupByClause<Expr>, H>: SelectQuery,
500 Expr: Expression + AppearsOnTable<F>,
501{
502 type Output = SelectStatement<F, S, D, W, O, LOf, GroupByClause<Expr>, H>;
503
504 fn group_by(self, expr: Expr) -> Self::Output {
505 let group_by = GroupByClause(expr);
506 SelectStatement::new(
507 self.select,
508 self.from,
509 self.distinct,
510 self.where_clause,
511 self.order,
512 self.limit_offset,
513 group_by,
514 self.having,
515 self.locking,
516 )
517 }
518}
519
520impl<F, S, W, O, LOf, Lock> LockingDsl<Lock>
521 for SelectStatement<F, S, NoDistinctClause, W, O, LOf>
522{
523 type Output = SelectStatement<
524 F,
525 S,
526 NoDistinctClause,
527 W,
528 O,
529 LOf,
530 NoGroupByClause,
531 NoHavingClause,
532 LockingClause<Lock, NoModifier>,
533 >;
534
535 fn with_lock(self, lock: Lock) -> Self::Output {
536 SelectStatement::new(
537 self.select,
538 self.from,
539 self.distinct,
540 self.where_clause,
541 self.order,
542 self.limit_offset,
543 self.group_by,
544 self.having,
545 LockingClause::new(lock, NoModifier),
546 )
547 }
548}
549
550impl<F, S, D, W, O, LOf, G, H, LC, LM, Modifier> ModifyLockDsl<Modifier>
551 for SelectStatement<F, S, D, W, O, LOf, G, H, LockingClause<LC, LM>>
552{
553 type Output = SelectStatement<F, S, D, W, O, LOf, G, H, LockingClause<LC, Modifier>>;
554
555 fn modify_lock(self, modifier: Modifier) -> Self::Output {
556 SelectStatement::new(
557 self.select,
558 self.from,
559 self.distinct,
560 self.where_clause,
561 self.order,
562 self.limit_offset,
563 self.group_by,
564 self.having,
565 LockingClause::new(self.locking.lock_mode, modifier),
566 )
567 }
568}
569
570impl<'a, F, S, D, W, O, LOf, G, H, DB> BoxedDsl<'a, DB>
571 for SelectStatement<FromClause<F>, S, D, W, O, LOf, G, H>
572where
573 Self: AsQuery,
574 DB: Backend,
575 F: QuerySource,
576 S: SelectClauseExpression<FromClause<F>> + QueryFragment<DB> + Send + 'a,
577 S::Selection: ValidGrouping<G::Expressions>,
578 D: QueryFragment<DB> + Send + 'a,
579 W: Into<BoxedWhereClause<'a, DB>>,
580 O: Into<Option<Box<dyn QueryFragment<DB> + Send + 'a>>>,
581 LOf: IntoBoxedClause<'a, DB, BoxedClause = BoxedLimitOffsetClause<'a, DB>>,
582 G: ValidGroupByClause + QueryFragment<DB> + Send + 'a,
583 H: QueryFragment<DB> + Send + 'a,
584{
585 type Output =
586 BoxedSelectStatement<'a, S::SelectClauseSqlType, FromClause<F>, DB, G::Expressions>;
587
588 fn internal_into_boxed(self) -> Self::Output {
589 BoxedSelectStatement::new(
590 self.select,
591 self.from,
592 Box::new(self.distinct),
593 self.where_clause.into(),
594 self.order.into(),
595 self.limit_offset.into_boxed(),
596 self.group_by,
597 Box::new(self.having),
598 )
599 }
600}
601
602impl<'a, S, D, W, O, LOf, G, H, DB> BoxedDsl<'a, DB>
603 for SelectStatement<NoFromClause, S, D, W, O, LOf, G, H>
604where
605 Self: AsQuery,
606 DB: Backend,
607 S: SelectClauseExpression<NoFromClause> + QueryFragment<DB> + Send + 'a,
608 S::Selection: ValidGrouping<G::Expressions>,
609 D: QueryFragment<DB> + Send + 'a,
610 W: Into<BoxedWhereClause<'a, DB>>,
611 O: Into<Option<Box<dyn QueryFragment<DB> + Send + 'a>>>,
612 LOf: IntoBoxedClause<'a, DB, BoxedClause = BoxedLimitOffsetClause<'a, DB>>,
613 G: ValidGroupByClause + QueryFragment<DB> + Send + 'a,
614 H: QueryFragment<DB> + Send + 'a,
615{
616 type Output =
617 BoxedSelectStatement<'a, S::SelectClauseSqlType, NoFromClause, DB, G::Expressions>;
618
619 fn internal_into_boxed(self) -> Self::Output {
620 BoxedSelectStatement::new_no_from_clause(
621 self.select,
622 self.from,
623 Box::new(self.distinct),
624 self.where_clause.into(),
625 self.order.into(),
626 self.limit_offset.into_boxed(),
627 self.group_by,
628 Box::new(self.having),
629 )
630 }
631}
632
633impl<F, S, D, W, O, LOf, G, H, LC> HasTable
634 for SelectStatement<FromClause<F>, S, D, W, O, LOf, G, H, LC>
635where
636 F: HasTable + QuerySource,
637{
638 type Table = F::Table;
639
640 fn table() -> Self::Table {
641 F::table()
642 }
643}
644
645impl<F, W> IntoUpdateTarget
646 for SelectStatement<FromClause<F>, DefaultSelectClause<FromClause<F>>, NoDistinctClause, W>
647where
648 F: QuerySource,
649 Self: HasTable,
650 W: ValidWhereClause<F>,
651{
652 type WhereClause = W;
653
654 fn into_update_target(self) -> UpdateTarget<Self::Table, Self::WhereClause> {
655 UpdateTarget {
656 table: Self::table(),
657 where_clause: self.where_clause,
658 }
659 }
660}
661
662impl<F, S, D, W, O, LOf, G, H, LC, Rhs> JoinTo<Rhs>
666 for SelectStatement<FromClause<F>, S, D, W, O, LOf, G, H, LC>
667where
668 F: JoinTo<Rhs> + QuerySource,
669{
670 type FromClause = <F as JoinTo<Rhs>>::FromClause;
671 type OnClause = F::OnClause;
672
673 fn join_target(rhs: Rhs) -> (Self::FromClause, Self::OnClause) {
674 F::join_target(rhs)
675 }
676}
677
678impl<F, S, D, W, O, LOf, G, H, LC> QueryDsl for SelectStatement<F, S, D, W, O, LOf, G, H, LC> {}
679
680impl<F, S, D, W, O, LOf, G, H, LC, Conn> RunQueryDsl<Conn>
681 for SelectStatement<F, S, D, W, O, LOf, G, H, LC>
682{
683}
684
685impl<F, S, D, W, O, LOf, G, H, LC, Tab> Insertable<Tab>
686 for SelectStatement<F, S, D, W, O, LOf, G, H, LC>
687where
688 Tab: Table,
689 Self: Query,
690 <Tab::AllColumns as ValidGrouping<()>>::IsAggregate:
691 MixedAggregates<is_aggregate::No, Output = is_aggregate::No>,
692{
693 type Values = InsertFromSelect<Self, Tab::AllColumns>;
694
695 fn values(self) -> Self::Values {
696 InsertFromSelect::new(self)
697 }
698}
699
700impl<F, S, D, W, O, LOf, G, H, LC, Tab> Insertable<Tab>
701 for &SelectStatement<F, S, D, W, O, LOf, G, H, LC>
702where
703 Tab: Table,
704 Self: Query,
705 <Tab::AllColumns as ValidGrouping<()>>::IsAggregate:
706 MixedAggregates<is_aggregate::No, Output = is_aggregate::No>,
707{
708 type Values = InsertFromSelect<Self, Tab::AllColumns>;
709
710 fn values(self) -> Self::Values {
711 InsertFromSelect::new(self)
712 }
713}
714
715impl<F, S, D, W, O, LOf, G, H> SelectNullableDsl
716 for SelectStatement<F, SelectClause<S>, D, W, O, LOf, G, H>
717{
718 type Output = SelectStatement<F, SelectClause<Nullable<S>>, D, W, O, LOf, G, H>;
719
720 fn nullable(self) -> Self::Output {
721 SelectStatement::new(
722 SelectClause(Nullable::new(self.select.0)),
723 self.from,
724 self.distinct,
725 self.where_clause,
726 self.order,
727 self.limit_offset,
728 self.group_by,
729 self.having,
730 self.locking,
731 )
732 }
733}
734
735impl<F, D, W, O, LOf, G, H> SelectNullableDsl
736 for SelectStatement<F, DefaultSelectClause<F>, D, W, O, LOf, G, H>
737where
738 F: AsQuerySource,
739{
740 type Output = SelectStatement<
741 F,
742 SelectClause<Nullable<<F::QuerySource as QuerySource>::DefaultSelection>>,
743 D,
744 W,
745 O,
746 LOf,
747 G,
748 H,
749 >;
750
751 fn nullable(self) -> Self::Output {
752 SelectStatement::new(
753 SelectClause(Nullable::new(
754 self.from.as_query_source().default_selection(),
755 )),
756 self.from,
757 self.distinct,
758 self.where_clause,
759 self.order,
760 self.limit_offset,
761 self.group_by,
762 self.having,
763 self.locking,
764 )
765 }
766}
767
768impl<F, S, D, W, O, LOf, G, H, Predicate> HavingDsl<Predicate>
769 for SelectStatement<FromClause<F>, S, D, W, O, LOf, GroupByClause<G>, H>
770where
771 F: QuerySource,
772 Predicate: AppearsOnTable<F>,
773 Predicate: Expression,
774 Predicate::SqlType: BoolOrNullableBool,
775{
776 type Output =
777 SelectStatement<FromClause<F>, S, D, W, O, LOf, GroupByClause<G>, HavingClause<Predicate>>;
778
779 fn having(self, predicate: Predicate) -> Self::Output {
780 SelectStatement::new(
781 self.select,
782 self.from,
783 self.distinct,
784 self.where_clause,
785 self.order,
786 self.limit_offset,
787 self.group_by,
788 HavingClause(predicate),
789 self.locking,
790 )
791 }
792}
793
794impl<F, S, D, W, O, LOf, G, H, LC> CombineDsl for SelectStatement<F, S, D, W, O, LOf, G, H, LC>
795where
796 Self: Query,
797{
798 type Query = Self;
799
800 fn union<Rhs>(self, rhs: Rhs) -> crate::dsl::Union<Self, Rhs>
801 where
802 Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
803 {
804 CombinationClause::new(Union, Distinct, self, rhs.as_query())
805 }
806
807 fn union_all<Rhs>(self, rhs: Rhs) -> crate::dsl::UnionAll<Self, Rhs>
808 where
809 Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
810 {
811 CombinationClause::new(Union, All, self, rhs.as_query())
812 }
813
814 fn intersect<Rhs>(self, rhs: Rhs) -> crate::dsl::Intersect<Self, Rhs>
815 where
816 Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
817 {
818 CombinationClause::new(Intersect, Distinct, self, rhs.as_query())
819 }
820
821 fn intersect_all<Rhs>(self, rhs: Rhs) -> crate::dsl::IntersectAll<Self, Rhs>
822 where
823 Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
824 {
825 CombinationClause::new(Intersect, All, self, rhs.as_query())
826 }
827
828 fn except<Rhs>(self, rhs: Rhs) -> crate::dsl::Except<Self, Rhs>
829 where
830 Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
831 {
832 CombinationClause::new(Except, Distinct, self, rhs.as_query())
833 }
834
835 fn except_all<Rhs>(self, rhs: Rhs) -> crate::dsl::ExceptAll<Self, Rhs>
836 where
837 Rhs: AsQuery<SqlType = <Self::Query as Query>::SqlType>,
838 {
839 CombinationClause::new(Except, All, self, rhs.as_query())
840 }
841}