use super::Function;
use crate::ast::{Column, Expression};
pub type GroupByDefinition<'a> = Expression<'a>;
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Grouping<'a>(pub Vec<GroupByDefinition<'a>>);
impl<'a> Grouping<'a> {
#[doc(hidden)]
pub fn append(mut self, value: GroupByDefinition<'a>) -> Self {
self.0.push(value);
self
}
pub fn new(values: Vec<GroupByDefinition<'a>>) -> Self {
Self(values)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
pub trait Groupable<'a>
where
Self: Sized,
{
fn group(self) -> GroupByDefinition<'a>;
}
pub trait IntoGroupByDefinition<'a> {
fn into_group_by_definition(self) -> GroupByDefinition<'a>;
}
impl<'a> IntoGroupByDefinition<'a> for &'a str {
fn into_group_by_definition(self) -> GroupByDefinition<'a> {
let column: Column = self.into();
column.into()
}
}
impl<'a> IntoGroupByDefinition<'a> for (&'a str, &'a str) {
fn into_group_by_definition(self) -> GroupByDefinition<'a> {
let column: Column = self.into();
column.into()
}
}
impl<'a> IntoGroupByDefinition<'a> for Column<'a> {
fn into_group_by_definition(self) -> GroupByDefinition<'a> {
self.into()
}
}
impl<'a> IntoGroupByDefinition<'a> for Function<'a> {
fn into_group_by_definition(self) -> GroupByDefinition<'a> {
self.into()
}
}
impl<'a> IntoGroupByDefinition<'a> for GroupByDefinition<'a> {
fn into_group_by_definition(self) -> GroupByDefinition<'a> {
self
}
}
impl<'a> Groupable<'a> for Column<'a> {
fn group(self) -> GroupByDefinition<'a> {
self.into()
}
}
impl<'a> Groupable<'a> for &'a str {
fn group(self) -> GroupByDefinition<'a> {
Column::from(self).group()
}
}