diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 97e220f4ed3d..cc793ae3092b 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -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; +} diff --git a/diesel/src/pg/expression/helper_types.rs b/diesel/src/pg/expression/helper_types.rs index 23af7657900d..c5394a1ecd5d 100644 --- a/diesel/src/pg/expression/helper_types.rs +++ b/diesel/src/pg/expression/helper_types.rs @@ -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>; diff --git a/diesel_derives/tests/auto_type.rs b/diesel_derives/tests/auto_type.rs index 8e3e8363bd93..deaedaec3dc4 100644 --- a/diesel_derives/tests/auto_type.rs +++ b/diesel_derives/tests/auto_type.rs @@ -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), ) }