Insert trait

pull/3707/head
dull b 2023-07-24 18:43:13 +00:00
parent 9f61be4035
commit 9d780c2403
1 changed files with 29 additions and 9 deletions

View File

@ -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<T> = DeleteStatement<<T as HasTable>::Table, <T as IntoUpdateTarget>::WhereClause>;
pub trait Insert<'a, Tb, U>
where
Self: 'a,
&'a Self: Insertable<Tb>,
InsertStatement<Tb, <&'a Self as Insertable<Tb>>::Values>:
LoadQuery<'a, AsyncPgConnection, U> + 'a + Send,
<&'a Self as Insertable<Tb>>::Values: 'a,
Tb: QuerySource,
{
}
impl<'a, Tb, U, T> Insert<'a, Tb, U> for T
where
Self: 'a,
&'a Self: Insertable<Tb>,
InsertStatement<Tb, <&'a Self as Insertable<Tb>>::Values>:
LoadQuery<'a, AsyncPgConnection, U> + 'a + Send,
<&'a Self as Insertable<Tb>>::Values: 'a,
Tb: QuerySource,
{
}
/*Self: Send + 'static + Sized + HasTable,
Self::Table:
FindDsl<Self::IdType> + Send + Sized + 'static,
@ -46,23 +70,19 @@ where
SqlType + TypedExpressionType,
for<'a> Delete<dsl::Find<Self::Table, Self::IdType>>: ExecuteDsl<AsyncPgConnection> + 'a + Send,
{
/*for<'a> &'a Self::InsertForm: Insertable<Self::Table>,
for<'a> InsertStatement<Self::Table, <&'a Self::InsertForm as Insertable<Self::Table>>::Values>:
LoadQuery<'a, AsyncPgConnection, Self> + 'a,
for<'a> <&'a Self::InsertForm as Insertable<Self::Table>>::Values: 'a,*/
type InsertForm;
type InsertForm: for<'a> Insert<'a, Self::Table, Self>;
type UpdateForm;
type IdType: Hash
+ Eq
+ Sized
+ Send
+ AsExpression<<<Self::Table as Table>::PrimaryKey as Expression>::SqlType>;
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error>;
/*{
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
let query = insert_into(Self::table()).values(form);
let conn = &mut *get_conn(pool).await?;
query.get_result::<Self>(conn).await
}*/
}
async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<Self, Error> {
let query = Self::table().find(id);
let conn = &mut *get_conn(pool).await?;