Skip to content

Commit c2d9b2c

Browse files
Azan AliAzan Ali
Azan Ali
authored and
Azan Ali
committed
added jsonb_insert
1 parent 810c3e3 commit c2d9b2c

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

diesel/src/pg/expression/functions.rs

+97
Original file line numberDiff line numberDiff line change
@@ -2078,3 +2078,100 @@ define_sql_function! {
20782078
20792079
fn jsonb_array_length<E: JsonbOrNullableJsonb + MaybeNullableValue<Integer>>(jsonb: E) -> E::Out;
20802080
}
2081+
2082+
#[cfg(feature = "postgres_backend")]
2083+
define_sql_function! {
2084+
/// Returns target with new_value inserted, if the item designated by the path is an array element.
2085+
///
2086+
/// # Example
2087+
///
2088+
/// ```rust
2089+
/// # include!("../../doctest_setup.rs");
2090+
/// #
2091+
/// # fn main() {
2092+
/// # run_test().unwrap();
2093+
/// # }
2094+
/// #
2095+
/// # fn run_test() -> QueryResult<()> {
2096+
/// # use diesel::dsl::jsonb_insert;
2097+
/// # use diesel::sql_types::{Array, Jsonb, Nullable, Text};
2098+
/// # use serde_json::{Value, json};
2099+
/// # let connection = &mut establish_connection();
2100+
/// let result = diesel::select(jsonb_insert::<Jsonb, Array<Text>, _, _, _>(json!([1, 2, 3]), vec!["2"], json!(1)))
2101+
/// .get_result::<Value>(connection)?;
2102+
/// assert_eq!(json!([1, 2, 1, 3]), result);
2103+
///
2104+
/// let result = diesel::select(jsonb_insert::<Jsonb, Array<Text>, _, _, _>(json!([1, 2, 3]), Vec::<String>::new(), json!(1)))
2105+
/// .get_result::<Value>(connection)?;
2106+
/// assert_eq!(json!([1, 2, 3]), result);
2107+
///
2108+
/// let result = diesel::select(jsonb_insert::<Nullable<Jsonb>, Array<Text>, _, _, _>(Option::<Value>::None, vec!["1"], json!(1)))
2109+
/// .get_result::<Option<Value>>(connection)?;
2110+
/// assert_eq!(None, result);
2111+
///
2112+
/// let result = diesel::select(jsonb_insert::<Jsonb, Nullable<Array<Text>>, _, _, _>(json!([1, 2, 3]), None::<Vec<String>>, json!(1)))
2113+
/// .get_result::<Option<Value>>(connection)?;
2114+
/// assert_eq!(None, result);
2115+
///
2116+
/// let result = diesel::select(jsonb_insert::<Nullable<Jsonb>, Array<Text>, _, _, _>(json!([1, 2, 3]), vec!["1"], Option::<Value>::None))
2117+
/// .get_result::<Option<Value>>(connection)?;
2118+
/// assert_eq!(None, result);
2119+
///
2120+
///
2121+
/// # Ok(())
2122+
/// # }
2123+
/// ```
2124+
fn jsonb_insert<J: JsonbOrNullableJsonb + SingleValue, P: TextArrayOrNullableTextArray + CombinedNullableValue<J, Jsonb>>(target: J, path: P, value: J) -> P::Out;
2125+
}
2126+
2127+
#[cfg(feature = "postgres_backend")]
2128+
define_sql_function! {
2129+
/// Returns target with new_value inserted, if the item designated by the path is an array element.
2130+
/// If insert_after is true, the value is inserted after the path specified
2131+
///
2132+
///
2133+
/// # Example
2134+
///
2135+
/// ```rust
2136+
/// # include!("../../doctest_setup.rs");
2137+
/// #
2138+
/// # fn main() {
2139+
/// # run_test().unwrap();
2140+
/// # }
2141+
/// #
2142+
/// # fn run_test() -> QueryResult<()> {
2143+
/// # use diesel::dsl::jsonb_insert_with_option_after;
2144+
/// # use diesel::sql_types::{Array, Jsonb, Nullable, Text};
2145+
/// # use serde_json::{Value, json};
2146+
/// # let connection = &mut establish_connection();
2147+
/// let result = diesel::select(jsonb_insert_with_option_after::<Jsonb, Array<Text>, _, _, _, _>(json!([1, 2, 3]), vec!["2"], json!(1), true))
2148+
/// .get_result::<Value>(connection)?;
2149+
/// assert_eq!(json!([1, 2, 3, 1]), result);
2150+
///
2151+
/// let result = diesel::select(jsonb_insert_with_option_after::<Jsonb, Array<Text>, _, _, _, _>(json!([1, 2, 3]), vec!["2"], json!(1), false))
2152+
/// .get_result::<Value>(connection)?;
2153+
/// assert_eq!(json!([1, 2, 1, 3]), result);
2154+
///
2155+
/// let result = diesel::select(jsonb_insert_with_option_after::<Jsonb, Array<Text>, _, _, _, _>(json!([1, 2, 3]), Vec::<String>::new(), json!(1), true))
2156+
/// .get_result::<Value>(connection)?;
2157+
/// assert_eq!(json!([1, 2, 3]), result);
2158+
///
2159+
/// let result = diesel::select(jsonb_insert_with_option_after::<Nullable<Jsonb>, Array<Text>, _, _, _, _>(Option::<Value>::None, vec!["1"], json!(1), true))
2160+
/// .get_result::<Option<Value>>(connection)?;
2161+
/// assert_eq!(None, result);
2162+
///
2163+
/// let result = diesel::select(jsonb_insert_with_option_after::<Jsonb, Nullable<Array<Text>>, _, _, _, _>(json!([1, 2, 3]), None::<Vec<String>>, json!(1), true))
2164+
/// .get_result::<Option<Value>>(connection)?;
2165+
/// assert_eq!(None, result);
2166+
///
2167+
/// let result = diesel::select(jsonb_insert_with_option_after::<Nullable<Jsonb>, Array<Text>, _, _, _, _>(json!([1, 2, 3]), vec!["1"], Option::<Value>::None, true))
2168+
/// .get_result::<Option<Value>>(connection)?;
2169+
/// assert_eq!(None, result);
2170+
///
2171+
///
2172+
/// # Ok(())
2173+
/// # }
2174+
/// ```
2175+
#[sql_name = "jsonb_insert"]
2176+
fn jsonb_insert_with_option_after<J: JsonbOrNullableJsonb + SingleValue, P: TextArrayOrNullableTextArray + CombinedNullableValue<J, Jsonb>>(target: J, path: P, value: J, insert_after: Bool) -> P::Out;
2177+
}

