pub fn jsonb_set_create_if_missing<E: JsonbOrNullableJsonb + SingleValue, Arr: TextArrayOrNullableTextArray + CombinedNullableValue<E, Jsonb>, base, path, new_value, create_if_missing>(
base: base,
path: path,
new_value: new_value,
create_if_missing: create_if_missing,
) -> jsonb_set_create_if_missing<E, Arr, base, path, new_value, create_if_missing>where
base: AsExpression<E>,
path: AsExpression<Arr>,
new_value: AsExpression<E>,
create_if_missing: AsExpression<Bool>,
Available on crate feature
postgres_backend
only.Expand description
Returns target with the item designated by path replaced by new_value, or with new_value added if create_if_missing is true (which is the default) 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, and create_if_missing is true, 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.
§Example
let result = diesel::select(jsonb_set_create_if_missing::<Jsonb, Array<Text>, _, _, _, _>(
json!([{"f1":1,"f2":null},2,null,3]),
vec!["0","f1"],
json!([2,3,4]),
true
)).get_result::<Value>(connection)?;
let expected: Value = json!([{"f1": [2, 3, 4], "f2": null}, 2, null, 3]);
assert_eq!(result, expected);
let result = diesel::select(jsonb_set_create_if_missing::<Jsonb, Array<Text>, _, _, _, _>(
json!([{"f1":1,"f2":null},2,null,3]),
vec!["0","f3"],
json!([2,3,4]),
false
)).get_result::<Value>(connection)?;
let expected: Value = json!([{"f1":1, "f2": null},2, null, 3]);
assert_eq!(result, expected);
let result = diesel::select(jsonb_set_create_if_missing::<Jsonb, Array<Text>, _, _, _, _>(
json!([{"odd":[2,4,6,8]}]),
// not vec!["odd"], cannot set path in scalar
vec!["0","odd"],
json!([1,3,5,7]),
true
)).get_result::<Value>(connection)?;
let expected: Value = json!([{"odd":[1,3,5,7]}]);
assert_eq!(result, expected);
let empty:Vec<String> = Vec::new();
let result = diesel::select(jsonb_set_create_if_missing::<Nullable<Jsonb>, Array<Nullable<Text>>, _, _, _, _>(
None::<Value>,
empty,
None::<Value>,
true
)).get_result::<Option<Value>>(connection)?;
assert!(result.is_none());
let empty:Vec<String> = Vec::new();
let result = diesel::select(jsonb_set_create_if_missing::<Jsonb, Array<Nullable<Text>>, _, _, _, _>(
// cannot be json!(null)
json!([]),
empty,
json!(null),
true
)).get_result::<Value>(connection)?;
let expected = json!([]);
assert_eq!(result, expected);
let result = diesel::select(jsonb_set_create_if_missing::<Jsonb, Nullable<Array<Nullable<Text>>>, _, _, _, _>(
json!(null),
None::<Vec<String>>,
json!({"foo": 42}),
true
)).get_result::<Option<Value>>(connection)?;
assert!(result.is_none());