From 1f108177d0092067551226e3581f7dfda628a668 Mon Sep 17 00:00:00 2001 From: dull b Date: Fri, 14 Jul 2023 04:14:48 +0000 Subject: [PATCH] Default impl for Crud::read --- crates/db_schema/src/traits.rs | 62 ++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/crates/db_schema/src/traits.rs b/crates/db_schema/src/traits.rs index f690c22bf..aa87322a8 100644 --- a/crates/db_schema/src/traits.rs +++ b/crates/db_schema/src/traits.rs @@ -8,8 +8,7 @@ use diesel::{ deserialize::{FromSqlRow, Queryable}, dsl::insert_into, pg::Pg, - query_builder::{AsQuery, IntoUpdateTarget, Query, QueryFragment, QueryId}, - query_dsl::methods::FindDsl, + query_builder::{AsQuery, IntoUpdateTarget, Query, QueryFragment, QueryId,Only}, result::Error, AsChangeset, Column, @@ -18,7 +17,14 @@ use diesel::{ Insertable, QuerySource, Table, - query_dsl::methods::LimitDsl, + query_dsl::methods::{SelectDsl,LimitDsl,FilterDsl}, + query_source::{Once,AppearsInFromClause}, + expression::{ValidGrouping,is_aggregate, IsContainedInGroupBy,is_contained_in_group_by}, + expression_methods::ExpressionMethods, + sql_types::{SqlType,is_nullable::NotNull}, + expression::AsExpression, + SelectableExpression, + dsl, }; use diesel_async::{AsyncConnection, RunQueryDsl,methods::LoadQuery}; use std::hash::Hash; @@ -26,42 +32,57 @@ use std::hash::Hash; #[async_trait] pub trait Crud where - Self: Send + HasTable + Send + 'static + FromSqlRow<::SqlType, Pg>, + /*Self: Send + HasTable + Send + 'static + FromSqlRow<::SqlType, Pg>, Self::Table: Send + FindDsl, >::Output: LimitDsl + Send, for<'conn> <>::Output as LimitDsl>::Output: Send + LoadQuery<'static, DbConn<'conn>, Self> /*+ 'query*/ + Send, - ::SqlType: Send, + ::SqlType: Send,*/ + + /*Self: Send + 'static + HasTable + Sized, + Self::Table: Send + 'static + AppearsInFromClause, Count = Once> + QueryFragment + HasTable + IntoUpdateTarget::Query as IntoUpdateTarget>::WhereClause> + AppearsInFromClause, + ::Query: IntoUpdateTarget, + //Only: AppearsInFromClause, Count = Once>, + ::PrimaryKey: Send + 'static + QueryFragment + ValidGrouping<(), IsAggregate = is_aggregate::No> + /*IsContainedInGroupBy<::PrimaryKey, Output = is_contained_in_group_by::Yes> +*/ Column
+ AppearsInFromClause> + SelectableExpression> + Expression + Sized, + <::PrimaryKey as Expression>::SqlType: SqlType,*/ + + Self: Send + 'static + Sized + HasTable, + Self::Table: FilterDsl::PrimaryKey, Self::IdType>> + Send + Sized + 'static, + ::PrimaryKey, Self::IdType>>>::Output: LimitDsl + Send + Sized + 'static, + //for<'conn> diesel::helper_types::Limit<::PrimaryKey, Self::IdType>>>::Output>: LoadQuery<'static, DbConn<'conn>, Self> + Send + 'static + Sized, + <::PrimaryKey as Expression>::SqlType: SqlType, + ::PrimaryKey: ExpressionMethods + Send + Sized + 'static, + //::AllColumns: Send + Sized + 'static, + //::Query: Send + Sized + 'static, + + //::PrimaryKey, Self::IdType>>>::Output: { type InsertForm; type UpdateForm; - type IdType: Hash + Eq + Send; - async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result - where - Self: Sized; /*{ + type IdType: Hash + Eq + Send + 'static + Sized + AsExpression<<::PrimaryKey as Expression>::SqlType>; + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result; /*{ let conn = &mut get_conn(pool).await?; insert_into(Self::table()) .values(form) .get_result::(conn) .await }*/ - async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result - where - Self: Sized, + async fn read<'conn, 'conn2: 'conn, 'a: 'conn>(pool: &'conn2 mut DbPool<'a>, id: Self::IdType) -> Result + where diesel::helper_types::Limit<::PrimaryKey, Self::IdType>>>::Output>: LoadQuery<'static, DbConn<'conn>, Self> + Send + 'static + Sized { - let mut conn = get_conn(pool).await?; - let table = Self::table(); - let find_dsl_output = FindDsl::find(table, id); - RunQueryDsl::first::(find_dsl_output, &mut conn).await + let mut conn = get_conn::<'conn2, 'a>(pool).await?; + let col = Self::table().primary_key(); + // FindDsl is not used because it uses a private trait + let query = FilterDsl::filter(Self::table(), ExpressionMethods::eq(col, id)); + let future = RunQueryDsl::first::<'static, 'conn, Self>(query, &mut conn); + future.await } /// when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column. async fn update( pool: &mut DbPool<'_>, id: Self::IdType, form: &Self::UpdateForm, - ) -> Result - where - Self: Sized; /*{ + ) -> Result; /*{ let conn = &mut get_conn(pool).await?; diesel::update(Self::table().find(id)) .set(form) @@ -69,9 +90,6 @@ where .await }*/ async fn delete(pool: &mut DbPool<'_>, id: Self::IdType) -> Result - where - Self: Sized, - Self::IdType: Send, { Err(Error::NotFound) /*let conn = &mut get_conn(pool).await?;