Skip to main content

diesel/query_builder/select_statement/
dsl_impls.rs

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