diesel::pg::expression::dsl

Function cardinality

Source
pub fn cardinality<Arr: ArrayOrNullableArray + SingleValue + MaybeNullableValue<Integer>, a>(
    a: a,
) -> cardinality<Arr, a>
where a: AsExpression<Arr>,
Available on crate feature postgres_backend only.
Expand description

Returns the total number of elements in the array, or 0 if the array is empty.

ยงExample


let array_cardinality = diesel::select(cardinality::<Array<Integer>, _>(vec![1, 2, 3, 4]))
    .get_result::<i32>(connection)?;
assert_eq!(4, array_cardinality);

let array_cardinality = diesel::select(cardinality::<Array<Nullable<Integer>>, _>(vec![Some(1), Some(2), Some(3)]))
    .get_result::<i32>(connection)?;
assert_eq!(3, array_cardinality);

let array_cardinality = diesel::select(cardinality::<Array<Integer>, _>(Vec::<i32>::new()))
    .get_result::<i32>(connection)?;
assert_eq!(0, array_cardinality);

let array_cardinality = diesel::select(cardinality::<Nullable<Array<Integer>>, _>(None::<Vec<i32>>))
    .get_result::<Option<i32>>(connection)?;
assert_eq!(None, array_cardinality);