Refactor ModelTrait
This commit is contained in:
parent
c65cb9687b
commit
a746f8b863
@ -42,10 +42,12 @@ pub fn expand_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream>
|
||||
#(<Self::Entity as EntityTrait>::Column::#name => self.#field = v.unwrap()),*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl sea_orm::FromQueryResult for #ident {
|
||||
fn from_query_result(row: &sea_orm::QueryResult, pre: &str) -> Result<Self, sea_orm::TypeErr> {
|
||||
Ok(Self {
|
||||
#(#field: row.try_get(pre, <Self::Entity as EntityTrait>::Column::#name.as_str().into())?),*
|
||||
#(#field: row.try_get(pre, <<Self as ModelTrait>::Entity as EntityTrait>::Column::#name.as_str().into())?),*
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
ActiveModelOf, ActiveModelTrait, ColumnTrait, Insert, ModelTrait, OneOrManyActiveModel,
|
||||
PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, RelationBuilder, RelationTrait, RelationType,
|
||||
Select,
|
||||
ActiveModelOf, ActiveModelTrait, ColumnTrait, FromQueryResult, Insert, ModelTrait,
|
||||
OneOrManyActiveModel, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, RelationBuilder,
|
||||
RelationTrait, RelationType, Select,
|
||||
};
|
||||
use sea_query::{Iden, IntoValueTuple};
|
||||
use std::fmt::Debug;
|
||||
@ -14,13 +14,13 @@ pub trait IdenStatic: Iden + Copy + Debug + 'static {
|
||||
pub trait EntityName: IdenStatic + Default {}
|
||||
|
||||
pub trait EntityTrait: EntityName {
|
||||
type Model: ModelTrait;
|
||||
type Model: ModelTrait<Entity = Self> + FromQueryResult;
|
||||
|
||||
type Column: ColumnTrait;
|
||||
|
||||
type Relation: RelationTrait;
|
||||
|
||||
type PrimaryKey: PrimaryKeyTrait;
|
||||
type PrimaryKey: PrimaryKeyTrait + PrimaryKeyToColumn<Self>;
|
||||
|
||||
fn has_one<R>(entity: R) -> RelationBuilder<Self, R>
|
||||
where
|
||||
|
@ -8,10 +8,6 @@ pub trait ModelTrait: Clone + Debug {
|
||||
fn get(&self, c: <Self::Entity as EntityTrait>::Column) -> Value;
|
||||
|
||||
fn set(&mut self, c: <Self::Entity as EntityTrait>::Column, v: Value);
|
||||
|
||||
fn from_query_result(res: &QueryResult, pre: &str) -> Result<Self, TypeErr>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
pub trait FromQueryResult {
|
||||
@ -19,12 +15,3 @@ pub trait FromQueryResult {
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
impl<M> FromQueryResult for M
|
||||
where
|
||||
M: ModelTrait + Sized,
|
||||
{
|
||||
fn from_query_result(res: &QueryResult, pre: &str) -> Result<M, TypeErr> {
|
||||
<Self as ModelTrait>::from_query_result(res, pre)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use super::{IdenStatic, Iterable, EntityTrait};
|
||||
use super::{EntityTrait, IdenStatic, Iterable};
|
||||
|
||||
pub trait PrimaryKeyTrait: IdenStatic + Iterable {}
|
||||
|
||||
|
@ -226,7 +226,6 @@ pub trait QueryFilter: Sized {
|
||||
fn belongs_to<E>(mut self, model: &E::Model) -> Self
|
||||
where
|
||||
E: EntityTrait,
|
||||
E::PrimaryKey: PrimaryKeyToColumn<<E::Model as ModelTrait>::Entity>,
|
||||
{
|
||||
for key in E::PrimaryKey::iter() {
|
||||
let col = key.into_column();
|
||||
|
@ -7,6 +7,7 @@ mod json;
|
||||
mod result;
|
||||
mod select;
|
||||
mod traits;
|
||||
// mod update;
|
||||
|
||||
// pub use combine::*;
|
||||
pub use helper::*;
|
||||
@ -17,3 +18,4 @@ pub use json::*;
|
||||
pub use result::*;
|
||||
pub use select::*;
|
||||
pub use traits::*;
|
||||
// pub use update::*;
|
||||
|
Loading…
x
Reference in New Issue
Block a user