diesel::pg::expression::dsl

Function array_position

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

Returns the subscript of the first occurrence of the second argument in the array, or NULL if it’s not present. If the third argument is given, the search begins at that subscript. This function omits the third argument. See [array_position_with_subscript].

The array must be one-dimensional. Comparisons are done using IS NOT DISTINCT FROM semantics, so it is possible to search for NULL.

§Example


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

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

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

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

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