From d11934952f2d46098b8adf953e91b37b96b3729e Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 7 Dec 2024 03:22:22 +0100 Subject: [PATCH] `database`: Add `insert` functions to all `Insert` structs --- acrate_database/src/meta.rs | 139 ++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/acrate_database/src/meta.rs b/acrate_database/src/meta.rs index c527ad8..2d014a6 100644 --- a/acrate_database/src/meta.rs +++ b/acrate_database/src/meta.rs @@ -28,6 +28,7 @@ use diesel::deserialize::FromSql; use diesel::{AsExpression, Associations, FromSqlRow, Identifiable, Insertable, IntoSql, PgTextExpressionMethods, QueryResult, Queryable, QueryableByName, Selectable, SelectableHelper, ExpressionMethods, BelongingToDsl}; +use diesel::dsl::insert_into; use diesel::pg::{Pg, PgConnection}; use diesel::serialize::{Output, ToSql}; use diesel_async::AsyncPgConnection; @@ -416,6 +417,29 @@ impl MetaSubject { } } +impl MetaSubjectInsert { + /// Syncronously insert the record into the table, returning the full inserted record. + pub fn insert(self, conn: &mut PgConnection) -> QueryResult { + 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 { + 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> { @@ -451,6 +475,29 @@ impl MetaAlias { } } +impl MetaAliasInsert { + /// Syncronously insert the record into the table, returning the full inserted record. + pub fn insert(self, conn: &mut PgConnection) -> QueryResult { + 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 { + 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> { @@ -486,6 +533,29 @@ impl MetaLink { } } +impl MetaLinkInsert { + /// Syncronously insert the record into the table, returning the full inserted record. + pub fn insert(self, conn: &mut PgConnection) -> QueryResult { + 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 { + 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> { @@ -505,6 +575,29 @@ impl MetaLinkProperty { } } +impl MetaLinkPropertyInsert { + /// Syncronously insert the record into the table, returning the full inserted record. + pub fn insert(self, conn: &mut PgConnection) -> QueryResult { + 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 { + 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> { @@ -524,6 +617,29 @@ impl MetaLinkTitle { } } +impl MetaLinkTitleInsert { + /// Syncronously insert the record into the table, returning the full inserted record. + pub fn insert(self, conn: &mut PgConnection) -> QueryResult { + 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 { + 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> { @@ -558,3 +674,26 @@ impl MetaProperty { .await } } + +impl MetaPropertyInsert { + /// Syncronously insert the record into the table, returning the full inserted record. + pub fn insert(self, conn: &mut PgConnection) -> QueryResult { + 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 { + use diesel_async::RunQueryDsl; + use schema::meta_properties::dsl::*; + + insert_into(meta_properties) + .values(self) + .get_result(conn) + .await + } +}