From 552f2110b1576b77773704f837b55b1a23cd96ee Mon Sep 17 00:00:00 2001 From: Ali Tariq Date: Mon, 26 Aug 2024 14:00:19 +0500 Subject: [PATCH 1/3] implement array_fill --- diesel/src/pg/expression/functions.rs | 79 ++++++++++++++++++++++++ diesel/src/pg/expression/helper_types.rs | 11 ++++ diesel_derives/tests/auto_type.rs | 2 + 3 files changed, 92 insertions(+) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 93624725dbc0..bd28977e611a 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -1096,3 +1096,82 @@ define_sql_function! { /// ``` fn array_length(array: Arr, dimension: Integer) -> Nullable; } + +#[cfg(feature = "postgres_backend")] +define_sql_function! { + /// Returns an array initialized with supplied value and dimensions, + /// optionally with lower bounds other than 1 + /// + /// # Example + /// + /// ```rust + /// # include!("../../doctest_setup.rs"); + /// # + /// # fn main(){ + /// # run_test().unwrap(); + /// # } + /// # fn run_test()->QueryResult<()>{ + /// # use diesel::dsl::array_fill; + /// # use diesel::sql_types::{Nullable,Array,Integer,Text}; + /// # let connection = &mut establish_connection(); + /// + /// let array = diesel::select(array_fill::(2,vec![2])) + /// .get_result::>(connection)?; + /// assert_eq!(vec![2,2],array); + /// + /// let array = diesel::select(array_fill::(String::from("abc"),vec![3])) + /// .get_result::>(connection)?; + /// assert_eq!(vec!["abc","abc","abc"],array); + /// + /// let array = diesel::select(array_fill::,_,_>(Some(4),vec![3])) + /// .get_result::>>(connection)?; + /// assert_eq!(vec![Some(4),Some(4),Some(4)],array); + /// + /// let array = diesel::select(array_fill::,_,_>(None::,vec![3])) + /// .get_result::>>(connection)?; + /// assert_eq!(vec![None::,None::,None::],array); + /// # Ok(()) + /// # } + /// + fn array_fill(value:E,dim:Array) -> Array; +} + +#[cfg(feature = "postgres_backend")] +define_sql_function! { + /// Returns an array initialized with supplied value and dimensions, + /// optionally with lower bounds other than 1 + /// + /// # Example + /// + /// ```rust + /// # include!("../../doctest_setup.rs"); + /// # + /// # fn main(){ + /// # run_test().unwrap(); + /// # } + /// # fn run_test()->QueryResult<()>{ + /// # use diesel::dsl::array_fill_with_lower_bound; + /// # use diesel::sql_types::{Nullable,Array,Integer,Text}; + /// # let connection = &mut establish_connection(); + /// + /// let array = diesel::select(array_fill_with_lower_bound::(2,vec![2],vec![2])) + /// .get_result::>(connection)?; + /// assert_eq!(vec![2,2],array); + /// + /// let array = diesel::select(array_fill_with_lower_bound::(String::from("abc"),vec![3],vec![3])) + /// .get_result::>(connection)?; + /// assert_eq!(vec!["abc","abc","abc"],array); + /// + /// let array = diesel::select(array_fill_with_lower_bound::,_,_,_>(Some(4),vec![3],vec![3])) + /// .get_result::>>(connection)?; + /// assert_eq!(vec![Some(4),Some(4),Some(4)],array); + /// + /// let array = diesel::select(array_fill_with_lower_bound::,_,_,_>(None::,vec![3],vec![3])) + /// .get_result::>>(connection)?; + /// assert_eq!(vec![None::,None::,None::],array); + /// # Ok(()) + /// # } + /// + #[sql_name = "array_fill"] + fn array_fill_with_lower_bound(value:E,dim:Array,lower_bound:Array) -> Array; +} diff --git a/diesel/src/pg/expression/helper_types.rs b/diesel/src/pg/expression/helper_types.rs index 862fcc83bf98..47303ee695d5 100644 --- a/diesel/src/pg/expression/helper_types.rs +++ b/diesel/src/pg/expression/helper_types.rs @@ -418,3 +418,14 @@ pub type array_cat = super::functions::array_cat, A, B>; #[allow(non_camel_case_types)] #[cfg(feature = "postgres_backend")] pub type array_length = super::functions::array_length, A, D>; + +/// Return type of [`array_fill(value,array)`](super::functions::array_fill()) +#[allow(non_camel_case_types)] +#[cfg(feature = "postgres_backend")] +pub type array_fill = super::functions::array_fill, E, Array>; + +/// Return type of [`array_fill_with_lower_bound(value,array,array)`](super::functions::array_fill_with_lower_bound()) +#[allow(non_camel_case_types)] +#[cfg(feature = "postgres_backend")] +pub type array_fill_with_lower_bound = + super::functions::array_fill_with_lower_bound, E, Array, Array>; diff --git a/diesel_derives/tests/auto_type.rs b/diesel_derives/tests/auto_type.rs index 7a602612ed80..536d94b4b22f 100644 --- a/diesel_derives/tests/auto_type.rs +++ b/diesel_derives/tests/auto_type.rs @@ -424,6 +424,8 @@ fn postgres_functions() -> _ { trim_array(pg_extras::array, pg_extras::id), array_cat(pg_extras::array, pg_extras::array), array_length(pg_extras::array, 1_i32), + array_fill(pg_extras::id, pg_extras::array), + array_fill_with_lower_bound(pg_extras::id, pg_extras::array, pg_extras::array), ) } From f71d0aaa7df3f36b97492df4062c41b31faf5b07 Mon Sep 17 00:00:00 2001 From: Ali Tariq Date: Mon, 26 Aug 2024 14:28:25 +0500 Subject: [PATCH 2/3] fixed auto type test --- diesel/src/pg/expression/functions.rs | 5 +++-- diesel/src/pg/expression/helper_types.rs | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index bd28977e611a..5ced518c96da 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -1100,7 +1100,8 @@ define_sql_function! { #[cfg(feature = "postgres_backend")] define_sql_function! { /// Returns an array initialized with supplied value and dimensions, - /// optionally with lower bounds other than 1 + /// optionally with lower bounds other than 1. This function omits the optional + /// lower bound argument. See [array_fill_with_lower_bound] for that. /// /// # Example /// @@ -1139,7 +1140,7 @@ define_sql_function! { #[cfg(feature = "postgres_backend")] define_sql_function! { /// Returns an array initialized with supplied value and dimensions, - /// optionally with lower bounds other than 1 + /// with lower bounds other than 1 /// /// # Example /// diff --git a/diesel/src/pg/expression/helper_types.rs b/diesel/src/pg/expression/helper_types.rs index 47303ee695d5..97978017bf64 100644 --- a/diesel/src/pg/expression/helper_types.rs +++ b/diesel/src/pg/expression/helper_types.rs @@ -422,10 +422,10 @@ pub type array_length = super::functions::array_length, A, D> /// Return type of [`array_fill(value,array)`](super::functions::array_fill()) #[allow(non_camel_case_types)] #[cfg(feature = "postgres_backend")] -pub type array_fill = super::functions::array_fill, E, Array>; +pub type array_fill = super::functions::array_fill, E, A>; /// Return type of [`array_fill_with_lower_bound(value,array,array)`](super::functions::array_fill_with_lower_bound()) #[allow(non_camel_case_types)] #[cfg(feature = "postgres_backend")] -pub type array_fill_with_lower_bound = - super::functions::array_fill_with_lower_bound, E, Array, Array>; +pub type array_fill_with_lower_bound = + super::functions::array_fill_with_lower_bound, E, A1, A2>; From 6e423d359c8a59693b455dcd3225528ceb079a05 Mon Sep 17 00:00:00 2001 From: Georg Semmler Date: Mon, 26 Aug 2024 12:20:22 +0000 Subject: [PATCH 3/3] Minor style fixes --- diesel/src/pg/expression/functions.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 5ced518c96da..3a9dcd0a1e4d 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -1134,7 +1134,7 @@ define_sql_function! { /// # Ok(()) /// # } /// - fn array_fill(value:E,dim:Array) -> Array; + fn array_fill(value: E, dim: Array) -> Array; } #[cfg(feature = "postgres_backend")] @@ -1174,5 +1174,5 @@ define_sql_function! { /// # } /// #[sql_name = "array_fill"] - fn array_fill_with_lower_bound(value:E,dim:Array,lower_bound:Array) -> Array; + fn array_fill_with_lower_bound(value: E, dim: Array, lower_bound: Array) -> Array; }