diesel::prelude

Trait PgTextExpressionMethods

Source
pub trait PgTextExpressionMethods: Expression + Sized {
    // Provided methods
    fn ilike<T>(self, other: T) -> ILike<Self, T>
       where T: AsExpression<Text> { ... }
    fn not_ilike<T>(self, other: T) -> NotILike<Self, T>
       where T: AsExpression<Text> { ... }
    fn similar_to<T>(self, other: T) -> SimilarTo<Self, T>
       where T: AsExpression<Text> { ... }
    fn not_similar_to<T>(self, other: T) -> NotSimilarTo<Self, T>
       where T: AsExpression<Text> { ... }
    fn is_json(self) -> IsJson<Self> { ... }
    fn is_not_json(self) -> IsNotJson<Self> { ... }
    fn is_json_object(self) -> IsJsonObject<Self> { ... }
    fn is_not_json_object(self) -> IsNotJsonObject<Self> { ... }
    fn is_json_array(self) -> IsJsonArray<Self> { ... }
    fn is_not_json_array(self) -> IsNotJsonArray<Self> { ... }
    fn is_json_scalar(self) -> IsJsonScalar<Self> { ... }
    fn is_not_json_scalar(self) -> IsNotJsonScalar<Self> { ... }
}
Available on crate feature postgres_backend only.
Expand description

PostgreSQL specific methods present on text expressions.

Provided Methods§

Source

fn ilike<T>(self, other: T) -> ILike<Self, T>
where T: AsExpression<Text>,

Creates a PostgreSQL ILIKE expression

§Example
let starts_with_s = animals
    .select(species)
    .filter(name.ilike("s%").or(species.ilike("s%")))
    .get_results::<String>(connection)?;
assert_eq!(vec!["spider"], starts_with_s);
Source

fn not_ilike<T>(self, other: T) -> NotILike<Self, T>
where T: AsExpression<Text>,

Creates a PostgreSQL NOT ILIKE expression

§Example
let doesnt_start_with_s = animals
    .select(species)
    .filter(name.not_ilike("s%").and(species.not_ilike("s%")))
    .get_results::<String>(connection)?;
assert_eq!(vec!["dog"], doesnt_start_with_s);
Source

fn similar_to<T>(self, other: T) -> SimilarTo<Self, T>
where T: AsExpression<Text>,

Creates a PostgreSQL SIMILAR TO expression

§Example
let starts_with_s = animals
    .select(species)
    .filter(name.similar_to("s%").or(species.similar_to("s%")))
    .get_results::<String>(connection)?;
assert_eq!(vec!["spider"], starts_with_s);
Source

fn not_similar_to<T>(self, other: T) -> NotSimilarTo<Self, T>
where T: AsExpression<Text>,

Creates a PostgreSQL NOT SIMILAR TO expression

§Example
let doesnt_start_with_s = animals
    .select(species)
    .filter(name.not_similar_to("s%").and(species.not_similar_to("s%")))
    .get_results::<String>(connection)?;
assert_eq!(vec!["dog"], doesnt_start_with_s);
Source

fn is_json(self) -> IsJson<Self>

Creates a PostgreSQL IS JSON expression. Requires PostgreSQL>=16

This operator returns true whether an object is a valid JSON

§Example

let res = diesel::select(("1".into_sql::<Text>().is_json())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("[1,2,3]".into_sql::<Text>().is_json())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("{\"products\": [1,2,3]}".into_sql::<Text>().is_json())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("(1,2,3)".into_sql::<Text>().is_json())).get_result::<bool>(conn)?;
assert_eq!(res, false);
Source

fn is_not_json(self) -> IsNotJson<Self>

Creates a PostgreSQL IS NOT JSON expression. Requires PostgreSQL>=16

This operator returns true whether an object is not a valid JSON

§Example

let res = diesel::select(("1".into_sql::<Text>().is_not_json())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("[1,2,3]".into_sql::<Text>().is_not_json())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("{\"products\": [1,2,3]}".into_sql::<Text>().is_not_json())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("(1,2,3)".into_sql::<Text>().is_not_json())).get_result::<bool>(conn)?;
assert_eq!(res, true);
Source

