use super::comparison::ComparisonOperator;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AggregateFunction {
Count,
Sum,
Avg,
Min,
Max,
}
#[derive(Debug, Clone)]
pub struct AggregateExpr {
function: AggregateFunction,
field: String,
}
impl AggregateExpr {
pub fn count(field: &str) -> Self {
Self {
function: AggregateFunction::Count,
field: field.to_string(),
}
}
pub fn sum(field: &str) -> Self {
Self {
function: AggregateFunction::Sum,
field: field.to_string(),
}
}
pub fn avg(field: &str) -> Self {
Self {
function: AggregateFunction::Avg,
field: field.to_string(),
}
}
pub fn min(field: &str) -> Self {
Self {
function: AggregateFunction::Min,
field: field.to_string(),
}
}
pub fn max(field: &str) -> Self {
Self {
function: AggregateFunction::Max,
field: field.to_string(),
}
}
pub fn function(&self) -> AggregateFunction {
self.function
}
pub fn field(&self) -> &str {
&self.field
}
pub fn gt(self, value: impl Into<ComparisonValue>) -> ComparisonExpr {
ComparisonExpr::new(self, ComparisonOperator::Gt, value.into())
}
pub fn gte(self, value: impl Into<ComparisonValue>) -> ComparisonExpr {
ComparisonExpr::new(self, ComparisonOperator::Gte, value.into())
}
pub fn lt(self, value: impl Into<ComparisonValue>) -> ComparisonExpr {
ComparisonExpr::new(self, ComparisonOperator::Lt, value.into())
}
pub fn lte(self, value: impl Into<ComparisonValue>) -> ComparisonExpr {
ComparisonExpr::new(self, ComparisonOperator::Lte, value.into())
}
pub fn eq(self, value: impl Into<ComparisonValue>) -> ComparisonExpr {
ComparisonExpr::new(self, ComparisonOperator::Eq, value.into())
}
pub fn ne(self, value: impl Into<ComparisonValue>) -> ComparisonExpr {
ComparisonExpr::new(self, ComparisonOperator::Ne, value.into())
}
}
#[derive(Debug, Clone)]
pub enum ComparisonValue {
Int(i64),
Float(f64),
}
impl From<i64> for ComparisonValue {
fn from(i: i64) -> Self {
Self::Int(i)
}
}
impl From<i32> for ComparisonValue {
fn from(i: i32) -> Self {
Self::Int(i.into())
}
}
impl From<f64> for ComparisonValue {
fn from(f: f64) -> Self {
Self::Float(f)
}
}
impl From<f32> for ComparisonValue {
fn from(f: f32) -> Self {
Self::Float(f.into())
}
}
#[derive(Debug, Clone)]
pub struct ComparisonExpr {
pub aggregate: AggregateExpr,
pub op: ComparisonOperator,
pub value: ComparisonValue,
}
impl ComparisonExpr {
pub fn new(aggregate: AggregateExpr, op: ComparisonOperator, value: ComparisonValue) -> Self {
Self {
aggregate,
op,
value,
}
}
}