pub trait PgJsonbExpressionMethods: Sized + Expression {
    // Provided methods
    fn concat<T>(
        self,
        other: T,
    ) -> Grouped<Concat<Self, <T as AsExpression<Self::SqlType>>::Expression>>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn has_key<T>(
        self,
        other: T,
    ) -> Grouped<HasKeyJsonb<Self, <T as AsExpression<Text>>::Expression>>
       where T: AsExpression<Text> { ... }
    fn has_any_key<T>(
        self,
        other: T,
    ) -> Grouped<HasAnyKeyJsonb<Self, <T as AsExpression<Array<Text>>>::Expression>>
       where T: AsExpression<Array<Text>> { ... }
    fn has_all_keys<T>(
        self,
        other: T,
    ) -> Grouped<HasAllKeysJsonb<Self, <T as AsExpression<Array<Text>>>::Expression>>
       where T: AsExpression<Array<Text>> { ... }
    fn contains<T>(
        self,
        other: T,
    ) -> Grouped<Contains<Self, <T as AsExpression<Self::SqlType>>::Expression>>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn is_contained_by<T>(
        self,
        other: T,
    ) -> Grouped<IsContainedBy<Self, <T as AsExpression<Self::SqlType>>::Expression>>
       where Self::SqlType: SqlType,
             T: AsExpression<Self::SqlType> { ... }
    fn remove<T>(
        self,
        other: T,
    ) -> Grouped<RemoveFromJsonb<Self, <<T as JsonRemoveIndex>::Expression as AsExpression<<<T as JsonRemoveIndex>::Expression as Expression>::SqlType>>::Expression>>
       where T: JsonRemoveIndex,
             <<T as JsonRemoveIndex>::Expression as Expression>::SqlType: SqlType { ... }
    fn remove_by_path<T>(
        self,
        other: T,
    ) -> Grouped<RemoveByPathFromJsonb<Self, <<T as AsExpression<Array<Text>>>::Expression as AsExpression<Array<Text>>>::Expression>>
       where T: AsExpression<Array<Text>> { ... }
}
Expand description

PostgreSQL specific methods present on JSONB expressions.

Provided Methods§

source

fn concat<T>( self, other: T, ) -> Grouped<Concat<Self, <T as AsExpression<Self::SqlType>>::Expression>>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL || expression.

This operator concatenates two JSONB values and returns JSONB value

§Example

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::<serde_json::Value>(conn)?;
assert_eq!(final_address, final_address_db);
source

fn has_key<T>( self, other: T, ) -> Grouped<HasKeyJsonb<Self, <T as AsExpression<Text>>::Expression>>
where T: AsExpression<Text>,

Creates a PostgreSQL ? expression.

This operator checks if the right hand side string exists as a top-level key within the JSONB

§Example

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::<bool>(conn)?;
assert!(key_exists);

let santas_with_address_postcode = contacts.select(id).filter(address.has_key("postcode")).get_result::<i32>(conn)?;
assert_eq!(1, santas_with_address_postcode);
source

fn has_any_key<T>( self, other: T, ) -> Grouped<HasAnyKeyJsonb<Self, <T as AsExpression<Array<Text>>>::Expression>>
where T: AsExpression<Array<Text>>,

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

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::<bool>(conn)?;
assert!(any_key_exists);

let santas_with_address_postcode = contacts.select(id).filter(address.has_any_key(vec!["street", "city", "rudolf"])).get_result::<i32>(conn)?;
assert_eq!(1, santas_with_address_postcode);
source

fn has_all_keys<T>( self, other: T, ) -> Grouped<HasAllKeysJsonb<Self, <T as AsExpression<Array<Text>>>::Expression>>
where T: AsExpression<Array<Text>>,

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

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::<bool>(conn)?;
assert!(all_keys_exist);

let santas_with_address_postcode = contacts.select(id).filter(address.has_all_keys(vec!["street", "city", "postcode"])).get_result::<i32>(conn)?;
assert_eq!(1, santas_with_address_postcode);
source

fn contains<T>( self, other: T, ) -> Grouped<Contains<Self, <T as AsExpression<Self::SqlType>>::Expression>>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

Creates a PostgreSQL @> expression.

This operator checks whether left hand side JSONB value contains right hand side JSONB value

§Example
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::<bool>(conn)?;
assert!(contains_country_chile);
source

fn is_contained_by<T>( self, other: T, ) -> Grouped<IsContainedBy<Self, <T as AsExpression<Self::SqlType>>::Expression>>
where Self::SqlType: SqlType, T: AsExpression<Self::SqlType>,

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
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::<bool>(conn)?;
assert!(address_is_contained_by);
source

fn remove<T>( self, other: T, ) -> Grouped<RemoveFromJsonb<Self, <<T as JsonRemoveIndex>::Expression as AsExpression<<<T as JsonRemoveIndex>::Expression as Expression>::SqlType>>::Expression>>
where T: JsonRemoveIndex, <<T as JsonRemoveIndex>::Expression as Expression>::SqlType: SqlType,

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

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::<serde_json::Value>(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::<serde_json::Value>(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::<serde_json::Value>(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);
source

fn remove_by_path<T>( self, other: T, ) -> Grouped<RemoveByPathFromJsonb<Self, <<T as AsExpression<Array<Text>>>::Expression as AsExpression<Array<Text>>>::Expression>>
where T: AsExpression<Array<Text>>,

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

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::<serde_json::Value>(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::<serde_json::Value>(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::<serde_json::Value>(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);

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T> PgJsonbExpressionMethods for T
where T: Expression, <T as Expression>::SqlType: JsonbOrNullableJsonb,