Skip to content

Commit 2a9e3df

Browse files
Azan AliAzan Ali
Azan Ali
authored and
Azan Ali
committed
added jsonb_insert
1 parent ecf6e92 commit 2a9e3df

File tree

3 files changed

+119
-2
lines changed

3 files changed

+119
-2
lines changed

diesel/src/pg/expression/functions.rs

+99-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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::CombinedNullableValue;
78
use crate::pg::expression::expression_methods::JsonOrNullableJson;
8-
use crate::pg::expression::expression_methods::JsonbOrNullableJsonb;
99
use crate::pg::expression::expression_methods::MaybeNullableValue;
1010
use crate::pg::expression::expression_methods::MultirangeOrNullableMultirange;
1111
use crate::pg::expression::expression_methods::MultirangeOrRangeMaybeNullable;
@@ -1692,7 +1692,7 @@ define_sql_function! {
16921692
/// let json = diesel::select(json_object_with_keys_and_values::<Array<Text>, Array<Text>, _, _>(
16931693
/// vec!["hello","John"], empty))
16941694
/// .get_result::<Value>(connection);
1695-
/// assert!(json.is_err());
1695+
/// assert!(json.is_err())
16961696
///
16971697
/// # Ok(())
16981698
/// # }
@@ -1992,3 +1992,100 @@ define_sql_function! {
19921992
/// ```
19931993
fn jsonb_strip_nulls<E: JsonbOrNullableJsonb + SingleValue>(jsonb: E) -> E;
19941994
}
1995+
1996+
#[cfg(feature = "postgres_backend")]
1997+
define_sql_function! {
1998+
/// Returns target with new_value inserted, if the item designated by the path is an array element.
1999+
///
2000+
/// # Example
2001+
///
2002+
/// ```rust
2003+
/// # include!("../../doctest_setup.rs");
2004+
/// #
2005+
/// # fn main() {
2006+
/// # run_test().unwrap();
2007+
/// # }
2008+
/// #
2009+
/// # fn run_test() -> QueryResult<()> {
2010+
/// # use diesel::dsl::jsonb_insert;
2011+
/// # use diesel::sql_types::{Array, Jsonb, Nullable, Text};
2012+
/// # use serde_json::{Value, json};
2013+
/// # let connection = &mut establish_connection();
2014+
/// let result = diesel::select(jsonb_insert::<Jsonb, Array<Text>, _, _, _>(json!([1, 2, 3]), vec!["2"], json!(1)))
2015+
/// .get_result::<Value>(connection)?;
2016+
/// assert_eq!(json!([1, 2, 1, 3]), result);
2017+
///
2018+
/// let result = diesel::select(jsonb_insert::<Jsonb, Array<Text>, _, _, _>(json!([1, 2, 3]), Vec::<String>::new(), json!(1)))
2019+
/// .get_result::<Value>(connection)?;
2020+
/// assert_eq!(json!([1, 2, 3]), result);
2021+
///
2022+
/// let result = diesel::select(jsonb_insert::<Nullable<Jsonb>, Array<Text>, _, _, _>(Option::<Value>::None, vec!["1"], json!(1)))
2023+
/// .get_result::<Option<Value>>(connection)?;
2024+
/// assert_eq!(None, result);
2025+
///
2026+
/// let result = diesel::select(jsonb_insert::<Jsonb, Nullable<Array<Text>>, _, _, _>(json!([1, 2, 3]), None::<Vec<String>>, json!(1)))
2027+
/// .get_result::<Option<Value>>(connection)?;
2028+
/// assert_eq!(None, result);
2029+
///
2030+
/// let result = diesel::select(jsonb_insert::<Nullable<Jsonb>, Array<Text>, _, _, _>(json!([1, 2, 3]), vec!["1"], Option::<Value>::None))
2031+
/// .get_result::<Option<Value>>(connection)?;
2032+
/// assert_eq!(None, result);
2033+
///
2034+
///
2035+
/// # Ok(())
2036+
/// # }
2037+
/// ```
2038+
fn jsonb_insert<J: JsonbOrNullableJsonb + SingleValue, P: TextArrayOrNullableTextArray + CombinedNullableValue<J, Jsonb>>(target: J, path: P, value: J) -> P::Out;
2039+
}
2040+
2041+
#[cfg(feature = "postgres_backend")]
2042+
define_sql_function! {
2043+
/// Returns target with new_value inserted, if the item designated by the path is an array element.
2044+
/// If insert_after is true, the value is inserted after the path specified
2045+
///
2046+
///
2047+
/// # Example
2048+
///
2049+
/// ```rust
2050+
/// # include!("../../doctest_setup.rs");
2051+
/// #
2052+
/// # fn main() {
2053+
/// # run_test().unwrap();
2054+
/// # }
2055+
/// #
2056+
/// # fn run_test() -> QueryResult<()> {
2057+
/// # use diesel::dsl::jsonb_insert_with_option_after;
2058+
/// # use diesel::sql_types::{Array, Jsonb, Nullable, Text};
2059+
/// # use serde_json::{Value, json};
2060+
/// # let connection = &mut establish_connection();
2061+
/// let result = diesel::select(jsonb_insert_with_option_after::<Jsonb, Array<Text>, _, _, _, _>(json!([1, 2, 3]), vec!["2"], json!(1), true))
2062+
/// .get_result::<Value>(connection)?;
2063+
/// assert_eq!(json!([1, 2, 3, 1]), result);
2064+
///
2065+
/// let result = diesel::select(jsonb_insert_with_option_after::<Jsonb, Array<Text>, _, _, _, _>(json!([1, 2, 3]), vec!["2"], json!(1), false))
2066+
/// .get_result::<Value>(connection)?;
2067+
/// assert_eq!(json!([1, 2, 1, 3]), result);
2068+
///
2069+
/// let result = diesel::select(jsonb_insert_with_option_after::<Jsonb, Array<Text>, _, _, _, _>(json!([1, 2, 3]), Vec::<String>::new(), json!(1), true))
2070+
/// .get_result::<Value>(connection)?;
2071+
/// assert_eq!(json!([1, 2, 3]), result);
2072+
///
2073+
/// let result = diesel::select(jsonb_insert_with_option_after::<Nullable<Jsonb>, Array<Text>, _, _, _, _>(Option::<Value>::None, vec!["1"], json!(1), true))
2074+
/// .get_result::<Option<Value>>(connection)?;
2075+
/// assert_eq!(None, result);
2076+
///
2077+
/// let result = diesel::select(jsonb_insert_with_option_after::<Jsonb, Nullable<Array<Text>>, _, _, _, _>(json!([1, 2, 3]), None::<Vec<String>>, json!(1), true))
2078+
/// .get_result::<Option<Value>>(connection)?;
2079+
/// assert_eq!(None, result);
2080+
///
2081+
/// let result = diesel::select(jsonb_insert_with_option_after::<Nullable<Jsonb>, Array<Text>, _, _, _, _>(json!([1, 2, 3]), vec!["1"], Option::<Value>::None, true))
2082+
/// .get_result::<Option<Value>>(connection)?;
2083+
/// assert_eq!(None, result);
2084+
///
2085+
///
2086+
/// # Ok(())
2087+
/// # }
2088+
/// ```
2089+
#[sql_name = "jsonb_insert"]
2090+
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;
2091+
}

diesel/src/pg/expression/helper_types.rs

+12
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,15 @@ pub type json_strip_nulls<E> = super::functions::json_strip_nulls<SqlTypeOf<E>,
517517
#[allow(non_camel_case_types)]
518518
#[cfg(feature = "postgres_backend")]
519519
pub type jsonb_strip_nulls<E> = super::functions::jsonb_strip_nulls<SqlTypeOf<E>, E>;
520+
521+
/// Return type of [`jsonb_insert(target, path, value)`](super::functions::jsonb_insert())
522+
#[allow(non_camel_case_types)]
523+
#[cfg(feature = "postgres_backend")]
524+
pub type jsonb_insert<T, P, V> =
525+
super::functions::jsonb_insert<SqlTypeOf<T>, SqlTypeOf<P>, T, P, V>;
526+
527+
/// Return type of [`jsonb_insert(target, path, value, insert_after)`](super::functions::jsonb_insert_with_option_after())
528+
#[allow(non_camel_case_types)]
529+
#[cfg(feature = "postgres_backend")]
530+
pub type jsonb_insert_with_option_after<T, P, V, I> =
531+
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,
@@ -444,6 +445,13 @@ fn postgres_functions() -> _ {
444445
jsonb_pretty(pg_extras::jsonb),
445446
json_strip_nulls(pg_extras::json),
446447
jsonb_strip_nulls(pg_extras::jsonb),
448+
jsonb_insert(pg_extras::jsonb, pg_extras::text_array, pg_extras::jsonb),
449+
jsonb_insert_with_option_after(
450+
pg_extras::jsonb,
451+
pg_extras::text_array,
452+
pg_extras::jsonb,
453+
pg_extras::boolean,
454+
),
447455
)
448456
}
449457

0 commit comments

Comments
 (0)