Skip to content
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

feat(pg): add jsonb_insert fn #4434

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
59 changes: 59 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2666,3 +2666,62 @@ define_sql_function! {
Arr: TextArrayOrNullableTextArray + CombinedNullableValue<E,Jsonb>,
>(base: E, path: Arr, new_value: E, create_if_missing: Bool, null_value_treatment: NullValueTreatmentEnum) -> Arr::Out;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Returns target with new_value inserted.
///
/// If the item designated by the path is an array element, new_value will be inserted before that item if insert_after is false (which is the default),
/// or after it if insert_after is true.
///
/// If the item designated by the path is an object field, new_value will be inserted only if the object does not already contain that key.
///
/// All earlier steps in the path must exist, or the target is returned unchanged.
/// As with the path oriented operators, negative integers that appear in the path count from the end of JSON arrays.
/// If the last path step is an array index that is out of range,
/// the new value is added at the beginning of the array if the index is negative,
/// or at the end of the array if it is positive.
///
/// # 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::jsonb_insert;
/// # use diesel::sql_types::{Jsonb, Array, Json, Nullable, Text};
/// # use serde_json::{json,Value};
/// # let connection = &mut establish_connection();
///
/// let result = diesel::select(jsonb_insert::<Jsonb, Array<Text>, _, _, _, _>(
/// json!({"a":[0,1,2]}),
/// vec!["a","1"],
/// json!("new_value"),
/// false
/// )).get_result::<Value>(connection)?;
/// let expected: Value = json!({"a":[0,"new_value",1,2]});
/// assert_eq!(result, expected);
///
/// let result = diesel::select(jsonb_insert::<Jsonb, Array<Text>, _, _, _, _>(
/// json!({"a":[0,1,2]}),
/// vec!["a","1"],
/// json!("new_value"),
/// true
/// )).get_result::<Value>(connection)?;
/// let expected: Value = json!({"a":[0,1,"new_value",2,]});
/// assert_eq!(result, expected);
///
/// # Ok(())
/// # }
/// ```
fn jsonb_insert<
E: JsonbOrNullableJsonb + SingleValue,
Arr: TextArrayOrNullableTextArray + CombinedNullableValue<E,Jsonb>,
>(base: E, path: Arr, new_value: E, insert_after: Bool) -> Arr::Out;
}
6 changes: 6 additions & 0 deletions diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,9 @@ pub type jsonb_set_create_if_missing<B, J, R, C> =
#[cfg(feature = "postgres_backend")]
pub type jsonb_set_lax<B, J, R, C, E> =
super::functions::jsonb_set_lax<SqlTypeOf<B>, SqlTypeOf<J>, B, J, R, C, E>;

/// Return type of [`jsonb_insert(base, path, new_value, insert_after)`](super::functions::jsonb_insert())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type jsonb_insert<B, J, R, I> =
super::functions::jsonb_insert<SqlTypeOf<B>, SqlTypeOf<J>, B, J, R, I>;
6 changes: 6 additions & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,12 @@ fn postgres_functions() -> _ {
pg_extras::boolean,
null_value_treatment,
),
jsonb_insert(
pg_extras::jsonb,
pg_extras::text_array,
pg_extras::jsonb,
pg_extras::boolean,
),
)
}

Expand Down
Loading