quaint_forked/ast/function/
aggregate_to_string.rs

1use super::Function;
2use crate::ast::Expression;
3
4#[derive(Debug, Clone, PartialEq)]
5/// An aggregate function that concatenates strings from a group into a single
6/// string with various options.
7pub struct AggregateToString<'a> {
8    pub(crate) value: Box<Expression<'a>>,
9}
10
11/// Aggregates the given field into a string.
12///
13/// ```rust
14/// # use quaint::{ast::*, visitor::{Visitor, Sqlite}};
15/// # fn main() -> Result<(), quaint::error::Error> {
16/// let query = Select::from_table("users").value(aggregate_to_string(Column::new("firstName")))
17///     .group_by("firstName");
18///
19/// let (sql, _) = Sqlite::build(query)?;
20/// assert_eq!("SELECT GROUP_CONCAT(`firstName`) FROM `users` GROUP BY `firstName`", sql);
21/// # Ok(())
22/// # }
23/// ```
24pub fn aggregate_to_string<'a, T>(expr: T) -> Function<'a>
25where
26    T: Into<Expression<'a>>,
27{
28    let fun = AggregateToString {
29        value: Box::new(expr.into()),
30    };
31
32    fun.into()
33}