Skip to content

Commit 07f37bc

Browse files
Azan AliAzan Ali
Azan Ali
authored and
Azan Ali
committed
added jsonb_insert
1 parent f78e6b8 commit 07f37bc

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

diesel/src/pg/expression/functions.rs

+46
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! PostgreSQL specific functions
22
33
use super::expression_methods::InetOrCidr;
4+
use super::expression_methods::JsonbOrNullableJsonb;
45
use crate::expression::functions::define_sql_function;
56
use crate::pg::expression::expression_methods::ArrayOrNullableArray;
67
use crate::pg::expression::expression_methods::MaybeNullableValue;
@@ -1634,3 +1635,48 @@ define_sql_function! {
16341635
text_array: Arr,
16351636
) -> Arr::Out;
16361637
}
1638+
1639+
#[cfg(feature = "postgres_backend")]
1640+
define_sql_function! {
1641+
/// Returns target with new_value inserted. If the item designated by the path is an array element
1642+
///
1643+
///
1644+
/// # Example
1645+
///
1646+
/// ```rust
1647+
/// # include!("../../doctest_setup.rs");
1648+
/// #
1649+
/// # fn main() {
1650+
/// # run_test().unwrap();
1651+
/// # }
1652+
/// #
1653+
/// # fn run_test() -> QueryResult<()> {
1654+
/// # use diesel::dsl::jsonb_insert;
1655+
/// # use diesel::sql_types::{Array, Jsonb, Nullable, Text};
1656+
/// # use serde_json::{Value, json};
1657+
/// # let connection = &mut establish_connection();
1658+
/// let result = diesel::select(jsonb_insert::<Jsonb, Array<Text>, _, _, _>(json!([1, 2, 3]), vec!["1"], json!(1)))
1659+
/// .get_result::<Value>(connection)?;
1660+
/// assert_eq!(json!([1, 1, 2, 3]), result);
1661+
///
1662+
/// let result = diesel::select(jsonb_insert::<Jsonb, Array<Text>, _, _, _>(json!([1, 2, 3]), Vec::<String>::new(), json!(1)))
1663+
/// .get_result::<Value>(connection)?;
1664+
/// assert_eq!(json!([1, 2, 3]), result);
1665+
///
1666+
/// let result = diesel::select(jsonb_insert::<Nullable<Jsonb>, Array<Text>, _, _, _>(Option::<Value>::None, vec!["1"], json!(1)))
1667+
/// .get_result::<Option<Value>>(connection)?;
1668+
/// assert_eq!(None, result);
1669+
///
1670+
/// let result = diesel::select(jsonb_insert::<Nullable<Jsonb>, Array<Text>, _, _, _>(json!([1, 2, 3]), vec!["1"], Option::<Value>::None))
1671+
/// .get_result::<Option<Value>>(connection)?;
1672+
/// assert_eq!(None, result);
1673+
///
1674+
/// let result = diesel::select(jsonb_insert::<Jsonb, Nullable<Array<Text>>, _, _, _>(json!([1, 2, 3]), None::<Vec<String>>, json!(1)))
1675+
/// .get_result::<Value>(connection)?;
1676+
/// assert!(result.is_err());
1677+
///
1678+
/// # Ok(())
1679+
/// # }
1680+
/// ```
1681+
fn jsonb_insert<J: JsonbOrNullableJsonb + MaybeNullableValue<Jsonb>, P: TextArrayOrNullableTextArray + MaybeNullableValue<Jsonb>>(target: J, path: P, value: J) -> J::Out;
1682+
}

diesel/src/pg/expression/helper_types.rs

+6
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,9 @@ pub type to_jsonb<E> = super::functions::to_jsonb<SqlTypeOf<E>, E>;
486486
#[allow(non_camel_case_types)]
487487
#[cfg(feature = "postgres_backend")]
488488
pub type json_object<A> = super::functions::json_object<SqlTypeOf<A>, A>;
489+
490+
/// Return type of [`jsonb_insert(target, path, value)`](super::functions::json_object())
491+
#[allow(non_camel_case_types)]
492+
#[cfg(feature = "postgres_backend")]
493+
pub type jsonb_insert<T, P, V> =
494+
super::functions::jsonb_insert<SqlTypeOf<T>, SqlTypeOf<P>, T, P, V>;

diesel_derives/tests/auto_type.rs

+1
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ fn postgres_functions() -> _ {
438438
to_json(pg_extras::id),
439439
to_jsonb(pg_extras::id),
440440
json_object(pg_extras::text_array),
441+
jsonb_insert(pg_extras::jsonb, pg_extras::text_array, pg_extras::jsonb),
441442
)
442443
}
443444

0 commit comments

Comments
 (0)