database
: Abstract insert
functions with impl_to_insert!
macro
This commit is contained in:
parent
d11934952f
commit
16e6d6b97a
4 changed files with 13819 additions and 139 deletions
|
@ -6,6 +6,7 @@
|
|||
mod schema;
|
||||
|
||||
pub mod meta;
|
||||
mod macros;
|
||||
|
||||
pub use diesel;
|
||||
pub use diesel_async;
|
||||
|
|
24
acrate_database/src/macros.rs
Normal file
24
acrate_database/src/macros.rs
Normal 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
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
|
@ -35,8 +35,8 @@ use diesel_async::AsyncPgConnection;
|
|||
use mediatype::MediaTypeBuf;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::schema;
|
||||
|
||||
use crate::schema;
|
||||
use crate::impl_to_insert;
|
||||
|
||||
/// Wrapper to use [`mime::Mime`] with [`diesel`].
|
||||
#[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 {
|
||||
/// Synchronously query the records matching the given document and resource.
|
||||
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 {
|
||||
/// 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>> {
|
||||
|
@ -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 {
|
||||
/// Synchronously query the records belonging to the given [`MetaLink`]s.
|
||||
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 {
|
||||
/// Synchronously query the records belonging to the given [`MetaLink`]s.
|
||||
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 {
|
||||
/// Synchronously query the records matching the given document and resource.
|
||||
pub fn query_matching(conn: &mut PgConnection, doc: &str, subject: &str) -> QueryResult<Vec<Self>> {
|
||||
|
@ -675,25 +560,9 @@ impl MetaProperty {
|
|||
}
|
||||
}
|
||||
|
||||
impl MetaPropertyInsert {
|
||||
/// Syncronously insert the record into the table, returning the full inserted record.
|
||||
pub fn insert(self, conn: &mut PgConnection) -> QueryResult<MetaProperty> {
|
||||
use diesel::RunQueryDsl;
|
||||
use schema::meta_properties::dsl::*;
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
impl_to_insert!(MetaSubjectInsert => schema::meta_subjects::table => MetaSubject);
|
||||
impl_to_insert!(MetaAliasInsert => schema::meta_aliases::table => MetaAlias);
|
||||
impl_to_insert!(MetaLinkInsert => schema::meta_links::table => MetaLink);
|
||||
impl_to_insert!(MetaLinkPropertyInsert => schema::meta_link_properties::table => MetaLinkProperty);
|
||||
impl_to_insert!(MetaLinkTitleInsert => schema::meta_link_titles::table => MetaLinkTitle);
|
||||
impl_to_insert!(MetaPropertyInsert => schema::meta_properties::table => MetaProperty);
|
||||
|
|
13786
expansion.rs
Normal file
13786
expansion.rs
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue