pull/3707/head
dull b 2023-07-14 05:05:09 +00:00
parent 8745a6cc45
commit ceab862021
1 changed files with 61 additions and 63 deletions

View File

@ -1,74 +1,71 @@
use crate::{ use crate::{
newtypes::{CommunityId, DbUrl, PersonId}, newtypes::{CommunityId, DbUrl, PersonId},
utils::{get_conn, DbPool, DbConn}, utils::{get_conn, DbConn, DbPool},
}; };
use diesel::{ use diesel::{
associations::HasTable, associations::HasTable,
backend::Backend, backend::Backend,
deserialize::{FromSqlRow, Queryable}, deserialize::{FromSqlRow, Queryable},
dsl,
dsl::insert_into, dsl::insert_into,
expression::{
is_aggregate,
is_contained_in_group_by,
AsExpression,
IsContainedInGroupBy,
ValidGrouping,
},
expression_methods::ExpressionMethods,
pg::Pg, pg::Pg,
query_builder::{AsQuery, IntoUpdateTarget, Query, QueryFragment, QueryId,Only}, query_builder::{AsQuery, IntoUpdateTarget, Only, Query, QueryFragment, QueryId},
query_dsl::methods::{FilterDsl, LimitDsl, SelectDsl},
query_source::{AppearsInFromClause, Once},
result::Error, result::Error,
sql_types::{is_nullable::NotNull, SqlType},
AsChangeset, AsChangeset,
Column, Column,
Expression, Expression,
Identifiable, Identifiable,
Insertable, Insertable,
QuerySource, QuerySource,
Table,
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, SelectableExpression,
dsl, Table,
}; };
use diesel_async::{AsyncConnection, RunQueryDsl,methods::LoadQuery}; use diesel_async::{methods::LoadQuery, AsyncConnection, RunQueryDsl};
use std::hash::Hash; use std::hash::Hash;
#[async_trait] #[async_trait]
pub trait Crud pub trait Crud
where where
/*Self: Send + HasTable + Send + 'static + FromSqlRow<<Self::Table as AsQuery>::SqlType, Pg>,
Self::Table: Send + FindDsl<Self::IdType>,
<Self::Table as FindDsl<Self::IdType>>::Output: LimitDsl + Send,
for<'conn> <<Self::Table as FindDsl<Self::IdType>>::Output as LimitDsl>::Output:
Send + LoadQuery<'static, DbConn<'conn>, Self> /*+ 'query*/ + Send,
<Self::Table as AsQuery>::SqlType: Send,*/
/*Self: Send + 'static + HasTable + Sized,
Self::Table: Send + 'static + AppearsInFromClause<Only<Self::Table>, Count = Once> + QueryFragment<Pg> + HasTable<Table = Self::Table> + IntoUpdateTarget<WhereClause = <<Self::Table as AsQuery>::Query as IntoUpdateTarget>::WhereClause> + AppearsInFromClause<Self::Table, Count = Once>,
<Self::Table as AsQuery>::Query: IntoUpdateTarget,
//Only<Self::Table>: AppearsInFromClause<Only<Self::Table>, Count = Once>,
<Self::Table as Table>::PrimaryKey: Send + 'static + QueryFragment<Pg> + ValidGrouping<(), IsAggregate = is_aggregate::No> + /*IsContainedInGroupBy<<Self::Table as Table>::PrimaryKey, Output = is_contained_in_group_by::Yes> +*/ Column<Table = Self::Table> + AppearsInFromClause<Only<Self::Table>> + SelectableExpression<Only<Self::Table>> + Expression + Sized,
<<Self::Table as Table>::PrimaryKey as Expression>::SqlType: SqlType<IsNull = NotNull>,*/
Self: Send + 'static + Sized + HasTable, Self: Send + 'static + Sized + HasTable,
Self::Table: FilterDsl<dsl::Eq<<Self::Table as Table>::PrimaryKey, Self::IdType>> + Send + Sized + 'static, Self::Table:
<Self::Table as FilterDsl<dsl::Eq<<Self::Table as Table>::PrimaryKey, Self::IdType>>>::Output: LimitDsl + Send + Sized + 'static, FilterDsl<dsl::Eq<<Self::Table as Table>::PrimaryKey, Self::IdType>> + Send + Sized + 'static,
//for<'conn> diesel::helper_types::Limit<<Self::Table as FilterDsl<dsl::Eq<<Self::Table as Table>::PrimaryKey, Self::IdType>>>::Output>: LoadQuery<'static, DbConn<'conn>, Self> + Send + 'static + Sized, <Self::Table as FilterDsl<dsl::Eq<<Self::Table as Table>::PrimaryKey, Self::IdType>>>::Output:
LimitDsl + Send + Sized + 'static,
<<Self::Table as Table>::PrimaryKey as Expression>::SqlType: SqlType, <<Self::Table as Table>::PrimaryKey as Expression>::SqlType: SqlType,
<Self::Table as Table>::PrimaryKey: ExpressionMethods + Send + Sized + 'static, <Self::Table as Table>::PrimaryKey: ExpressionMethods + Send + Sized + 'static,
//<Self::Table as Table>::AllColumns: Send + Sized + 'static,
//<Self::Table as AsQuery>::Query: Send + Sized + 'static,
//<Self::Table as FilterDsl<dsl::Eq<<Self::Table as Table>::PrimaryKey, Self::IdType>>>::Output:
{ {
type InsertForm; type InsertForm;
type UpdateForm; type UpdateForm;
type IdType: Hash + Eq + Send + 'static + Sized + AsExpression<<<Self::Table as Table>::PrimaryKey as Expression>::SqlType>; type IdType: Hash
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error>; /*{ + Eq
let conn = &mut get_conn(pool).await?; + Send
insert_into(Self::table()) + 'static
.values(form) + Sized
.get_result::<Self>(conn) + AsExpression<<<Self::Table as Table>::PrimaryKey as Expression>::SqlType>;
.await async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error>;
}*/ /*{
let conn = &mut get_conn(pool).await?;
insert_into(Self::table())
.values(form)
.get_result::<Self>(conn)
.await
}*/
async fn read(pool: &'async_trait mut DbPool<'_>, id: Self::IdType) -> Result<Self, Error> async fn read(pool: &'async_trait mut DbPool<'_>, id: Self::IdType) -> Result<Self, Error>
where diesel::helper_types::Limit<<Self::Table as FilterDsl<dsl::Eq<<Self::Table as Table>::PrimaryKey, Self::IdType>>>::Output>: LoadQuery<'static, DbConn<'async_trait>, Self> + Send + 'static + Sized where
diesel::helper_types::Limit<
<Self::Table as FilterDsl<dsl::Eq<<Self::Table as Table>::PrimaryKey, Self::IdType>>>::Output,
>: LoadQuery<'static, DbConn<'async_trait>, Self> + Send + 'static + Sized,
{ {
let col = Self::table().primary_key(); let col = Self::table().primary_key();
// FindDsl is not used because it uses a private trait // FindDsl is not used because it uses a private trait
@ -82,15 +79,15 @@ where
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
id: Self::IdType, id: Self::IdType,
form: &Self::UpdateForm, form: &Self::UpdateForm,
) -> Result<Self, Error>; /*{ ) -> Result<Self, Error>;
let conn = &mut get_conn(pool).await?; /*{
diesel::update(Self::table().find(id)) let conn = &mut get_conn(pool).await?;
.set(form) diesel::update(Self::table().find(id))
.get_result::<Self>(conn) .set(form)
.await .get_result::<Self>(conn)
}*/ .await
async fn delete(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<usize, Error> }*/
{ async fn delete(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<usize, Error> {
Err(Error::NotFound) Err(Error::NotFound)
/*let conn = &mut get_conn(pool).await?; /*let conn = &mut get_conn(pool).await?;
diesel::delete(Self::table().find(id)).execute(conn).await*/ diesel::delete(Self::table().find(id)).execute(conn).await*/
@ -108,19 +105,20 @@ where
type Form; type Form;
async fn follow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error> async fn follow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
where where
Self: Sized; /*{ Self: Sized;
let conn = &mut get_conn(pool).await?; /*{
insert_into(Self::table()) let conn = &mut get_conn(pool).await?;
.values(form) insert_into(Self::table())
.on_conflict(( .values(form)
Self::TargetColumn::default(), .on_conflict((
Self::FollowerColumn::default(), Self::TargetColumn::default(),
)) Self::FollowerColumn::default(),
.do_update() ))
.set(form) .do_update()
.get_result::<Self>(conn) .set(form)
.await .get_result::<Self>(conn)
}*/ .await
}*/
async fn follow_accepted( async fn follow_accepted(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
community_id: CommunityId, community_id: CommunityId,