From f56cc0b18e17ba0877d73123b55ea59d1785a3a0 Mon Sep 17 00:00:00 2001 From: Azan Ali Date: Mon, 26 Aug 2024 14:01:36 +0500 Subject: [PATCH] added postgres trim_array function --- diesel/src/pg/expression/functions.rs | 42 ++++++++++++++++++++++++ diesel/src/pg/expression/helper_types.rs | 5 +++ diesel_derives/tests/auto_type.rs | 1 + 3 files changed, 48 insertions(+) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index e27371ee3107..57c6024034f4 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -996,3 +996,45 @@ define_sql_function! { /// fn cardinality(a: Arr) -> Nullable; } + +#[cfg(feature = "postgres_backend")] +define_sql_function! { + /// Trims an array by removing the last n elements. If the array is multidimensional, only the first dimension is trimmed. + /// + /// # Example + /// + /// ```rust + /// # include!("../../doctest_setup.rs"); + /// # + /// # fn main(){ + /// # run_test().unwrap(); + /// # } + /// # fn run_test()->QueryResult<()>{ + /// # use diesel::dsl::trim_array; + /// # use diesel::sql_types::{Nullable,Array,Integer}; + /// # let connection = &mut establish_connection(); + /// + /// let trimmed_array = diesel::select(trim_array::, _, _>(vec![1, 2, 3, 4], 3)) + /// .get_result::>(connection)?; + /// assert_eq!(vec![1], trimmed_array); + /// + /// let trimmed_array = diesel::select(trim_array::>, _, _>(vec![Some(1), Some(2), Some(3)], 1)) + /// .get_result::>>(connection)?; + /// assert_eq!(vec![Some(1), Some(2)], trimmed_array); + /// + /// let trimmed_array = diesel::select(trim_array::, _, _>(Vec::::new(), 0)) + /// .get_result::>(connection)?; + /// assert_eq!(Vec::::new(), trimmed_array); + /// + /// let trimmed_array = diesel::select(trim_array::>, _, _>(None::>, 0)) + /// .get_result::>>(connection)?; + /// assert_eq!(None, trimmed_array); + /// + /// let trimmed_array = diesel::select(trim_array::>, _, _>(None::>, 1)) + /// .get_result::>>(connection)?; + /// assert_eq!(None, trimmed_array); + /// # Ok(()) + /// # } + /// + fn trim_array(a: Arr, n: Integer) -> Arr; +} diff --git a/diesel/src/pg/expression/helper_types.rs b/diesel/src/pg/expression/helper_types.rs index d7a2ecac308d..7ba69119b833 100644 --- a/diesel/src/pg/expression/helper_types.rs +++ b/diesel/src/pg/expression/helper_types.rs @@ -403,3 +403,8 @@ pub type array_remove = super::functions::array_remove, SqlTy #[allow(non_camel_case_types)] #[cfg(feature = "postgres_backend")] pub type cardinality = super::functions::cardinality, A>; + +/// Return type of [`trim_array(array)`](super::functions::trim_array()) +#[allow(non_camel_case_types)] +#[cfg(feature = "postgres_backend")] +pub type trim_array = super::functions::trim_array, A, N>; diff --git a/diesel_derives/tests/auto_type.rs b/diesel_derives/tests/auto_type.rs index b2c476a9fc08..ff21c0072ba5 100644 --- a/diesel_derives/tests/auto_type.rs +++ b/diesel_derives/tests/auto_type.rs @@ -421,6 +421,7 @@ fn postgres_functions() -> _ { array_to_string(pg_extras::array, pg_extras::name), array_to_string_with_null_string(pg_extras::array, pg_extras::name, pg_extras::name), cardinality(pg_extras::array), + trim_array(pg_extras::array, pg_extras::id), ) }