sz_orm_core/
partial_model.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum AggFunc {
34 Count,
36 Sum,
38 Avg,
40 Max,
42 Min,
44}
45
46impl AggFunc {
47 pub fn as_sql(self) -> &'static str {
49 match self {
50 AggFunc::Count => "COUNT",
51 AggFunc::Sum => "SUM",
52 AggFunc::Avg => "AVG",
53 AggFunc::Max => "MAX",
54 AggFunc::Min => "MIN",
55 }
56 }
57}
58
59#[derive(Debug, Clone)]
63pub struct Expr {
64 func: AggFunc,
65 column: String,
66}
67
68impl Expr {
69 pub fn count(column: impl Into<String>) -> Self {
71 Self {
72 func: AggFunc::Count,
73 column: column.into(),
74 }
75 }
76
77 pub fn sum(column: impl Into<String>) -> Self {
79 Self {
80 func: AggFunc::Sum,
81 column: column.into(),
82 }
83 }
84
85 pub fn avg(column: impl Into<String>) -> Self {
87 Self {
88 func: AggFunc::Avg,
89 column: column.into(),
90 }
91 }
92
93 pub fn max(column: impl Into<String>) -> Self {
95 Self {
96 func: AggFunc::Max,
97 column: column.into(),
98 }
99 }
100
101 pub fn min(column: impl Into<String>) -> Self {
103 Self {
104 func: AggFunc::Min,
105 column: column.into(),
106 }
107 }
108
109 pub fn render(&self) -> String {
111 format!("{}({})", self.func.as_sql(), self.column)
112 }
113
114 pub fn render_as(&self, alias: &str) -> String {
116 format!("{}({}) AS {}", self.func.as_sql(), self.column, alias)
117 }
118}
119
120#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
122pub enum SelectMode {
123 #[default]
125 All,
126 Partial,
128}
129
130#[cfg(test)]
131mod tests {
132 use super::*;
133
134 #[test]
135 fn test_agg_func_as_sql() {
136 assert_eq!(AggFunc::Count.as_sql(), "COUNT");
137 assert_eq!(AggFunc::Sum.as_sql(), "SUM");
138 assert_eq!(AggFunc::Avg.as_sql(), "AVG");
139 assert_eq!(AggFunc::Max.as_sql(), "MAX");
140 assert_eq!(AggFunc::Min.as_sql(), "MIN");
141 }
142
143 #[test]
144 fn test_expr_count() {
145 let expr = Expr::count("id");
146 assert_eq!(expr.render(), "COUNT(id)");
147 assert_eq!(expr.render_as("total"), "COUNT(id) AS total");
148 }
149
150 #[test]
151 fn test_expr_sum() {
152 let expr = Expr::sum("amount");
153 assert_eq!(expr.render(), "SUM(amount)");
154 assert_eq!(
155 expr.render_as("total_amount"),
156 "SUM(amount) AS total_amount"
157 );
158 }
159
160 #[test]
161 fn test_expr_avg() {
162 let expr = Expr::avg("score");
163 assert_eq!(expr.render(), "AVG(score)");
164 }
165
166 #[test]
167 fn test_expr_max() {
168 let expr = Expr::max("price");
169 assert_eq!(expr.render(), "MAX(price)");
170 }
171
172 #[test]
173 fn test_expr_min() {
174 let expr = Expr::min("price");
175 assert_eq!(expr.render(), "MIN(price)");
176 }
177
178 #[test]
179 fn test_select_mode_default() {
180 assert_eq!(SelectMode::default(), SelectMode::All);
181 }
182}