//! PostgreSQL specific expression methods pub(in crate::pg) use self::private::{ ArrayOrNullableArray, CombinedAllNullableValue, InetOrCidr, JsonIndex, JsonOrNullableJson, JsonOrNullableJsonOrJsonbOrNullableJsonb, JsonRemoveIndex, JsonbOrNullableJsonb, MaybeNullableValue, MultirangeOrNullableMultirange, MultirangeOrRangeMaybeNullable, RangeOrMultirange, RangeOrNullableRange, RecordOrNullableRecord, TextArrayOrNullableTextArray, TextOrNullableText, }; use super::date_and_time::{AtTimeZone, DateTimeLike}; use super::operators::*; use crate::dsl; use crate::expression::grouped::Grouped; use crate::expression::operators::{Asc, Concat, Desc, Like, NotLike}; use crate::expression::{AsExpression, Expression, IntoSql, TypedExpressionType}; use crate::pg::expression::expression_methods::private::BinaryOrNullableBinary; use crate::sql_types::{Array, Inet, Integer, Range, SqlType, Text, VarChar}; use crate::EscapeExpressionMethods; /// PostgreSQL specific methods which are present on all expressions. #[cfg(feature = "postgres_backend")] pub trait PgExpressionMethods: Expression + Sized { /// Creates a PostgreSQL `IS NOT DISTINCT FROM` expression. /// /// This behaves identically to the `=` operator, except that `NULL` is /// treated as a normal value. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # use schema::users::dsl::*; /// # let connection = &mut establish_connection(); /// let distinct = users.select(id).filter(name.is_distinct_from("Sean")); /// let not_distinct = users.select(id).filter(name.is_not_distinct_from("Sean")); /// assert_eq!(Ok(2), distinct.first(connection)); /// assert_eq!(Ok(1), not_distinct.first(connection)); /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_not_distinct_from(self, other: T) -> dsl::IsNotDistinctFrom where Self::SqlType: SqlType, T: AsExpression, { Grouped(IsNotDistinctFrom::new(self, other.as_expression())) } /// Creates a PostgreSQL `IS DISTINCT FROM` expression. /// /// This behaves identically to the `!=` operator, except that `NULL` is /// treated as a normal value. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # use schema::users::dsl::*; /// # let connection = &mut establish_connection(); /// let distinct = users.select(id).filter(name.is_distinct_from("Sean")); /// let not_distinct = users.select(id).filter(name.is_not_distinct_from("Sean")); /// assert_eq!(Ok(2), distinct.first(connection)); /// assert_eq!(Ok(1), not_distinct.first(connection)); /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_distinct_from(self, other: T) -> dsl::IsDistinctFrom where Self::SqlType: SqlType, T: AsExpression, { Grouped(IsDistinctFrom::new(self, other.as_expression())) } /// Creates a PostgreSQL `<@` expression. /// /// This operator returns true whether a element is contained by a range /// /// This operator evaluates to true for the following cases: /// /// ```text /// self: | /// other: [-----] /// ``` /// /// ```text /// self: | /// other: [----] /// ``` /// /// ```text /// self: | /// other: [----] /// ``` /// /// This operator evaluates to false for the following cases: /// /// ```text /// self: | /// other: [-----] /// ``` /// /// ```text /// self: | /// other: [----] /// ``` /// /// ```text /// self: | /// other: [----) /// ``` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # posts { /// # id -> Integer, /// # versions -> Range, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use std::collections::Bound; /// # use diesel::dsl::int4range; /// # use diesel::sql_types::Integer; /// # /// # let conn = &mut establish_connection(); /// # /// let my_range = int4range( /// 1, /// 5, /// diesel::sql_types::RangeBound::LowerBoundInclusiveUpperBoundExclusive, /// ); /// /// let (first, second) = diesel::select(( /// 4.into_sql::().is_contained_by_range(my_range), /// 10.into_sql::().is_contained_by_range(my_range), /// )) /// .get_result::<(bool, bool)>(conn)?; /// /// assert_eq!(first, true); /// assert_eq!(second, false); /// # /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_contained_by_range(self, other: T) -> dsl::IsContainedByRange where Self::SqlType: SqlType, T: AsExpression>, { Grouped(IsContainedBy::new(self, other.as_expression())) } } impl PgExpressionMethods for T {} /// PostgreSQL specific methods present on timestamp expressions. #[cfg(feature = "postgres_backend")] pub trait PgTimestampExpressionMethods: Expression + Sized { /// Creates a PostgreSQL "AT TIME ZONE" expression. /// /// When this is called on a `TIMESTAMP WITHOUT TIME ZONE` column, /// the value will be treated as if were in the given time zone, /// and then converted to UTC. /// /// When this is called on a `TIMESTAMP WITH TIME ZONE` column, /// the value will be converted to the given time zone, /// and then have its time zone information removed. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # timestamps (timestamp) { /// # timestamp -> Timestamp, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # #[cfg(all(feature = "postgres", feature = "chrono"))] /// # fn run_test() -> QueryResult<()> { /// # use self::timestamps::dsl::*; /// # use chrono::*; /// # let connection = &mut establish_connection(); /// # diesel::sql_query("CREATE TABLE timestamps (\"timestamp\" /// # timestamp NOT NULL)").execute(connection)?; /// let christmas_morning = NaiveDate::from_ymd(2017, 12, 25).and_hms(8, 0, 0); /// diesel::insert_into(timestamps) /// .values(timestamp.eq(christmas_morning)) /// .execute(connection)?; /// /// let utc_time = timestamps /// .select(timestamp.at_time_zone("UTC")) /// .first(connection)?; /// assert_eq!(christmas_morning, utc_time); /// /// let eastern_time = timestamps /// .select(timestamp.at_time_zone("EST")) /// .first(connection)?; /// let five_hours_later = christmas_morning + Duration::hours(5); /// assert_eq!(five_hours_later, eastern_time); /// # Ok(()) /// # } /// # /// # #[cfg(not(all(feature = "postgres", feature = "chrono")))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn at_time_zone(self, timezone: T) -> dsl::AtTimeZone where T: AsExpression, { Grouped(AtTimeZone::new(self, timezone.as_expression())) } } impl PgTimestampExpressionMethods for T where T::SqlType: DateTimeLike {} /// PostgreSQL specific methods present on array expressions. #[cfg(feature = "postgres_backend")] pub trait PgArrayExpressionMethods: Expression + Sized { /// Creates a PostgreSQL `&&` expression. /// /// This operator returns whether two arrays have common elements. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # posts { /// # id -> Integer, /// # tags -> Array, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::posts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS posts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE posts (id SERIAL PRIMARY KEY, tags TEXT[] NOT NULL)") /// # .execute(conn) /// # .unwrap(); /// # /// diesel::insert_into(posts) /// .values(&vec![ /// tags.eq(vec!["cool", "awesome"]), /// tags.eq(vec!["awesome", "great"]), /// tags.eq(vec!["cool", "great"]), /// ]) /// .execute(conn)?; /// /// let data = posts.select(id) /// .filter(tags.overlaps_with(vec!["horrid", "cool"])) /// .load::(conn)?; /// assert_eq!(vec![1, 3], data); /// /// let data = posts.select(id) /// .filter(tags.overlaps_with(vec!["cool", "great"])) /// .load::(conn)?; /// assert_eq!(vec![1, 2, 3], data); /// /// let data = posts.select(id) /// .filter(tags.overlaps_with(vec!["horrid"])) /// .load::(conn)?; /// assert!(data.is_empty()); /// # Ok(()) /// # } /// ``` fn overlaps_with(self, other: T) -> dsl::OverlapsWith where Self::SqlType: SqlType, T: AsExpression, { Grouped(OverlapsWith::new(self, other.as_expression())) } /// Creates a PostgreSQL `@>` expression. /// /// This operator returns whether an array contains another array. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # posts { /// # id -> Integer, /// # tags -> Array, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::posts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS posts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE posts (id SERIAL PRIMARY KEY, tags TEXT[] NOT NULL)") /// # .execute(conn) /// # .unwrap(); /// # /// diesel::insert_into(posts) /// .values(tags.eq(vec!["cool", "awesome"])) /// .execute(conn)?; /// /// let cool_posts = posts.select(id) /// .filter(tags.contains(vec!["cool"])) /// .load::(conn)?; /// assert_eq!(vec![1], cool_posts); /// /// let amazing_posts = posts.select(id) /// .filter(tags.contains(vec!["cool", "amazing"])) /// .load::(conn)?; /// assert!(amazing_posts.is_empty()); /// # Ok(()) /// # } /// ``` fn contains(self, other: T) -> dsl::Contains where Self::SqlType: SqlType, T: AsExpression, { Grouped(Contains::new(self, other.as_expression())) } /// Creates a PostgreSQL `<@` expression. /// /// This operator returns whether an array is contained by another array. /// `foo.contains(bar)` is the same as `bar.is_contained_by(foo)` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # posts { /// # id -> Integer, /// # tags -> Array, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::posts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS posts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE posts (id SERIAL PRIMARY KEY, tags TEXT[] NOT NULL)") /// # .execute(conn) /// # .unwrap(); /// # /// diesel::insert_into(posts) /// .values(tags.eq(vec!["cool", "awesome"])) /// .execute(conn)?; /// /// let data = posts.select(id) /// .filter(tags.is_contained_by(vec!["cool", "awesome", "amazing"])) /// .load::(conn)?; /// assert_eq!(vec![1], data); /// /// let data = posts.select(id) /// .filter(tags.is_contained_by(vec!["cool"])) /// .load::(conn)?; /// assert!(data.is_empty()); /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_contained_by(self, other: T) -> dsl::IsContainedBy where Self::SqlType: SqlType, T: AsExpression, { Grouped(IsContainedBy::new(self, other.as_expression())) } /// Indexes a PostgreSQL array. /// /// This operator indexes in to an array to access a single element. /// /// Note that PostgreSQL arrays are 1-indexed, so `foo.index(1)` is the /// first element in the array. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # posts { /// # id -> Integer, /// # tags -> Array, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::posts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS posts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE posts (id SERIAL PRIMARY KEY, tags TEXT[] NOT NULL)") /// # .execute(conn) /// # .unwrap(); /// # /// diesel::insert_into(posts) /// .values(&vec![ /// tags.eq(vec!["cool", "awesome"]), /// tags.eq(vec!["splendid", "marvellous"]), /// ]) /// .execute(conn)?; /// /// let data = posts.select(tags.index(id)) /// .load::(conn)?; /// assert_eq!(vec!["cool", "marvellous"], data); /// /// let data = posts.select(id) /// .filter(tags.index(1).eq("splendid")) /// .load::(conn)?; /// assert_eq!(vec![2], data); /// # Ok(()) /// # } /// ``` fn index(self, other: T) -> dsl::Index where Self::SqlType: SqlType, T: AsExpression, { ArrayIndex::new(self, other.as_expression()) } /// Creates a PostgreSQL `||` expression. /// /// This operator concatenates two Array values and returns Array value /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # posts { /// # id -> Integer, /// # tags -> Array, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::posts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS posts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE posts (id SERIAL PRIMARY KEY, tags TEXT[] NOT NULL)") /// # .execute(conn) /// # .unwrap(); /// # /// diesel::insert_into(posts) /// .values(tags.eq(vec!["cool", "awesome"])) /// .execute(conn)?; /// /// let res = posts.select(tags.concat(vec!["amazing"])).load::>(conn)?; /// let expected_tags = vec!["cool", "awesome", "amazing"]; /// assert_eq!(expected_tags, res[0]); /// # Ok(()) /// # } fn concat(self, other: T) -> dsl::Concat where Self::SqlType: SqlType, T: AsExpression, { Grouped(Concat::new(self, other.as_expression())) } } impl PgArrayExpressionMethods for T where T: Expression, T::SqlType: ArrayOrNullableArray, { } /// PostgreSQL expression methods related to sorting. /// /// This trait is only implemented for `Asc` and `Desc`. Although `.asc` is /// implicit if no order is given, you will need to call `.asc()` explicitly in /// order to call these methods. #[cfg(feature = "postgres_backend")] pub trait PgSortExpressionMethods: Sized { /// Specify that nulls should come before other values in this ordering. /// /// Normally, nulls come last when sorting in ascending order and first /// when sorting in descending order. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # nullable_numbers (nullable_number) { /// # nullable_number -> Nullable, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::nullable_numbers::dsl::*; /// # let connection = &mut connection_no_data(); /// # diesel::sql_query("CREATE TABLE nullable_numbers (nullable_number INTEGER)").execute(connection)?; /// diesel::insert_into(nullable_numbers) /// .values(&vec![ /// nullable_number.eq(None), /// nullable_number.eq(Some(1)), /// nullable_number.eq(Some(2)), /// ]) /// .execute(connection)?; /// /// let asc_default_nulls = nullable_numbers.select(nullable_number) /// .order(nullable_number.asc()) /// .load::>(connection)?; /// assert_eq!(vec![Some(1), Some(2), None], asc_default_nulls); /// /// let asc_nulls_first = nullable_numbers.select(nullable_number) /// .order(nullable_number.asc().nulls_first()) /// .load::>(connection)?; /// assert_eq!(vec![None, Some(1), Some(2)], asc_nulls_first); /// # Ok(()) /// # } /// ``` fn nulls_first(self) -> dsl::NullsFirst { NullsFirst::new(self) } /// Specify that nulls should come after other values in this ordering. /// /// Normally, nulls come last when sorting in ascending order and first /// when sorting in descending order. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # nullable_numbers (nullable_number) { /// # nullable_number -> Nullable, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::nullable_numbers::dsl::*; /// # let connection = &mut connection_no_data(); /// # diesel::sql_query("CREATE TABLE nullable_numbers (nullable_number INTEGER)").execute(connection)?; /// diesel::insert_into(nullable_numbers) /// .values(&vec![ /// nullable_number.eq(None), /// nullable_number.eq(Some(1)), /// nullable_number.eq(Some(2)), /// ]) /// .execute(connection)?; /// /// let desc_default_nulls = nullable_numbers.select(nullable_number) /// .order(nullable_number.desc()) /// .load::>(connection)?; /// assert_eq!(vec![None, Some(2), Some(1)], desc_default_nulls); /// /// let desc_nulls_last = nullable_numbers.select(nullable_number) /// .order(nullable_number.desc().nulls_last()) /// .load::>(connection)?; /// assert_eq!(vec![Some(2), Some(1), None], desc_nulls_last); /// # Ok(()) /// # } /// ``` fn nulls_last(self) -> dsl::NullsLast { NullsLast::new(self) } } impl PgSortExpressionMethods for Asc {} impl PgSortExpressionMethods for Desc {} /// PostgreSQL specific methods present on text expressions. #[cfg(feature = "postgres_backend")] pub trait PgTextExpressionMethods: Expression + Sized { /// Creates a PostgreSQL `ILIKE` expression /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use schema::animals::dsl::*; /// # let connection = &mut establish_connection(); /// let starts_with_s = animals /// .select(species) /// .filter(name.ilike("s%").or(species.ilike("s%"))) /// .get_results::(connection)?; /// assert_eq!(vec!["spider"], starts_with_s); /// # Ok(()) /// # } /// ``` fn ilike(self, other: T) -> dsl::ILike where T: AsExpression, { Grouped(ILike::new(self, other.as_expression())) } /// Creates a PostgreSQL `NOT ILIKE` expression /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use schema::animals::dsl::*; /// # let connection = &mut establish_connection(); /// let doesnt_start_with_s = animals /// .select(species) /// .filter(name.not_ilike("s%").and(species.not_ilike("s%"))) /// .get_results::(connection)?; /// assert_eq!(vec!["dog"], doesnt_start_with_s); /// # Ok(()) /// # } /// ``` fn not_ilike(self, other: T) -> dsl::NotILike where T: AsExpression, { Grouped(NotILike::new(self, other.as_expression())) } /// Creates a PostgreSQL `SIMILAR TO` expression /// /// # Example /// ``` /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use schema::animals::dsl::*; /// # let connection = &mut establish_connection(); /// let starts_with_s = animals /// .select(species) /// .filter(name.similar_to("s%").or(species.similar_to("s%"))) /// .get_results::(connection)?; /// assert_eq!(vec!["spider"], starts_with_s); /// # Ok(()) /// # } /// ``` fn similar_to(self, other: T) -> dsl::SimilarTo where T: AsExpression, { Grouped(SimilarTo::new(self, other.as_expression())) } /// Creates a PostgreSQL `NOT SIMILAR TO` expression /// /// # Example /// ``` /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use schema::animals::dsl::*; /// # let connection = &mut establish_connection(); /// let doesnt_start_with_s = animals /// .select(species) /// .filter(name.not_similar_to("s%").and(species.not_similar_to("s%"))) /// .get_results::(connection)?; /// assert_eq!(vec!["dog"], doesnt_start_with_s); /// # Ok(()) /// # } /// ``` fn not_similar_to(self, other: T) -> dsl::NotSimilarTo where T: AsExpression, { Grouped(NotSimilarTo::new(self, other.as_expression())) } /// Creates a PostgreSQL `IS JSON` expression. /// Requires PostgreSQL>=16 /// /// This operator returns true whether an object is a valid JSON /// /// # Example /// /// ```rust,no_run /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use std::collections::Bound; /// # use diesel::sql_types::Text; /// # /// # let conn = &mut establish_connection(); /// # /// /// let res = diesel::select(("1".into_sql::().is_json())).get_result::(conn)?; /// assert_eq!(res, true); /// let res = diesel::select(("[1,2,3]".into_sql::().is_json())).get_result::(conn)?; /// assert_eq!(res, true); /// let res = diesel::select(("{\"products\": [1,2,3]}".into_sql::().is_json())) /// .get_result::(conn)?; /// assert_eq!(res, true); /// let res = diesel::select(("(1,2,3)".into_sql::().is_json())).get_result::(conn)?; /// assert_eq!(res, false); /// # /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_json(self) -> dsl::IsJson { IsJson::new(self) } /// Creates a PostgreSQL `IS NOT JSON` expression. /// Requires PostgreSQL>=16 /// /// This operator returns true whether an object is not a valid JSON /// /// # Example /// /// ```rust,no_run /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use std::collections::Bound; /// # use diesel::sql_types::Text; /// # /// # let conn = &mut establish_connection(); /// # /// /// let res = diesel::select(("1".into_sql::().is_not_json())).get_result::(conn)?; /// assert_eq!(res, false); /// let res = /// diesel::select(("[1,2,3]".into_sql::().is_not_json())).get_result::(conn)?; /// assert_eq!(res, false); /// let res = diesel::select(("{\"products\": [1,2,3]}".into_sql::().is_not_json())) /// .get_result::(conn)?; /// assert_eq!(res, false); /// let res = /// diesel::select(("(1,2,3)".into_sql::().is_not_json())).get_result::(conn)?; /// assert_eq!(res, true); /// # /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_not_json(self) -> dsl::IsNotJson { IsNotJson::new(self) } /// Creates a PostgreSQL `IS JSON OBJECT` expression. /// Requires PostgreSQL>=16 /// /// This operator returns true whether an object is a valid JSON /// /// # Example /// /// ```rust,no_run /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use std::collections::Bound; /// # use diesel::sql_types::Text; /// # /// # let conn = &mut establish_connection(); /// # /// /// let res = /// diesel::select(("123".into_sql::().is_json_object())).get_result::(conn)?; /// assert_eq!(res, false); /// let res = /// diesel::select(("abc".into_sql::().is_json_object())).get_result::(conn)?; /// assert_eq!(res, false); /// let res = diesel::select( /// ("{\"products\": [1,2,3]}" /// .into_sql::() /// .is_json_object()), /// ) /// .get_result::(conn)?; /// assert_eq!(res, true); /// let res = /// diesel::select(("[1,2,3]".into_sql::().is_json_object())).get_result::(conn)?; /// assert_eq!(res, false); /// let res = /// diesel::select(("\"abc\"".into_sql::().is_json_object())).get_result::(conn)?; /// assert_eq!(res, false); /// # /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_json_object(self) -> dsl::IsJsonObject { IsJsonObject::new(self) } /// Creates a PostgreSQL `IS NOT JSON OBJECT` expression. /// Requires PostgreSQL>=16 /// /// This operator returns true whether an object is not a valid JSON /// /// # Example /// /// ```rust,no_run /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use std::collections::Bound; /// # use diesel::sql_types::Text; /// # /// # let conn = &mut establish_connection(); /// # /// /// let res = /// diesel::select(("123".into_sql::().is_not_json_object())).get_result::(conn)?; /// assert_eq!(res, true); /// let res = /// diesel::select(("abc".into_sql::().is_not_json_object())).get_result::(conn)?; /// assert_eq!(res, true); /// let res = diesel::select( /// ("{\"products\": [1,2,3]}" /// .into_sql::() /// .is_not_json_object()), /// ) /// .get_result::(conn)?; /// assert_eq!(res, false); /// let res = diesel::select(("[1,2,3]".into_sql::().is_not_json_object())) /// .get_result::(conn)?; /// assert_eq!(res, true); /// let res = diesel::select(("\"abc\"".into_sql::().is_not_json_object())) /// .get_result::(conn)?; /// assert_eq!(res, true); /// # /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_not_json_object(self) -> dsl::IsNotJsonObject { IsNotJsonObject::new(self) } /// Creates a PostgreSQL `IS JSON ARRAY` expression. /// Requires PostgreSQL>=16 /// /// This operator returns true whether an object is a valid JSON ARRAY /// /// # Example /// /// ```rust,no_run /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use std::collections::Bound; /// # use diesel::sql_types::Text; /// # /// # let conn = &mut establish_connection(); /// # /// /// let res = /// diesel::select(("123".into_sql::().is_json_array())).get_result::(conn)?; /// assert_eq!(res, false); /// let res = /// diesel::select(("abc".into_sql::().is_json_array())).get_result::(conn)?; /// assert_eq!(res, false); /// let res = diesel::select(("{\"products\": [1,2,3]}".into_sql::().is_json_array())) /// .get_result::(conn)?; /// assert_eq!(res, false); /// let res = /// diesel::select(("[1,2,3]".into_sql::().is_json_array())).get_result::(conn)?; /// assert_eq!(res, true); /// let res = /// diesel::select(("\"abc\"".into_sql::().is_json_array())).get_result::(conn)?; /// assert_eq!(res, false); /// # /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_json_array(self) -> dsl::IsJsonArray { IsJsonArray::new(self) } /// Creates a PostgreSQL `IS NOT JSON ARRAY` expression. /// Requires PostgreSQL>=16 /// /// This operator returns true whether an object is not a valid JSON ARRAY /// /// # Example /// /// ```rust,no_run /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use std::collections::Bound; /// # use diesel::sql_types::Text; /// # /// # let conn = &mut establish_connection(); /// # /// /// let res = /// diesel::select(("123".into_sql::().is_not_json_array())).get_result::(conn)?; /// assert_eq!(res, true); /// let res = /// diesel::select(("abc".into_sql::().is_not_json_array())).get_result::(conn)?; /// assert_eq!(res, true); /// let res = diesel::select( /// ("{\"products\": [1,2,3]}" /// .into_sql::() /// .is_not_json_array()), /// ) /// .get_result::(conn)?; /// assert_eq!(res, true); /// let res = diesel::select(("[1,2,3]".into_sql::().is_not_json_array())) /// .get_result::(conn)?; /// assert_eq!(res, false); /// let res = diesel::select(("\"abc\"".into_sql::().is_not_json_array())) /// .get_result::(conn)?; /// assert_eq!(res, true); /// # /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_not_json_array(self) -> dsl::IsNotJsonArray { IsNotJsonArray::new(self) } /// Creates a PostgreSQL `IS JSON SCALAR` expression. /// Requires PostgreSQL>=16 /// /// This operator returns true whether an object is a valid JSON SCALAR /// /// # Example /// /// ```rust,no_run /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use std::collections::Bound; /// # use diesel::sql_types::{Text, Nullable}; /// # use diesel::dsl::sql; /// # /// # let conn = &mut establish_connection(); /// # /// /// let res = /// diesel::select(("123".into_sql::().is_json_scalar())).get_result::(conn)?; /// assert_eq!(res, true); /// let res = /// diesel::select(("abc".into_sql::().is_json_scalar())).get_result::(conn)?; /// assert_eq!(res, false); /// let res = diesel::select( /// ("{\"products\": [1,2,3]}" /// .into_sql::() /// .is_json_scalar()), /// ) /// .get_result::(conn)?; /// assert_eq!(res, false); /// let res = /// diesel::select(("[1,2,3]".into_sql::().is_json_scalar())).get_result::(conn)?; /// assert_eq!(res, false); /// let res = /// diesel::select(("\"abc\"".into_sql::().is_json_scalar())).get_result::(conn)?; /// assert_eq!(res, true); /// let res = diesel::select(sql::>("NULL").is_json_scalar()) /// .get_result::>(conn)?; /// assert!(res.is_none()); /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_json_scalar(self) -> dsl::IsJsonScalar { IsJsonScalar::new(self) } /// Creates a PostgreSQL `IS NOT JSON SCALAR` expression. /// Requires PostgreSQL>=16 /// /// This operator returns true whether an object is not a valid JSON SCALAR /// /// # Example /// /// ```rust,no_run /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use std::collections::Bound; /// # use diesel::sql_types::Text; /// # /// # let conn = &mut establish_connection(); /// # /// /// let res = /// diesel::select(("123".into_sql::().is_not_json_scalar())).get_result::(conn)?; /// assert_eq!(res, false); /// let res = /// diesel::select(("abc".into_sql::().is_not_json_scalar())).get_result::(conn)?; /// assert_eq!(res, true); /// let res = diesel::select( /// ("{\"products\": [1,2,3]}" /// .into_sql::() /// .is_not_json_scalar()), /// ) /// .get_result::(conn)?; /// assert_eq!(res, true); /// let res = diesel::select(("[1,2,3]".into_sql::().is_not_json_scalar())) /// .get_result::(conn)?; /// assert_eq!(res, true); /// let res = diesel::select(("\"abc\"".into_sql::().is_not_json_scalar())) /// .get_result::(conn)?; /// assert_eq!(res, false); /// # /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_not_json_scalar(self) -> dsl::IsNotJsonScalar { IsNotJsonScalar::new(self) } } impl PgTextExpressionMethods for T where T: Expression, T::SqlType: TextOrNullableText, { } impl EscapeExpressionMethods for Grouped> { type TextExpression = ILike; fn escape(self, character: char) -> dsl::Escape { Grouped(crate::expression::operators::Escape::new( self.0, character.to_string().into_sql::(), )) } } impl EscapeExpressionMethods for Grouped> { type TextExpression = NotILike; fn escape(self, character: char) -> dsl::Escape { Grouped(crate::expression::operators::Escape::new( self.0, character.to_string().into_sql::(), )) } } impl EscapeExpressionMethods for Grouped> { type TextExpression = SimilarTo; fn escape(self, character: char) -> dsl::Escape { Grouped(crate::expression::operators::Escape::new( self.0, character.to_string().into_sql::(), )) } } impl EscapeExpressionMethods for Grouped> { type TextExpression = NotSimilarTo; fn escape(self, character: char) -> dsl::Escape { Grouped(crate::expression::operators::Escape::new( self.0, character.to_string().into_sql::(), )) } } /// PostgreSQL specific methods present on range expressions. #[cfg(feature = "postgres_backend")] pub trait PgRangeExpressionMethods: Expression + Sized { /// Creates a PostgreSQL `@>` expression. /// /// This operator returns true whether a range contains an specific element /// /// This operator evaluates to true for the following cases: /// /// ```text /// self: [-----] /// other: | /// ``` /// /// ```text /// self: [----] /// other: | /// ``` /// /// ```text /// self: [----] /// other: | /// ``` /// /// This operator evaluates to false for the following cases: /// /// ```text /// self: [-----] /// other: | /// ``` /// /// ```text /// self: [----] /// other: | /// ``` /// /// ```text /// self: [----) /// other: | /// ``` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use diesel::sql_types::{Integer, Range, Multirange}; /// # /// # let conn = &mut establish_connection(); /// assert!( /// diesel::select((1..5).into_sql::>().contains(4)) /// .first::(conn) /// .unwrap() /// ); /// assert!( /// !diesel::select((1..5).into_sql::>().contains(8)) /// .first::(conn) /// .unwrap() /// ); /// /// assert!( /// diesel::select((vec![1..5]).into_sql::>().contains(4)) /// .first::(conn) /// .unwrap() /// ); /// assert!( /// !diesel::select((vec![1..5]).into_sql::>().contains(8)) /// .first::(conn) /// .unwrap() /// ); /// /// # Ok(()) /// # } /// ``` fn contains(self, other: T) -> dsl::RangeContains where Self::SqlType: RangeOrMultirange, ::Inner: SqlType + TypedExpressionType, T: AsExpression<::Inner>, { Grouped(Contains::new(self, other.as_expression())) } /// Creates a PostgreSQL `@>` expression. /// /// This operator returns true whether a range contains another range /// /// This operator evaluates to true for the following cases: /// /// ```text /// self: [-------] /// other: [--] /// ``` /// /// ```text /// self: [------] /// other: [------] /// ``` /// /// This operator evaluates to false for the following cases: /// /// ```text /// self: [-----] /// other: [----] /// ``` /// /// ```text /// self: [----] /// other: [--------] /// ``` /// /// ```text /// self: [----) /// other: [----] /// ``` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use diesel::sql_types::{Integer, Range, Multirange}; /// # let conn = &mut establish_connection(); /// assert!( /// diesel::select((1..5).into_sql::>().contains_range(1..5)) /// .first::(conn) /// .unwrap() /// ); /// assert!( /// !diesel::select((1..5).into_sql::>().contains_range(3..7)) /// .first::(conn) /// .unwrap() /// ); /// /// assert!(diesel::select( /// vec![1..5] /// .into_sql::>() /// .contains_range(vec![1..5]) /// ) /// .first::(conn) /// .unwrap()); /// assert!(!diesel::select( /// vec![1..5] /// .into_sql::>() /// .contains_range(vec![3..7]) /// ) /// .first::(conn) /// .unwrap()); /// # Ok(()) /// # } /// ``` fn contains_range(self, other: T) -> dsl::ContainsRange where Self::SqlType: SqlType, T: AsExpression, { Grouped(Contains::new(self, other.as_expression())) } /// Creates a PostgreSQL `<@` expression. /// /// This operator returns true whether a range is contained by another range /// /// This operator evaluates to true for the following cases: /// /// ```text /// self: [-----] /// other: [-------] /// ``` /// /// ```text /// self: [----] /// other: [----] /// ``` /// /// This operator evaluates to false for the following cases: /// /// ```text /// self: [------] /// other: [---] /// ``` /// /// ```text /// self: [----] /// other: [-----) /// ``` /// /// ```text /// self: [----] /// other: [----] /// ``` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use diesel::sql_types::{Integer, Range, Multirange}; /// # let conn = &mut establish_connection(); /// assert!( /// diesel::select((1..5).into_sql::>().is_contained_by(1..5)) /// .first::(conn) /// .unwrap() /// ); /// assert!( /// !diesel::select((1..5).into_sql::>().is_contained_by(3..7)) /// .first::(conn) /// .unwrap() /// ); /// /// assert!(diesel::select( /// vec![1..5] /// .into_sql::>() /// .is_contained_by(vec![1..5]) /// ) /// .first::(conn) /// .unwrap()); /// assert!(!diesel::select( /// vec![1..5] /// .into_sql::>() /// .is_contained_by(vec![3..7]) /// ) /// .first::(conn) /// .unwrap()); /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_contained_by(self, other: T) -> dsl::IsContainedBy where Self::SqlType: SqlType, T: AsExpression, { Grouped(IsContainedBy::new(self, other.as_expression())) } /// Creates a PostgreSQL `&&` expression. /// /// This operator returns true whether two ranges overlap. /// /// This operator evaluates to true for the following cases: /// /// ```text /// self: [-----] /// other: [-----] /// ``` /// /// ```text /// self: [----] /// other: [----] /// ``` /// /// ```text /// self: [----] /// other: [-------] /// ``` /// /// ```text /// self: [----] /// other: [----] /// ``` /// /// ```text /// self: [----] /// other: [----) /// ``` /// /// This operator evaluates to false for the following cases: /// /// ```text /// self: [-----] /// other: [-----] /// ``` /// /// ```text /// self: [----] /// other: [--] /// ``` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use diesel::sql_types::{Integer, Range, Multirange}; /// # let conn = &mut establish_connection(); /// assert!( /// diesel::select((1..5).into_sql::>().overlaps_with(3..7)) /// .first::(conn) /// .unwrap() /// ); /// assert!( /// !diesel::select((1..5).into_sql::>().overlaps_with(10..15)) /// .first::(conn) /// .unwrap() /// ); /// /// assert!(diesel::select( /// vec![1..5] /// .into_sql::>() /// .overlaps_with(vec![3..7]) /// ) /// .first::(conn) /// .unwrap()); /// assert!(!diesel::select( /// vec![1..5] /// .into_sql::>() /// .overlaps_with(vec![10..15]) /// ) /// .first::(conn) /// .unwrap()); /// # Ok(()) /// # } /// ``` fn overlaps_with(self, other: T) -> dsl::OverlapsWith where Self::SqlType: SqlType, T: AsExpression, { Grouped(OverlapsWith::new(self, other.as_expression())) } /// Creates a PostgreSQL `&<` expression. /// /// This operator returns true whether the argument range extend to the right of the current range /// /// Postgresql defines "extends" as does not have a lower bound smaller than the lower bound of the /// self range. That means the right hand side range can overlap parts of the left hand side /// range or be on the right side of the left hand side range /// /// The following constelations evaluate to true: /// ```text /// self: [------) /// other: [----) /// ``` /// /// ```text /// self: [----) /// other: [----) /// ``` /// /// ```text /// self: [------) /// other: [------) /// ``` /// /// The following constelations evaluate to false: /// /// ```text /// self: [------] /// other: [----] /// ``` /// /// ```text /// self: [------] /// other: [----] /// ``` /// /// ```text /// self: [------] /// other: [------) /// ``` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use diesel::sql_types::{Integer, Range, Multirange}; /// # let conn = &mut establish_connection(); /// # /// assert!(diesel::select( /// (1..20) /// .into_sql::>() /// .range_extends_right_to(18..20) /// ) /// .first::(conn) /// .unwrap()); /// assert!(diesel::select( /// (1..20) /// .into_sql::>() /// .range_extends_right_to(25..30) /// ) /// .first::(conn) /// .unwrap()); /// assert!(!diesel::select( /// (1..20) /// .into_sql::>() /// .range_extends_right_to(-10..0) /// ) /// .first::(conn) /// .unwrap()); /// /// assert!(diesel::select( /// vec![1..20] /// .into_sql::>() /// .range_extends_right_to(vec![18..20]) /// ) /// .first::(conn) /// .unwrap()); /// assert!(diesel::select( /// vec![1..20] /// .into_sql::>() /// .range_extends_right_to(vec![25..30]) /// ) /// .first::(conn) /// .unwrap()); /// assert!(!diesel::select( /// vec![1..20] /// .into_sql::>() /// .range_extends_right_to(vec![-10..0]) /// ) /// .first::(conn) /// .unwrap()); /// # Ok(()) /// # } /// ``` fn range_extends_right_to(self, other: T) -> dsl::RangeExtendsRightTo where Self::SqlType: SqlType, T: AsExpression, { Grouped(ExtendsRightTo::new(self, other.as_expression())) } /// Creates a PostgreSQL `&>` expression. /// /// This operator returns true whether a range does extend to the left of another /// /// Postgresql defines "extends" as does not have a upper bound greater than the upper bound of the /// self range. That means the right hand side range can overlap parts of the left hand side /// range or be on the left side of the left hand side range /// /// The following constelations evaluate to true: /// ```text /// self: [------) /// other: [----) /// ``` /// /// ```text /// self: [----) /// other: [----) /// ``` /// /// ```text /// self: [------) /// other: [------) /// ``` /// /// The following constelations evaluate to false: /// /// ```text /// self: [--------] /// other: [----] /// ``` /// /// ```text /// self: [------] /// other: [----] /// ``` /// /// ```text /// self: [------] /// other: (------] /// ``` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use diesel::sql_types::{Integer, Range, Multirange}; /// # let conn = &mut establish_connection(); /// # /// assert!(diesel::select( /// (1..20) /// .into_sql::>() /// .range_extends_left_to(-10..5) /// ) /// .first::(conn) /// .unwrap()); /// assert!(diesel::select( /// (1..20) /// .into_sql::>() /// .range_extends_left_to(-10..-5) /// ) /// .first::(conn) /// .unwrap()); /// assert!(!diesel::select( /// (1..20) /// .into_sql::>() /// .range_extends_left_to(25..30) /// ) /// .first::(conn) /// .unwrap()); /// /// assert!(diesel::select( /// vec![1..20] /// .into_sql::>() /// .range_extends_left_to(vec![-10..5]) /// ) /// .first::(conn) /// .unwrap()); /// assert!(diesel::select( /// vec![1..20] /// .into_sql::>() /// .range_extends_left_to(vec![-10..-5]) /// ) /// .first::(conn) /// .unwrap()); /// assert!(!diesel::select( /// vec![1..20] /// .into_sql::>() /// .range_extends_left_to(vec![25..30]) /// ) /// .first::(conn) /// .unwrap()); /// # Ok(()) /// # } /// ``` fn range_extends_left_to(self, other: T) -> dsl::RangeExtendsLeftTo where Self::SqlType: SqlType, T: AsExpression, { Grouped(ExtendsLeftTo::new(self, other.as_expression())) } /// Creates a PostgreSQL `<<` expression. /// /// Is the first range strictly left of the second? /// /// The following constelations evaluate to true: /// ```text /// self: [------) /// other: [----) /// ``` /// /// ```text /// self: [----) /// other: [----) /// ``` /// /// The following constelations evaluate to false: /// /// ```text /// self: [------] /// other: [----] /// ``` /// /// ```text /// self: [------] /// other: [----] /// ``` /// /// ```text /// self: [------] /// other: [------) /// ``` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # posts { /// # id -> Integer, /// # versions -> Range, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::posts::dsl::*; /// # use std::collections::Bound; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS posts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE posts (id SERIAL PRIMARY KEY, versions INT4RANGE NOT NULL)").execute(conn).unwrap(); /// # /// diesel::insert_into(posts) /// .values(&vec![ /// (versions.eq((Bound::Included(1), Bound::Included(2)))), /// (versions.eq((Bound::Included(3), Bound::Included(4)))), /// (versions.eq((Bound::Included(5), Bound::Included(6)))) /// ]) /// .execute(conn)?; /// /// let data = posts.select(id) /// .filter(versions.lesser_than((Bound::Included(1), Bound::Included(4)))) /// .load::(conn)?; /// assert!(data.is_empty()); /// /// let data = posts.select(id) /// .filter(versions.lesser_than((Bound::Included(5), Bound::Included(8)))) /// .load::(conn)?; /// assert_eq!(vec![1, 2], data); /// # Ok(()) /// # } /// ``` fn lesser_than(self, other: T) -> dsl::LesserThanRange where Self::SqlType: SqlType, T: AsExpression, { Grouped(IsContainedByNet::new(self, other.as_expression())) } /// Creates a PostgreSQL `>>` expression. /// /// Is the first range strictly right of the second? /// /// The following constelations evaluate to true: /// ```text /// self: [------) /// other: [----) /// ``` /// /// ```text /// self: [----) /// other: [----) /// ``` /// /// The following constelations evaluate to false: /// /// ```text /// self: [------] /// other: [----] /// ``` /// /// ```text /// self: [------] /// other: [----] /// ``` /// /// ```text /// self: [------] /// other: [------] /// ``` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # posts { /// # id -> Integer, /// # versions -> Range, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use self::posts::dsl::*; /// # use std::collections::Bound; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS posts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE posts (id SERIAL PRIMARY KEY, versions INT4RANGE NOT NULL)").execute(conn).unwrap(); /// # /// diesel::insert_into(posts) /// .values(&vec![ /// (versions.eq((Bound::Included(1), Bound::Included(2)))), /// (versions.eq((Bound::Included(3), Bound::Included(4)))), /// (versions.eq((Bound::Included(5), Bound::Included(6)))) /// ]) /// .execute(conn)?; /// /// let data = posts.select(id) /// .filter(versions.greater_than((Bound::Included(1), Bound::Included(2)))) /// .load::(conn)?; /// assert_eq!(vec![2, 3], data); /// /// let data = posts.select(id) /// .filter(versions.greater_than((Bound::Included(5), Bound::Included(8)))) /// .load::(conn)?; /// assert!(data.is_empty()); /// # Ok(()) /// # } /// ``` fn greater_than(self, other: T) -> dsl::GreaterThanRange where Self::SqlType: SqlType, T: AsExpression, { Grouped(ContainsNet::new(self, other.as_expression())) } /// Creates a PostgreSQL ` -|- ` expression. /// /// This operator evaluates to true if the two ranges are adjacent /// /// The following constelations evaluate to true: /// ```text /// self: [------) /// other: [----) /// ``` /// /// ```text /// self: [----) /// other: [----) /// ``` /// /// ```text /// self: [----) /// other: [----] /// ``` /// /// ```text /// self: [----] /// other: [----] /// ``` /// /// The following constelations evaluate to false: /// /// ```text /// self: [------] /// other: [----] /// ``` /// /// ```text /// self: [------] /// other: [----] /// ``` /// /// ```text /// self: [------] /// other: [------] /// ``` /// /// ```text /// self: [------] /// other: [------] /// ``` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use diesel::sql_types::{Integer, Range, Multirange}; /// # let conn = &mut establish_connection(); /// assert!( /// diesel::select((1..2).into_sql::>().range_adjacent(2..=6)) /// .first::(conn) /// .unwrap() /// ); /// assert!( /// !diesel::select((4..7).into_sql::>().range_adjacent(2..=6)) /// .first::(conn) /// .unwrap() /// ); /// /// assert!(diesel::select( /// vec![1..2] /// .into_sql::>() /// .range_adjacent(vec![2..=6]) /// ) /// .first::(conn) /// .unwrap()); /// assert!(!diesel::select( /// vec![4..7] /// .into_sql::>() /// .range_adjacent(vec![2..=6]) /// ) /// .first::(conn) /// .unwrap()); /// # Ok(()) /// # } /// ``` fn range_adjacent(self, other: T) -> dsl::RangeAdjacent where Self::SqlType: SqlType, T: AsExpression, { Grouped(RangeAdjacent::new(self, other.as_expression())) } /// Creates a PostgreSQL ` + ` expression. /// /// This operator unions two ranges and returns the union. /// /// ```text /// self: [------) /// other: [----) /// result: [--------) /// ``` /// /// ```text /// self: [----) /// other: [----) /// result: error /// ``` /// /// ```text /// self: [----) /// other: [----] /// result [--------) /// ``` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use diesel::sql_types::{Integer, Range, Multirange}; /// # let conn = &mut establish_connection(); /// assert!(diesel::select( /// (1..=2) /// .into_sql::>() /// .union_range(2..=6) /// .eq(1..=6) /// ) /// .first::(conn) /// .unwrap()); /// /// assert!(diesel::select( /// vec![1..=2] /// .into_sql::>() /// .union_range(vec![1..=6]) /// .eq(vec![1..=6]) /// ) /// .first::(conn) /// .unwrap()); /// # Ok(()) /// # } /// ``` fn union_range(self, other: T) -> dsl::UnionRange where Self::SqlType: SqlType, T: AsExpression, { Grouped(UnionsRange::new(self, other.as_expression())) } /// Creates a PostgreSQL ` - ` expression. /// /// This operator takes two ranges and returns the difference. /// /// The second range must not be contained in the first in such a way that the /// difference would not be a single range. /// /// ```text /// self: [------) /// other: [----) /// result: [---) /// ``` /// /// ```text /// self: [----) /// other: [----) /// result: [--) /// ``` /// /// ```text /// self: [--------) /// other: [----] /// result: error /// ``` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use diesel::sql_types::{Integer, Range, Multirange}; /// # let conn = &mut establish_connection(); /// assert!(diesel::select( /// (1..=8) /// .into_sql::>() /// .difference_range(3..=8) /// .eq(1..3) /// ) /// .first::(conn) /// .unwrap()); /// /// assert!(diesel::select( /// vec![1..=8] /// .into_sql::>() /// .difference_range(vec![3..=8]) /// .eq(vec![1..3]) /// ) /// .first::(conn) /// .unwrap()); /// # Ok(()) /// # } /// ``` fn difference_range(self, other: T) -> dsl::Difference where Self::SqlType: SqlType, T: AsExpression, { Grouped(DifferenceRange::new(self, other.as_expression())) } /// Creates a PostgreSQL ` * ` expression. /// /// This operator takes two ranges and returns the intersection. /// /// ```text /// self: [------) /// other: [----) /// result: [--) /// ``` /// /// ```text /// self: [----) /// other: [----) /// result: [-) /// ``` /// /// ```text /// self: [--------) /// other: [----] /// result: [----] /// ``` /// /// ```text /// self: [--------) /// other: [----] /// result: empty range /// ``` /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # fn run_test() -> QueryResult<()> { /// # use diesel::sql_types::{Integer, Range, Multirange}; /// # let conn = &mut establish_connection(); /// assert!(diesel::select( /// (1..=8) /// .into_sql::>() /// .intersection_range(3..=8) /// .eq(3..=8) /// ) /// .first::(conn) /// .unwrap()); /// /// assert!(diesel::select( /// vec![1..=8] /// .into_sql::>() /// .intersection_range(vec![3..=8]) /// .eq(vec![3..=8]) /// ) /// .first::(conn) /// .unwrap()); /// # Ok(()) /// # } /// ``` fn intersection_range(self, other: T) -> dsl::Intersection where Self::SqlType: SqlType, T: AsExpression, { Grouped(IntersectionRange::new(self, other.as_expression())) } } impl PgRangeExpressionMethods for T where T: Expression, T::SqlType: MultirangeOrRangeMaybeNullable, { } /// PostgreSQL specific methods present between CIDR/INET expressions #[cfg(feature = "postgres_backend")] pub trait PgNetExpressionMethods: Expression + Sized { /// Creates a PostgreSQL `>>` expression. /// /// This operator returns whether a subnet strictly contains another subnet or address. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # hosts { /// # id -> Integer, /// # address -> Inet, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # #[cfg(feature = "ipnetwork")] /// # fn run_test() -> QueryResult<()> { /// # use self::hosts::dsl::*; /// # use ipnetwork::IpNetwork; /// # use std::str::FromStr; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS hosts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE hosts (id SERIAL PRIMARY KEY, address INET NOT NULL)").execute(conn).unwrap(); /// diesel::insert_into(hosts) /// .values(vec![address.eq(IpNetwork::from_str("10.0.2.3/24").unwrap()), /// address.eq(IpNetwork::from_str("10.0.3.4/23").unwrap())]) /// .execute(conn)?; /// /// let my_hosts = hosts.select(id) /// .filter(address.contains(IpNetwork::from_str("10.0.2.5").unwrap())) /// .load::(conn)?; /// assert_eq!(vec![1, 2], my_hosts); /// /// let my_hosts = hosts.select(id) /// .filter(address.contains(IpNetwork::from_str("10.0.2.5/24").unwrap())) /// .load::(conn)?; /// assert_eq!(vec![2], my_hosts); /// /// let my_hosts = hosts.select(id) /// .filter(address.contains(IpNetwork::from_str("10.0.3.31").unwrap())) /// .load::(conn)?; /// assert_eq!(vec![2], my_hosts); /// # Ok(()) /// # } /// # /// # #[cfg(not(feature = "ipnetwork"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn contains(self, other: T) -> dsl::ContainsNet where T: AsExpression, { Grouped(ContainsNet::new(self, other.as_expression())) } /// Creates a PostgreSQL `>>=` expression. /// /// This operator returns whether a subnet contains or is equal to another subnet. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # hosts { /// # id -> Integer, /// # address -> Inet, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # #[cfg(feature = "ipnetwork")] /// # fn run_test() -> QueryResult<()> { /// # use self::hosts::dsl::*; /// # use ipnetwork::IpNetwork; /// # use std::str::FromStr; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS hosts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE hosts (id SERIAL PRIMARY KEY, address INET NOT NULL)").execute(conn).unwrap(); /// diesel::insert_into(hosts) /// .values(vec![address.eq(IpNetwork::from_str("10.0.2.3/24").unwrap()), /// address.eq(IpNetwork::from_str("10.0.3.4/23").unwrap())]) /// .execute(conn)?; /// /// let my_hosts = hosts.select(id) /// .filter(address.contains_or_eq(IpNetwork::from_str("10.0.2.5").unwrap())) /// .load::(conn)?; /// assert_eq!(vec![1, 2], my_hosts); /// /// let my_hosts = hosts.select(id) /// .filter(address.contains_or_eq(IpNetwork::from_str("10.0.2.5/24").unwrap())) /// .load::(conn)?; /// assert_eq!(vec![1, 2], my_hosts); /// /// let my_hosts = hosts.select(id) /// .filter(address.contains_or_eq(IpNetwork::from_str("10.0.3.31").unwrap())) /// .load::(conn)?; /// assert_eq!(vec![2], my_hosts); /// # Ok(()) /// # } /// # /// # #[cfg(not(feature = "ipnetwork"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn contains_or_eq(self, other: T) -> dsl::ContainsNetLoose where T: AsExpression, { Grouped(ContainsNetLoose::new(self, other.as_expression())) } /// Creates a PostgreSQL `<<` expression. /// /// This operator returns whether a subnet or address is strictly contained by another subnet. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # hosts { /// # id -> Integer, /// # address -> Inet, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # #[cfg(feature = "ipnetwork")] /// # fn run_test() -> QueryResult<()> { /// # use self::hosts::dsl::*; /// # use ipnetwork::IpNetwork; /// # use std::str::FromStr; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS hosts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE hosts (id SERIAL PRIMARY KEY, address INET NOT NULL)").execute(conn).unwrap(); /// diesel::insert_into(hosts) /// .values(vec![address.eq(IpNetwork::from_str("10.0.2.3/24").unwrap()), /// address.eq(IpNetwork::from_str("10.0.3.4/23").unwrap())]) /// .execute(conn)?; /// /// let my_hosts = hosts.select(id) /// .filter(address.is_contained_by(IpNetwork::from_str("10.0.2.5/24").unwrap())) /// .load::(conn)?; /// assert_eq!(my_hosts.len(), 0); /// /// let my_hosts = hosts.select(id) /// .filter(address.is_contained_by(IpNetwork::from_str("10.0.3.31/23").unwrap())) /// .load::(conn)?; /// assert_eq!(vec![1], my_hosts); /// /// let my_hosts = hosts.select(id) /// .filter(address.is_contained_by(IpNetwork::from_str("10.0.3.31/22").unwrap())) /// .load::(conn)?; /// assert_eq!(vec![1, 2], my_hosts); /// # Ok(()) /// # } /// # /// # #[cfg(not(feature = "ipnetwork"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_contained_by(self, other: T) -> dsl::IsContainedByNet where T: AsExpression, { Grouped(IsContainedByNet::new(self, other.as_expression())) } /// Creates a PostgreSQL `<<= expression. /// /// This operator returns whether a subnet is contained by or equal to another subnet. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # hosts { /// # id -> Integer, /// # address -> Inet, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # #[cfg(feature = "ipnetwork")] /// # fn run_test() -> QueryResult<()> { /// # use self::hosts::dsl::*; /// # use ipnetwork::IpNetwork; /// # use std::str::FromStr; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS hosts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE hosts (id SERIAL PRIMARY KEY, address INET NOT NULL)").execute(conn).unwrap(); /// diesel::insert_into(hosts) /// .values(vec![address.eq(IpNetwork::from_str("10.0.2.3/24").unwrap()), /// address.eq(IpNetwork::from_str("10.0.3.4/23").unwrap())]) /// .execute(conn)?; /// /// let my_hosts = hosts.select(id) /// .filter(address.is_contained_by_or_eq(IpNetwork::from_str("10.0.2.5/24").unwrap())) /// .load::(conn)?; /// assert_eq!(vec![1], my_hosts); /// /// let my_hosts = hosts.select(id) /// .filter(address.is_contained_by_or_eq(IpNetwork::from_str("10.0.3.31/23").unwrap())) /// .load::(conn)?; /// assert_eq!(vec![1, 2], my_hosts); /// # Ok(()) /// # } /// # /// # #[cfg(not(feature = "ipnetwork"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_contained_by_or_eq(self, other: T) -> dsl::IsContainedByNetLoose where T: AsExpression, { Grouped(IsContainedByNetLoose::new(self, other.as_expression())) } /// Creates a PostgreSQL `&&` expression. /// /// This operator returns whether a subnet contains or is contained by another subnet. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # hosts { /// # id -> Integer, /// # address -> Inet, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # #[cfg(feature = "ipnetwork")] /// # fn run_test() -> QueryResult<()> { /// # use self::hosts::dsl::*; /// # use ipnetwork::IpNetwork; /// # use std::str::FromStr; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS hosts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE hosts (id SERIAL PRIMARY KEY, address INET NOT NULL)").execute(conn).unwrap(); /// diesel::insert_into(hosts) /// .values(vec![address.eq(IpNetwork::from_str("10.0.2.3/24").unwrap()), /// address.eq(IpNetwork::from_str("10.0.3.4/23").unwrap())]) /// .execute(conn)?; /// /// let my_hosts = hosts.select(id) /// .filter(address.overlaps_with(IpNetwork::from_str("10.0.2.5/24").unwrap())) /// .load::(conn)?; /// assert_eq!(vec![1, 2], my_hosts); /// /// let my_hosts = hosts.select(id) /// .filter(address.overlaps_with(IpNetwork::from_str("10.0.3.31/24").unwrap())) /// .load::(conn)?; /// assert_eq!(vec![2], my_hosts); /// /// let my_hosts = hosts.select(id) /// .filter(address.overlaps_with(IpNetwork::from_str("10.0.3.31/23").unwrap())) /// .load::(conn)?; /// assert_eq!(vec![1, 2], my_hosts); /// # Ok(()) /// # } /// # /// # #[cfg(not(feature = "ipnetwork"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn overlaps_with(self, other: T) -> dsl::OverlapsWithNet where T: AsExpression, { Grouped(OverlapsWith::new(self, other.as_expression())) } /// Creates a PostgreSQL `&` expression. /// /// This operator computes the bitwise AND between two network addresses. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # hosts { /// # id -> Integer, /// # address -> Inet, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # #[cfg(feature = "ipnetwork")] /// # fn run_test() -> QueryResult<()> { /// # use self::hosts::dsl::*; /// # use ipnetwork::IpNetwork; /// # use std::str::FromStr; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS hosts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE hosts (id SERIAL PRIMARY KEY, address INET NOT NULL)").execute(conn).unwrap(); /// diesel::insert_into(hosts) /// .values(vec![address.eq(IpNetwork::from_str("10.0.2.3").unwrap())]) /// .execute(conn)?; /// /// let addr = hosts /// .select(address.and(IpNetwork::from_str("0.0.0.255").unwrap())) /// .first::(conn)?; /// assert_eq!(addr, IpNetwork::from_str("0.0.0.3").unwrap()); /// # Ok(()) /// # } /// # /// # #[cfg(not(feature = "ipnetwork"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn and(self, other: T) -> dsl::AndNet where T: AsExpression, { Grouped(AndNet::new(self, other.as_expression())) } /// Creates a PostgreSQL `|` expression. /// /// This operator computes the bitwise OR between two network addresses. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # hosts { /// # id -> Integer, /// # address -> Inet, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # #[cfg(feature = "ipnetwork")] /// # fn run_test() -> QueryResult<()> { /// # use self::hosts::dsl::*; /// # use ipnetwork::IpNetwork; /// # use std::str::FromStr; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS hosts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE hosts (id SERIAL PRIMARY KEY, address INET NOT NULL)").execute(conn).unwrap(); /// diesel::insert_into(hosts) /// .values(vec![address.eq(IpNetwork::from_str("10.0.2.3").unwrap())]) /// .execute(conn)?; /// /// let addr = hosts /// .select(address.or(IpNetwork::from_str("0.0.0.255").unwrap())) /// .first::(conn)?; /// assert_eq!(addr, IpNetwork::from_str("10.0.2.255").unwrap()); /// # Ok(()) /// # } /// # /// # #[cfg(not(feature = "ipnetwork"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn or(self, other: T) -> dsl::OrNet where T: AsExpression, { Grouped(OrNet::new(self, other.as_expression())) } /// Creates a PostgreSQL `-` expression. /// /// This operator subtracts an address from an address to compute the distance between the two /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # hosts { /// # id -> Integer, /// # address -> Inet, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # #[cfg(feature = "ipnetwork")] /// # fn run_test() -> QueryResult<()> { /// # use self::hosts::dsl::*; /// # use ipnetwork::IpNetwork; /// # use std::str::FromStr; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS hosts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE hosts (id SERIAL PRIMARY KEY, address INET NOT NULL)").execute(conn).unwrap(); /// diesel::insert_into(hosts) /// .values(vec![address.eq(IpNetwork::from_str("10.0.2.53").unwrap())]) /// .execute(conn)?; /// /// let offset = hosts /// .select(address.diff(IpNetwork::from_str("10.0.2.42").unwrap())) /// .first::(conn)?; /// assert_eq!(offset, 11); /// # Ok(()) /// # } /// # /// # #[cfg(not(feature = "ipnetwork"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn diff(self, other: T) -> dsl::DifferenceNet where T: AsExpression, { Grouped(DifferenceNet::new(self, other.as_expression())) } } impl PgNetExpressionMethods for T where T: Expression, T::SqlType: InetOrCidr, { } /// PostgreSQL specific methods present on JSONB expressions. #[cfg(feature = "postgres_backend")] pub trait PgJsonbExpressionMethods: Expression + Sized { /// Creates a PostgreSQL `||` expression. /// /// This operator concatenates two JSONB values and returns JSONB value /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # contacts { /// # id -> Integer, /// # name -> VarChar, /// # address -> Jsonb, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use self::contacts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS contacts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE contacts ( /// # id SERIAL PRIMARY KEY, /// # name VARCHAR NOT NULL, /// # address JSONB NOT NULL /// # )").execute(conn).unwrap(); /// # /// let santas_address: serde_json::Value = serde_json::json!({ /// "street": "Article Circle Expressway 1", /// "city": "North Pole", /// "postcode": "99705", /// "state": "Alaska" /// }); /// diesel::insert_into(contacts) /// .values((name.eq("Claus"), address.eq(&santas_address))) /// .execute(conn)?; /// /// let to_concatenate: serde_json::Value = serde_json::json!({ /// "continent": "NA", /// "planet": "Earth" /// }); /// /// let final_address: serde_json::Value = serde_json::json!({ /// "street": "Article Circle Expressway 1", /// "city": "North Pole", /// "postcode": "99705", /// "state": "Alaska", /// "continent": "NA", /// "planet": "Earth" /// }); /// /// let final_address_db = contacts.select(address.concat(&to_concatenate)).get_result::(conn)?; /// assert_eq!(final_address, final_address_db); /// # Ok(()) /// # } /// # #[cfg(not(feature = "serde_json"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn concat(self, other: T) -> dsl::Concat where Self::SqlType: SqlType, T: AsExpression, { Grouped(Concat::new(self, other.as_expression())) } /// Creates a PostgreSQL `?` expression. /// /// This operator checks if the right hand side string exists as a top-level key within the JSONB /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # contacts { /// # id -> Integer, /// # name -> VarChar, /// # address -> Jsonb, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use self::contacts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS contacts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE contacts ( /// # id SERIAL PRIMARY KEY, /// # name VARCHAR NOT NULL, /// # address JSONB NOT NULL /// # )").execute(conn).unwrap(); /// # /// let santas_address: serde_json::Value = serde_json::json!({ /// "street": "Article Circle Expressway 1", /// "city": "North Pole", /// "postcode": "99705", /// "state": "Alaska" /// }); /// diesel::insert_into(contacts) /// .values((name.eq("Claus"), address.eq(&santas_address))) /// .execute(conn)?; /// /// let key_exists = contacts.select(address.has_key("street")).get_result::(conn)?; /// assert!(key_exists); /// /// let santas_with_address_postcode = contacts.select(id).filter(address.has_key("postcode")).get_result::(conn)?; /// assert_eq!(1, santas_with_address_postcode); /// # Ok(()) /// # } /// # #[cfg(not(feature = "serde_json"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn has_key(self, other: T) -> dsl::HasKeyJsonb where T: AsExpression, { Grouped(HasKeyJsonb::new(self, other.as_expression())) } /// Creates a PostgreSQL `?|` expression. /// /// This operator checks if any of the strings in the right hand side array exists as top level key in the given JSONB /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # contacts { /// # id -> Integer, /// # name -> VarChar, /// # address -> Jsonb, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use self::contacts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS contacts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE contacts ( /// # id SERIAL PRIMARY KEY, /// # name VARCHAR NOT NULL, /// # address JSONB NOT NULL /// # )").execute(conn).unwrap(); /// # /// let santas_address: serde_json::Value = serde_json::json!({ /// "street": "Article Circle Expressway 1", /// "city": "North Pole", /// "postcode": "99705", /// "state": "Alaska" /// }); /// diesel::insert_into(contacts) /// .values((name.eq("Claus"), address.eq(&santas_address))) /// .execute(conn)?; /// /// let any_key_exists = contacts.select(address.has_any_key(vec!["street", "city", "rudolf"])).get_result::(conn)?; /// assert!(any_key_exists); /// /// let santas_with_address_postcode = contacts.select(id).filter(address.has_any_key(vec!["street", "city", "rudolf"])).get_result::(conn)?; /// assert_eq!(1, santas_with_address_postcode); /// # Ok(()) /// # } /// # #[cfg(not(feature = "serde_json"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// `````` fn has_any_key(self, other: T) -> dsl::HasAnyKeyJsonb where T: AsExpression>, { Grouped(HasAnyKeyJsonb::new(self, other.as_expression())) } /// Creates a PostgreSQL `?&` expression. /// /// This operator checks if all the strings in the right hand side array exist as top level keys in the given JSONB /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # contacts { /// # id -> Integer, /// # name -> VarChar, /// # address -> Jsonb, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use self::contacts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS contacts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE contacts ( /// # id SERIAL PRIMARY KEY, /// # name VARCHAR NOT NULL, /// # address JSONB NOT NULL /// # )").execute(conn).unwrap(); /// # /// let santas_address: serde_json::Value = serde_json::json!({ /// "street": "Article Circle Expressway 1", /// "city": "North Pole", /// "postcode": "99705", /// "state": "Alaska" /// }); /// diesel::insert_into(contacts) /// .values((name.eq("Claus"), address.eq(&santas_address))) /// .execute(conn)?; /// /// let all_keys_exist = contacts.select(address.has_all_keys(vec!["street", "city", "postcode"])).get_result::(conn)?; /// assert!(all_keys_exist); /// /// let santas_with_address_postcode = contacts.select(id).filter(address.has_all_keys(vec!["street", "city", "postcode"])).get_result::(conn)?; /// assert_eq!(1, santas_with_address_postcode); /// # Ok(()) /// # } /// # #[cfg(not(feature = "serde_json"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// `````` fn has_all_keys(self, other: T) -> dsl::HasAllKeysJsonb where T: AsExpression>, { Grouped(HasAllKeysJsonb::new(self, other.as_expression())) } /// Creates a PostgreSQL `@>` expression. /// /// This operator checks whether left hand side JSONB value contains right hand side JSONB value /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # contacts { /// # id -> Integer, /// # name -> VarChar, /// # address -> Jsonb, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use self::contacts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS contacts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE contacts ( /// # id SERIAL PRIMARY KEY, /// # name VARCHAR NOT NULL, /// # address JSONB NOT NULL /// # )").execute(conn).unwrap(); /// # /// let easter_bunny_address: serde_json::Value = serde_json::json!({ /// "street": "123 Carrot Road", /// "province": "Easter Island", /// "region": "Valparaíso", /// "country": "Chile", /// "postcode": "88888", /// }); /// diesel::insert_into(contacts) /// .values((name.eq("Bunny"), address.eq(&easter_bunny_address))) /// .execute(conn)?; /// /// let country_chile: serde_json::Value = serde_json::json!({"country": "Chile"}); /// let contains_country_chile = contacts.select(address.contains(&country_chile)).get_result::(conn)?; /// assert!(contains_country_chile); /// # Ok(()) /// # } /// # #[cfg(not(feature = "serde_json"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn contains(self, other: T) -> dsl::Contains where Self::SqlType: SqlType, T: AsExpression, { Grouped(Contains::new(self, other.as_expression())) } /// Creates a PostgreSQL `<@` expression. /// /// This operator checks whether left hand side JSONB value is contained by right hand side JSON value. /// `foo.contains(bar)` is the same as `bar.is_contained_by(foo)`. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # contacts { /// # id -> Integer, /// # name -> VarChar, /// # address -> Jsonb, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// # /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use self::contacts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS contacts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE contacts ( /// # id SERIAL PRIMARY KEY, /// # name VARCHAR NOT NULL, /// # address JSONB NOT NULL /// # )").execute(conn).unwrap(); /// # /// let partial_easter_bunny_address: serde_json::Value = serde_json::json!({ /// "street": "123 Carrot Road", /// "country": "Chile", /// }); /// diesel::insert_into(contacts) /// .values((name.eq("Bunny"), address.eq(&partial_easter_bunny_address))) /// .execute(conn)?; /// /// let full_easter_bunny_address: serde_json::Value = serde_json::json!({ /// "street": "123 Carrot Road", /// "province": "Easter Island", /// "region": "Valparaíso", /// "country": "Chile", /// "postcode": "88888", /// }); /// let address_is_contained_by = contacts.select(address.is_contained_by(&full_easter_bunny_address)).get_result::(conn)?; /// assert!(address_is_contained_by); /// # Ok(()) /// # } /// # #[cfg(not(feature = "serde_json"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` #[allow(clippy::wrong_self_convention)] // This is named after the sql operator fn is_contained_by(self, other: T) -> dsl::IsContainedBy where Self::SqlType: SqlType, T: AsExpression, { Grouped(IsContainedBy::new(self, other.as_expression())) } /// Creates a PostgreSQL `-` expression. /// /// This operator removes the value associated with the given key, that is provided on the /// Right Hand Side of the operator. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # contacts { /// # id -> Integer, /// # name -> VarChar, /// # address -> Jsonb, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use self::contacts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS contacts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE contacts ( /// # id SERIAL PRIMARY KEY, /// # name VARCHAR NOT NULL, /// # address JSONB NOT NULL /// # )").execute(conn) /// # .unwrap(); /// # /// let santas_address: serde_json::Value = serde_json::json!({ /// "street": "Article Circle Expressway 1", /// "city": "North Pole", /// "postcode": "99705", /// "state": "Alaska" /// }); /// diesel::insert_into(contacts) /// .values((name.eq("Claus"), address.eq(&santas_address))) /// .execute(conn)?; /// /// let santas_modified_address = contacts.select(address.remove("postcode")).get_result::(conn)?; /// assert_eq!(santas_modified_address, serde_json::json!({ /// "street": "Article Circle Expressway 1", /// "city": "North Pole", /// "state": "Alaska" /// })); /// diesel::insert_into(contacts) /// .values((name.eq("Claus"), address.eq(&santas_address))) /// .execute(conn)?; /// /// let santas_modified_address = contacts.select(address.remove(vec!["postcode", "state"])).get_result::(conn)?; /// assert_eq!(santas_modified_address, serde_json::json!({ /// "street": "Article Circle Expressway 1", /// "city": "North Pole", /// })); /// /// let robert_downey_jr_addresses: serde_json::Value = serde_json::json!([ /// { /// "street": "Somewhere In La 251", /// "city": "Los Angeles", /// "postcode": "12231223", /// "state": "California" /// }, /// { /// "street": "Somewhere In Ny 251", /// "city": "New York", /// "postcode": "3213212", /// "state": "New York" /// } /// ]); /// /// diesel::insert_into(contacts) /// .values((name.eq("Robert Downey Jr."), address.eq(&robert_downey_jr_addresses))) /// .execute(conn)?; /// /// let roberts_address_in_db = contacts /// .filter(name.eq("Robert Downey Jr.")) /// .select(address.remove(1)) /// .get_result::(conn)?; /// /// let roberts_first_address = serde_json::json!([{ /// "street": "Somewhere In La 251", /// "city": "Los Angeles", /// "postcode": "12231223", /// "state": "California" /// }]); /// assert_eq!(roberts_first_address, roberts_address_in_db); /// # Ok(()) /// # } /// # #[cfg(not(feature = "serde_json"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } fn remove( self, other: T, ) -> dsl::RemoveFromJsonb::SqlType> where T: JsonRemoveIndex, ::SqlType: SqlType, { Grouped(RemoveFromJsonb::new( self, other.into_json_index_expression(), )) } /// Creates a PostgreSQL `#-` expression. /// /// This operator removes the value associated with the given json path, that is provided on the /// Right Hand Side of the operator. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # contacts { /// # id -> Integer, /// # name -> VarChar, /// # address -> Jsonb, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use self::contacts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS contacts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE contacts ( /// # id SERIAL PRIMARY KEY, /// # name VARCHAR NOT NULL, /// # address JSONB NOT NULL /// # )").execute(conn) /// # .unwrap(); /// # /// let santas_address: serde_json::Value = serde_json::json!({ /// "street": "Article Circle Expressway 1", /// "city": "North Pole", /// "postcode": "99705", /// "state": "Alaska" /// }); /// diesel::insert_into(contacts) /// .values((name.eq("Claus"), address.eq(&santas_address))) /// .execute(conn)?; /// /// let santas_modified_address = contacts.select(address.remove("postcode")).get_result::(conn)?; /// assert_eq!(santas_modified_address, serde_json::json!({ /// "street": "Article Circle Expressway 1", /// "city": "North Pole", /// "state": "Alaska" /// })); /// diesel::insert_into(contacts) /// .values((name.eq("Claus"), address.eq(&santas_address))) /// .execute(conn)?; /// /// let santas_modified_address = contacts.select(address.remove_by_path(vec!["postcode"])).get_result::(conn)?; /// assert_eq!(santas_modified_address, serde_json::json!({ /// "street": "Article Circle Expressway 1", /// "city": "North Pole", /// "state": "Alaska" /// })); /// /// let robert_downey_jr_addresses: serde_json::Value = serde_json::json!([ /// { /// "street": "Somewhere In La 251", /// "city": "Los Angeles", /// "postcode": "12231223", /// "state": "California" /// }, /// { /// "street": "Somewhere In Ny 251", /// "city": "New York", /// "postcode": "3213212", /// "state": "New York" /// } /// ]); /// /// diesel::insert_into(contacts) /// .values((name.eq("Robert Downey Jr."), address.eq(&robert_downey_jr_addresses))) /// .execute(conn)?; /// /// let roberts_address_in_db = contacts /// .filter(name.eq("Robert Downey Jr.")) /// .select(address.remove_by_path(vec!["1", "postcode"])) /// .get_result::(conn)?; /// /// let roberts_address = serde_json::json!([ /// { /// "street": "Somewhere In La 251", /// "city": "Los Angeles", /// "postcode": "12231223", /// "state": "California" /// }, /// { /// "street": "Somewhere In Ny 251", /// "city": "New York", /// "state": "New York" /// } /// ]); /// assert_eq!(roberts_address, roberts_address_in_db); /// # Ok(()) /// # } /// # #[cfg(not(feature = "serde_json"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } fn remove_by_path(self, other: T) -> dsl::RemoveByPathFromJsonb where T: AsExpression>, { Grouped(RemoveByPathFromJsonb::new(self, other.as_expression())) } } impl PgJsonbExpressionMethods for T where T: Expression, T::SqlType: JsonbOrNullableJsonb, { } /// PostgreSQL specific methods present on JSON and JSONB expressions. #[cfg(feature = "postgres_backend")] pub trait PgAnyJsonExpressionMethods: Expression + Sized { /// Creates a PostgreSQL `->` expression. /// /// This operator extracts the value associated with the given key, that is provided on the /// Right Hand Side of the operator. /// /// Extracts n'th element of JSON array (array elements are indexed from zero, but negative integers count from the end). /// Extracts JSON object field with the given key. /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # contacts { /// # id -> Integer, /// # name -> VarChar, /// # address -> Jsonb, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use self::contacts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS contacts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE contacts ( /// # id SERIAL PRIMARY KEY, /// # name VARCHAR NOT NULL, /// # address JSONB NOT NULL /// # )").execute(conn) /// # .unwrap(); /// # /// let santas_address: serde_json::Value = serde_json::json!({ /// "street": "Article Circle Expressway 1", /// "city": "North Pole", /// "postcode": "99705", /// "state": "Alaska" /// }); /// diesel::insert_into(contacts) /// .values((name.eq("Claus"), address.eq(&santas_address))) /// .execute(conn)?; /// /// let santas_postcode = contacts.select(address.retrieve_as_object("postcode")).get_result::(conn)?; /// assert_eq!(santas_postcode, serde_json::json!("99705")); /// /// /// let robert_downey_jr_addresses: serde_json::Value = serde_json::json!([ /// { /// "street": "Somewhere In La 251", /// "city": "Los Angeles", /// "postcode": "12231223", /// "state": "California" /// }, /// { /// "street": "Somewhere In Ny 251", /// "city": "New York", /// "postcode": "3213212", /// "state": "New York" /// } /// ]); /// /// diesel::insert_into(contacts) /// .values((name.eq("Robert Downey Jr."), address.eq(&robert_downey_jr_addresses))) /// .execute(conn)?; /// /// let roberts_second_address_in_db = contacts /// .filter(name.eq("Robert Downey Jr.")) /// .select(address.retrieve_as_object(1)) /// .get_result::(conn)?; /// /// let roberts_second_address = serde_json::json!({ /// "street": "Somewhere In Ny 251", /// "city": "New York", /// "postcode": "3213212", /// "state": "New York" /// }); /// assert_eq!(roberts_second_address, roberts_second_address_in_db); /// # Ok(()) /// # } /// # #[cfg(not(feature = "serde_json"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn retrieve_as_object( self, other: T, ) -> dsl::RetrieveAsObjectJson::SqlType> where T: JsonIndex, ::SqlType: SqlType, { Grouped(RetrieveAsObjectJson::new( self, other.into_json_index_expression(), )) } /// Creates a PostgreSQL `->>` expression. /// /// This operator extracts the value associated with the given key, that is provided on the /// Right Hand Side of the operator. /// /// Extracts n'th element of JSON array (array elements are indexed from zero, but negative integers count from the end). /// Extracts JSON object field as Text with the given key. /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # contacts { /// # id -> Integer, /// # name -> VarChar, /// # address -> Jsonb, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use self::contacts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS contacts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE contacts ( /// # id SERIAL PRIMARY KEY, /// # name VARCHAR NOT NULL, /// # address JSONB NOT NULL /// # )").execute(conn) /// # .unwrap(); /// # /// let santas_address: serde_json::Value = serde_json::json!({ /// "street": "Article Circle Expressway 1", /// "city": "North Pole", /// "postcode": "99705", /// "state": "Alaska" /// }); /// diesel::insert_into(contacts) /// .values((name.eq("Claus"), address.eq(&santas_address))) /// .execute(conn)?; /// /// let santas_postcode = contacts.select(address.retrieve_as_text("postcode")).get_result::(conn)?; /// assert_eq!(santas_postcode, "99705"); /// /// /// let robert_downey_jr_addresses: serde_json::Value = serde_json::json!([ /// { /// "street": "Somewhere In La 251", /// "city": "Los Angeles", /// "postcode": "12231223", /// "state": "California" /// }, /// { /// "street": "Somewhere In Ny 251", /// "city": "New York", /// "postcode": "3213212", /// "state": "New York" /// } /// ]); /// /// diesel::insert_into(contacts) /// .values((name.eq("Robert Downey Jr."), address.eq(&robert_downey_jr_addresses))) /// .execute(conn)?; /// /// let roberts_second_address_in_db = contacts /// .filter(name.eq("Robert Downey Jr.")) /// .select(address.retrieve_as_text(1)) /// .get_result::(conn)?; /// /// let roberts_second_address = String::from( /// "{\"city\": \"New York\", \ /// \"state\": \"New York\", \ /// \"street\": \"Somewhere In Ny 251\", \ /// \"postcode\": \"3213212\"}" /// ); /// assert_eq!(roberts_second_address, roberts_second_address_in_db); /// # Ok(()) /// # } /// # #[cfg(not(feature = "serde_json"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn retrieve_as_text( self, other: T, ) -> dsl::RetrieveAsTextJson::SqlType> where T: JsonIndex, ::SqlType: SqlType, { Grouped(RetrieveAsTextJson::new( self, other.into_json_index_expression(), )) } /// Creates a PostgreSQL `#>` expression. /// /// This operator extracts the value associated with the given key, that is provided on the /// Right Hand Side of the operator. /// /// Extracts n'th element of JSON array (array elements are indexed from zero, but negative integers count from the end). /// Extracts JSON object field with the given key. /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # contacts { /// # id -> Integer, /// # name -> VarChar, /// # address -> Jsonb, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use self::contacts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS contacts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE contacts ( /// # id SERIAL PRIMARY KEY, /// # name VARCHAR NOT NULL, /// # address JSONB NOT NULL /// # )").execute(conn) /// # .unwrap(); /// # /// let robert_downey_jr_addresses: serde_json::Value = serde_json::json!([ /// { /// "street": "Somewhere In La 251", /// "city": "Los Angeles", /// "postcode": "12231223", /// "state": "California" /// }, /// { /// "street": "Somewhere In Ny 251", /// "city": "New York", /// "postcode": "3213212", /// "state": "New York" /// } /// ]); /// /// diesel::insert_into(contacts) /// .values((name.eq("Robert Downey Jr."), address.eq(&robert_downey_jr_addresses))) /// .execute(conn)?; /// /// let roberts_second_street_in_db = contacts /// .filter(name.eq("Robert Downey Jr.")) /// .select(address.retrieve_by_path_as_object(vec!["1", "street"])) /// .get_result::(conn)?; /// /// assert_eq!(roberts_second_street_in_db, serde_json::json!("Somewhere In Ny 251")); /// # Ok(()) /// # } /// # #[cfg(not(feature = "serde_json"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn retrieve_by_path_as_object( self, other: T, ) -> dsl::RetrieveByPathAsObjectJson where T: AsExpression>, { Grouped(RetrieveByPathAsObjectJson::new(self, other.as_expression())) } /// Creates a PostgreSQL `#>>` expression. /// /// This operator extracts the value associated with the given key, that is provided on the /// Right Hand Side of the operator. /// /// Extracts n'th element of JSON array (array elements are indexed from zero, but negative integers count from the end). /// Extracts JSON object field as Text with the given key. /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # contacts { /// # id -> Integer, /// # name -> VarChar, /// # address -> Jsonb, /// # } /// # } /// # /// # fn main() { /// # run_test().unwrap(); /// # } /// /// # #[cfg(feature = "serde_json")] /// # fn run_test() -> QueryResult<()> { /// # use self::contacts::dsl::*; /// # let conn = &mut establish_connection(); /// # diesel::sql_query("DROP TABLE IF EXISTS contacts").execute(conn).unwrap(); /// # diesel::sql_query("CREATE TABLE contacts ( /// # id SERIAL PRIMARY KEY, /// # name VARCHAR NOT NULL, /// # address JSONB NOT NULL /// # )").execute(conn) /// # .unwrap(); /// # /// let robert_downey_jr_addresses: serde_json::Value = serde_json::json!([ /// { /// "street": "Somewhere In La 251", /// "city": "Los Angeles", /// "postcode": "12231223", /// "state": "California" /// }, /// { /// "street": "Somewhere In Ny 251", /// "city": "New York", /// "postcode": "3213212", /// "state": "New York" /// } /// ]); /// /// diesel::insert_into(contacts) /// .values((name.eq("Robert Downey Jr."), address.eq(&robert_downey_jr_addresses))) /// .execute(conn)?; /// /// let roberts_second_street_in_db = contacts /// .filter(name.eq("Robert Downey Jr.")) /// .select(address.retrieve_by_path_as_text(vec!["1", "street"])) /// .get_result::(conn)?; /// /// assert_eq!(roberts_second_street_in_db, "Somewhere In Ny 251"); /// /// # Ok(()) /// # } /// # #[cfg(not(feature = "serde_json"))] /// # fn run_test() -> QueryResult<()> { /// # Ok(()) /// # } /// ``` fn retrieve_by_path_as_text( self, other: T, ) -> dsl::RetrieveByPathAsTextJson where T: AsExpression>, { Grouped(RetrieveByPathAsTextJson::new(self, other.as_expression())) } } #[doc(hidden)] impl PgAnyJsonExpressionMethods for T where T: Expression, T::SqlType: JsonOrNullableJsonOrJsonbOrNullableJsonb, { } /// PostgreSQL specific methods present on Binary expressions. #[cfg(feature = "postgres_backend")] pub trait PgBinaryExpressionMethods: Expression + Sized { /// Concatenates two PostgreSQL byte arrays using the `||` operator. /// /// # Example /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # users { /// # id -> Integer, /// # name -> Binary, /// # hair_color -> Nullable, /// # } /// # } /// # /// # fn main() { /// # use self::users::dsl::*; /// # use diesel::insert_into; /// # /// # let connection = &mut connection_no_data(); /// # diesel::sql_query("CREATE TABLE users ( /// # id INTEGER PRIMARY KEY, /// # name BYTEA NOT NULL, /// # hair_color BYTEA /// # )").execute(connection).unwrap(); /// # /// # insert_into(users) /// # .values(&vec![ /// # (id.eq(1), name.eq("Sean".as_bytes()), Some(hair_color.eq(Some("Green".as_bytes())))), /// # (id.eq(2), name.eq("Tess".as_bytes()), None), /// # ]) /// # .execute(connection) /// # .unwrap(); /// # /// let names = users.select(name.concat(" the Greatest".as_bytes())).load(connection); /// let expected_names = vec![ /// b"Sean the Greatest".to_vec(), /// b"Tess the Greatest".to_vec() /// ]; /// assert_eq!(Ok(expected_names), names); /// /// // If the value is nullable, the output will be nullable /// let names = users.select(hair_color.concat("ish".as_bytes())).load(connection); /// let expected_names = vec![ /// Some(b"Greenish".to_vec()), /// None, /// ]; /// assert_eq!(Ok(expected_names), names); /// # } /// ``` fn concat(self, other: T) -> dsl::Concat where Self::SqlType: SqlType, T: AsExpression, { Grouped(Concat::new(self, other.as_expression())) } /// Creates a PostgreSQL binary `LIKE` expression. /// /// This method is case sensitive. There is no case-insensitive /// equivalent as of PostgreSQL 14. /// /// # Examples /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # users { /// # id -> Integer, /// # name -> Binary, /// # } /// # } /// # /// # fn main() { /// # use self::users::dsl::*; /// # use diesel::insert_into; /// # /// # let connection = &mut connection_no_data(); /// # diesel::sql_query("CREATE TABLE users ( /// # id INTEGER PRIMARY KEY, /// # name BYTEA NOT NULL /// # )").execute(connection).unwrap(); /// # /// # insert_into(users) /// # .values(&vec![ /// # (id.eq(1), name.eq("Sean".as_bytes())), /// # (id.eq(2), name.eq("Tess".as_bytes())) /// # ]) /// # .execute(connection) /// # .unwrap(); /// # /// let starts_with_s = users /// .select(name) /// .filter(name.like(b"S%".to_vec())) /// .load(connection); /// assert_eq!(Ok(vec![b"Sean".to_vec()]), starts_with_s); /// # } /// ``` fn like(self, other: T) -> dsl::Like where Self::SqlType: SqlType, T: AsExpression, { Grouped(Like::new(self, other.as_expression())) } /// Creates a PostgreSQL binary `LIKE` expression. /// /// This method is case sensitive. There is no case-insensitive /// equivalent as of PostgreSQL 14. /// /// # Examples /// /// ```rust /// # include!("../../doctest_setup.rs"); /// # /// # table! { /// # users { /// # id -> Integer, /// # name -> Binary, /// # } /// # } /// # /// # fn main() { /// # use self::users::dsl::*; /// # use diesel::insert_into; /// # /// # let connection = &mut connection_no_data(); /// # diesel::sql_query("CREATE TABLE users ( /// # id INTEGER PRIMARY KEY, /// # name BYTEA NOT NULL /// # )").execute(connection).unwrap(); /// # /// # insert_into(users) /// # .values(&vec![ /// # (id.eq(1), name.eq("Sean".as_bytes())), /// # (id.eq(2), name.eq("Tess".as_bytes())) /// # ]) /// # .execute(connection) /// # .unwrap(); /// # /// let starts_with_s = users /// .select(name) /// .filter(name.not_like(b"S%".to_vec())) /// .load(connection); /// assert_eq!(Ok(vec![b"Tess".to_vec()]), starts_with_s); /// # } /// ``` fn not_like(self, other: T) -> dsl::NotLike where Self::SqlType: SqlType, T: AsExpression, { Grouped(NotLike::new(self, other.as_expression())) } } #[doc(hidden)] impl PgBinaryExpressionMethods for T where T: Expression, T::SqlType: BinaryOrNullableBinary, { } pub(in crate::pg) mod private { use crate::sql_types::{ AllAreNullable, Array, Binary, Cidr, Inet, Integer, Json, Jsonb, MaybeNullableType, Multirange, Nullable, Range, Record, SingleValue, SqlType, Text, }; use crate::{Expression, IntoSql}; /// Marker trait used to implement `ArrayExpressionMethods` on the appropriate /// types. Once coherence takes associated types into account, we can remove /// this trait. #[diagnostic::on_unimplemented( message = "`{Self}` is neither `diesel::sql_types::Array<_>` nor `diesel::sql_types::Nullable>`", note = "try to provide an expression that produces one of the expected sql types" )] pub trait ArrayOrNullableArray { type Inner; } impl ArrayOrNullableArray for Array { type Inner = T; } impl ArrayOrNullableArray for Nullable> { type Inner = T; } /// Marker trait used to implement `PgNetExpressionMethods` on the appropriate types. #[diagnostic::on_unimplemented( message = "`{Self}` is neither `diesel::sql_types::Inet`, `diesel::sql_types::Cidr`, `diesel::sql_types::Nullable` nor `diesel::sql_types::Nullable", note = "try to provide an expression that produces one of the expected sql types" )] pub trait InetOrCidr {} impl InetOrCidr for Inet {} impl InetOrCidr for Cidr {} impl InetOrCidr for Nullable {} impl InetOrCidr for Nullable {} /// Marker trait used to implement `PgTextExpressionMethods` on the appropriate /// types. Once coherence takes associated types into account, we can remove /// this trait. #[diagnostic::on_unimplemented( message = "`{Self}` is neither `diesel::sql_types::Text` nor `diesel::sql_types::Nullable`", note = "try to provide an expression that produces one of the expected sql types" )] pub trait TextOrNullableText {} impl TextOrNullableText for Text {} impl TextOrNullableText for Nullable {} /// Marker trait used to extract the inner type /// of our `Range` and `Multirange` sql type, used to implement `PgRangeExpressionMethods` pub trait RangeOrMultirange: SqlType + SingleValue { type Inner: SingleValue; } impl RangeOrMultirange for Range where Self: 'static, ST: SingleValue, { type Inner = ST; } impl RangeOrMultirange for Multirange where Self: 'static, ST: SingleValue, { type Inner = ST; } /// Marker trait used to implement `PgRangeExpressionMethods` on the appropriate /// types. Once coherence takes associated types into account, we can remove /// this trait. #[diagnostic::on_unimplemented( message = "`{Self}` is neither `diesel::sql_types::Range<_>` nor `diesel::sql_types::Nullable>`", note = "try to provide an expression that produces one of the expected sql types" )] pub trait RangeOrNullableRange { type Inner: SingleValue; } impl RangeOrNullableRange for Range { type Inner = ST; } impl RangeOrNullableRange for Nullable> { type Inner = ST; } /// Marker trait used to implement `PgRangeExpressionMethods` on the appropriate /// types. Once coherence takes associated types into account, we can remove /// this trait. #[diagnostic::on_unimplemented( message = "`{Self}` is neither `diesel::sql_types::Range<_>` nor `diesel::sql_types::Nullable>`", note = "try to provide an expression that produces one of the expected sql types" )] pub trait MultirangeOrNullableMultirange { type Inner: SingleValue; type Range: SingleValue; } impl MultirangeOrNullableMultirange for Multirange { type Inner = ST; type Range = Range; } impl MultirangeOrNullableMultirange for Nullable> { type Inner = ST; type Range = Nullable>; } /// Marker trait used to implement `PgRangeExpressionMethods` on the appropriate /// types. Once coherence takes associated types into account, we can remove /// this trait. #[diagnostic::on_unimplemented( message = "`{Self}` is neither `diesel::sql_types::Range<_>` nor `diesel::sql_types::Multirange<_>`", note = "try to provide an expression that produces one of the expected sql types" )] pub trait MultirangeOrRangeMaybeNullable { type Inner: SingleValue; } impl MultirangeOrRangeMaybeNullable for Range { type Inner = ST; } impl MultirangeOrRangeMaybeNullable for Nullable> { type Inner = ST; } impl MultirangeOrRangeMaybeNullable for Multirange { type Inner = ST; } impl MultirangeOrRangeMaybeNullable for Nullable> { type Inner = ST; } /// Marker trait used to implement `PgJsonbExpressionMethods` on the appropriate types. #[diagnostic::on_unimplemented( message = "`{Self}` is neither `diesel::sql_types::Jsonb` nor `diesel::sql_types::Nullable`", note = "try to provide an expression that produces one of the expected sql types" )] pub trait JsonbOrNullableJsonb {} impl JsonbOrNullableJsonb for Jsonb {} impl JsonbOrNullableJsonb for Nullable {} #[diagnostic::on_unimplemented( message = "`{Self}` is neither `diesel::sql_types::Json` nor `diesel::sql_types::Nullable`", note = "try to provide an expression that produces one of the expected sql types" )] pub trait JsonOrNullableJson {} impl JsonOrNullableJson for Json {} impl JsonOrNullableJson for Nullable {} /// A trait that describes valid json indices used by postgresql pub trait JsonRemoveIndex { /// The Expression node created by this index type type Expression: Expression; /// Convert a index value into the corresponding index expression fn into_json_index_expression(self) -> Self::Expression; } impl<'a> JsonRemoveIndex for &'a str { type Expression = crate::dsl::AsExprOf<&'a str, crate::sql_types::Text>; fn into_json_index_expression(self) -> Self::Expression { self.into_sql::() } } impl JsonRemoveIndex for String { type Expression = crate::dsl::AsExprOf; fn into_json_index_expression(self) -> Self::Expression { self.into_sql::() } } impl JsonRemoveIndex for Vec { type Expression = crate::dsl::AsExprOf>; fn into_json_index_expression(self) -> Self::Expression { self.into_sql::>() } } impl JsonRemoveIndex for Vec<&str> { type Expression = crate::dsl::AsExprOf>; fn into_json_index_expression(self) -> Self::Expression { self.into_sql::>() } } impl<'a> JsonRemoveIndex for &'a [&'a str] { type Expression = crate::dsl::AsExprOf>; fn into_json_index_expression(self) -> Self::Expression { self.into_sql::>() } } impl JsonRemoveIndex for i32 { type Expression = crate::dsl::AsExprOf; fn into_json_index_expression(self) -> Self::Expression { self.into_sql::() } } impl JsonRemoveIndex for T where T: Expression, T::SqlType: TextArrayOrTextOrInteger, { type Expression = Self; fn into_json_index_expression(self) -> Self::Expression { self } } #[diagnostic::on_unimplemented( message = "`{Self}` is neither `diesel::sql_types::Text`, `diesel::sql_types::Integer` nor `diesel::sql_types::Array`", note = "try to provide an expression that produces one of the expected sql types" )] pub trait TextArrayOrTextOrInteger {} impl TextArrayOrTextOrInteger for Array {} impl TextArrayOrTextOrInteger for Text {} impl TextArrayOrTextOrInteger for Integer {} /// Marker trait used to implement `PgAnyJsonExpressionMethods` on the appropriate types. #[diagnostic::on_unimplemented( message = "`{Self}` is neither `diesel::sql_types::Json`, `diesel::sql_types::Jsonb`, `diesel::sql_types::Nullable` nor `diesel::sql_types::Nullable`", note = "try to provide an expression that produces one of the expected sql types" )] pub trait JsonOrNullableJsonOrJsonbOrNullableJsonb {} impl JsonOrNullableJsonOrJsonbOrNullableJsonb for Json {} impl JsonOrNullableJsonOrJsonbOrNullableJsonb for Nullable {} impl JsonOrNullableJsonOrJsonbOrNullableJsonb for Jsonb {} impl JsonOrNullableJsonOrJsonbOrNullableJsonb for Nullable {} pub trait JsonIndex { type Expression: Expression; fn into_json_index_expression(self) -> Self::Expression; } impl<'a> JsonIndex for &'a str { type Expression = crate::dsl::AsExprOf<&'a str, crate::sql_types::Text>; fn into_json_index_expression(self) -> Self::Expression { self.into_sql::() } } impl JsonIndex for String { type Expression = crate::dsl::AsExprOf; fn into_json_index_expression(self) -> Self::Expression { self.into_sql::() } } impl JsonIndex for i32 { type Expression = crate::dsl::AsExprOf; fn into_json_index_expression(self) -> Self::Expression { self.into_sql::() } } impl JsonIndex for T where T: Expression, T::SqlType: TextOrInteger, { type Expression = Self; fn into_json_index_expression(self) -> Self::Expression { self } } #[diagnostic::on_unimplemented( message = "`{Self}` is neither `diesel::sql_types::Text` nor `diesel::sql_types::Integer`", note = "try to provide an expression that produces one of the expected sql types" )] pub trait TextOrInteger {} impl TextOrInteger for Text {} impl TextOrInteger for Integer {} #[diagnostic::on_unimplemented( message = "`{Self}` is neither `diesel::sql_types::Binary` nor `diesel::sql_types::Nullable`", note = "try to provide an expression that produces one of the expected sql types" )] pub trait BinaryOrNullableBinary {} impl BinaryOrNullableBinary for Binary {} impl BinaryOrNullableBinary for Nullable {} pub trait MaybeNullableValue: SingleValue { type Out: SingleValue; } impl MaybeNullableValue for T where T: SingleValue, T::IsNull: MaybeNullableType, >::Out: SingleValue, { type Out = >::Out; } #[diagnostic::on_unimplemented( message = "`{Self}` is neither `Array`, `Array>`,\ `Nullable>` nor `diesel::sql_types::Nullable>>`", note = "try to provide an expression that produces one of the expected sql types" )] pub trait TextArrayOrNullableTextArray {} impl TextArrayOrNullableTextArray for Array {} impl TextArrayOrNullableTextArray for Array> {} impl TextArrayOrNullableTextArray for Nullable> {} impl TextArrayOrNullableTextArray for Nullable>> {} #[diagnostic::on_unimplemented( message = "`{Self}` is neither `Record` nor `Nullable>`", note = "try to provide an expression that produces one of the expected sql types" )] pub trait RecordOrNullableRecord {} impl RecordOrNullableRecord for Record {} impl RecordOrNullableRecord for Nullable> {} pub trait CombinedAllNullableValue: SingleValue { type Out: SingleValue; } impl CombinedAllNullableValue for T where T: SingleValue, O: SingleValue, T::IsNull: AllAreNullable, >::Out: MaybeNullableType, <>::Out as MaybeNullableType>::Out: SingleValue, { type Out = <>::Out as MaybeNullableType>::Out; } }