Skip to content

Commit

Permalink
Merge pull request #4252 from zaira-bibi/implement-json-array-length-…
Browse files Browse the repository at this point in the history
…function

added support for `json_array_length` and `jsonb_array_length` functions
  • Loading branch information
weiznich authored Sep 16, 2024
2 parents ecf6e92 + e9348ee commit 810c3e3
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
86 changes: 86 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1992,3 +1992,89 @@ define_sql_function! {
/// ```
fn jsonb_strip_nulls<E: JsonbOrNullableJsonb + SingleValue>(jsonb: E) -> E;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Returns the number of elements in the top-level JSON array
///
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # #[cfg(feature = "serde_json")]
/// # run_test().unwrap();
/// # }
/// #
/// # #[cfg(feature = "serde_json")]
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::json_array_length;
/// # use serde_json::{json, Value};
/// # use diesel::sql_types::{Integer, Json, Nullable};
/// # let connection = &mut establish_connection();
///
/// let result = diesel::select(json_array_length::<Json, _>(json!([1, 2, 3])))
/// .get_result::<i32>(connection)?;
/// assert_eq!(result, 3);
///
/// let result = diesel::select(json_array_length::<Json, _>(json!([])))
/// .get_result::<i32>(connection)?;
/// assert_eq!(result, 0);
///
/// let result = diesel::select(json_array_length::<Nullable<Json>, _>(None::<Value>))
/// .get_result::<Option<i32>>(connection)?;
/// assert!(result.is_none());
///
///
///
/// # Ok(())
/// # }
/// ```
fn json_array_length<E: JsonOrNullableJson + MaybeNullableValue<Integer>>(json: E) -> E::Out;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Returns the number of elements in the top-level JSON array
///
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # #[cfg(feature = "serde_json")]
/// # run_test().unwrap();
/// # }
/// #
/// # #[cfg(feature = "serde_json")]
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::jsonb_array_length;
/// # use serde_json::{json, Value};
/// # use diesel::sql_types::{Integer, Jsonb, Nullable};
/// # let connection = &mut establish_connection();
///
/// let result = diesel::select(jsonb_array_length::<Jsonb, _>(json!([1, 2, 3])))
/// .get_result::<i32>(connection)?;
/// assert_eq!(result, 3);
///
/// let result = diesel::select(jsonb_array_length::<Jsonb, _>(json!([])))
/// .get_result::<i32>(connection)?;
/// assert_eq!(result, 0);
///
/// let result = diesel::select(jsonb_array_length::<Nullable<Jsonb>, _>(None::<Value>))
/// .get_result::<Option<i32>>(connection)?;
/// assert!(result.is_none());
///
///
///
/// # Ok(())
/// # }
/// ```
fn jsonb_array_length<E: JsonbOrNullableJsonb + MaybeNullableValue<Integer>>(jsonb: E) -> E::Out;
}
10 changes: 10 additions & 0 deletions diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,13 @@ pub type json_strip_nulls<E> = super::functions::json_strip_nulls<SqlTypeOf<E>,
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type jsonb_strip_nulls<E> = super::functions::jsonb_strip_nulls<SqlTypeOf<E>, E>;

/// Return type of [`json_array_length(json)`](super::functions::json_array_length())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type json_array_length<E> = super::functions::json_array_length<SqlTypeOf<E>, E>;

/// Return type of [`jsonb_array_length(jsonb)`](super::functions::jsonb_array_length())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type jsonb_array_length<E> = super::functions::jsonb_array_length<SqlTypeOf<E>, E>;
2 changes: 2 additions & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ fn postgres_functions() -> _ {
jsonb_pretty(pg_extras::jsonb),
json_strip_nulls(pg_extras::json),
jsonb_strip_nulls(pg_extras::jsonb),
json_array_length(pg_extras::json),
jsonb_array_length(pg_extras::jsonb),
)
}

Expand Down

0 comments on commit 810c3e3

Please sign in to comment.