use cratestack_sql::IntoColumnName;
use crate::{ModelDescriptor, SqlxRuntime};
use super::aggregate_column::AggregateColumn;
use super::aggregate_count::AggregateCount;
#[derive(Debug, Clone, Copy)]
pub(super) enum AggregateOp {
Sum,
Avg,
Min,
Max,
}
impl AggregateOp {
pub(super) fn function_name(self) -> &'static str {
match self {
Self::Sum => "SUM",
Self::Avg => "AVG",
Self::Min => "MIN",
Self::Max => "MAX",
}
}
}
#[derive(Debug, Clone)]
pub struct Aggregate<'a, M: 'static, PK: 'static> {
pub(crate) runtime: &'a SqlxRuntime,
pub(crate) descriptor: &'static ModelDescriptor<M, PK>,
}
impl<'a, M: 'static, PK: 'static> Aggregate<'a, M, PK> {
pub fn count(self) -> AggregateCount<'a, M, PK> {
AggregateCount::new(self.runtime, self.descriptor)
}
pub fn sum<C: IntoColumnName>(self, column: C) -> AggregateColumn<'a, M, PK> {
AggregateColumn::new(self.runtime, self.descriptor, AggregateOp::Sum, column)
}
pub fn avg<C: IntoColumnName>(self, column: C) -> AggregateColumn<'a, M, PK> {
AggregateColumn::new(self.runtime, self.descriptor, AggregateOp::Avg, column)
}
pub fn min<C: IntoColumnName>(self, column: C) -> AggregateColumn<'a, M, PK> {
AggregateColumn::new(self.runtime, self.descriptor, AggregateOp::Min, column)
}
pub fn max<C: IntoColumnName>(self, column: C) -> AggregateColumn<'a, M, PK> {
AggregateColumn::new(self.runtime, self.descriptor, AggregateOp::Max, column)
}
}