Skip to main content

json_insert_2

Function json_insert_2 

Source
pub fn json_insert_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_insert_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_insert(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 the values V inserted at the paths P, but only where nothing already exists at

§Variadic functions

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

json_insert_0, json_insert_1, … json_insert_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. the path. Paths that already select an element of X are left unchanged.

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_insert(X) function is called with no path/value pairs, then it returns the input X reformatted, with excess whitespace removed.

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

// A path that already exists is left unchanged.
let json = json!({"a": 1});
let result = diesel::select(json_insert_1::<Json, Integer, _, _, _>(json, "$.a", 99))
    .get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"a": 1}), result);

// A path ending in "[#]" appends to an array.
let json = json!(['a', 'b', 'c']);
let result = diesel::select(json_insert_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_insert_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_insert_0::<Json, _>(json))
    .get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"a": 1}), result);

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