diesel::pg::expression::dsl

Function array_positions

source
pub fn array_positions<Arr: ArrayOrNullableArray<Inner = E> + SingleValue + MaybeNullableValue<Array<Integer>>, E: SingleValue, a, elem>(
    a: a,
    elem: elem,
) -> array_positions<Arr, E, a, elem>
where a: AsExpression<Arr>, elem: AsExpression<E>,
Available on crate feature postgres_backend only.
Expand description

Returns an array of the subscripts of all occurrences of the second argument in the array given as first argument.

The array must be one-dimensional. Comparisons are done using IS NOT DISTINCT FROM semantics, so it is possible to search for NULL. NULL is returned only if the array is NULL; if the value is not found in the array, an empty array is returned.

ยงExample


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

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

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

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