fn is_json_object(self) -> IsJsonObject<Self>

Creates a PostgreSQL IS JSON OBJECT expression. Requires PostgreSQL>=16

This operator returns true whether an object is a valid JSON

§Example

let res = diesel::select(("123".into_sql::<Text>().is_json_object())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("abc".into_sql::<Text>().is_json_object())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("{\"products\": [1,2,3]}".into_sql::<Text>().is_json_object())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("[1,2,3]".into_sql::<Text>().is_json_object())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("\"abc\"".into_sql::<Text>().is_json_object())).get_result::<bool>(conn)?;
assert_eq!(res, false);
Source

fn is_not_json_object(self) -> IsNotJsonObject<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

let res = diesel::select(("123".into_sql::<Text>().is_not_json_object())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("abc".into_sql::<Text>().is_not_json_object())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("{\"products\": [1,2,3]}".into_sql::<Text>().is_not_json_object())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("[1,2,3]".into_sql::<Text>().is_not_json_object())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("\"abc\"".into_sql::<Text>().is_not_json_object())).get_result::<bool>(conn)?;
assert_eq!(res, true);
Source

fn is_json_array(self) -> IsJsonArray<Self>

Creates a PostgreSQL IS JSON ARRAY expression. Requires PostgreSQL>=16

This operator returns true whether an object is a valid JSON ARRAY

§Example

let res = diesel::select(("123".into_sql::<Text>().is_json_array())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("abc".into_sql::<Text>().is_json_array())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("{\"products\": [1,2,3]}".into_sql::<Text>().is_json_array())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("[1,2,3]".into_sql::<Text>().is_json_array())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("\"abc\"".into_sql::<Text>().is_json_array())).get_result::<bool>(conn)?;
assert_eq!(res, false);
Source

fn is_not_json_array(self) -> IsNotJsonArray<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

let res = diesel::select(("123".into_sql::<Text>().is_not_json_array())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("abc".into_sql::<Text>().is_not_json_array())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("{\"products\": [1,2,3]}".into_sql::<Text>().is_not_json_array())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("[1,2,3]".into_sql::<Text>().is_not_json_array())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("\"abc\"".into_sql::<Text>().is_not_json_array())).get_result::<bool>(conn)?;
assert_eq!(res, true);
Source

fn is_json_scalar(self) -> IsJsonScalar<Self>

Creates a PostgreSQL IS JSON SCALAR expression. Requires PostgreSQL>=16

This operator returns true whether an object is a valid JSON SCALAR

§Example

let res = diesel::select(("123".into_sql::<Text>().is_json_scalar())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("abc".into_sql::<Text>().is_json_scalar())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("{\"products\": [1,2,3]}".into_sql::<Text>().is_json_scalar())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("[1,2,3]".into_sql::<Text>().is_json_scalar())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("\"abc\"".into_sql::<Text>().is_json_scalar())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(sql::<Nullable<Text>>("NULL").is_json_scalar()).get_result::<Option<bool>>(conn)?;
assert!(res.is_none());
Source

fn is_not_json_scalar(self) -> IsNotJsonScalar<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

let res = diesel::select(("123".into_sql::<Text>().is_not_json_scalar())).get_result::<bool>(conn)?;
assert_eq!(res, false);
let res = diesel::select(("abc".into_sql::<Text>().is_not_json_scalar())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("{\"products\": [1,2,3]}".into_sql::<Text>().is_not_json_scalar())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("[1,2,3]".into_sql::<Text>().is_not_json_scalar())).get_result::<bool>(conn)?;
assert_eq!(res, true);
let res = diesel::select(("\"abc\"".into_sql::<Text>().is_not_json_scalar())).get_result::<bool>(conn)?;
assert_eq!(res, false);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> PgTextExpressionMethods for T
where T: Expression, T::SqlType: TextOrNullableText,