pub trait PgAnyJsonExpressionMethods: Expression + Sized {
// Provided methods
fn retrieve_as_object<T>(
self,
other: T
) -> RetrieveAsObjectJson<Self, T::Expression, <T::Expression as Expression>::SqlType>
where T: JsonIndex,
<T::Expression as Expression>::SqlType: SqlType { ... }
fn retrieve_as_text<T>(
self,
other: T
) -> RetrieveAsTextJson<Self, T::Expression, <T::Expression as Expression>::SqlType>
where T: JsonIndex,
<T::Expression as Expression>::SqlType: SqlType { ... }
fn retrieve_by_path_as_object<T>(
self,
other: T
) -> RetrieveByPathAsObjectJson<Self, T::Expression>
where T: AsExpression<Array<Text>> { ... }
fn retrieve_by_path_as_text<T>(
self,
other: T
) -> RetrieveByPathAsTextJson<Self, T::Expression>
where T: AsExpression<Array<Text>> { ... }
}
postgres_backend
only.Expand description
PostgreSQL specific methods present on JSON and JSONB expressions.
Provided Methods§
sourcefn retrieve_as_object<T>(
self,
other: T
) -> RetrieveAsObjectJson<Self, T::Expression, <T::Expression as Expression>::SqlType>where
T: JsonIndex,
<T::Expression as Expression>::SqlType: SqlType,
fn retrieve_as_object<T>( self, other: T ) -> RetrieveAsObjectJson<Self, T::Expression, <T::Expression as Expression>::SqlType>where T: JsonIndex, <T::Expression as Expression>::SqlType: SqlType,
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
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::<serde_json::Value>(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::<serde_json::Value>(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);
sourcefn retrieve_as_text<T>(
self,
other: T
) -> RetrieveAsTextJson<Self, T::Expression, <T::Expression as Expression>::SqlType>where
T: JsonIndex,
<T::Expression as Expression>::SqlType: SqlType,
fn retrieve_as_text<T>( self, other: T ) -> RetrieveAsTextJson<Self, T::Expression, <T::Expression as Expression>::SqlType>where T: JsonIndex, <T::Expression as Expression>::SqlType: SqlType,
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
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::<String>(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::<String>(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);
sourcefn retrieve_by_path_as_object<T>(
self,
other: T
) -> RetrieveByPathAsObjectJson<Self, T::Expression>where
T: AsExpression<Array<Text>>,
fn retrieve_by_path_as_object<T>( self, other: T ) -> RetrieveByPathAsObjectJson<Self, T::Expression>where T: AsExpression<Array<Text>>,
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
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::<serde_json::Value>(conn)?;
assert_eq!(roberts_second_street_in_db, serde_json::json!("Somewhere In Ny 251"));
sourcefn retrieve_by_path_as_text<T>(
self,
other: T
) -> RetrieveByPathAsTextJson<Self, T::Expression>where
T: AsExpression<Array<Text>>,
fn retrieve_by_path_as_text<T>( self, other: T ) -> RetrieveByPathAsTextJson<Self, T::Expression>where T: AsExpression<Array<Text>>,
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
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::<String>(conn)?;
assert_eq!(roberts_second_street_in_db, "Somewhere In Ny 251");