use std::marker::PhantomData;
use crate::col::Column;
use crate::model::RowDecode;
#[cfg(feature = "postgres-tokio-postgres")]
use tokio_postgres::types::FromSqlOwned;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Aggregate<M, R> {
pub(crate) table: &'static str,
pub(crate) column: &'static str,
pub(crate) kind: AggregateKind,
pub(crate) _marker: PhantomData<fn() -> (M, R)>,
}
impl<M, R> Aggregate<M, R> {
pub(crate) const fn new(
table: &'static str,
column: &'static str,
kind: AggregateKind,
) -> Self {
Self {
table,
column,
kind,
_marker: PhantomData,
}
}
#[must_use]
pub fn alias(&self) -> String {
format!("{}_{}", self.kind.as_str(), self.column)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AggregateKind {
Sum,
Avg,
Min,
Max,
Count,
CountDistinct,
}
impl AggregateKind {
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Sum => "sum",
Self::Avg => "avg",
Self::Min => "min",
Self::Max => "max",
Self::Count => "count",
Self::CountDistinct => "count_distinct",
}
}
#[must_use]
pub const fn sql_fn(&self) -> &'static str {
match self {
Self::Sum => "SUM",
Self::Avg => "AVG",
Self::Min => "MIN",
Self::Max => "MAX",
Self::Count => "COUNT",
Self::CountDistinct => "COUNT",
}
}
}
pub trait Numeric: Send + Sync + 'static {
type Sum: Send + Sync + 'static;
type Avg: Send + Sync + 'static;
type MinMax: Send + Sync + 'static;
}
impl Numeric for i32 {
type Sum = i64;
type Avg = f64;
type MinMax = i32;
}
impl Numeric for i64 {
type Sum = i64;
type Avg = f64;
type MinMax = i64;
}
impl Numeric for f64 {
type Sum = f64;
type Avg = f64;
type MinMax = f64;
}
impl<T: Numeric> Numeric for Option<T> {
type Sum = Option<T::Sum>;
type Avg = Option<T::Avg>;
type MinMax = Option<T::MinMax>;
}
impl<M, T: Numeric> Column<M, T> {
#[must_use]
pub fn sum(self) -> Aggregate<M, Option<T::Sum>> {
Aggregate::new(self.table, self.column, AggregateKind::Sum)
}
#[must_use]
pub fn avg(self) -> Aggregate<M, Option<T::Avg>> {
Aggregate::new(self.table, self.column, AggregateKind::Avg)
}
#[must_use]
pub fn min(self) -> Aggregate<M, Option<T::MinMax>> {
Aggregate::new(self.table, self.column, AggregateKind::Min)
}
#[must_use]
pub fn max(self) -> Aggregate<M, Option<T::MinMax>> {
Aggregate::new(self.table, self.column, AggregateKind::Max)
}
}
impl<M, T> Column<M, T> {
#[must_use]
pub fn count(self) -> Aggregate<M, i64> {
Aggregate::new(self.table, self.column, AggregateKind::Count)
}
#[must_use]
pub fn count_distinct(self) -> Aggregate<M, i64> {
Aggregate::new(self.table, self.column, AggregateKind::CountDistinct)
}
}
#[cfg(all(feature = "sqlite-rusqlite", not(feature = "postgres-tokio-postgres")))]
pub trait AggregateScalar:
Send
+ Sync
+ 'static
+ for<'r> sqlx::Decode<'r, sqlx::Any>
+ sqlx::Type<sqlx::Any>
+ for<'r> sqlx::Decode<'r, sqlx::Postgres>
+ sqlx::Type<sqlx::Postgres>
+ for<'r> sqlx::Decode<'r, sqlx::Sqlite>
+ sqlx::Type<sqlx::Sqlite>
+ for<'r> sqlx::Decode<'r, sqlx::MySql>
+ sqlx::Type<sqlx::MySql>
+ crate::rusqlite::FromValue
{
}
#[cfg(all(not(feature = "sqlite-rusqlite"), feature = "postgres-tokio-postgres"))]
pub trait AggregateScalar:
Send
+ Sync
+ 'static
+ for<'r> sqlx::Decode<'r, sqlx::Any>
+ sqlx::Type<sqlx::Any>
+ for<'r> sqlx::Decode<'r, sqlx::Postgres>
+ sqlx::Type<sqlx::Postgres>
+ for<'r> sqlx::Decode<'r, sqlx::Sqlite>
+ sqlx::Type<sqlx::Sqlite>
+ for<'r> sqlx::Decode<'r, sqlx::MySql>
+ sqlx::Type<sqlx::MySql>
+ FromSqlOwned
{
}
#[cfg(all(feature = "sqlite-rusqlite", feature = "postgres-tokio-postgres"))]
pub trait AggregateScalar:
Send
+ Sync
+ 'static
+ for<'r> sqlx::Decode<'r, sqlx::Any>
+ sqlx::Type<sqlx::Any>
+ for<'r> sqlx::Decode<'r, sqlx::Postgres>
+ sqlx::Type<sqlx::Postgres>
+ for<'r> sqlx::Decode<'r, sqlx::Sqlite>
+ sqlx::Type<sqlx::Sqlite>
+ for<'r> sqlx::Decode<'r, sqlx::MySql>
+ sqlx::Type<sqlx::MySql>
+ crate::rusqlite::FromValue
+ FromSqlOwned
{
}
#[cfg(all(
not(feature = "sqlite-rusqlite"),
not(feature = "postgres-tokio-postgres")
))]
pub trait AggregateScalar:
Send
+ Sync
+ 'static
+ for<'r> sqlx::Decode<'r, sqlx::Any>
+ sqlx::Type<sqlx::Any>
+ for<'r> sqlx::Decode<'r, sqlx::Postgres>
+ sqlx::Type<sqlx::Postgres>
+ for<'r> sqlx::Decode<'r, sqlx::Sqlite>
+ sqlx::Type<sqlx::Sqlite>
+ for<'r> sqlx::Decode<'r, sqlx::MySql>
+ sqlx::Type<sqlx::MySql>
{
}
impl AggregateScalar for i32 {}
impl AggregateScalar for i64 {}
impl AggregateScalar for f64 {}
impl<T: AggregateScalar> AggregateScalar for Option<T> {}
#[derive(Debug, Clone)]
pub struct AggregateEntry {
pub table: &'static str,
pub column: &'static str,
pub kind: AggregateKind,
pub alias: String,
}
pub trait IntoAggregate<M> {
type Out: AggregateScalar;
fn push_entry(&self, out: &mut Vec<AggregateEntry>);
}
impl<M, T: AggregateScalar> IntoAggregate<M> for Aggregate<M, T> {
type Out = T;
fn push_entry(&self, out: &mut Vec<AggregateEntry>) {
out.push(AggregateEntry {
table: self.table,
column: self.column,
kind: self.kind,
alias: self.alias(),
});
}
}
pub trait AggregateSet<M, R: RowDecode> {
fn push_entries(&self, out: &mut Vec<AggregateEntry>);
}
impl<M, T: AggregateScalar> AggregateSet<M, (T,)> for Aggregate<M, T> {
fn push_entries(&self, out: &mut Vec<AggregateEntry>) {
IntoAggregate::push_entry(self, out);
}
}
macro_rules! impl_aggregate_set_tuples {
($($n:tt $T:ident),+) => {
impl<M, $($T),+> AggregateSet<M, ($($T::Out,)+)> for ($($T,)+)
where
$( $T: IntoAggregate<M>, )+
{
fn push_entries(&self, out: &mut Vec<AggregateEntry>) {
$(
self.$n.push_entry(out);
)+
}
}
};
}
impl_aggregate_set_tuples! { 0 A0 }
impl_aggregate_set_tuples! { 0 A0, 1 A1 }
impl_aggregate_set_tuples! { 0 A0, 1 A1, 2 A2 }
impl_aggregate_set_tuples! { 0 A0, 1 A1, 2 A2, 3 A3 }
impl_aggregate_set_tuples! { 0 A0, 1 A1, 2 A2, 3 A3, 4 A4 }
impl_aggregate_set_tuples! { 0 A0, 1 A1, 2 A2, 3 A3, 4 A4, 5 A5 }
impl_aggregate_set_tuples! { 0 A0, 1 A1, 2 A2, 3 A3, 4 A4, 5 A5, 6 A6 }
impl_aggregate_set_tuples! { 0 A0, 1 A1, 2 A2, 3 A3, 4 A4, 5 A5, 6 A6, 7 A7 }
pub trait GroupBy<M> {
fn columns(&self) -> Vec<&'static str>;
}
impl<M, T> GroupBy<M> for Column<M, T> {
fn columns(&self) -> Vec<&'static str> {
vec![self.column]
}
}
macro_rules! impl_group_by_tuples {
($($n:tt $T:ident),+) => {
impl<M, $($T),+> GroupBy<M> for ($($T,)+)
where
$( $T: GroupBy<M>, )+
{
fn columns(&self) -> Vec<&'static str> {
let mut cols = Vec::new();
$(
cols.extend(self.$n.columns());
)+
cols
}
}
};
}
impl_group_by_tuples! { 0 G0 }
impl_group_by_tuples! { 0 G0, 1 G1 }
impl_group_by_tuples! { 0 G0, 1 G1, 2 G2 }
impl_group_by_tuples! { 0 G0, 1 G1, 2 G2, 3 G3 }
impl_group_by_tuples! { 0 G0, 1 G1, 2 G2, 3 G3, 4 G4 }
impl_group_by_tuples! { 0 G0, 1 G1, 2 G2, 3 G3, 4 G4, 5 G5 }
impl_group_by_tuples! { 0 G0, 1 G1, 2 G2, 3 G3, 4 G4, 5 G5, 6 G6 }
impl_group_by_tuples! { 0 G0, 1 G1, 2 G2, 3 G3, 4 G4, 5 G5, 6 G6, 7 G7 }