use diesel::{Associations, Identifiable, Insertable, QueryResult, Queryable, QueryableByName, Selectable}; use diesel_async::AsyncPgConnection; use uuid::Uuid; use super::schema; #[derive(Debug, Queryable, QueryableByName, Identifiable, Selectable, Insertable)] #[diesel(table_name = schema::meta_aliases)] pub struct MetaAlias { pub id: Uuid, pub document: String, pub pattern: String, pub alias: String, } #[derive(Debug, Queryable, QueryableByName, Identifiable, Selectable, Insertable)] #[diesel(table_name = schema::meta_links)] pub struct MetaLink { pub id: Uuid, pub document: String, pub pattern: String, pub rel: String, pub r#type: Option, pub href: Option, } #[derive(Debug, Queryable, QueryableByName, Identifiable, Selectable, Insertable, Associations)] #[diesel(belongs_to(MetaLink))] #[diesel(table_name = schema::meta_link_properties)] pub struct MetaLinkProperty { pub id: Uuid, pub meta_link_id: Uuid, pub rel: String, pub value: Option, } #[derive(Debug, Queryable, QueryableByName, Identifiable, Selectable, Insertable)] #[diesel(table_name = schema::meta_properties)] pub struct MetaProperty { pub id: Uuid, pub document: String, pub pattern: String, pub value: Option, } impl MetaAlias { pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult> { use diesel::prelude::*; use diesel_async::RunQueryDsl; use schema::meta_aliases::dsl::*; let document_is_equal = document.eq(doc); let subject_matches_pattern = subject.into_sql::().ilike(pattern); meta_aliases .filter(document_is_equal) .filter(subject_matches_pattern) .select(Self::as_select()) .load(conn) .await } } impl MetaLink { pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult> { use diesel::prelude::*; use diesel_async::RunQueryDsl; use schema::meta_links::dsl::*; let document_is_equal = document.eq(doc); let subject_matches_pattern = subject.into_sql::().ilike(pattern); meta_links .filter(document_is_equal) .filter(subject_matches_pattern) .select(Self::as_select()) .load(conn) .await } pub async fn query_properties(&self, conn: &mut AsyncPgConnection) -> QueryResult> { use diesel::prelude::*; use diesel_async::RunQueryDsl; MetaLinkProperty::belonging_to(self) .load(conn) .await } } impl MetaProperty { pub async fn query_matching(conn: &mut AsyncPgConnection, doc: &str, subject: &str) -> QueryResult> { use diesel::prelude::*; use diesel_async::RunQueryDsl; use schema::meta_properties::dsl::*; let document_is_equal = document.eq(doc); let subject_matches_pattern = subject.into_sql::().ilike(pattern); meta_properties .filter(document_is_equal) .filter(subject_matches_pattern) .select(Self::as_select()) .load(conn) .await } }