Skip to content

Commit

Permalink
Merge pull request #4200 from aznszn/add_array_cardinality
Browse files Browse the repository at this point in the history
added the postgres `cardinality` function
  • Loading branch information
weiznich authored Aug 24, 2024
2 parents cd37481 + 95cbd9e commit 30a6b01
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
38 changes: 38 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,3 +958,41 @@ define_sql_function! {
array: Arr, del: Text
) -> Text;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Returns the total number of elements in the array, or 0 if the array is empty.
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main(){
/// # run_test().unwrap();
/// # }
/// # fn run_test()->QueryResult<()>{
/// # use diesel::dsl::cardinality;
/// # use diesel::sql_types::{Nullable,Array,Integer};
/// # let connection = &mut establish_connection();
///
/// let array_cardinality = diesel::select(cardinality::<Array<Integer>, _>(vec![1, 2, 3, 4]))
/// .get_result::<Option<i32>>(connection)?;
/// assert_eq!(4, array_cardinality.unwrap());
///
/// let array_cardinality = diesel::select(cardinality::<Array<Nullable<Integer>>, _>(vec![Some(1), Some(2), Some(3)]))
/// .get_result::<Option<i32>>(connection)?;
/// assert_eq!(3, array_cardinality.unwrap());
///
/// let array_cardinality = diesel::select(cardinality::<Array<Integer>, _>(Vec::<i32>::new()))
/// .get_result::<Option<i32>>(connection)?;
/// assert_eq!(0, array_cardinality.unwrap());
///
/// let array_cardinality = diesel::select(cardinality::<Nullable<Array<Integer>>, _>(None::<Vec<i32>>))
/// .get_result::<Option<i32>>(connection)?;
/// assert_eq!(None, array_cardinality);
/// # Ok(())
/// # }
///
fn cardinality<Arr:ArrayOrNullableArray + SingleValue>(a: Arr) -> 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 @@ -398,3 +398,8 @@ pub type array_prepend<E, A> = super::functions::array_prepend<SqlTypeOf<E>, Sql
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_remove<A, E> = super::functions::array_remove<SqlTypeOf<A>, SqlTypeOf<E>, A, E>;

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

Expand Down

0 comments on commit 30a6b01

Please sign in to comment.