Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement array_upper #4219

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading