Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Postgres Json functions: JSON_BUILD_OBJECT and JSON_AGG #787

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/backend/mysql/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl TableBuilder for MysqlQueryBuilder {
}
TableAlterOption::DropForeignKey(name) => {
let mut foreign_key = TableForeignKey::new();
foreign_key.name(&name.to_string());
foreign_key.name(name.to_string());
let drop = ForeignKeyDropStatement {
foreign_key,
table: None,
Expand Down
2 changes: 2 additions & 0 deletions src/backend/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ impl QueryBuilder for PostgresQueryBuilder {
PgFunction::TsRankCd => "TS_RANK_CD",
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
2 changes: 1 addition & 1 deletion src/backend/postgres/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl TableBuilder for PostgresQueryBuilder {
}
TableAlterOption::DropForeignKey(name) => {
let mut foreign_key = TableForeignKey::new();
foreign_key.name(&name.to_string());
foreign_key.name(name.to_string());
let drop = ForeignKeyDropStatement {
foreign_key,
table: None,
Expand Down
8 changes: 4 additions & 4 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,20 +614,20 @@ impl Expr {
/// let query = Query::select()
/// .columns([Char::Character, Char::SizeW, Char::SizeH])
/// .from(Char::Table)
/// .and_where(Expr::col((Char::Table, Char::FontId)).equals((Font::Table, Font::Id)))
/// .and_where(Expr::col((Char::Table, Char::FontId)).not_equals((Font::Table, Font::Id)))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`font_id` = `font`.`id`"#
/// r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`font_id` <> `font`.`id`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" = "font"."id""#
/// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" <> "font"."id""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" = "font"."id""#
/// r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" <> "font"."id""#
/// );
/// ```
pub fn not_equals<C>(self, col: C) -> SimpleExpr
Expand Down
57 changes: 57 additions & 0 deletions src/extension/postgres/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub enum PgFunction {
TsRankCd,
StartsWith,
GenRandomUUID,
JsonBuildObject,
JsonAgg,
#[cfg(feature = "postgres-array")]
Any,
#[cfg(feature = "postgres-array")]
Expand Down Expand Up @@ -350,4 +352,59 @@ impl PgFunc {
pub fn gen_random_uuid() -> FunctionCall {
FunctionCall::new(Function::PgFunction(PgFunction::GenRandomUUID))
}

/// Call the `JSON_BUILD_OBJECT` function. Postgres only.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(PgFunc::json_build_object(vec![
/// (Expr::val("a"), Expr::val(1)),
/// (Expr::val("b"), Expr::val("2")),
/// ]))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT JSON_BUILD_OBJECT('a', 1, 'b', '2')"#
/// );
/// ```
pub fn json_build_object<T>(pairs: Vec<(T, T)>) -> FunctionCall
where
T: Into<SimpleExpr>,
{
let mut args = vec![];
for (key, value) in pairs {
args.push(key.into());
args.push(value.into());
}
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)
}
}
Loading