From cbf1323538d7693d6779ca4dfc912d78fc8cfa91 Mon Sep 17 00:00:00 2001 From: Azan Ali Date: Thu, 22 Aug 2024 14:57:01 +0500 Subject: [PATCH] added array_replace pg array function --- diesel/src/pg/expression/functions.rs | 38 ++++++++++++++++++++++++ diesel/src/pg/expression/helper_types.rs | 5 ++++ diesel_derives/tests/auto_type.rs | 1 + 3 files changed, 44 insertions(+) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 0b7bfa9eb989..721e874cfda1 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -748,3 +748,41 @@ define_sql_function! { /// ``` fn array_append + SingleValue, T: SingleValue>(a: Arr, e: T) -> Array; } + +#[cfg(feature = "postgres_backend")] +define_sql_function! { + /// Append an element to the end of an array + /// + /// # Example + /// + /// ```rust + /// # include!("../../doctest_setup.rs"); + /// # + /// # fn main() { + /// # run_test().unwrap(); + /// # } + /// # + /// # fn run_test() -> QueryResult<()> { + /// # use diesel::dsl::array_replace; + /// # use diesel::sql_types::{Nullable, Integer, Array}; + /// # let connection = &mut establish_connection(); + /// let ints = diesel::select(array_replace::, Integer, _, _>(vec![1, 2, 5, 4], 5, 3)) + /// .get_result::>(connection)?; + /// assert_eq!(vec![1, 2, 3, 4], ints); + /// + /// let ints = diesel::select(array_replace::, Nullable, _, _>(vec![Some(1), Some(2), Some(3)], Some(3), None::)) + /// .get_result::>>(connection)?; + /// assert_eq!(vec![Some(1), Some(2), None], ints); + /// + /// let ints = diesel::select(array_append::>, Integer, _, _>(None::>, Some(1), Some(2))) + /// .get_result::>(connection)?; + /// assert_eq!(vec![None], ints); + /// + /// let ints = diesel::select(array_append::>, Nullable, _, _>(None::>, None::)) + /// .get_result::>>(connection)?; + /// assert_eq!(vec![None], ints); + /// # Ok(()) + /// # } + /// ``` + fn array_replace + SingleValue, T: SingleValue>(a: Arr, e: T, r: T) -> Array; +} diff --git a/diesel/src/pg/expression/helper_types.rs b/diesel/src/pg/expression/helper_types.rs index 5bcaadfc3e98..0a582ba1ebfc 100644 --- a/diesel/src/pg/expression/helper_types.rs +++ b/diesel/src/pg/expression/helper_types.rs @@ -352,3 +352,8 @@ pub type range_merge = super::functions::range_merge, SqlT #[allow(non_camel_case_types)] #[cfg(feature = "postgres_backend")] pub type array_append = super::functions::array_append, SqlTypeOf, A, E>; + +/// Return type of [`array_replace(array, element, replace_with)`](super::functions::array_replace()) +#[allow(non_camel_case_types)] +#[cfg(feature = "postgres_backend")] +pub type array_replace = super::functions::array_replace, SqlTypeOf, A, E, R>; diff --git a/diesel_derives/tests/auto_type.rs b/diesel_derives/tests/auto_type.rs index dc2c91c50c11..c556e2f10b32 100644 --- a/diesel_derives/tests/auto_type.rs +++ b/diesel_derives/tests/auto_type.rs @@ -411,6 +411,7 @@ fn postgres_functions() -> _ { bound, ), array_append(pg_extras::array, pg_extras::id), + array_replace(pg_extras::array, pg_extras::id, pg_extras::id) ) }