From f2d4e258f889943d2b07e81115bc598478cdd201 Mon Sep 17 00:00:00 2001 From: danila-b Date: Sat, 17 Aug 2024 14:20:46 +0300 Subject: [PATCH 1/2] Implement array_ndims function --- diesel/src/pg/expression/functions.rs | 33 ++++++++++++++++++++++++ diesel/src/pg/expression/helper_types.rs | 5 ++++ diesel_derives/tests/auto_type.rs | 1 + 3 files changed, 39 insertions(+) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index dbda1ad38731..77b4bea0ff42 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -1361,3 +1361,36 @@ define_sql_function! { elem: E, ) -> Nullable>; } + +#[cfg(feature = "postgres_backend")] +define_sql_function! { + /// Returns the number of dimensions of the array + /// + /// # Example + /// + /// ```rust + /// # include!("../../doctest_setup.rs"); + /// # + /// # fn main() { + /// # run_test().unwrap(); + /// # } + /// # + /// # fn run_test() -> QueryResult<()> { + /// # use diesel::dsl::array_ndims; + /// # use diesel::sql_types::{Nullable, Array, Integer}; + /// # let connection = &mut establish_connection(); + /// + /// let dims = diesel::select(array_ndims::, _>(vec![1, 2])) + /// .get_result::(connection)?; + /// assert_eq!(1, dims); + /// + /// let dims = diesel::select(array_ndims::, _>(vec![vec![1, 2], vec![3, 4]])) + /// .get_result::(connection)?; + /// assert_eq!(2, dims); + /// + /// + /// # Ok(()) + /// # } + /// ``` + fn array_ndims(arr: Arr) -> Integer; +} diff --git a/diesel/src/pg/expression/helper_types.rs b/diesel/src/pg/expression/helper_types.rs index 58e5ea9ecae1..fff1e5ff8767 100644 --- a/diesel/src/pg/expression/helper_types.rs +++ b/diesel/src/pg/expression/helper_types.rs @@ -451,3 +451,8 @@ pub type array_position_with_subscript = #[cfg(feature = "postgres_backend")] pub type array_positions = super::functions::array_positions, SqlTypeOf, A, E>; + +/// Return type of [`array_ndims(array)`](super::functions::array_ndims()) +#[allow(non_camel_case_types)] +#[cfg(feature = "postgres_backend")] +pub type array_ndims = super::functions::array_ndims, A>; diff --git a/diesel_derives/tests/auto_type.rs b/diesel_derives/tests/auto_type.rs index b8dbfd4c8741..5c6419029ef4 100644 --- a/diesel_derives/tests/auto_type.rs +++ b/diesel_derives/tests/auto_type.rs @@ -430,6 +430,7 @@ fn postgres_functions() -> _ { array_position(pg_extras::array, pg_extras::id), array_position_with_subscript(pg_extras::array, pg_extras::id, pg_extras::id), array_positions(pg_extras::array, pg_extras::id), + array_ndims(pg_extras::array), ) } From 614df9b5107d5e9fb110bf7176f859e0ffd137b1 Mon Sep 17 00:00:00 2001 From: Georg Semmler Date: Fri, 30 Aug 2024 11:03:14 +0200 Subject: [PATCH 2/2] Fix the doc tests --- diesel/src/pg/expression/functions.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/diesel/src/pg/expression/functions.rs b/diesel/src/pg/expression/functions.rs index 77b4bea0ff42..67af06fd08cd 100644 --- a/diesel/src/pg/expression/functions.rs +++ b/diesel/src/pg/expression/functions.rs @@ -1380,15 +1380,11 @@ define_sql_function! { /// # use diesel::sql_types::{Nullable, Array, Integer}; /// # let connection = &mut establish_connection(); /// - /// let dims = diesel::select(array_ndims::, _>(vec![1, 2])) + /// // diesel currently only supports 1D arrays + /// let dims = diesel::select(array_ndims::, _>(vec![1, 2])) /// .get_result::(connection)?; /// assert_eq!(1, dims); /// - /// let dims = diesel::select(array_ndims::, _>(vec![vec![1, 2], vec![3, 4]])) - /// .get_result::(connection)?; - /// assert_eq!(2, dims); - /// - /// /// # Ok(()) /// # } /// ```