zino_orm/
aggregate.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use self::Aggregation::*;
use super::{query::QueryExt, Entity};
use zino_core::model::Query;

/// SQL aggregate functions.
///
/// # Examples
/// ```rust,ignore
/// use crate::model::{Task, TaskColumn};
/// use zino_core::Map;
/// use zino_orm::{Aggregation, QueryBuilder, Schema};
///
/// let query = QueryBuilder::<Task>::new()
///     .aggregate(Aggregation::Count(TaskColumn::Id, false), Some("num_tasks"))
///     .aggregate(Aggregation::Sum(TaskColumn::Manhours), Some("total_manhours"))
///     .aggregate(Aggregation::Avg(TaskColumn::Manhours), Some("average_manhours"))
///     .and_eq(TaskColumn::Status, "Completed")
///     .group_by(TaskColumn::ProjectId)
///     .having_ge(Aggregation::Avg(TaskColumn::Manhours), 50)
///     .order_desc("total_manhours")
///     .limit(10)
///     .build();
/// let entries = Task::aggregate::<Map>(&query).await?;
/// ```
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum Aggregation<E: Entity> {
    /// The `COUNT` function with the `DISTINCT` modifier.
    Count(E::Column, bool),
    /// The `SUM` function.
    Sum(E::Column),
    /// The `AVG` function.
    Avg(E::Column),
    /// The `MIN` function.
    Min(E::Column),
    /// The `MAX` function.
    Max(E::Column),
    /// The `STDDEV` function.
    Stddev(E::Column),
    /// The `VARIANCE` function.
    Variance(E::Column),
    /// The `JSON_ARRAYAGG` function.
    JsonArrayagg(E::Column),
    /// The `JSON_OBJECTAGG` function.
    JsonObjectagg(E::Column, E::Column),
}

impl<E: Entity> Aggregation<E> {
    /// Returns a default alias for the aggregation.
    pub(super) fn default_alias(&self) -> String {
        match self {
            Count(col, distinct) => {
                if *distinct {
                    [col.as_ref(), "_distinct"].concat()
                } else {
                    [col.as_ref(), "_count"].concat()
                }
            }
            Sum(col) => [col.as_ref(), "_sum"].concat(),
            Avg(col) => [col.as_ref(), "_avg"].concat(),
            Min(col) => [col.as_ref(), "_min"].concat(),
            Max(col) => [col.as_ref(), "_max"].concat(),
            Stddev(col) => [col.as_ref(), "_stddev"].concat(),
            Variance(col) => [col.as_ref(), "_variance"].concat(),
            JsonArrayagg(col) => [col.as_ref(), "_arrayagg"].concat(),
            JsonObjectagg(key_col, val_col) => {
                [key_col.as_ref(), "_", val_col.as_ref(), "_objectagg"].concat()
            }
        }
    }

    /// Returns the SQL expression.
    pub(super) fn expr(&self) -> String {
        match self {
            Count(col, distinct) => {
                let col_name = E::format_column(col);
                let field = Query::format_field(&col_name);
                if *distinct {
                    format!("count(distinct {field})")
                } else {
                    format!("count({field})")
                }
            }
            Sum(col) => {
                let col_name = E::format_column(col);
                let field = Query::format_field(&col_name);
                format!("sum({field})")
            }
            Avg(col) => {
                let col_name = E::format_column(col);
                let field = Query::format_field(&col_name);
                format!("avg({field})")
            }
            Min(col) => {
                let col_name = E::format_column(col);
                let field = Query::format_field(&col_name);
                format!("min({field})")
            }
            Max(col) => {
                let col_name = E::format_column(col);
                let field = Query::format_field(&col_name);
                format!("max({field})")
            }
            Stddev(col) => {
                let col_name = E::format_column(col);
                let field = Query::format_field(&col_name);
                format!("stddev({field})")
            }
            Variance(col) => {
                let col_name = E::format_column(col);
                let field = Query::format_field(&col_name);
                format!("variance({field})")
            }
            JsonArrayagg(col) => {
                let col_name = E::format_column(col);
                let field = Query::format_field(&col_name);
                if cfg!(any(
                    feature = "orm-mariadb",
                    feature = "orm-mysql",
                    feature = "orm-tidb"
                )) {
                    format!("json_arrayagg({field})")
                } else if cfg!(feature = "orm-postgres") {
                    format!("jsonb_agg({field})")
                } else {
                    format!("json_group_array({field})")
                }
            }
            JsonObjectagg(key_col, val_col) => {
                let key_col_name = E::format_column(key_col);
                let val_col_name = E::format_column(val_col);
                let key_field = Query::format_field(&key_col_name);
                let val_field = Query::format_field(&val_col_name);
                if cfg!(any(
                    feature = "orm-mariadb",
                    feature = "orm-mysql",
                    feature = "orm-tidb"
                )) {
                    format!("json_objectagg({key_field}, {val_field})")
                } else if cfg!(feature = "orm-postgres") {
                    format!("jsonb_object_agg({key_field}, {val_field})")
                } else {
                    format!("json_group_object({key_field}, {val_field})")
                }
            }
        }
    }
}