1use crate::{column::ColumnName, driver::PushPrql, sort::Sorted};
2
3pub struct Aggregate<Query, Expr> {
4 pub query: Query,
5 pub aggregations: Vec<(ColumnName, Expr)>,
6}
7
8impl<Query, Expr> Aggregate<Query, Expr> {
9 pub fn aggregate(mut self, name: &'static str, expr: Expr) -> Self {
10 self.aggregations.push((ColumnName { name }, expr));
11 self
12 }
13
14 pub fn sort<Sort>(self, sort: Sort) -> Sorted<Self, Sort> {
15 Sorted { query: self, sort }
16 }
17}
18
19impl<Query, Expr> PushPrql for Aggregate<Query, Expr>
20where
21 Query: PushPrql,
22 Expr: PushPrql,
23{
24 fn push_to_driver(&self, driver: &mut crate::driver::Driver) {
25 self.query.push_to_driver(driver);
26 driver.push("\naggregate {");
27 for (i, (col, expr)) in self.aggregations.iter().enumerate() {
28 if i > 0 {
29 driver.push(',');
30 }
31 driver.push(' ');
32 col.push_to_driver(driver);
33 driver.push(" = ");
34 expr.push_to_driver(driver);
35 }
36 driver.push(" }");
37 }
38}