pub fn jsonb_set_lax<E: JsonbOrNullableJsonb + SingleValue, Arr: TextArrayOrNullableTextArray + CombinedNullableValue<E, Jsonb>, base, path, new_value, create_if_missing, null_value_treatment>(
base: base,
path: path,
new_value: new_value,
create_if_missing: create_if_missing,
null_value_treatment: null_value_treatment,
) -> jsonb_set_lax<E, Arr, base, path, new_value, create_if_missing, null_value_treatment>where
base: AsExpression<E>,
path: AsExpression<Arr>,
new_value: AsExpression<E>,
create_if_missing: AsExpression<Bool>,
null_value_treatment: AsExpression<NullValueTreatmentEnum>,
postgres_backend
only.Expand description
Returns target with the item designated by path replaced by new_value, or with new_value added and the item designated by path does not exist.
It can’t set path in scalar
All earlier steps in the path must exist, or the target is returned unchanged. As with the path oriented operators, negative integers that appear in the path count from the end of JSON arrays. If the last path step is an array index that is out of range, the new value is added at the beginning of the array if the index is negative, or at the end of the array if it is positive.
If new_value is not NULL, behaves identically to jsonb_set. Otherwise behaves according to the value of null_value_treatment which must be one of ‘raise_exception’, ‘use_json_null’, ‘delete_key’, or ‘return_target’. The default is ‘use_json_null’.
§Example
let null_value_treatment = NullValueTreatment::UseJsonNull;
let result = diesel::select(jsonb_set_lax::<Jsonb, Array<Text>, _, _, _, _, _>(
json!([{"f1":1,"f2":null},2,null,3]),
vec!["0","f1"],
json!([2,3,4]),
true,
null_value_treatment
)).get_result::<Value>(connection)?;
let expected: Value = json!([{"f1": [2, 3, 4], "f2": null}, 2, null, 3]);
assert_eq!(result, expected);
let null_value_treatment = NullValueTreatment::ReturnTarget;
let result = diesel::select(jsonb_set_lax::<Nullable<Jsonb>, Array<Nullable<Text>>, _, _, _, _, _>(
json!([{"f1":99,"f2":null},2]),
vec!["0","f3"],
None::<Value>,
true,
null_value_treatment
)).get_result::<Option<Value>>(connection)?;
assert_eq!(result, Some(json!([{"f1":99,"f2":null},2])));
let null_value_treatment = NullValueTreatment::UseJsonNull;
let empty:Vec<String> = Vec::new();
let result = diesel::select(jsonb_set_lax::<Jsonb, Array<Nullable<Text>>, _, _, _, _, _>(
// cannot be json!(null)
json!([]),
empty,
json!(null),
true,
null_value_treatment
)).get_result::<Value>(connection)?;
let expected = json!([]);
assert_eq!(result, expected);
let null_value_treatment = NullValueTreatment::UseJsonNull;
let result = diesel::select(jsonb_set_lax::<Jsonb, Nullable<Array<Nullable<Text>>>, _, _, _, _, _,>(
json!(null),
None::<Vec<String>>,
json!({"foo": 42}),
true,
null_value_treatment
)).get_result::<Option<Value>>(connection)?;
assert!(result.is_none());