From fa12eebb62e98892d19c5d219d5f907ca3978dec Mon Sep 17 00:00:00 2001 From: accestfromim <3478936082@qq.com> Date: Sun, 24 Nov 2024 19:28:25 +0800 Subject: [PATCH 1/8] add jsonb_set support --- diesel/src/pg/expression/functions.rs | 150 +++++++++++++++++++++++ diesel/src/pg/expression/helper_types.rs | 11 ++ diesel_derives/tests/auto_type.rs | 8 ++ 3 files changed, 169 insertions(+) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index cd2dbeafb565..04e5fcd5ff29 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -2,6 +2,7 @@ use super::expression_methods::InetOrCidr; use crate::expression::functions::define_sql_function; +use crate::pg::expression::expression_methods; use crate::pg::expression::expression_methods::ArrayOrNullableArray; use crate::pg::expression::expression_methods::CombinedAllNullableValue; use crate::pg::expression::expression_methods::CombinedNullableValue; @@ -2398,3 +2399,152 @@ define_sql_function! { J: JsonbOrNullableJsonb + CombinedAllNullableValue >(base: B, from_json: J) -> J::Out; } + +#[cfg(feature = "postgres_backend")] +define_sql_function! { + /// Returns target with the item designated by path replaced by new_value, + /// or with new_value added if create_if_missing is true (which is the default) + /// and the item designated by path does not exist. (see jsonb_set_with_create_if_missing below) + /// + /// All earlier steps in the path must exist, or the target is returned unchanged. + /// As with the path oriented operators, negative integers that appear in the path count from the end of JSON arrays. + /// If the last path step is an array index that is out of range, + /// and create_if_missing is true, the new value is added at the beginning of the array if the index is negative, + /// or at the end of the array if it is positive. + /// + /// # 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_set; + /// # use diesel::sql_types::{Jsonb, Nullable}; + /// # use diesel::sql_types::{Array, Json, Nullable, Text}; + /// # use serde_json::{json,Value}; + /// # let connection = &mut establish_connection(); + /// + /// let result = diesel::select(jsonb_set::( + /// json!([{"f1":1,"f2":null},2,null,3]), + /// vec!["0","f1"], + /// json!([2,3,4]) + /// )).get_result::(connection)?; + /// let expected: Value = json!([{"f1": [2, 3, 4], "f2": null}, 2, null, 3]); + /// assert_eq!(result, expected); + /// + /// let result = diesel::select(jsonb_set::( + /// json!({"odd":[2,4,6,8]}), + /// vec!["odd"], + /// json!([1,3,5,7]) + /// )).get_result::(connection)?; + /// let expected: Value = json!({"odd":[1,3,5,7]}); + /// assert_eq!(result, expected); + /// + /// let result = diesel::select(jsonb_set::, _>( + /// None::, + /// vec![], + /// None:: + /// )).get_result::>(connection)?; + /// assert!(result.is_none()); + /// + /// let result = diesel::select(jsonb_set::( + /// json!(null), + /// vec![], + /// json!(null) + /// )).get_result::(connection)?; + /// let expected = json!(null); + /// assert_eq!(result, expected); + /// + /// + /// + /// # Ok(()) + /// # } + /// ``` + fn jsonb_set< + E: JsonbOrNullableJsonb + SingleValue, + Arr: TextArrayOrNullableTextArray + SingleValue + >(base: E, path: Arr, new_value: E) -> E; +} + +#[cfg(feature = "postgres_backend")] +define_sql_function! { + /// Returns target with the item designated by path replaced by new_value, + /// or with new_value added if create_if_missing is true (which is the default) + /// and the item designated by path does not exist. + /// + /// All earlier steps in the path must exist, or the target is returned unchanged. + /// As with the path oriented operators, negative integers that appear in the path count from the end of JSON arrays. + /// If the last path step is an array index that is out of range, + /// and create_if_missing is true, the new value is added at the beginning of the array if the index is negative, + /// or at the end of the array if it is positive. + /// + /// # 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_set; + /// # use diesel::sql_types::{Jsonb, Nullable}; + /// # use diesel::sql_types::{Array, Json, Nullable, Text}; + /// # use serde_json::{json,Value}; + /// # let connection = &mut establish_connection(); + /// + /// let result = diesel::select(jsonb_set_with_create_if_missing::( + /// json!([{"f2":null},2,null,3]), + /// vec!["0","f1"], + /// json!([2,3,4]), + /// true + /// )).get_result::(connection)?; + /// let expected: Value = json!([{"f1": [2, 3, 4], "f2": null}, 2, null, 3]); + /// assert_eq!(result, expected); + /// + /// let result = diesel::select(jsonb_set_with_create_if_missing::( + /// json!({"odd":[2,4,6,8]}), + /// vec!["odd"], + /// json!([1,3,5,7]) + /// true + /// )).get_result::(connection)?; + /// let expected: Value = json!({"odd":[1,3,5,7]}); + /// assert_eq!(result, expected); + /// + /// let result = diesel::select(jsonb_set_with_create_if_missing::, _>( + /// None::, + /// vec![], + /// None:: + /// )).get_result::>(connection)?; + /// assert!(result.is_none()); + /// + /// let result = diesel::select(jsonb_set_with_create_if_missing::( + /// json!(null), + /// vec![], + /// json!({"hello":"world"}), + /// false + /// )).get_result::(connection)?; + /// let expected = json!(null); + /// assert_eq!(result, expected); + /// + /// + /// + /// # Ok(()) + /// # } + /// ``` + + #[sql_name = "jsonb_set"] + fn jsonb_set_with_create_if_missing< + E: JsonbOrNullableJsonb + SingleValue, + Arr: TextArrayOrNullableTextArray + SingleValue + >(base: E, path: Arr, new_value: E, create_if_missing: bool) -> E; +} diff --git a/diesel/src/pg/expression/helper_types.rs b/diesel/src/pg/expression/helper_types.rs index 42b51f785b2b..e26729489fdc 100644 --- a/diesel/src/pg/expression/helper_types.rs +++ b/diesel/src/pg/expression/helper_types.rs @@ -592,3 +592,14 @@ pub type json_populate_record = #[cfg(feature = "postgres_backend")] pub type jsonb_populate_record = super::functions::jsonb_populate_record, SqlTypeOf, B, J>; + +/// Return type of [`jsonb_set(base, path, new_value)`](super::functions::jsonb_set()) +#[allow(non_camel_case_types)] +#[cfg(feature = "postgres_backend")] +pub type jsonb_set = super::functions::jsonb_set, SqlTypeOf, B, J>; + +/// Return type of [`jsonb_set_with_create_if_missing(base, path, new_value, create_if_missing)`](super::functions::jsonb_set_with_create_if_missing()) +#[allow(non_camel_case_types)] +#[cfg(feature = "postgres_backend")] +pub type jsonb_set_with_create_if_missing = + super::functions::jsonb_set_with_create_if_missing, SqlTypeOf, B, J>; diff --git a/diesel_derives/tests/auto_type.rs b/diesel_derives/tests/auto_type.rs index d96a4da591e5..6f815999b739 100644 --- a/diesel_derives/tests/auto_type.rs +++ b/diesel_derives/tests/auto_type.rs @@ -54,6 +54,7 @@ table! { name -> Text, text_array -> Array, record -> Record<(Integer, Text, Date)>, + bool -> Bool, } } @@ -461,6 +462,13 @@ fn postgres_functions() -> _ { row_to_json(pg_extras::record), json_populate_record(pg_extras::record, pg_extras::json), jsonb_populate_record(pg_extras::record, pg_extras::jsonb), + jsonb_set(pg_extras::jsonb, pg_extras::text_array, pg_extras::jsonb), + jsonb_set_with_create_if_missing( + pg_extras::jsonb, + pg_extras::text_array, + pg_extras::jsonb, + pg_extras::bool, + ), ) } From 489e77bb17c20dac25df2b3ecbb5952c3fdb46c1 Mon Sep 17 00:00:00 2001 From: accestfromim <3478936082@qq.com> Date: Sun, 24 Nov 2024 19:31:12 +0800 Subject: [PATCH 2/8] add jsonb_set support --- diesel/src/pg/expression/functions.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 04e5fcd5ff29..220a8269ec21 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -2,7 +2,6 @@ use super::expression_methods::InetOrCidr; use crate::expression::functions::define_sql_function; -use crate::pg::expression::expression_methods; use crate::pg::expression::expression_methods::ArrayOrNullableArray; use crate::pg::expression::expression_methods::CombinedAllNullableValue; use crate::pg::expression::expression_methods::CombinedNullableValue; From 3db1df641752ee1acc69c25aefd42535f9bd0be4 Mon Sep 17 00:00:00 2001 From: accestfromim <3478936082@qq.com> Date: Sun, 24 Nov 2024 19:43:45 +0800 Subject: [PATCH 3/8] give up create_if_missing option --- diesel/src/pg/expression/functions.rs | 77 ------------------------ diesel/src/pg/expression/helper_types.rs | 8 +-- diesel_derives/tests/auto_type.rs | 7 --- 3 files changed, 1 insertion(+), 91 deletions(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 220a8269ec21..13fbb85e7302 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -2470,80 +2470,3 @@ define_sql_function! { Arr: TextArrayOrNullableTextArray + SingleValue >(base: E, path: Arr, new_value: E) -> E; } - -#[cfg(feature = "postgres_backend")] -define_sql_function! { - /// Returns target with the item designated by path replaced by new_value, - /// or with new_value added if create_if_missing is true (which is the default) - /// and the item designated by path does not exist. - /// - /// All earlier steps in the path must exist, or the target is returned unchanged. - /// As with the path oriented operators, negative integers that appear in the path count from the end of JSON arrays. - /// If the last path step is an array index that is out of range, - /// and create_if_missing is true, the new value is added at the beginning of the array if the index is negative, - /// or at the end of the array if it is positive. - /// - /// # 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_set; - /// # use diesel::sql_types::{Jsonb, Nullable}; - /// # use diesel::sql_types::{Array, Json, Nullable, Text}; - /// # use serde_json::{json,Value}; - /// # let connection = &mut establish_connection(); - /// - /// let result = diesel::select(jsonb_set_with_create_if_missing::( - /// json!([{"f2":null},2,null,3]), - /// vec!["0","f1"], - /// json!([2,3,4]), - /// true - /// )).get_result::(connection)?; - /// let expected: Value = json!([{"f1": [2, 3, 4], "f2": null}, 2, null, 3]); - /// assert_eq!(result, expected); - /// - /// let result = diesel::select(jsonb_set_with_create_if_missing::( - /// json!({"odd":[2,4,6,8]}), - /// vec!["odd"], - /// json!([1,3,5,7]) - /// true - /// )).get_result::(connection)?; - /// let expected: Value = json!({"odd":[1,3,5,7]}); - /// assert_eq!(result, expected); - /// - /// let result = diesel::select(jsonb_set_with_create_if_missing::, _>( - /// None::, - /// vec![], - /// None:: - /// )).get_result::>(connection)?; - /// assert!(result.is_none()); - /// - /// let result = diesel::select(jsonb_set_with_create_if_missing::( - /// json!(null), - /// vec![], - /// json!({"hello":"world"}), - /// false - /// )).get_result::(connection)?; - /// let expected = json!(null); - /// assert_eq!(result, expected); - /// - /// - /// - /// # Ok(()) - /// # } - /// ``` - - #[sql_name = "jsonb_set"] - fn jsonb_set_with_create_if_missing< - E: JsonbOrNullableJsonb + SingleValue, - Arr: TextArrayOrNullableTextArray + SingleValue - >(base: E, path: Arr, new_value: E, create_if_missing: bool) -> E; -} diff --git a/diesel/src/pg/expression/helper_types.rs b/diesel/src/pg/expression/helper_types.rs index e26729489fdc..b76b4f3ada50 100644 --- a/diesel/src/pg/expression/helper_types.rs +++ b/diesel/src/pg/expression/helper_types.rs @@ -596,10 +596,4 @@ pub type jsonb_populate_record = /// Return type of [`jsonb_set(base, path, new_value)`](super::functions::jsonb_set()) #[allow(non_camel_case_types)] #[cfg(feature = "postgres_backend")] -pub type jsonb_set = super::functions::jsonb_set, SqlTypeOf, B, J>; - -/// Return type of [`jsonb_set_with_create_if_missing(base, path, new_value, create_if_missing)`](super::functions::jsonb_set_with_create_if_missing()) -#[allow(non_camel_case_types)] -#[cfg(feature = "postgres_backend")] -pub type jsonb_set_with_create_if_missing = - super::functions::jsonb_set_with_create_if_missing, SqlTypeOf, B, J>; +pub type jsonb_set = super::functions::jsonb_set, SqlTypeOf, B, J, B>; diff --git a/diesel_derives/tests/auto_type.rs b/diesel_derives/tests/auto_type.rs index 6f815999b739..2cc13b35c277 100644 --- a/diesel_derives/tests/auto_type.rs +++ b/diesel_derives/tests/auto_type.rs @@ -54,7 +54,6 @@ table! { name -> Text, text_array -> Array, record -> Record<(Integer, Text, Date)>, - bool -> Bool, } } @@ -463,12 +462,6 @@ fn postgres_functions() -> _ { json_populate_record(pg_extras::record, pg_extras::json), jsonb_populate_record(pg_extras::record, pg_extras::jsonb), jsonb_set(pg_extras::jsonb, pg_extras::text_array, pg_extras::jsonb), - jsonb_set_with_create_if_missing( - pg_extras::jsonb, - pg_extras::text_array, - pg_extras::jsonb, - pg_extras::bool, - ), ) } From cc945f9ae14e4fd8610cd7d88d6060697fcc3f74 Mon Sep 17 00:00:00 2001 From: accestfromim <3478936082@qq.com> Date: Sun, 24 Nov 2024 20:10:53 +0800 Subject: [PATCH 4/8] fix bugs in test doc --- diesel/src/pg/expression/functions.rs | 11 +++++------ diesel_derives/tests/auto_type.rs | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 13fbb85e7302..7a4cf650f075 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -2424,12 +2424,11 @@ define_sql_function! { /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use diesel::dsl::jsonb_set; - /// # use diesel::sql_types::{Jsonb, Nullable}; - /// # use diesel::sql_types::{Array, Json, Nullable, Text}; + /// # use diesel::sql_types::{Jsonb,Array, Json, Nullable, Text}; /// # use serde_json::{json,Value}; /// # let connection = &mut establish_connection(); /// - /// let result = diesel::select(jsonb_set::( + /// let result = diesel::select(jsonb_set::( /// json!([{"f1":1,"f2":null},2,null,3]), /// vec!["0","f1"], /// json!([2,3,4]) @@ -2437,7 +2436,7 @@ define_sql_function! { /// let expected: Value = json!([{"f1": [2, 3, 4], "f2": null}, 2, null, 3]); /// assert_eq!(result, expected); /// - /// let result = diesel::select(jsonb_set::( + /// let result = diesel::select(jsonb_set::( /// json!({"odd":[2,4,6,8]}), /// vec!["odd"], /// json!([1,3,5,7]) @@ -2445,14 +2444,14 @@ define_sql_function! { /// let expected: Value = json!({"odd":[1,3,5,7]}); /// assert_eq!(result, expected); /// - /// let result = diesel::select(jsonb_set::, _>( + /// let result = diesel::select(jsonb_set::, _, _, _, _>( /// None::, /// vec![], /// None:: /// )).get_result::>(connection)?; /// assert!(result.is_none()); /// - /// let result = diesel::select(jsonb_set::( + /// let result = diesel::select(jsonb_set::( /// json!(null), /// vec![], /// json!(null) diff --git a/diesel_derives/tests/auto_type.rs b/diesel_derives/tests/auto_type.rs index 2cc13b35c277..893e191938ca 100644 --- a/diesel_derives/tests/auto_type.rs +++ b/diesel_derives/tests/auto_type.rs @@ -461,7 +461,7 @@ fn postgres_functions() -> _ { row_to_json(pg_extras::record), json_populate_record(pg_extras::record, pg_extras::json), jsonb_populate_record(pg_extras::record, pg_extras::jsonb), - jsonb_set(pg_extras::jsonb, pg_extras::text_array, pg_extras::jsonb), + jsonb_set(pg_extras::jsonb, pg_extras::text_array), ) } From 86a39f1cd2d9105a5ab19144a155a857e395f74f Mon Sep 17 00:00:00 2001 From: accestfromim <3478936082@qq.com> Date: Sun, 24 Nov 2024 20:37:10 +0800 Subject: [PATCH 5/8] fix type anotation bug in test doc --- diesel/src/pg/expression/functions.rs | 8 ++++---- diesel_derives/tests/auto_type.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 7a4cf650f075..470ddf5f49bb 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -2428,7 +2428,7 @@ define_sql_function! { /// # use serde_json::{json,Value}; /// # let connection = &mut establish_connection(); /// - /// let result = diesel::select(jsonb_set::( + /// let result = diesel::select(jsonb_set::, _, _, _>( /// json!([{"f1":1,"f2":null},2,null,3]), /// vec!["0","f1"], /// json!([2,3,4]) @@ -2436,7 +2436,7 @@ define_sql_function! { /// let expected: Value = json!([{"f1": [2, 3, 4], "f2": null}, 2, null, 3]); /// assert_eq!(result, expected); /// - /// let result = diesel::select(jsonb_set::( + /// let result = diesel::select(jsonb_set::, _, _, _>( /// json!({"odd":[2,4,6,8]}), /// vec!["odd"], /// json!([1,3,5,7]) @@ -2444,14 +2444,14 @@ define_sql_function! { /// let expected: Value = json!({"odd":[1,3,5,7]}); /// assert_eq!(result, expected); /// - /// let result = diesel::select(jsonb_set::, _, _, _, _>( + /// let result = diesel::select(jsonb_set::, Array>, _, _, _>( /// None::, /// vec![], /// None:: /// )).get_result::>(connection)?; /// assert!(result.is_none()); /// - /// let result = diesel::select(jsonb_set::( + /// let result = diesel::select(jsonb_set::>, _, _, _>( /// json!(null), /// vec![], /// json!(null) diff --git a/diesel_derives/tests/auto_type.rs b/diesel_derives/tests/auto_type.rs index 893e191938ca..2cc13b35c277 100644 --- a/diesel_derives/tests/auto_type.rs +++ b/diesel_derives/tests/auto_type.rs @@ -461,7 +461,7 @@ fn postgres_functions() -> _ { row_to_json(pg_extras::record), json_populate_record(pg_extras::record, pg_extras::json), jsonb_populate_record(pg_extras::record, pg_extras::jsonb), - jsonb_set(pg_extras::jsonb, pg_extras::text_array), + jsonb_set(pg_extras::jsonb, pg_extras::text_array, pg_extras::jsonb), ) } From b79bea5c258a00afbfeed4b47522e16621965f59 Mon Sep 17 00:00:00 2001 From: accestfromim <3478936082@qq.com> Date: Sun, 24 Nov 2024 21:17:17 +0800 Subject: [PATCH 6/8] fix bug in helper_types --- diesel/src/pg/expression/functions.rs | 6 ++++-- diesel/src/pg/expression/helper_types.rs | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 470ddf5f49bb..11de01a6fcba 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -2444,16 +2444,18 @@ define_sql_function! { /// let expected: Value = json!({"odd":[1,3,5,7]}); /// assert_eq!(result, expected); /// + /// let empty:Vec = Vec::new(); /// let result = diesel::select(jsonb_set::, Array>, _, _, _>( /// None::, - /// vec![], + /// empty, /// None:: /// )).get_result::>(connection)?; /// assert!(result.is_none()); /// + /// let empty:Vec = Vec::new(); /// let result = diesel::select(jsonb_set::>, _, _, _>( /// json!(null), - /// vec![], + /// empty, /// json!(null) /// )).get_result::(connection)?; /// let expected = json!(null); diff --git a/diesel/src/pg/expression/helper_types.rs b/diesel/src/pg/expression/helper_types.rs index b76b4f3ada50..66f30e7af3bf 100644 --- a/diesel/src/pg/expression/helper_types.rs +++ b/diesel/src/pg/expression/helper_types.rs @@ -596,4 +596,4 @@ pub type jsonb_populate_record = /// Return type of [`jsonb_set(base, path, new_value)`](super::functions::jsonb_set()) #[allow(non_camel_case_types)] #[cfg(feature = "postgres_backend")] -pub type jsonb_set = super::functions::jsonb_set, SqlTypeOf, B, J, B>; +pub type jsonb_set = super::functions::jsonb_set, SqlTypeOf, B, J, R>; From c951f3d397333a08462a1a38325ef49f0b842c9f Mon Sep 17 00:00:00 2001 From: accestfromim <3478936082@qq.com> Date: Sun, 24 Nov 2024 22:35:39 +0800 Subject: [PATCH 7/8] test in doc passed --- diesel/src/pg/expression/functions.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 11de01a6fcba..2220a588b12e 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -2403,7 +2403,9 @@ define_sql_function! { define_sql_function! { /// Returns target with the item designated by path replaced by new_value, /// or with new_value added if create_if_missing is true (which is the default) - /// and the item designated by path does not exist. (see jsonb_set_with_create_if_missing below) + /// and the item designated by path does not exist. + /// + /// It can't set path in scalar /// /// All earlier steps in the path must exist, or the target is returned unchanged. /// As with the path oriented operators, negative integers that appear in the path count from the end of JSON arrays. @@ -2437,11 +2439,12 @@ define_sql_function! { /// assert_eq!(result, expected); /// /// let result = diesel::select(jsonb_set::, _, _, _>( - /// json!({"odd":[2,4,6,8]}), - /// vec!["odd"], + /// json!([{"odd":[2,4,6,8]}]), + /// // not vec!["odd"], cannot set path in scalar + /// vec!["0","odd"], /// json!([1,3,5,7]) /// )).get_result::(connection)?; - /// let expected: Value = json!({"odd":[1,3,5,7]}); + /// let expected: Value = json!([{"odd":[1,3,5,7]}]); /// assert_eq!(result, expected); /// /// let empty:Vec = Vec::new(); @@ -2454,11 +2457,12 @@ define_sql_function! { /// /// let empty:Vec = Vec::new(); /// let result = diesel::select(jsonb_set::>, _, _, _>( - /// json!(null), + /// // cannot be json!(null) + /// json!([]), /// empty, /// json!(null) /// )).get_result::(connection)?; - /// let expected = json!(null); + /// let expected = json!([]); /// assert_eq!(result, expected); /// /// From 18be33fc1a9fc5e68bad58e8de6b493de1f37065 Mon Sep 17 00:00:00 2001 From: accestfromim <3478936082@qq.com> Date: Wed, 27 Nov 2024 19:15:50 +0800 Subject: [PATCH 8/8] fix: can't infer Null when second param is null --- diesel/src/pg/expression/functions.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 2220a588b12e..c8a22d2c15e1 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -2465,6 +2465,12 @@ define_sql_function! { /// let expected = json!([]); /// assert_eq!(result, expected); /// + /// let result = diesel::select(jsonb_set::>>, _, _, _,>( + /// json!(null), + /// None::>, + /// json!({"foo": 42}) + /// )).get_result::>(connection)?; + /// assert!(result.is_none()); /// /// /// # Ok(()) @@ -2472,6 +2478,6 @@ define_sql_function! { /// ``` fn jsonb_set< E: JsonbOrNullableJsonb + SingleValue, - Arr: TextArrayOrNullableTextArray + SingleValue - >(base: E, path: Arr, new_value: E) -> E; + Arr: TextArrayOrNullableTextArray + CombinedNullableValue + >(base: E, path: Arr, new_value: E) -> Arr::Out; }