Skip to content

Commit

Permalink
Implement array_ndims function
Browse files Browse the repository at this point in the history
  • Loading branch information
danila-b committed Aug 17, 2024
1 parent cee97fb commit e601cb5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
33 changes: 33 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,36 @@ define_sql_function! {
/// ```
fn array_append<Arr: ArrayOrNullableArray<Inner=T> + SingleValue, T: SingleValue>(a: Arr, e: T) -> Array<T>;
}

#[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::<Array<_>, _>(vec![1, 2]))
/// .get_result::<i32>(connection)?;
/// assert_eq!(1, dims);
///
/// let dims = diesel::select(array_ndims::<Array<_>, _>(vec![vec![1, 2], vec![3, 4]]))
/// .get_result::<i32>(connection)?;
/// assert_eq!(2, 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 @@ -352,3 +352,8 @@ pub type range_merge<R1, R2> = super::functions::range_merge<SqlTypeOf<R1>, SqlT
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_append<A, E> = super::functions::array_append<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 @@ -411,6 +411,7 @@ fn postgres_functions() -> _ {
bound,
),
array_append(pg_extras::array, pg_extras::id),
array_ndims(pg_extras::array),
)
}

Expand Down

0 comments on commit e601cb5

Please sign in to comment.