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

added postgres trim_array function #4204

Merged
merged 1 commit into from
Aug 26, 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
42 changes: 42 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,3 +996,45 @@ define_sql_function! {
///
fn cardinality<Arr:ArrayOrNullableArray + SingleValue>(a: Arr) -> Nullable<Integer>;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Trims an array by removing the last n elements. If the array is multidimensional, only the first dimension is trimmed.
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main(){
/// # run_test().unwrap();
/// # }
/// # fn run_test()->QueryResult<()>{
/// # use diesel::dsl::trim_array;
/// # use diesel::sql_types::{Nullable,Array,Integer};
/// # let connection = &mut establish_connection();
///
/// let trimmed_array = diesel::select(trim_array::<Array<Integer>, _, _>(vec![1, 2, 3, 4], 3))
/// .get_result::<Vec<i32>>(connection)?;
/// assert_eq!(vec![1], trimmed_array);
///
/// let trimmed_array = diesel::select(trim_array::<Array<Nullable<Integer>>, _, _>(vec![Some(1), Some(2), Some(3)], 1))
/// .get_result::<Vec<Option<i32>>>(connection)?;
/// assert_eq!(vec![Some(1), Some(2)], trimmed_array);
///
/// let trimmed_array = diesel::select(trim_array::<Array<Integer>, _, _>(Vec::<i32>::new(), 0))
/// .get_result::<Vec<i32>>(connection)?;
/// assert_eq!(Vec::<i32>::new(), trimmed_array);
///
/// let trimmed_array = diesel::select(trim_array::<Nullable<Array<Integer>>, _, _>(None::<Vec<i32>>, 0))
/// .get_result::<Option<Vec<i32>>>(connection)?;
/// assert_eq!(None, trimmed_array);
///
/// let trimmed_array = diesel::select(trim_array::<Nullable<Array<Integer>>, _, _>(None::<Vec<i32>>, 1))
/// .get_result::<Option<Vec<i32>>>(connection)?;
/// assert_eq!(None, trimmed_array);
/// # Ok(())
/// # }
///
fn trim_array<Arr:ArrayOrNullableArray + SingleValue>(a: Arr, n: Integer) -> Arr;
}
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 @@ -403,3 +403,8 @@ pub type array_remove<A, E> = super::functions::array_remove<SqlTypeOf<A>, SqlTy
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type cardinality<A> = super::functions::cardinality<SqlTypeOf<A>, A>;

/// Return type of [`trim_array(array)`](super::functions::trim_array())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type trim_array<A, N> = super::functions::trim_array<SqlTypeOf<A>, A, N>;
1 change: 1 addition & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ fn postgres_functions() -> _ {
array_to_string(pg_extras::array, pg_extras::name),
array_to_string_with_null_string(pg_extras::array, pg_extras::name, pg_extras::name),
cardinality(pg_extras::array),
trim_array(pg_extras::array, pg_extras::id),
)
}

Expand Down
Loading