Skip to content

Commit

Permalink
added jsonb_insert
Browse files Browse the repository at this point in the history
  • Loading branch information
Azan Ali authored and Azan Ali committed Sep 12, 2024
1 parent f78e6b8 commit 07f37bc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
46 changes: 46 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! PostgreSQL specific functions

use super::expression_methods::InetOrCidr;
use super::expression_methods::JsonbOrNullableJsonb;
use crate::expression::functions::define_sql_function;
use crate::pg::expression::expression_methods::ArrayOrNullableArray;
use crate::pg::expression::expression_methods::MaybeNullableValue;
Expand Down Expand Up @@ -1634,3 +1635,48 @@ define_sql_function! {
text_array: Arr,
) -> 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
///
///
/// # Example
///
/// ```rust
/// # include!("../../doctest_setup.rs");
/// #
/// # fn main() {
/// # run_test().unwrap();
/// # }
/// #
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::jsonb_insert;
/// # use diesel::sql_types::{Array, Jsonb, Nullable, Text};
/// # use serde_json::{Value, json};
/// # let connection = &mut establish_connection();
/// let result = diesel::select(jsonb_insert::<Jsonb, Array<Text>, _, _, _>(json!([1, 2, 3]), vec!["1"], json!(1)))
/// .get_result::<Value>(connection)?;
/// assert_eq!(json!([1, 1, 2, 3]), result);
///
/// let result = diesel::select(jsonb_insert::<Jsonb, Array<Text>, _, _, _>(json!([1, 2, 3]), Vec::<String>::new(), json!(1)))
/// .get_result::<Value>(connection)?;
/// assert_eq!(json!([1, 2, 3]), result);
///
/// let result = diesel::select(jsonb_insert::<Nullable<Jsonb>, Array<Text>, _, _, _>(Option::<Value>::None, vec!["1"], json!(1)))
/// .get_result::<Option<Value>>(connection)?;
/// assert_eq!(None, result);
///
/// let result = diesel::select(jsonb_insert::<Nullable<Jsonb>, Array<Text>, _, _, _>(json!([1, 2, 3]), vec!["1"], Option::<Value>::None))
/// .get_result::<Option<Value>>(connection)?;
/// assert_eq!(None, result);
///
/// let result = diesel::select(jsonb_insert::<Jsonb, Nullable<Array<Text>>, _, _, _>(json!([1, 2, 3]), None::<Vec<String>>, json!(1)))
/// .get_result::<Value>(connection)?;
/// assert!(result.is_err());
///
/// # Ok(())
/// # }
/// ```
fn jsonb_insert<J: JsonbOrNullableJsonb + MaybeNullableValue<Jsonb>, P: TextArrayOrNullableTextArray + MaybeNullableValue<Jsonb>>(target: J, path: P, value: J) -> J::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 @@ -486,3 +486,9 @@ pub type to_jsonb<E> = super::functions::to_jsonb<SqlTypeOf<E>, E>;
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type json_object<A> = super::functions::json_object<SqlTypeOf<A>, A>;

/// Return type of [`jsonb_insert(target, path, value)`](super::functions::json_object())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type jsonb_insert<T, P, V> =
super::functions::jsonb_insert<SqlTypeOf<T>, SqlTypeOf<P>, T, P, V>;
1 change: 1 addition & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ fn postgres_functions() -> _ {
to_json(pg_extras::id),
to_jsonb(pg_extras::id),
json_object(pg_extras::text_array),
jsonb_insert(pg_extras::jsonb, pg_extras::text_array, pg_extras::jsonb),
)
}

Expand Down

0 comments on commit 07f37bc

Please sign in to comment.