#[cfg(doc)]
use super::functions::aggregate_expressions::{
AggregateExpressionMethods, WindowExpressionMethods,
};
use super::functions::declare_sql_function;
use super::{Expression, ValidGrouping};
use crate::backend::Backend;
use crate::internal::sql_functions::{
FunctionFragment, IsWindowFunction, OverClause, WindowFunctionFragment,
};
use crate::query_builder::*;
use crate::result::QueryResult;
use crate::sql_types::{BigInt, DieselNumericOps, SingleValue, SqlType};
#[declare_sql_function]
extern "SQL" {
#[aggregate]
#[window]
fn count<T: SqlType + SingleValue>(expr: T) -> BigInt;
}
pub fn count_star() -> CountStar {
CountStar
}
#[derive(Debug, Clone, Copy, QueryId, DieselNumericOps, ValidGrouping)]
#[diesel(aggregate)]
#[doc(hidden)]
pub struct CountStar;
impl Expression for CountStar {
type SqlType = BigInt;
}
impl<DB: Backend> FunctionFragment<DB> for CountStar {
const FUNCTION_NAME: &'static str = "COUNT";
fn walk_arguments<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
out.push_sql("*");
Ok(())
}
}
impl<DB: Backend> QueryFragment<DB> for CountStar {
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, DB>) -> QueryResult<()> {
out.push_sql("COUNT(*)");
Ok(())
}
}
impl<Partition, Order, Frame, DB: Backend> WindowFunctionFragment<CountStar, DB>
for OverClause<Partition, Order, Frame>
{
}
impl IsWindowFunction for CountStar {
type ArgTypes = ();
}
impl_selectable_expression!(CountStar);
#[doc(hidden)]
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
#[deprecated(note = "Use `AggregateExpressionMethods::aggregate_distinct` instead")]
pub fn count_distinct<T, E>(expr: E) -> CountDistinct<T, E::Expression>
where
T: SqlType + SingleValue,
E: crate::expression::AsExpression<T>,
{
use crate::AggregateExpressionMethods;
count(expr).aggregate_distinct()
}
#[doc(hidden)]
#[cfg(all(feature = "with-deprecated", not(feature = "without-deprecated")))]
pub type CountDistinct<T, E> = crate::dsl::AggregateDistinct<self::count<T, E>>;