-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Sqlite json/jsonb patch #4554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Sqlite json/jsonb patch #4554
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -959,4 +959,157 @@ extern "SQL" { | |||||||||||||||||||||||||||||||||
#[sql_name = "json_quote"] | ||||||||||||||||||||||||||||||||||
#[cfg(feature = "sqlite")] | ||||||||||||||||||||||||||||||||||
fn json_quote<J: SqlType + SingleValue>(j: J) -> Json; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
/// The json_patch(T,P) SQL function runs the RFC-7396 MergePatch algorithm to apply patch P against input T. The patched copy of T is returned. | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// # Example | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// ```rust | ||||||||||||||||||||||||||||||||||
/// # include!("../../doctest_setup.rs"); | ||||||||||||||||||||||||||||||||||
/// # | ||||||||||||||||||||||||||||||||||
/// # fn main() { | ||||||||||||||||||||||||||||||||||
/// # #[cfg(feature = "serde_json")] | ||||||||||||||||||||||||||||||||||
/// # run_test().unwrap(); | ||||||||||||||||||||||||||||||||||
/// # } | ||||||||||||||||||||||||||||||||||
/// # | ||||||||||||||||||||||||||||||||||
/// # #[cfg(feature = "serde_json")] | ||||||||||||||||||||||||||||||||||
/// # fn run_test() -> QueryResult<()> { | ||||||||||||||||||||||||||||||||||
/// # use diesel::dsl::{sql, json_patch}; | ||||||||||||||||||||||||||||||||||
/// # use serde_json::{json, Value}; | ||||||||||||||||||||||||||||||||||
/// # use diesel::sql_types::{Text, Json, Nullable}; | ||||||||||||||||||||||||||||||||||
/// # let connection = &mut establish_connection(); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let version = diesel::select(sql::<Text>("sqlite_version();")) | ||||||||||||||||||||||||||||||||||
/// .get_result::<String>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// // Querying SQLite version should not fail. | ||||||||||||||||||||||||||||||||||
/// let version_components: Vec<&str> = version.split('.').collect(); | ||||||||||||||||||||||||||||||||||
/// let major: u32 = version_components[0].parse().unwrap(); | ||||||||||||||||||||||||||||||||||
/// let minor: u32 = version_components[1].parse().unwrap(); | ||||||||||||||||||||||||||||||||||
/// let patch: u32 = version_components[2].parse().unwrap(); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// if major > 3 || (major == 3 && minor >= 38) { | ||||||||||||||||||||||||||||||||||
/// /* Valid sqlite version, do nothing */ | ||||||||||||||||||||||||||||||||||
/// } else { | ||||||||||||||||||||||||||||||||||
/// println!("SQLite version is too old, skipping the test."); | ||||||||||||||||||||||||||||||||||
/// return Ok(()); | ||||||||||||||||||||||||||||||||||
/// } | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(json_patch::<Json, _, _>(json!({"a":1,"b":2}), json!({"c":3,"d":4}))) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!({"a":1, "b":2, "c":3, "d":4}), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(json_patch::<Json, _, _>(json!({"a":[1,2],"b":2}), json!({"a":9}))) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!({"a":9,"b":2}), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(json_patch::<Json, _, _>(json!({"a":[1,2],"b":2}), json!({"a":null}))) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!({"b":2}), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(json_patch::<Json, _, _>(json!({"a":1,"b":2}), json!({"a":9,"b":null,"c":8}))) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!({"a":9,"c":8}), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(json_patch::<Json, _, _>(json!({"a":{"x":1,"y":2},"b":3}), json!({"a":{"y":9},"c":8}))) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!({"a":{"x":1,"y":9},"b":3,"c":8}), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(json_patch::<Nullable<Json>, _, _>(None, json!({"a":{"x":1,"y":2},"b":3}))) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!(null), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(json_patch::<Nullable<Json>, _, _>(json!({"a":{"x":1,"y":2},"b":3}), None)) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!(null), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// # Ok(()) | ||||||||||||||||||||||||||||||||||
/// # } | ||||||||||||||||||||||||||||||||||
/// ``` | ||||||||||||||||||||||||||||||||||
#[sql_name = "json_patch"] | ||||||||||||||||||||||||||||||||||
#[cfg(feature = "sqlite")] | ||||||||||||||||||||||||||||||||||
fn json_patch<J: JsonOrNullableJson + SingleValue>(j: J, patch: J) -> Json; | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type is not correct. This function (and also We might want something like
Suggested change
with the diesel/diesel/src/pg/expression/expression_methods.rs Lines 3936 to 3949 in c5cdaa4
(That needs to be moved to |
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
/// The jsonb_patch() function works just like the json_patch() function except that the patched JSON is returned in the binary JSONB format. | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// # Example | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// ```rust | ||||||||||||||||||||||||||||||||||
/// # include!("../../doctest_setup.rs"); | ||||||||||||||||||||||||||||||||||
/// # | ||||||||||||||||||||||||||||||||||
/// # fn main() { | ||||||||||||||||||||||||||||||||||
/// # #[cfg(feature = "serde_json")] | ||||||||||||||||||||||||||||||||||
/// # run_test().unwrap(); | ||||||||||||||||||||||||||||||||||
/// # } | ||||||||||||||||||||||||||||||||||
/// # | ||||||||||||||||||||||||||||||||||
/// # #[cfg(feature = "serde_json")] | ||||||||||||||||||||||||||||||||||
/// # fn run_test() -> QueryResult<()> { | ||||||||||||||||||||||||||||||||||
/// # use diesel::dsl::{sql, jsonb_patch}; | ||||||||||||||||||||||||||||||||||
/// # use serde_json::{json, Value}; | ||||||||||||||||||||||||||||||||||
/// # use diesel::sql_types::{Text, Json, Jsonb, Nullable}; | ||||||||||||||||||||||||||||||||||
/// # let connection = &mut establish_connection(); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let version = diesel::select(sql::<Text>("sqlite_version();")) | ||||||||||||||||||||||||||||||||||
/// .get_result::<String>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// // Querying SQLite version should not fail. | ||||||||||||||||||||||||||||||||||
/// let version_components: Vec<&str> = version.split('.').collect(); | ||||||||||||||||||||||||||||||||||
/// let major: u32 = version_components[0].parse().unwrap(); | ||||||||||||||||||||||||||||||||||
/// let minor: u32 = version_components[1].parse().unwrap(); | ||||||||||||||||||||||||||||||||||
/// let patch: u32 = version_components[2].parse().unwrap(); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// if major > 3 || (major == 3 && minor >= 38) { | ||||||||||||||||||||||||||||||||||
/// /* Valid sqlite version, do nothing */ | ||||||||||||||||||||||||||||||||||
/// } else { | ||||||||||||||||||||||||||||||||||
/// println!("SQLite version is too old, skipping the test."); | ||||||||||||||||||||||||||||||||||
/// return Ok(()); | ||||||||||||||||||||||||||||||||||
/// } | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(jsonb_patch::<Json, _, _>(json!({"a":1,"b":2}), json!({"c":3,"d":4}))) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!({"a":1, "b":2, "c":3, "d":4}), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(jsonb_patch::<Json, _, _>(json!({"a":[1,2],"b":2}), json!({"a":9}))) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!({"a":9,"b":2}), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(jsonb_patch::<Json, _, _>(json!({"a":[1,2],"b":2}), json!({"a":null}))) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!({"b":2}), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(jsonb_patch::<Json, _, _>(json!({"a":1,"b":2}), json!({"a":9,"b":null,"c":8}))) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!({"a":9,"c":8}), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(jsonb_patch::<Json, _, _>(json!({"a":{"x":1,"y":2},"b":3}), json!({"a":{"y":9},"c":8}))) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!({"a":{"x":1,"y":9},"b":3,"c":8}), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(jsonb_patch::<Nullable<Json>, _, _>(None, json!({"a":{"x":1,"y":2},"b":3}))) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!(null), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// let result = diesel::select(jsonb_patch::<Nullable<Json>, _, _>(json!({"a":{"x":1,"y":2},"b":3}), None)) | ||||||||||||||||||||||||||||||||||
/// .get_result::<Value>(connection)?; | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
/// assert_eq!(json!(null), result); | ||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
/// # Ok(()) | ||||||||||||||||||||||||||||||||||
/// # } | ||||||||||||||||||||||||||||||||||
/// ``` | ||||||||||||||||||||||||||||||||||
#[sql_name = "jsonb_patch"] | ||||||||||||||||||||||||||||||||||
#[cfg(feature = "sqlite")] | ||||||||||||||||||||||||||||||||||
fn jsonb_patch<J: JsonOrNullableJson + SingleValue>(j: J, patch: J) -> Jsonb; | ||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to test for
patch_json::<Nullable<Json>, _>(None, json!({something}))
andpatch_json(json!{something}, None)
here. (Also forpatch_jsonb
)