From 9d780c24035d3a9fb12968d3009a28724046dc3a Mon Sep 17 00:00:00 2001 From: dull b Date: Mon, 24 Jul 2023 18:43:13 +0000 Subject: [PATCH] Insert trait --- crates/db_schema/src/traits.rs | 38 ++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/crates/db_schema/src/traits.rs b/crates/db_schema/src/traits.rs index 7cafaef7f..1c17fbdd7 100644 --- a/crates/db_schema/src/traits.rs +++ b/crates/db_schema/src/traits.rs @@ -7,13 +7,15 @@ use diesel::{ dsl, expression::{AsExpression, TypedExpressionType}, expression_methods::ExpressionMethods, - query_builder::{DeleteStatement, IntoUpdateTarget}, + insert_into, + query_builder::{DeleteStatement, InsertStatement, IntoUpdateTarget}, query_dsl::methods::{FindDsl, LimitDsl}, result::Error, sql_types::SqlType, AsChangeset, Expression, Insertable, + QuerySource, Table, }; use diesel_async::{ @@ -26,6 +28,28 @@ use std::hash::Hash; /// Returned by `diesel::delete` pub type Delete = DeleteStatement<::Table, ::WhereClause>; +pub trait Insert<'a, Tb, U> +where + Self: 'a, + &'a Self: Insertable, + InsertStatement>::Values>: + LoadQuery<'a, AsyncPgConnection, U> + 'a + Send, + <&'a Self as Insertable>::Values: 'a, + Tb: QuerySource, +{ +} + +impl<'a, Tb, U, T> Insert<'a, Tb, U> for T +where + Self: 'a, + &'a Self: Insertable, + InsertStatement>::Values>: + LoadQuery<'a, AsyncPgConnection, U> + 'a + Send, + <&'a Self as Insertable>::Values: 'a, + Tb: QuerySource, +{ +} + /*Self: Send + 'static + Sized + HasTable, Self::Table: FindDsl + Send + Sized + 'static, @@ -46,23 +70,19 @@ where SqlType + TypedExpressionType, for<'a> Delete>: ExecuteDsl + 'a + Send, { - /*for<'a> &'a Self::InsertForm: Insertable, - for<'a> InsertStatement>::Values>: - LoadQuery<'a, AsyncPgConnection, Self> + 'a, - for<'a> <&'a Self::InsertForm as Insertable>::Values: 'a,*/ - type InsertForm; + type InsertForm: for<'a> Insert<'a, Self::Table, Self>; type UpdateForm; type IdType: Hash + Eq + Sized + Send + AsExpression<<::PrimaryKey as Expression>::SqlType>; - async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result; - /*{ + + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result { let query = insert_into(Self::table()).values(form); let conn = &mut *get_conn(pool).await?; query.get_result::(conn).await - }*/ + } async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result { let query = Self::table().find(id); let conn = &mut *get_conn(pool).await?;