Skip to content

Commit

Permalink
feat: add json_agg function for postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnisDa committed Jun 19, 2024
1 parent ce4e2af commit f117ed3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/backend/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
25 changes: 25 additions & 0 deletions src/extension/postgres/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum PgFunction {
StartsWith,
GenRandomUUID,
JsonBuildObject,
JsonAgg,
#[cfg(feature = "postgres-array")]
Any,
#[cfg(feature = "postgres-array")]
Expand Down Expand Up @@ -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<T>(expr: T) -> FunctionCall
where
T: Into<SimpleExpr>,
{
FunctionCall::new(Function::PgFunction(PgFunction::JsonAgg)).arg(expr)
}
}

0 comments on commit f117ed3

Please sign in to comment.