diesel::pg::expression::dsl

Function array_replace

source
pub fn array_replace<Arr: ArrayOrNullableArray<Inner = T> + SingleValue, T: SingleValue, a, e, r>(
    a: a,
    e: e,
    r: r,
) -> array_replace<Arr, T, a, e, r>
where a: AsExpression<Arr>, e: AsExpression<T>, r: AsExpression<T>,
Available on crate feature postgres_backend only.
Expand description

Replace all occurrences of an element in an array with a given element

ยงExample

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

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

let ints = diesel::select(array_replace::<Nullable<Array<_>>, Integer, _, _, _>(None::<Vec<i32>>, 1, 2))
    .get_result::<Option<Vec<i32>>>(connection)?;

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