From da5486b04c2fb86f8560be73fd7dc5d6944a26b3 Mon Sep 17 00:00:00 2001 From: meeshal Date: Thu, 10 Oct 2024 16:50:17 +0200 Subject: [PATCH] add pg array_to_json support --- diesel/src/pg/expression/functions.rs | 43 ++++++++++++++++++++++++ diesel/src/pg/expression/helper_types.rs | 5 +++ diesel_derives/tests/auto_type.rs | 1 + 3 files changed, 49 insertions(+) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 2feb1ce4d0f0..773a0d16bf0f 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -1518,6 +1518,49 @@ define_sql_function! { fn array_sample(array: Arr, n: Integer) -> Arr; } +#[cfg(feature = "postgres_backend")] +define_sql_function! { + /// Converts any Array to json. + /// + /// # 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::array_to_json; + /// # use diesel::sql_types::{Array, Integer, Text, Nullable}; + /// # use serde_json::Value; + /// # let connection = &mut establish_connection(); + /// let json = diesel::select(array_to_json::, _>(vec![1, 2, 3, 4, 5])) + /// .get_result::(connection)?; + /// let expected:Value = serde_json::json!([1, 2, 3, 4, 5]); + /// assert_eq!(expected,json); + /// let json = diesel::select(array_to_json::,_>(vec!["hello","world","John","Doe"])) + /// .get_result::(connection)?; + /// let expected:Value = serde_json::json!(["hello","world","John","Doe"]); + /// assert_eq!(expected,json); + /// let empty:Vec = Vec::new(); + /// let json = diesel::select(array_to_json::>,_>(empty)) + /// .get_result::(connection)?; + /// assert_eq!(serde_json::json!([]),json); + /// let json = diesel::select(array_to_json::>, _>(None::>)) + /// .get_result::>(connection)?; + /// assert_eq!(None, json); + /// # Ok(()) + /// # } + /// ``` + fn array_to_json>( + array: Arr, + ) -> Arr::Out; +} + #[cfg(feature = "postgres_backend")] define_sql_function! { /// Converts any SQL value to json diff --git a/diesel/src/pg/expression/helper_types.rs b/diesel/src/pg/expression/helper_types.rs index efc1d8e68975..8a052c1be9d7 100644 --- a/diesel/src/pg/expression/helper_types.rs +++ b/diesel/src/pg/expression/helper_types.rs @@ -472,6 +472,11 @@ pub type array_shuffle = super::functions::array_shuffle, A>; #[cfg(feature = "postgres_backend")] pub type array_sample = super::functions::array_sample, A, N>; +/// Return type of [`array_to_json(array)`](super::functions::array_to_json()) +#[allow(non_camel_case_types)] +#[cfg(feature = "postgres_backend")] +pub type array_to_json = super::functions::array_to_json, A>; + /// Return type of [`to_json(element)`](super::functions::to_json()) #[allow(non_camel_case_types)] #[cfg(feature = "postgres_backend")] diff --git a/diesel_derives/tests/auto_type.rs b/diesel_derives/tests/auto_type.rs index 69f202fa2c69..f9cefcf69570 100644 --- a/diesel_derives/tests/auto_type.rs +++ b/diesel_derives/tests/auto_type.rs @@ -435,6 +435,7 @@ fn postgres_functions() -> _ { array_ndims(pg_extras::array), array_shuffle(pg_extras::array), array_sample(pg_extras::array, pg_extras::id), + array_to_json(pg_extras::array), to_json(pg_extras::id), to_jsonb(pg_extras::id), json_object(pg_extras::text_array),