json_remove_2

Function json_remove_2 

Source
pub fn json_remove_2<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + SingleValue, json, path_1, path_2>(
    json: json,
    path_1: path_1,
    path_2: path_2,
) -> json_remove_2<J, json, path_1, path_2>
where json: AsExpression<J>, path_1: AsExpression<Text>, path_2: AsExpression<Text>,
Available on crate feature sqlite only.
Expand description

The json_remove(X,P,...) SQL function takes a single JSON value as its first argument followed by zero or more path arguments. The json_remove(X,P,...) function returns a copy of the X parameter with all the elements identified by path arguments removed. Paths that select elements not found in X are silently ignored.

Removals occurs sequentially from left to right. Changes caused by prior removals can affect the path search for subsequent arguments.

If the json_remove(X) function is called with no path arguments, then it returns the input X reformatted, with excess whitespace removed.

The json_remove() 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', 'b', 'c', 'd']);
let result = diesel::select(json_remove_0::<Json, _>(json))
    .get_result::<Option<serde_json::Value>>(connection)?;
assert_eq!(Some(json!(['a', 'b', 'c', 'd'])), result);

// Values are removed sequentially from left to right.
let json = json!(['a', 'b', 'c', 'd']);
let result = diesel::select(json_remove_2::<Json, _, _, _>(json, "$[0]", "$[2]"))
    .get_result::<Option<serde_json::Value>>(connection)?;
assert_eq!(Some(json!(['b', 'c'])), result);

let json = json!({"a": 10, "b": 20});
let result = diesel::select(json_remove_1::<Json, _, _>(json, "$.a"))
    .get_result::<Option<serde_json::Value>>(connection)?;
assert_eq!(Some(json!({"b": 20})), result);

// Paths that select not existing elements are silently ignored.
let json = json!({"a": 10, "b": 20});
let result = diesel::select(json_remove_1::<Json, _, _>(json, "$.c"))
    .get_result::<Option<serde_json::Value>>(connection)?;
assert_eq!(Some(json!({"a": 10, "b": 20})), result);

let json = json!({"a": 10, "b": 20});
let result = diesel::select(json_remove_1::<Json, _, _>(json, "$"))
    .get_result::<Option<serde_json::Value>>(connection)?;
assert_eq!(None, result);

§Variadic functions

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

json_remove_0, json_remove_1, … json_remove_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.