database: Abstract insert functions with impl_to_insert! macro

This commit is contained in:
Steffo 2024-12-10 07:14:42 +01:00
parent d11934952f
commit 16e6d6b97a
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
4 changed files with 13819 additions and 139 deletions

View file

@ -6,6 +6,7 @@
mod schema; mod schema;
pub mod meta; pub mod meta;
mod macros;
pub use diesel; pub use diesel;
pub use diesel_async; pub use diesel_async;

View file

@ -0,0 +1,24 @@
/// Implement `to_insert` and `to_inserted_async` for the given `$insert_type`, resulting in the given `$result_type`, adding a record to `$table_type`.
#[macro_export]
macro_rules! impl_to_insert {
($insert_type:ty => $table_type:expr => $result_type:ty) => {
impl $insert_type {
/// Syncronously insert the record into the table, returning the full inserted record.
pub fn to_inserted(self, conn: &mut diesel::pg::PgConnection) -> diesel::QueryResult<$result_type> {
use diesel::RunQueryDsl;
self.insert_into($table_type)
.get_result::<$result_type>(conn)
}
/// Asyncronously insert the record into the table, returning the full inserted record.
pub async fn to_inserted_async(self, conn: &mut diesel_async::AsyncPgConnection) -> diesel::QueryResult<$result_type> {
use diesel_async::RunQueryDsl;
self.insert_into($table_type)
.get_result::<$result_type>(conn)
.await
}
}
};
}

View file

