Skip to content

Commit 4be7713

Browse files
authored
Merge pull request #4178 from danila-b/feat/add/array-ndims-function
Implement array_ndims function
2 parents 2e6f840 + 614df9b commit 4be7713

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

diesel/src/pg/expression/functions.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1361,3 +1361,32 @@ define_sql_function! {
13611361
elem: E,
13621362
) -> Nullable<Array<Integer>>;
13631363
}
1364+
1365+
#[cfg(feature = "postgres_backend")]
1366+
define_sql_function! {
1367+
/// Returns the number of dimensions of the array
1368+
///
1369+
/// # Example
1370+
///
1371+
/// ```rust
1372+
/// # include!("../../doctest_setup.rs");
1373+
/// #
1374+
/// # fn main() {
1375+
/// # run_test().unwrap();
1376+
/// # }
1377+
/// #
1378+
/// # fn run_test() -> QueryResult<()> {
1379+
/// # use diesel::dsl::array_ndims;
1380+
/// # use diesel::sql_types::{Nullable, Array, Integer};
1381+
/// # let connection = &mut establish_connection();
1382+
///
1383+
/// // diesel currently only supports 1D arrays
1384+
/// let dims = diesel::select(array_ndims::<Array<Integer>, _>(vec![1, 2]))
1385+
/// .get_result::<i32>(connection)?;
1386+
/// assert_eq!(1, dims);
1387+
///
1388+
/// # Ok(())
1389+
/// # }
1390+
/// ```
1391+
fn array_ndims<Arr: ArrayOrNullableArray + SingleValue>(arr: Arr) -> Integer;
1392+
}

diesel/src/pg/expression/helper_types.rs

+5
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,8 @@ pub type array_position_with_subscript<A, E, S> =
451451
#[cfg(feature = "postgres_backend")]
452452
pub type array_positions<A, E> =
453453
super::functions::array_positions<SqlTypeOf<A>, SqlTypeOf<E>, A, E>;
454+
455+
/// Return type of [`array_ndims(array)`](super::functions::array_ndims())
456+
#[allow(non_camel_case_types)]
457+
#[cfg(feature = "postgres_backend")]
458+
pub type array_ndims<A> = super::functions::array_ndims<SqlTypeOf<A>, A>;

diesel_derives/tests/auto_type.rs

+1
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ fn postgres_functions() -> _ {
430430
array_position(pg_extras::array, pg_extras::id),
431431
array_position_with_subscript(pg_extras::array, pg_extras::id, pg_extras::id),
432432
array_positions(pg_extras::array, pg_extras::id),
433+
array_ndims(pg_extras::array),
433434
)
434435
}
435436

0 commit comments

Comments
 (0)