use super::BoxedSelectStatement; use crate::associations::HasTable; use crate::backend::Backend; use crate::dsl::AsExprOf; use crate::expression::nullable::Nullable; use crate::expression::*; use crate::insertable::Insertable; use crate::query_builder::combination_clause::*; use crate::query_builder::distinct_clause::*; use crate::query_builder::from_clause::AsQuerySource; use crate::query_builder::from_clause::FromClause; use crate::query_builder::group_by_clause::*; use crate::query_builder::insert_statement::InsertFromSelect; use crate::query_builder::limit_clause::*; use crate::query_builder::limit_offset_clause::{BoxedLimitOffsetClause, LimitOffsetClause}; use crate::query_builder::locking_clause::*; use crate::query_builder::offset_clause::*; use crate::query_builder::order_clause::*; use crate::query_builder::select_clause::*; use crate::query_builder::update_statement::target::*; use crate::query_builder::where_clause::*; use crate::query_builder::NoFromClause; use crate::query_builder::{ AsQuery, IntoBoxedClause, Query, QueryFragment, SelectQuery, SelectStatement, }; use crate::query_dsl::group_by_dsl::ValidDistinctForGroupBy; use crate::query_dsl::methods::*; use crate::query_dsl::order_dsl::ValidOrderingForDistinct; use crate::query_dsl::*; use crate::query_source::joins::{Join, JoinOn, JoinTo}; use crate::query_source::QuerySource; use crate::sql_types::{BigInt, BoolOrNullableBool}; impl InternalJoinDsl for SelectStatement, DefaultSelectClause>, D, W, O, LOf, G, H, LC> where F: QuerySource, Rhs: QuerySource, JoinOn, On>: QuerySource, SelectStatement< FromClause, On>>, DefaultSelectClause, On>>>, D, W, O, LOf, G, H, LC, >: AsQuery, { type Output = SelectStatement< FromClause, On>>, DefaultSelectClause, On>>>, D, W, O, LOf, G, H, LC, >; fn join(self, rhs: Rhs, kind: Kind, on: On) -> Self::Output { let from = FromClause::new(Join::new(self.from.source, rhs, kind).on(on)); SelectStatement::new( DefaultSelectClause::new(&from), from, self.distinct, self.where_clause, self.order, self.limit_offset, self.group_by, self.having, self.locking, ) } } impl InternalJoinDsl for SelectStatement, SelectClause, D, W, O, LOf, G, H, LC> where F: QuerySource, Rhs: QuerySource, JoinOn, On>: QuerySource, SelectStatement< FromClause, On>>, SelectClause, D, W, O, LOf, G, H, LC, >: AsQuery, { type Output = SelectStatement< FromClause, On>>, SelectClause, D, W, O, LOf, G, H, LC, >; fn join(self, rhs: Rhs, kind: Kind, on: On) -> Self::Output { SelectStatement::new( self.select, FromClause::new(Join::new(self.from.source, rhs, kind).on(on)), self.distinct, self.where_clause, self.order, self.limit_offset, self.group_by, self.having, self.locking, ) } } impl SelectDsl for SelectStatement, S, D, W, O, LOf, G, H, LC> where G: ValidGroupByClause, F: QuerySource, Selection: SelectableExpression + ValidGrouping, SelectStatement, SelectClause, D, W, O, LOf, G, H, LC>: SelectQuery, D: ValidDistinctForGroupBy, O: ValidGrouping, >::IsAggregate: MixedAggregates<>::IsAggregate>, { type Output = SelectStatement, SelectClause, D, W, O, LOf, G, H, LC>; fn select(self, selection: Selection) -> Self::Output { SelectStatement::new( SelectClause(selection), self.from, self.distinct, self.where_clause, self.order, self.limit_offset, self.group_by, self.having, self.locking, ) } } impl SelectDsl for SelectStatement where G: ValidGroupByClause, Selection: SelectableExpression + ValidGrouping, SelectStatement, D, W, O, LOf, G, H, LC>: SelectQuery, D: ValidDistinctForGroupBy, { type Output = SelectStatement, D, W, O, LOf, G, H, LC>; fn select(self, selection: Selection) -> Self::Output { SelectStatement::new( SelectClause(selection), self.from, self.distinct, self.where_clause, self.order, self.limit_offset, self.group_by, self.having, self.locking, ) } } impl DistinctDsl for SelectStatement where Self: SelectQuery, SelectStatement: SelectQuery, { type Output = SelectStatement; fn distinct(self) -> Self::Output { SelectStatement::new( self.select, self.from, DistinctClause, self.where_clause, self.order, self.limit_offset, self.group_by, self.having, self.locking, ) } } impl FilterDsl for SelectStatement where Predicate: Expression + NonAggregate, Predicate::SqlType: BoolOrNullableBool, W: WhereAnd, { type Output = SelectStatement; fn filter(self, predicate: Predicate) -> Self::Output { SelectStatement::new( self.select, self.from, self.distinct, self.where_clause.and(predicate), self.order, self.limit_offset, self.group_by, self.having, self.locking, ) } } impl OrFilterDsl for SelectStatement where Predicate: Expression + NonAggregate, Predicate::SqlType: BoolOrNullableBool, W: WhereOr, { type Output = SelectStatement; fn or_filter(self, predicate: Predicate) -> Self::Output { SelectStatement::new( self.select, self.from, self.distinct, self.where_clause.or(predicate), self.order, self.limit_offset, self.group_by, self.having, self.locking, ) } } use crate::dsl::Filter; use crate::expression_methods::EqAll; use crate::query_builder::having_clause::{HavingClause, NoHavingClause}; use crate::query_source::Table; impl FindDsl for SelectStatement, S, D, W, O, LOf, G, H, LC> where F: Table, F::PrimaryKey: EqAll, Self: FilterDsl<>::Output>, { type Output = Filter>::Output>; fn find(self, id: PK) -> Self::Output { let primary_key = self.from.source.primary_key(); FilterDsl::filter(self, primary_key.eq_all(id)) } } // no impls for `NoFromClause` here because order is not really supported there yet // Without GROUP BY: validate that SELECT and ORDER BY have matching aggregate nature. impl OrderDsl for SelectStatement, S, D, W, O, LOf, NoGroupByClause, H, LC> where F: QuerySource, Expr: AppearsOnTable, Self: SelectQuery, SelectStatement, S, D, W, OrderClause, LOf, NoGroupByClause, H, LC>: SelectQuery, OrderClause: ValidOrderingForDistinct, S: SelectClauseExpression>, Expr: ValidGrouping<()>, S::Selection: ValidGrouping<()>, >::IsAggregate: MixedAggregates<>::IsAggregate>, { type Output = SelectStatement, S, D, W, OrderClause, LOf, NoGroupByClause, H, LC>; fn order(self, expr: Expr) -> Self::Output { let order = OrderClause(expr); SelectStatement::new( self.select, self.from, self.distinct, self.where_clause, order, self.limit_offset, self.group_by, self.having, self.locking, ) } } // With GROUP BY: only validate that the ORDER BY expression is valid for the group // (grouped column or aggregate). SELECT validity is enforced by the Query impl at // execution time, so checking it here would reject valid queries where order_by() // is called before select() with a non-trivial GROUP BY. impl OrderDsl for SelectStatement, S, D, W, O, LOf, GroupByClause, H, LC> where F: QuerySource, Expr: AppearsOnTable, Self: SelectQuery, SelectStatement, S, D, W, OrderClause, LOf, GroupByClause, H, LC>: SelectQuery, OrderClause: ValidOrderingForDistinct, Expr: ValidGrouping, { type Output = SelectStatement, S, D, W, OrderClause, LOf, GroupByClause, H, LC>; fn order(self, expr: Expr) -> Self::Output { let order = OrderClause(expr); SelectStatement::new( self.select, self.from, self.distinct, self.where_clause, order, self.limit_offset, self.group_by, self.having, self.locking, ) } } // Without GROUP BY: validate that SELECT and the new ORDER BY term have matching // aggregate nature. impl ThenOrderDsl for SelectStatement, S, D, W, OrderClause, LOf, NoGroupByClause, H, LC> where F: QuerySource, Expr: AppearsOnTable, S: SelectClauseExpression>, Expr: ValidGrouping<()>, S::Selection: ValidGrouping<()>, >::IsAggregate: MixedAggregates<>::IsAggregate>, { type Output = SelectStatement< FromClause, S, D, W, OrderClause<(O, Expr)>, LOf, NoGroupByClause, H, LC, >; fn then_order_by(self, expr: Expr) -> Self::Output { SelectStatement::new( self.select, self.from, self.distinct, self.where_clause, OrderClause((self.order.0, expr)), self.limit_offset, self.group_by, self.having, self.locking, ) } } // With GROUP BY: Validate the whole order expression against the given group by expression impl ThenOrderDsl for SelectStatement, S, D, W, OrderClause, LOf, GroupByClause, H, LC> where F: QuerySource, Expr: AppearsOnTable, Self: SelectQuery, SelectStatement, S, D, W, OrderClause<(O, Expr)>, LOf, GroupByClause, H, LC>: SelectQuery, (O, Expr): ValidGrouping, { type Output = SelectStatement< FromClause, S, D, W, OrderClause<(O, Expr)>, LOf, GroupByClause, H, LC, >; fn then_order_by(self, expr: Expr) -> Self::Output { SelectStatement::new( self.select, self.from, self.distinct, self.where_clause, OrderClause((self.order.0, expr)), self.limit_offset, self.group_by, self.having, self.locking, ) } } impl ThenOrderDsl for SelectStatement where Expr: Expression, Self: OrderDsl, { type Output = crate::dsl::Order; fn then_order_by(self, expr: Expr) -> Self::Output { self.order_by(expr) } } #[doc(hidden)] type Limit = AsExprOf; impl LimitDsl for SelectStatement, G, H, LC> where Self: SelectQuery, SelectStatement, Of>, G, H, LC>: SelectQuery, { type Output = SelectStatement, Of>, G, H, LC>; fn limit(self, limit: i64) -> Self::Output { let limit_clause = LimitClause(limit.into_sql::()); SelectStatement::new( self.select, self.from, self.distinct, self.where_clause, self.order, LimitOffsetClause { limit_clause, offset_clause: self.limit_offset.offset_clause, }, self.group_by, self.having, self.locking, ) } } #[doc(hidden)] type Offset = Limit; impl OffsetDsl for SelectStatement, G, H, LC> where Self: SelectQuery, SelectStatement>, G, H, LC>: SelectQuery, { type Output = SelectStatement>, G, H, LC>; fn offset(self, offset: i64) -> Self::Output { let offset_clause = OffsetClause(offset.into_sql::()); SelectStatement::new( self.select, self.from, self.distinct, self.where_clause, self.order, LimitOffsetClause { limit_clause: self.limit_offset.limit_clause, offset_clause, }, self.group_by, self.having, self.locking, ) } } impl GroupByDsl for SelectStatement where SelectStatement, H>: SelectQuery, Expr: Expression + AppearsOnTable, { type Output = SelectStatement, H>; fn group_by(self, expr: Expr) -> Self::Output { let group_by = GroupByClause(expr); SelectStatement::new( self.select, self.from, self.distinct, self.where_clause, self.order, self.limit_offset, group_by, self.having, self.locking, ) } } impl LockingDsl for SelectStatement { type Output = SelectStatement< F, S, NoDistinctClause, W, O, LOf, NoGroupByClause, NoHavingClause, LockingClause, >; fn with_lock(self, lock: Lock) -> Self::Output { SelectStatement::new( self.select, self.from, self.distinct, self.where_clause, self.order, self.limit_offset, self.group_by, self.having, LockingClause::new(lock, NoModifier), ) } } impl ModifyLockDsl for SelectStatement> { type Output = SelectStatement>; fn modify_lock(self, modifier: Modifier) -> Self::Output { SelectStatement::new( self.select, self.from, self.distinct, self.where_clause, self.order, self.limit_offset, self.group_by, self.having, LockingClause::new(self.locking.lock_mode, modifier), ) } } impl<'a, F, S, D, W, O, LOf, G, H, DB> BoxedDsl<'a, DB> for SelectStatement, S, D, W, O, LOf, G, H> where Self: AsQuery, DB: Backend, F: QuerySource, S: SelectClauseExpression> + QueryFragment + Send + 'a, S::Selection: ValidGrouping, D: QueryFragment + Send + 'a, W: Into>, O: Into + Send + 'a>>>, LOf: IntoBoxedClause<'a, DB, BoxedClause = BoxedLimitOffsetClause<'a, DB>>, G: ValidGroupByClause + QueryFragment + Send + 'a, H: QueryFragment + Send + 'a, { type Output = BoxedSelectStatement<'a, S::SelectClauseSqlType, FromClause, DB, G::Expressions>; fn internal_into_boxed(self) -> Self::Output { BoxedSelectStatement::new( self.select, self.from, Box::new(self.distinct), self.where_clause.into(), self.order.into(), self.limit_offset.into_boxed(), self.group_by, Box::new(self.having), ) } } impl<'a, S, D, W, O, LOf, G, H, DB> BoxedDsl<'a, DB> for SelectStatement where Self: AsQuery, DB: Backend, S: SelectClauseExpression + QueryFragment + Send + 'a, S::Selection: ValidGrouping, D: QueryFragment + Send + 'a, W: Into>, O: Into + Send + 'a>>>, LOf: IntoBoxedClause<'a, DB, BoxedClause = BoxedLimitOffsetClause<'a, DB>>, G: ValidGroupByClause + QueryFragment + Send + 'a, H: QueryFragment + Send + 'a, { type Output = BoxedSelectStatement<'a, S::SelectClauseSqlType, NoFromClause, DB, G::Expressions>; fn internal_into_boxed(self) -> Self::Output { BoxedSelectStatement::new_no_from_clause( self.select, self.from, Box::new(self.distinct), self.where_clause.into(), self.order.into(), self.limit_offset.into_boxed(), self.group_by, Box::new(self.having), ) } } impl HasTable for SelectStatement, S, D, W, O, LOf, G, H, LC> where F: HasTable + QuerySource, { type Table = F::Table; fn table() -> Self::Table { F::table() } } impl IntoUpdateTarget for SelectStatement, DefaultSelectClause>, NoDistinctClause, W> where F: QuerySource, Self: HasTable, W: ValidWhereClause, { type WhereClause = W; fn into_update_target(self) -> UpdateTarget { UpdateTarget { table: Self::table(), where_clause: self.where_clause, } } } // FIXME: Should we disable joining when `.group_by` has been called? Are there // any other query methods where a join no longer has the same semantics as // joining on just the table? impl JoinTo for SelectStatement, S, D, W, O, LOf, G, H, LC> where F: JoinTo + QuerySource, { type FromClause = >::FromClause; type OnClause = F::OnClause; fn join_target(rhs: Rhs) -> (Self::FromClause, Self::OnClause) { F::join_target(rhs) } } impl QueryDsl for SelectStatement {} impl RunQueryDsl for SelectStatement { } impl Insertable for SelectStatement where Tab: Table, Self: Query, >::IsAggregate: MixedAggregates, { type Values = InsertFromSelect; fn values(self) -> Self::Values { InsertFromSelect::new(self) } } impl Insertable for &SelectStatement where Tab: Table, Self: Query, >::IsAggregate: MixedAggregates, { type Values = InsertFromSelect; fn values(self) -> Self::Values { InsertFromSelect::new(self) } } impl SelectNullableDsl for SelectStatement, D, W, O, LOf, G, H> { type Output = SelectStatement>, D, W, O, LOf, G, H>; fn nullable(self) -> Self::Output { SelectStatement::new( SelectClause(Nullable::new(self.select.0)), self.from, self.distinct, self.where_clause, self.order, self.limit_offset, self.group_by, self.having, self.locking, ) } } impl SelectNullableDsl for SelectStatement, D, W, O, LOf, G, H> where F: AsQuerySource, { type Output = SelectStatement< F, SelectClause::DefaultSelection>>, D, W, O, LOf, G, H, >; fn nullable(self) -> Self::Output { SelectStatement::new( SelectClause(Nullable::new( self.from.as_query_source().default_selection(), )), self.from, self.distinct, self.where_clause, self.order, self.limit_offset, self.group_by, self.having, self.locking, ) } } impl HavingDsl for SelectStatement, S, D, W, O, LOf, GroupByClause, H> where F: QuerySource, Predicate: AppearsOnTable, Predicate: Expression, Predicate::SqlType: BoolOrNullableBool, { type Output = SelectStatement, S, D, W, O, LOf, GroupByClause, HavingClause>; fn having(self, predicate: Predicate) -> Self::Output { SelectStatement::new( self.select, self.from, self.distinct, self.where_clause, self.order, self.limit_offset, self.group_by, HavingClause(predicate), self.locking, ) } } impl CombineDsl for SelectStatement where Self: Query, { type Query = Self; fn union(self, rhs: Rhs) -> crate::dsl::Union where Rhs: AsQuery::SqlType>, { CombinationClause::new(Union, Distinct, self, rhs.as_query()) } fn union_all(self, rhs: Rhs) -> crate::dsl::UnionAll where Rhs: AsQuery::SqlType>, { CombinationClause::new(Union, All, self, rhs.as_query()) } fn intersect(self, rhs: Rhs) -> crate::dsl::Intersect where Rhs: AsQuery::SqlType>, { CombinationClause::new(Intersect, Distinct, self, rhs.as_query()) } fn intersect_all(self, rhs: Rhs) -> crate::dsl::IntersectAll where Rhs: AsQuery::SqlType>, { CombinationClause::new(Intersect, All, self, rhs.as_query()) } fn except(self, rhs: Rhs) -> crate::dsl::Except where Rhs: AsQuery::SqlType>, { CombinationClause::new(Except, Distinct, self, rhs.as_query()) } fn except_all(self, rhs: Rhs) -> crate::dsl::ExceptAll where Rhs: AsQuery::SqlType>, { CombinationClause::new(Except, All, self, rhs.as_query()) } }