grafbase_sql_ast/ast/function/
json_agg.rs

1use super::Function;
2use crate::ast::{Expression, Ordering};
3
4#[derive(Debug, Clone, PartialEq)]
5/// A representation of the `json_agg` function in PostgreSQL.
6pub struct JsonAgg<'a> {
7    pub(crate) expression: Expression<'a>,
8    pub(crate) distinct: bool,
9    pub(crate) order_by: Option<Ordering<'a>>,
10}
11
12/// Return the given table as JSONB collection.
13pub fn json_agg<'a>(
14    expression: impl Into<Expression<'a>>,
15    order_by: Option<Ordering<'a>>,
16    distinct: bool,
17) -> Function<'a> {
18    let fun = JsonAgg {
19        expression: expression.into(),
20        distinct,
21        order_by,
22    };
23
24    fun.into()
25}
26
27impl<'a> From<JsonAgg<'a>> for Function<'a> {
28    fn from(value: JsonAgg<'a>) -> Self {
29        Self {
30            typ_: super::FunctionType::JsonAgg(value),
31            alias: None,
32        }
33    }
34}