Skip to content

Commit

Permalink
added postgres trim_array function
Browse files Browse the repository at this point in the history
  • Loading branch information
Azan Ali authored and Azan Ali committed Aug 26, 2024
1 parent 30a6b01 commit f56cc0b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
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

0 comments on commit f56cc0b

Please sign in to comment.