Skip to main content

json_replace_1

Function json_replace_1 

Source
pub fn json_replace_1<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Json> + SingleValue, V1: NotBlob, json, path_1, value_1>(
    json: json,
    path_1: path_1,
    value_1: value_1,
) -> json_replace_1<J, V1, json, path_1, value_1>
where json: AsExpression<J>, path_1: AsExpression<Text>, value_1: AsExpression<V1>,
Available on crate feature __sqlite-shared only.
Expand description

The json_replace(X,P,V,...) SQL function takes a single JSON value as its first argument followed by zero or more pairs of path and value arguments. It returns a copy of the X argument with values V substituted at the paths P, but only where something already exists

§Variadic functions

This function is variadic in SQL, so there’s a family of functions on a diesel side:

json_replace_0, json_replace_1, … json_replace_n

Here, the postfix number indicates repetitions of variadic arguments. To use this function, the appropriate version with the correct argument count must be selected.

§Controlling the generation of variadic function variants

By default, only variants with 0, 1, and 2 repetitions of variadic arguments are generated. To generate more variants, set the DIESEL_VARIADIC_FUNCTION_ARGS environment variable to the desired number of variants.

For a greater convenience this environment variable can also be set in a .cargo/config.toml file as described in the cargo documentation. at the path. Paths that select elements not found in X are silently ignored.

The path/value pairs are applied sequentially from left to right.

If the json_replace(X) function is called with no path/value pairs, then it returns the input X reformatted, with excess whitespace removed.

The json_replace() function throws an error if any of the path arguments is not a well-formed path.

This function requires at least SQLite 3.38 or newer

§Examples

let json = json!({"a": 1, "b": 2});
let result = diesel::select(json_replace_1::<Json, Integer, _, _, _>(json, "$.a", 99))
    .get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"a": 99, "b": 2}), result);

// A path that does not exist is silently ignored.
let json = json!({"a": 1});
let result = diesel::select(json_replace_1::<Json, Integer, _, _, _>(json, "$.b", 2))
    .get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"a": 1}), result);

let json = json!({"a": 1, "b": 2});
let result = diesel::select(
    json_replace_2::<Json, Integer, Text, _, _, _, _, _>(json, "$.a", 99, "$.b", "x"),
)
.get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"a": 99, "b": "x"}), result);

let json = json!({"a": 1});
let result = diesel::select(json_replace_0::<Json, _>(json))
    .get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"a": 1}), result);

let result = diesel::select(json_replace_1::<Nullable<Json>, Integer, _, _, _>(None::<serde_json::Value>, "$.a", 1))
    .get_result::<Option<serde_json::Value>>(connection)?;
assert_eq!(result, None);