Skip to content

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions diesel/src/sqlite/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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})))
Copy link
Member

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})) and patch_json(json!{something}, None) here. (Also for patch_jsonb)

/// .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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type is not correct. This function (and also jsonb_patch) returns Null if either the first or second argument is nullable.

We might want something like

Suggested change
fn json_patch<J: JsonOrNullableJson + SingleValue>(j: J, patch: J) -> Json;
fn json_patch<J1: JsonOrNullableJson + SingleValue + CombinedNullableValue<J2, Json>, J2: JsonOrNullableJson + SingleValue>(j: J1, patch: J1) -> J1::Out;

with the CombinedNullableValue trait defined here:

pub trait CombinedNullableValue<O, Out>: SingleValue {
type Out: SingleValue;
}
impl<T, O, Out> CombinedNullableValue<O, Out> for T
where
T: SingleValue,
O: SingleValue,
T::IsNull: OneIsNullable<O::IsNull>,
<T::IsNull as OneIsNullable<O::IsNull>>::Out: MaybeNullableType<Out>,
<<T::IsNull as OneIsNullable<O::IsNull>>::Out as MaybeNullableType<Out>>::Out: SingleValue,
{
type Out = <<T::IsNull as OneIsNullable<O::IsNull>>::Out as MaybeNullableType<Out>>::Out;
}

(That needs to be moved to diesel/src/expressions/nullable.rs into a pub(crate) private module.


/// 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;
}
10 changes: 10 additions & 0 deletions diesel/src/sqlite/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ pub type json_type_with_path<J, P> = super::functions::json_type_with_path<SqlTy
#[allow(non_camel_case_types)]
#[cfg(feature = "sqlite")]
pub type json_quote<J> = super::functions::json_quote<SqlTypeOf<J>, J>;

/// Return type of [`json_patch(json, json)`](super::functions::json_patch())
#[allow(non_camel_case_types)]
#[cfg(feature = "sqlite")]
pub type json_patch<J, P> = super::functions::json_patch<SqlTypeOf<J>, J, P>;

/// Return type of [`jsonb_patch(json, json)`](super::functions::jsonb_patch())
#[allow(non_camel_case_types)]
#[cfg(feature = "sqlite")]
pub type jsonb_patch<J, P> = super::functions::jsonb_patch<SqlTypeOf<J>, J, P>;
2 changes: 2 additions & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ fn sqlite_functions() -> _ {
json_type(sqlite_extras::json),
json_type_with_path(sqlite_extras::json, sqlite_extras::text),
json_quote(sqlite_extras::json),
json_patch(sqlite_extras::json, sqlite_extras::json),
jsonb_patch(sqlite_extras::json, sqlite_extras::json),
)
}

Expand Down