Skip to main content

json_set_2

Function json_set_2 

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

The json_set(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 inserted or replaced at the paths P. Unlike json_replace(),

§Variadic functions

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

json_set_0, json_set_1, … json_set_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. json_set() also inserts new values where the path does not already exist.

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

A path that ends in [#] appends the value to an existing array.

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

The json_set() 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});
let result = diesel::select(json_set_1::<Json, Integer, _, _, _>(json, "$.b", 2))
    .get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"a": 1, "b": 2}), result);

// Existing values are replaced.
let json = json!({"a": 1});
let result = diesel::select(json_set_1::<Json, Integer, _, _, _>(json, "$.a", 99))
    .get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"a": 99}), result);

// A path ending in "[#]" appends to an array.
let json = json!(['a', 'b', 'c']);
let result = diesel::select(json_set_1::<Json, Text, _, _, _>(json, "$[#]", "d"))
    .get_result::<serde_json::Value>(connection)?;
assert_eq!(json!(['a', 'b', 'c', 'd']), result);

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

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

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