Skip to content

Commit

Permalink
Merge pull request #4178 from danila-b/feat/add/array-ndims-function
Browse files Browse the repository at this point in the history
Implement array_ndims function
  • Loading branch information
weiznich authored Aug 30, 2024
2 parents 2e6f840 + 614df9b commit 4be7713
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
29 changes: 29 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1361,3 +1361,32 @@ define_sql_function! {
elem: E,
) -> Nullable<Array<Integer>>;
}

#[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();
///
/// // diesel currently only supports 1D arrays
/// let dims = diesel::select(array_ndims::<Array<Integer>, _>(vec![1, 2]))
/// .get_result::<i32>(connection)?;
/// assert_eq!(1, dims);
///
/// # Ok(())
/// # }
/// ```
fn array_ndims<Arr: ArrayOrNullableArray + SingleValue>(arr: Arr) -> Integer;
}
5 changes: 5 additions & 0 deletions diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,8 @@ pub type array_position_with_subscript<A, E, S> =
#[cfg(feature = "postgres_backend")]
pub type array_positions<A, E> =
super::functions::array_positions<SqlTypeOf<A>, SqlTypeOf<E>, 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<A> = super::functions::array_ndims<SqlTypeOf<A>, A>;
1 change: 1 addition & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}

Expand Down

0 comments on commit 4be7713

Please sign in to comment.