From f117ed317cdda81d629c4a0df676f4430a29865a Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Wed, 19 Jun 2024 06:42:19 +0530 Subject: [PATCH] feat: add `json_agg` function for postgres --- src/backend/postgres/query.rs | 1 + src/extension/postgres/func.rs | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/backend/postgres/query.rs b/src/backend/postgres/query.rs index b843dc58..6fa5f263 100644 --- a/src/backend/postgres/query.rs +++ b/src/backend/postgres/query.rs @@ -117,6 +117,7 @@ impl QueryBuilder for PostgresQueryBuilder { PgFunction::StartsWith => "STARTS_WITH", PgFunction::GenRandomUUID => "GEN_RANDOM_UUID", PgFunction::JsonBuildObject => "JSON_BUILD_OBJECT", + PgFunction::JsonAgg => "JSON_AGG", #[cfg(feature = "postgres-array")] PgFunction::Any => "ANY", #[cfg(feature = "postgres-array")] diff --git a/src/extension/postgres/func.rs b/src/extension/postgres/func.rs index defc241e..062d2828 100644 --- a/src/extension/postgres/func.rs +++ b/src/extension/postgres/func.rs @@ -15,6 +15,7 @@ pub enum PgFunction { StartsWith, GenRandomUUID, JsonBuildObject, + JsonAgg, #[cfg(feature = "postgres-array")] Any, #[cfg(feature = "postgres-array")] @@ -382,4 +383,28 @@ impl PgFunc { } FunctionCall::new(Function::PgFunction(PgFunction::JsonBuildObject)).args(args) } + + /// Call the `JSON_AGG` function. Postgres only. + /// + /// # Examples + /// + /// ``` + /// use sea_query::{tests_cfg::*, *}; + /// + /// let query = Query::select() + /// .from(Char::Table) + /// .expr(PgFunc::json_agg(Expr::col(Char::SizeW))) + /// .to_owned(); + /// + /// assert_eq!( + /// query.to_string(PostgresQueryBuilder), + /// r#"SELECT JSON_AGG("size_w") FROM "character""# + /// ); + /// ``` + pub fn json_agg(expr: T) -> FunctionCall + where + T: Into, + { + FunctionCall::new(Function::PgFunction(PgFunction::JsonAgg)).arg(expr) + } }