toasty_core/stmt/func_count.rs
1use super::{Expr, ExprFunc};
2
3/// The SQL `COUNT` aggregate function.
4///
5/// When `arg` is `None`, represents `COUNT(*)` (counts all rows). When `arg` is
6/// `Some`, counts the number of rows where the argument expression is non-null.
7///
8/// # Examples
9///
10/// ```text
11/// COUNT(*) // arg: None, filter: None
12/// COUNT(column) // arg: Some(column), filter: None
13/// COUNT(*) FILTER (WHERE cond) // arg: None, filter: Some(cond)
14/// ```
15#[derive(Clone, Debug, PartialEq)]
16pub struct FuncCount {
17 /// The expression to count. `None` means `COUNT(*)`.
18 pub arg: Option<Box<Expr>>,
19
20 /// Optional filter applied before counting.
21 pub filter: Option<Box<Expr>>,
22}
23
24impl Expr {
25 /// Creates a `COUNT(*)` expression.
26 pub fn count_star() -> Self {
27 Self::Func(ExprFunc::Count(FuncCount {
28 arg: None,
29 filter: None,
30 }))
31 }
32}
33
34impl From<FuncCount> for ExprFunc {
35 fn from(value: FuncCount) -> Self {
36 Self::Count(value)
37 }
38}
39
40impl From<FuncCount> for Expr {
41 fn from(value: FuncCount) -> Self {
42 Self::Func(value.into())
43 }
44}