Skip to content

Commit

Permalink
Add support for get_json_field and cast_json_field in postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Apr 7, 2023
1 parent ba5e52d commit a6db270
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/extension/postgres/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,56 @@ pub trait PgExpr: Expression {
{
self.like_like(PgBinOper::NotILike, like.into_like_expr())
}

/// Express a postgres retrieves JSON field as JSON value (`->`).
///
/// # Examples
///
/// ```
/// use sea_query::{extension::postgres::PgExpr, tests_cfg::*, *};
///
/// let query = Query::select()
/// .column(Font::Variant)
/// .from(Font::Table)
/// .and_where(Expr::col(Font::Variant).get_json_field("a"))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "variant" FROM "font" WHERE "variant" -> 'a'"#
/// );
/// ```
fn get_json_field<T>(self, right: T) -> SimpleExpr
where
T: Into<SimpleExpr>,
{
self.bin_op(PgBinOper::GetJsonField, right)
}

/// Express a postgres retrieves JSON field and casts it to an appropriate SQL type (`->>`).
///
/// # Examples
///
/// ```
/// use sea_query::{extension::postgres::PgExpr, tests_cfg::*, *};
///
/// let query = Query::select()
/// .column(Font::Variant)
/// .from(Font::Table)
/// .and_where(Expr::col(Font::Variant).cast_json_field("a"))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT "variant" FROM "font" WHERE "variant" ->> 'a'"#
/// );
/// ```
fn cast_json_field<T>(self, right: T) -> SimpleExpr
where
T: Into<SimpleExpr>,
{
self.bin_op(PgBinOper::CastJsonField, right)
}
}

impl PgExpr for Expr {}
Expand Down

0 comments on commit a6db270

Please sign in to comment.