Skip to content

Commit

Permalink
Merge pull request #4219 from gushul/feat/add/array_upper
Browse files Browse the repository at this point in the history
implement array_upper
  • Loading branch information
weiznich authored Aug 30, 2024
2 parents 253cce2 + 1b78739 commit 08911c8
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 @@ -1390,3 +1390,36 @@ define_sql_function! {
/// ```
fn array_ndims<Arr: ArrayOrNullableArray + SingleValue>(arr: Arr) -> Integer;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Returns the upper bound of the requested array
///
/// This function returns null for dimensions that do not exist
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::array_upper;
/// # use diesel::sql_types::{Integer, Array};
/// # let connection = &mut establish_connection();
/// let result = diesel::select(array_upper::<Array<Integer>, _, _>(vec![1, 2, 3], 1))
/// .get_result::<Option<i32>>(connection)?;
/// assert_eq!(Some(3), result);
///
/// // the array has only one dimension
/// let result = diesel::select(array_upper::<Array<Integer>, _, _>(vec![1, 2, 3], 2))
/// .get_result::<Option<i32>>(connection)?;
/// assert_eq!(None, result);
/// # Ok(())
/// # }
/// ```
fn array_upper<Arr: ArrayOrNullableArray + SingleValue>(array: Arr, dimension: Integer) -> Nullable<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 @@ -435,6 +435,11 @@ pub type array_fill_with_lower_bound<E, A1, A2> =
#[cfg(feature = "postgres_backend")]
pub type array_lower<A, D> = super::functions::array_lower<SqlTypeOf<A>, A, D>;

/// Return type of [`array_upper(array, bound)`](super::functions::array_upper())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_upper<A, D> = super::functions::array_upper<SqlTypeOf<A>, A, D>;

/// Return type of [`array_position(array,element)`](super::functions::array_position)
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
Expand Down
1 change: 1 addition & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ fn postgres_functions() -> _ {
array_fill(pg_extras::id, pg_extras::array),
array_fill_with_lower_bound(pg_extras::id, pg_extras::array, pg_extras::array),
array_lower(pg_extras::array, 1_i32),
array_upper(pg_extras::array, 1_i32),
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),
Expand Down

0 comments on commit 08911c8

Please sign in to comment.