diesel/src/pg/expression/helper_types.rs

+12
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,15 @@ pub type json_array_length<E> = super::functions::json_array_length<SqlTypeOf<E>
527527
#[allow(non_camel_case_types)]
528528
#[cfg(feature = "postgres_backend")]
529529
pub type jsonb_array_length<E> = super::functions::jsonb_array_length<SqlTypeOf<E>, E>;
530+
531+
/// Return type of [`jsonb_insert(target, path, value)`](super::functions::jsonb_insert())
532+
#[allow(non_camel_case_types)]
533+
#[cfg(feature = "postgres_backend")]
534+
pub type jsonb_insert<T, P, V> =
535+
super::functions::jsonb_insert<SqlTypeOf<T>, SqlTypeOf<P>, T, P, V>;
536+
537+
/// Return type of [`jsonb_insert(target, path, value, insert_after)`](super::functions::jsonb_insert_with_option_after())
538+
#[allow(non_camel_case_types)]
539+
#[cfg(feature = "postgres_backend")]
540+
pub type jsonb_insert_with_option_after<T, P, V, I> =
541+
super::functions::jsonb_insert_with_option_after<SqlTypeOf<T>, SqlTypeOf<P>, T, P, V, I>;

diesel_derives/tests/auto_type.rs

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ table! {
4242
table! {
4343
pg_extras(id) {
4444
id -> Integer,
45+
boolean -> Bool,
4546
json -> Json,
4647
jsonb -> Jsonb,
4748
net -> Inet,
@@ -446,6 +447,13 @@ fn postgres_functions() -> _ {
446447
jsonb_strip_nulls(pg_extras::jsonb),
447448
json_array_length(pg_extras::json),
448449
jsonb_array_length(pg_extras::jsonb),
450+
jsonb_insert(pg_extras::jsonb, pg_extras::text_array, pg_extras::jsonb),
451+
jsonb_insert_with_option_after(
452+
pg_extras::jsonb,
453+
pg_extras::text_array,
454+
pg_extras::jsonb,
455+
pg_extras::boolean,
456+
),
449457
)
450458
}
451459

0 commit comments

Comments
 (0)