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, dsl,
expression::{AsExpression, TypedExpressionType}, expression::{AsExpression, TypedExpressionType},
expression_methods::ExpressionMethods, expression_methods::ExpressionMethods,
query_builder::{DeleteStatement, IntoUpdateTarget}, insert_into,
query_builder::{DeleteStatement, InsertStatement, IntoUpdateTarget},
query_dsl::methods::{FindDsl, LimitDsl}, query_dsl::methods::{FindDsl, LimitDsl},
result::Error, result::Error,
sql_types::SqlType, sql_types::SqlType,
AsChangeset, AsChangeset,
Expression, Expression,
Insertable, Insertable,
QuerySource,
Table, Table,
}; };
use diesel_async::{ use diesel_async::{
@ -26,6 +28,28 @@ use std::hash::Hash;
/// Returned by `diesel::delete` /// Returned by `diesel::delete`
pub type Delete<T> = DeleteStatement<<T as HasTable>::Table, <T as IntoUpdateTarget>::WhereClause>; 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: Send + 'static + Sized + HasTable,
Self::Table: Self::Table:
FindDsl<Self::IdType> + Send + Sized + 'static, FindDsl<Self::IdType> + Send + Sized + 'static,
@ -46,23 +70,19 @@ where
SqlType + TypedExpressionType, SqlType + TypedExpressionType,
for<'a> Delete<dsl::Find<Self::Table, Self::IdType>>: ExecuteDsl<AsyncPgConnection> + 'a + Send, for<'a> Delete<dsl::Find<Self::Table, Self::IdType>>: ExecuteDsl<AsyncPgConnection> + 'a + Send,
{ {
/*for<'a> &'a Self::InsertForm: Insertable<Self::Table>, type InsertForm: for<'a> Insert<'a, Self::Table, Self>;
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 UpdateForm; type UpdateForm;
type IdType: Hash type IdType: Hash
+ Eq + Eq
+ Sized + Sized
+ Send + Send
+ AsExpression<<<Self::Table as Table>::PrimaryKey as Expression>::SqlType>; + 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 query = insert_into(Self::table()).values(form);
let conn = &mut *get_conn(pool).await?; let conn = &mut *get_conn(pool).await?;
query.get_result::<Self>(conn).await query.get_result::<Self>(conn).await
}*/ }
async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<Self, Error> { async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<Self, Error> {
let query = Self::table().find(id); let query = Self::table().find(id);
let conn = &mut *get_conn(pool).await?; let conn = &mut *get_conn(pool).await?;