Skip to main content

jsonb_set_1

Function jsonb_set_1 

Source
pub fn jsonb_set_1<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Jsonb> + SingleValue, V1: NotBlob, json, path_1, value_1>(
    json: json,
    path_1: path_1,
    value_1: value_1,
) -> jsonb_set_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 jsonb_set(X,P,V,...) SQL function works just like the json_set() function except that the result is returned in SQLite’s private binary JSONB format rather than in the standard RFC 8259 text format.

§Variadic functions

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

jsonb_set_0, jsonb_set_1, … jsonb_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.

This function requires at least SQLite 3.38 or newer

§Examples

let json = json!({"a": 1});
let result = diesel::select(jsonb_set_1::<Jsonb, 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(jsonb_set_1::<Jsonb, 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(jsonb_set_1::<Jsonb, 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(jsonb_set_0::<Jsonb, _>(json))
    .get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"a": 1}), result);

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