Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
AVG and SUM implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn committed Feb 28, 2020
1 parent ac264e7 commit a941fbc
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/ast/function.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
mod aggregate_to_string;
mod average;
mod count;
mod row_number;
mod sum;

pub use aggregate_to_string::*;
pub use average::*;
pub use count::*;
pub use row_number::*;
pub use sum::*;

use super::DatabaseValue;
use std::borrow::Cow;
Expand All @@ -22,6 +26,8 @@ pub(crate) enum FunctionType<'a> {
RowNumber(RowNumber<'a>),
Count(Count<'a>),
AggregateToString(AggregateToString<'a>),
Average(Average<'a>),
Sum(Sum<'a>),
}

impl<'a> Function<'a> {
Expand Down Expand Up @@ -58,4 +64,4 @@ macro_rules! function {
);
}

function!(RowNumber, Count, AggregateToString);
function!(RowNumber, Count, AggregateToString, Average, Sum);
22 changes: 22 additions & 0 deletions src/ast/function/average.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::ast::Column;

#[derive(Debug, Clone, PartialEq)]
pub struct Average<'a> {
pub(crate) column: Column<'a>,
}

/// Calculates the average value of a numeric column.
///
/// ```rust
/// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
/// let query = Select::from_table("users").value(avg("age"));
/// let (sql, _) = Sqlite::build(query);
/// assert_eq!("SELECT AVG(`age`) FROM `users`", sql);
/// ```
#[inline]
pub fn avg<'a, C>(col: C) -> Average<'a>
where
C: Into<Column<'a>>,
{
Average { column: col.into() }
}
22 changes: 22 additions & 0 deletions src/ast/function/sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::ast::Column;

#[derive(Debug, Clone, PartialEq)]
pub struct Sum<'a> {
pub(crate) column: Column<'a>,
}

/// Calculates the sum value of a numeric column.
///
/// ```rust
/// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
/// let query = Select::from_table("users").value(sum("age"));
/// let (sql, _) = Sqlite::build(query);
/// assert_eq!("SELECT SUM(`age`) FROM `users`", sql);
/// ```
#[inline]
pub fn sum<'a, C>(col: C) -> Sum<'a>
where
C: Into<Column<'a>>,
{
Sum { column: col.into() }
}
8 changes: 8 additions & 0 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,14 @@ pub trait Visitor<'a> {
FunctionType::AggregateToString(agg) => {
self.visit_aggregate_to_string(agg.value.as_ref().clone())?;
}
FunctionType::Average(avg) => {
self.write("AVG")?;
self.surround_with("(", ")", |ref mut s| s.visit_column(avg.column))?;
}
FunctionType::Sum(sum) => {
self.write("SUM")?;
self.surround_with("(", ")", |ref mut s| s.visit_column(sum.column))?;
}
};

if let Some(alias) = fun.alias {
Expand Down

0 comments on commit a941fbc

Please sign in to comment.