From 07f37bceaa2d851a93874c0798f782d5de4b18a4 Mon Sep 17 00:00:00 2001 From: Azan Ali Date: Thu, 12 Sep 2024 15:34:07 +0500 Subject: [PATCH] added jsonb_insert --- diesel/src/pg/expression/functions.rs | 46 ++++++++++++++++++++++++ diesel/src/pg/expression/helper_types.rs | 6 ++++ diesel_derives/tests/auto_type.rs | 1 + 3 files changed, 53 insertions(+) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index bc851a5258e5..6a269d1d163e 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -1,6 +1,7 @@ //! PostgreSQL specific functions use super::expression_methods::InetOrCidr; +use super::expression_methods::JsonbOrNullableJsonb; use crate::expression::functions::define_sql_function; use crate::pg::expression::expression_methods::ArrayOrNullableArray; use crate::pg::expression::expression_methods::MaybeNullableValue; @@ -1634,3 +1635,48 @@ define_sql_function! { text_array: Arr, ) -> Arr::Out; } + +#[cfg(feature = "postgres_backend")] +define_sql_function! { + /// Returns target with new_value inserted. If the item designated by the path is an array element + /// + /// + /// # Example + /// + /// ```rust + /// # include!("../../doctest_setup.rs"); + /// # + /// # fn main() { + /// # run_test().unwrap(); + /// # } + /// # + /// # fn run_test() -> QueryResult<()> { + /// # use diesel::dsl::jsonb_insert; + /// # use diesel::sql_types::{Array, Jsonb, Nullable, Text}; + /// # use serde_json::{Value, json}; + /// # let connection = &mut establish_connection(); + /// let result = diesel::select(jsonb_insert::, _, _, _>(json!([1, 2, 3]), vec!["1"], json!(1))) + /// .get_result::(connection)?; + /// assert_eq!(json!([1, 1, 2, 3]), result); + /// + /// let result = diesel::select(jsonb_insert::, _, _, _>(json!([1, 2, 3]), Vec::::new(), json!(1))) + /// .get_result::(connection)?; + /// assert_eq!(json!([1, 2, 3]), result); + /// + /// let result = diesel::select(jsonb_insert::, Array, _, _, _>(Option::::None, vec!["1"], json!(1))) + /// .get_result::>(connection)?; + /// assert_eq!(None, result); + /// + /// let result = diesel::select(jsonb_insert::, Array, _, _, _>(json!([1, 2, 3]), vec!["1"], Option::::None)) + /// .get_result::>(connection)?; + /// assert_eq!(None, result); + /// + /// let result = diesel::select(jsonb_insert::>, _, _, _>(json!([1, 2, 3]), None::>, json!(1))) + /// .get_result::(connection)?; + /// assert!(result.is_err()); + /// + /// # Ok(()) + /// # } + /// ``` + fn jsonb_insert, P: TextArrayOrNullableTextArray + MaybeNullableValue>(target: J, path: P, value: J) -> J::Out; +} diff --git a/diesel/src/pg/expression/helper_types.rs b/diesel/src/pg/expression/helper_types.rs index 81cd81bd10a1..ba4dae31abf7 100644 --- a/diesel/src/pg/expression/helper_types.rs +++ b/diesel/src/pg/expression/helper_types.rs @@ -486,3 +486,9 @@ pub type to_jsonb = super::functions::to_jsonb, E>; #[allow(non_camel_case_types)] #[cfg(feature = "postgres_backend")] pub type json_object = super::functions::json_object, A>; + +/// Return type of [`jsonb_insert(target, path, value)`](super::functions::json_object()) +#[allow(non_camel_case_types)] +#[cfg(feature = "postgres_backend")] +pub type jsonb_insert = + super::functions::jsonb_insert, SqlTypeOf

, T, P, V>; diff --git a/diesel_derives/tests/auto_type.rs b/diesel_derives/tests/auto_type.rs index 6c96ad2b20b4..0a474bd053f6 100644 --- a/diesel_derives/tests/auto_type.rs +++ b/diesel_derives/tests/auto_type.rs @@ -438,6 +438,7 @@ fn postgres_functions() -> _ { to_json(pg_extras::id), to_jsonb(pg_extras::id), json_object(pg_extras::text_array), + jsonb_insert(pg_extras::jsonb, pg_extras::text_array, pg_extras::jsonb), ) }