Skip to content

Commit f2d4e25

Browse files
danila-bweiznich
authored andcommitted
Implement array_ndims function
1 parent 953e555 commit f2d4e25

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

diesel/src/pg/expression/functions.rs

+33
Original file line numberDiff line numberDiff line change
@@ -1361,3 +1361,36 @@ 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+
/// let dims = diesel::select(array_ndims::<Array<_>, _>(vec![1, 2]))
1384+
/// .get_result::<i32>(connection)?;
1385+
/// assert_eq!(1, dims);
1386+
///
1387+
/// let dims = diesel::select(array_ndims::<Array<_>, _>(vec![vec![1, 2], vec![3, 4]]))
1388+
/// .get_result::<i32>(connection)?;
1389+
/// assert_eq!(2, dims);
1390+
///
1391+
///
1392+
/// # Ok(())
1393+
/// # }
1394+
/// ```
1395+
fn array_ndims<Arr: ArrayOrNullableArray + SingleValue>(arr: Arr) -> Integer;
1396+
}

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)