use super::aggregate_filter::NoFilter; use super::aggregate_order::NoOrder; use super::frame_clause::NoFrame; use super::over_clause::{OverClause, ValidAggregateFilterForWindow}; use super::prefix::NoPrefix; use super::NoWindow; use super::{AggregateExpression, IsWindowFunction}; use crate::query_builder::QueryFragment; use crate::query_builder::{AstPass, QueryId}; use crate::QueryResult; empty_clause!(NoPartition); #[derive(QueryId, Clone, Copy, Debug)] pub struct PartitionBy(T); impl QueryFragment for PartitionBy where T: QueryFragment, DB: crate::backend::Backend, { fn walk_ast<'b>(&'b self, mut pass: AstPass<'_, 'b, DB>) -> QueryResult<()> { pass.push_sql(" PARTITION BY "); self.0.walk_ast(pass.reborrow())?; Ok(()) } } pub trait PartitionByDsl { type Output; fn partition_by(self, expr: E) -> Self::Output; } impl PartitionByDsl for AggregateExpression> where Filter: ValidAggregateFilterForWindow, Order, Frame>>, { type Output = AggregateExpression< Fn, NoPrefix, NoOrder, Filter, OverClause, Order, Frame>, >; fn partition_by(self, expr: E) -> Self::Output { AggregateExpression { prefix: NoPrefix, function: self.function, order: NoOrder, filter: self.filter, window: OverClause { partition_by: PartitionBy(expr), order: self.window.order, frame_clause: self.window.frame_clause, }, } } } impl PartitionByDsl for AggregateExpression where Filter: ValidAggregateFilterForWindow, NoOrder, NoFrame>>, { type Output = AggregateExpression< Fn, NoPrefix, NoOrder, Filter, OverClause, NoOrder, NoFrame>, >; fn partition_by(self, expr: E) -> Self::Output { AggregateExpression { prefix: NoPrefix, function: self.function, order: NoOrder, filter: self.filter, window: OverClause { partition_by: PartitionBy(expr), order: NoOrder, frame_clause: NoFrame, }, } } } impl PartitionByDsl for Fn where Fn: IsWindowFunction, { type Output = AggregateExpression< Fn, NoPrefix, NoOrder, NoFilter, OverClause, NoOrder, NoFrame>, >; fn partition_by(self, expr: E) -> Self::Output { AggregateExpression { prefix: NoPrefix, function: self, order: NoOrder, filter: NoFilter, window: OverClause { partition_by: PartitionBy(expr), order: NoOrder, frame_clause: NoFrame, }, } } }