pub trait PgJsonbExpressionMethods: Expression + Sized {
// Provided methods
fn concat<T>(self, other: T) -> ConcatJsonb<Self, T>
where T: AsExpression<Jsonb> { ... }
fn has_key<T>(self, other: T) -> HasKeyJsonb<Self, T>
where T: AsExpression<VarChar> { ... }
fn has_any_key<T>(self, other: T) -> HasAnyKeyJsonb<Self, T>
where T: AsExpression<Array<VarChar>> { ... }
fn has_all_keys<T>(self, other: T) -> HasAllKeysJsonb<Self, T>
where T: AsExpression<Array<VarChar>> { ... }
fn contains<T>(self, other: T) -> ContainsJsonb<Self, T>
where T: AsExpression<Jsonb> { ... }
fn is_contained_by<T>(self, other: T) -> IsContainedByJsonb<Self, T>
where T: AsExpression<Jsonb> { ... }
fn remove<T>(
self,
other: T
) -> RemoveFromJsonb<Self, T::Expression, <T::Expression as Expression>::SqlType>
where T: JsonRemoveIndex,
<T::Expression as Expression>::SqlType: SqlType { ... }
fn remove_by_path<T>(
self,
other: T
) -> RemoveByPathFromJsonb<Self, T::Expression>
where T: AsExpression<Array<Text>> { ... }
}
Expand description
PostgreSQL specific methods present on JSONB expressions.
Provided Methods§
sourcefn concat<T>(self, other: T) -> ConcatJsonb<Self, T>where
T: AsExpression<Jsonb>,
fn concat<T>(self, other: T) -> ConcatJsonb<Self, T>where T: AsExpression<Jsonb>,
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);
sourcefn has_key<T>(self, other: T) -> HasKeyJsonb<Self, T>where
T: AsExpression<VarChar>,
fn has_key<T>(self, other: T) -> HasKeyJsonb<Self, T>where T: AsExpression<VarChar>,
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);
sourcefn has_any_key<T>(self, other: T) -> HasAnyKeyJsonb<Self, T>where
T: AsExpression<Array<VarChar>>,
fn has_any_key<T>(self, other: T) -> HasAnyKeyJsonb<Self, T>where T: AsExpression<Array<VarChar>>,
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);
sourcefn has_all_keys<T>(self, other: T) -> HasAllKeysJsonb<Self, T>where
T: AsExpression<Array<VarChar>>,
fn has_all_keys<T>(self, other: T) -> HasAllKeysJsonb<Self, T>where T: AsExpression<Array<VarChar>>,
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);
sourcefn contains<T>(self, other: T) -> ContainsJsonb<Self, T>where
T: AsExpression<Jsonb>,
fn contains<T>(self, other: T) -> ContainsJsonb<Self, T>where T: AsExpression<Jsonb>,
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);
sourcefn is_contained_by<T>(self, other: T) -> IsContainedByJsonb<Self, T>where
T: AsExpression<Jsonb>,
fn is_contained_by<T>(self, other: T) -> IsContainedByJsonb<Self, T>where T: AsExpression<Jsonb>,
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);
sourcefn remove<T>(
self,
other: T
) -> RemoveFromJsonb<Self, T::Expression, <T::Expression as Expression>::SqlType>where
T: JsonRemoveIndex,
<T::Expression as Expression>::SqlType: SqlType,
fn remove<T>( self, other: T ) -> RemoveFromJsonb<Self, T::Expression, <T::Expression as Expression>::SqlType>where T: JsonRemoveIndex, <T::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);
sourcefn remove_by_path<T>(
self,
other: T
) -> RemoveByPathFromJsonb<Self, T::Expression>where
T: AsExpression<Array<Text>>,
fn remove_by_path<T>( self, other: T ) -> RemoveByPathFromJsonb<Self, T::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);