@ -35,8 +35,8 @@ use diesel_async::AsyncPgConnection;
use mediatype::MediaTypeBuf; use mediatype::MediaTypeBuf;
use uuid::Uuid; use uuid::Uuid;
use super::schema; use crate::schema;
use crate::impl_to_insert;
/// Wrapper to use [`mime::Mime`] with [`diesel`]. /// Wrapper to use [`mime::Mime`] with [`diesel`].
#[derive(Debug, Clone, PartialEq, Eq, FromSqlRow, AsExpression)] #[derive(Debug, Clone, PartialEq, Eq, FromSqlRow, AsExpression)]
@ -417,29 +417,6 @@ impl MetaSubject {
} }
} }
impl MetaSubjectInsert {
/// Syncronously insert the record into the table, returning the full inserted record.
pub fn insert(self, conn: &mut PgConnection) -> QueryResult<MetaSubject> {
use diesel::RunQueryDsl;
use schema::meta_subjects::dsl::*;
insert_into(meta_subjects)
.values(self)
.get_result(conn)
}
/// Asyncronously insert the record into the table, returning the full inserted record.
pub async fn ainsert(self, conn: &mut AsyncPgConnection) -> QueryResult<MetaSubject> {
use diesel_async::RunQueryDsl;
use schema::meta_subjects::dsl::*;
insert_into(meta_subjects)
.values(self)
.get_result(conn)
.await
}
}
impl MetaAlias { impl MetaAlias {
/// Synchronously query the records matching the given document and resource. /// Synchronously query the records matching the given document and resource.
pub fn query_matching(conn: &mut PgConnection, doc: &str, subject: &str) -> QueryResult<Vec<Self>> { pub fn query_matching(conn: &mut PgConnection, doc: &str, subject: &str) -> QueryResult<Vec<Self>> {
@ -475,29 +452,6 @@ impl MetaAlias {
} }
} }
impl MetaAliasInsert {
/// Syncronously insert the record into the table, returning the full inserted record.
pub fn insert(self, conn: &mut PgConnection) -> QueryResult<MetaAlias> {
use diesel::RunQueryDsl;
use schema::meta_aliases::dsl::*;
insert_into(meta_aliases)
.values(self)
.get_result(conn)
}
/// Asyncronously insert the record into the table, returning the full inserted record.
pub async fn ainsert(self, conn: &mut AsyncPgConnection) -> QueryResult<MetaAlias> {
use diesel_async::RunQueryDsl;
use schema::meta_aliases::dsl::*;
insert_into(meta_aliases)
.values(self)
.get_result(conn)
.await
}
}
impl MetaLink { impl MetaLink {
/// Synchronously query the records matching the given document and resource. /// Synchronously query the records matching the given document and resource.
pub async fn query_matching(conn: &mut PgConnection, doc: &str, subject: &str) -> QueryResult<Vec<Self>> { pub async fn query_matching(conn: &mut PgConnection, doc: &str, subject: &str) -> QueryResult<Vec<Self>> {
@ -533,29 +487,6 @@ impl MetaLink {
} }
} }
impl MetaLinkInsert {
/// Syncronously insert the record into the table, returning the full inserted record.
pub fn insert(self, conn: &mut PgConnection) -> QueryResult<MetaLink> {
use diesel::RunQueryDsl;
use schema::meta_links::dsl::*;
insert_into(meta_links)
.values(self)
.get_result(conn)
}
/// Asyncronously insert the record into the table, returning the full inserted record.
pub async fn ainsert(self, conn: &mut AsyncPgConnection) -> QueryResult<MetaLink> {
use diesel_async::RunQueryDsl;
use schema::meta_links::dsl::*;
insert_into(meta_links)
.values(self)
.get_result(conn)
.await
}
}
impl MetaLinkProperty { impl MetaLinkProperty {
/// Synchronously query the records belonging to the given [`MetaLink`]s. /// Synchronously query the records belonging to the given [`MetaLink`]s.
pub fn query_by_link(conn: &mut PgConnection, links: &[MetaLink]) -> QueryResult<Vec<Self>> { pub fn query_by_link(conn: &mut PgConnection, links: &[MetaLink]) -> QueryResult<Vec<Self>> {
@ -575,29 +506,6 @@ impl MetaLinkProperty {
} }
} }
impl MetaLinkPropertyInsert {
/// Syncronously insert the record into the table, returning the full inserted record.
pub fn insert(self, conn: &mut PgConnection) -> QueryResult<MetaLinkProperty> {
use diesel::RunQueryDsl;
use schema::meta_link_properties::dsl::*;
insert_into(meta_link_properties)
.values(self)
.get_result(conn)
}
/// Asyncronously insert the record into the table, returning the full inserted record.
pub async fn ainsert(self, conn: &mut AsyncPgConnection) -> QueryResult<MetaLinkProperty> {
use diesel_async::RunQueryDsl;
use schema::meta_link_properties::dsl::*;
insert_into(meta_link_properties)
.values(self)
.get_result(conn)
.await
}
}
impl MetaLinkTitle { impl MetaLinkTitle {
/// Synchronously query the records belonging to the given [`MetaLink`]s. /// Synchronously query the records belonging to the given [`MetaLink`]s.
pub fn query_by_link(conn: &mut PgConnection, links: &[MetaLink]) -> QueryResult<Vec<Self>> { pub fn query_by_link(conn: &mut PgConnection, links: &[MetaLink]) -> QueryResult<Vec<Self>> {
@ -617,29 +525,6 @@ impl MetaLinkTitle {
} }
} }
impl MetaLinkTitleInsert {
/// Syncronously insert the record into the table, returning the full inserted record.
pub fn insert(self, conn: &mut PgConnection) -> QueryResult<MetaLinkTitle> {
use diesel::RunQueryDsl;
use schema::meta_link_titles::dsl::*;
insert_into(meta_link_titles)
.values(self)
.get_result(conn)
}
/// Asyncronously insert the record into the table, returning the full inserted record.
pub async fn ainsert(self, conn: &mut AsyncPgConnection) -> QueryResult<MetaLinkTitle> {
use diesel_async::RunQueryDsl;
use schema::meta_link_titles::dsl::*;
insert_into(meta_link_titles)
.values(self)
.get_result(conn)
.await
}
}
impl MetaProperty { impl MetaProperty {
/// Synchronously query the records matching the given document and resource. /// Synchronously query the records matching the given document and resource.
pub fn query_matching(conn: &mut PgConnection, doc: &str, subject: &str) -> QueryResult<Vec<Self>> { pub fn query_matching(conn: &mut PgConnection, doc: &str, subject: &str) -> QueryResult<Vec<Self>> {
@ -675,25 +560,9 @@ impl MetaProperty {
} }
} }
impl MetaPropertyInsert { impl_to_insert!(MetaSubjectInsert => schema::meta_subjects::table => MetaSubject);
/// Syncronously insert the record into the table, returning the full inserted record. impl_to_insert!(MetaAliasInsert => schema::meta_aliases::table => MetaAlias);
pub fn insert(self, conn: &mut PgConnection) -> QueryResult<MetaProperty> { impl_to_insert!(MetaLinkInsert => schema::meta_links::table => MetaLink);
use diesel::RunQueryDsl; impl_to_insert!(MetaLinkPropertyInsert => schema::meta_link_properties::table => MetaLinkProperty);
use schema::meta_properties::dsl::*; impl_to_insert!(MetaLinkTitleInsert => schema::meta_link_titles::table => MetaLinkTitle);
impl_to_insert!(MetaPropertyInsert => schema::meta_properties::table => MetaProperty);
insert_into(meta_properties)
.values(self)
.get_result(conn)
}
/// Asyncronously insert the record into the table, returning the full inserted record.
pub async fn ainsert(self, conn: &mut AsyncPgConnection) -> QueryResult<MetaProperty> {
use diesel_async::RunQueryDsl;
use schema::meta_properties::dsl::*;
insert_into(meta_properties)
.values(self)
.get_result(conn)
.await
}
}

13786
expansion.rs Normal file

File diff suppressed because it is too large Load diff