This commit is contained in:
Chris Tsang 2021-05-09 02:43:40 +08:00
parent 7bb521fad2
commit c72d7b0289
6 changed files with 19 additions and 15 deletions

View File

@ -18,7 +18,7 @@ macro_rules! bind_oper {
pub trait ColumnTrait: Iden + Copy + Debug + 'static { pub trait ColumnTrait: Iden + Copy + Debug + 'static {
type Entity: EntityTrait; type Entity: EntityTrait;
fn col_type(&self) -> ColumnType; fn def(&self) -> ColumnType;
fn entity_iden(&self) -> Rc<dyn Iden> { fn entity_iden(&self) -> Rc<dyn Iden> {
Rc::new(Self::Entity::default()) as Rc<dyn Iden> Rc::new(Self::Entity::default()) as Rc<dyn Iden>

View File

@ -11,17 +11,21 @@ pub enum RelationType {
} }
pub trait RelationTrait: Debug + 'static { pub trait RelationTrait: Debug + 'static {
fn rel_def(&self) -> RelationDef; fn def(&self) -> RelationDef;
} }
pub trait Related<R> pub trait Related<R>
where where
R: EntityTrait, R: EntityTrait,
{ {
fn via() -> RelationDef; fn to() -> RelationDef;
fn via() -> Option<RelationDef> {
None
}
fn find_related() -> Select<R> { fn find_related() -> Select<R> {
Select::<R>::new().prepare_reverse_join(Self::via()) Select::<R>::new().prepare_reverse_join(Self::to())
} }
} }

View File

@ -120,22 +120,22 @@ where
} }
pub fn left_join(self, rel: E::Relation) -> Self { pub fn left_join(self, rel: E::Relation) -> Self {
self.prepare_join(JoinType::LeftJoin, E::Relation::rel_def(&rel)) self.prepare_join(JoinType::LeftJoin, E::Relation::def(&rel))
} }
pub fn right_join(self, rel: E::Relation) -> Self { pub fn right_join(self, rel: E::Relation) -> Self {
self.prepare_join(JoinType::RightJoin, E::Relation::rel_def(&rel)) self.prepare_join(JoinType::RightJoin, E::Relation::def(&rel))
} }
pub fn inner_join(self, rel: E::Relation) -> Self { pub fn inner_join(self, rel: E::Relation) -> Self {
self.prepare_join(JoinType::InnerJoin, E::Relation::rel_def(&rel)) self.prepare_join(JoinType::InnerJoin, E::Relation::def(&rel))
} }
pub fn reverse_join<R>(self, rel: R) -> Self pub fn reverse_join<R>(self, rel: R) -> Self
where where
R: RelationTrait, R: RelationTrait,
{ {
self.prepare_reverse_join(rel.rel_def()) self.prepare_reverse_join(rel.def())
} }
/// Get a mutable ref to the query builder /// Get a mutable ref to the query builder

View File

@ -48,7 +48,7 @@ impl ModelTrait for Model {
impl ColumnTrait for Column { impl ColumnTrait for Column {
type Entity = Entity; type Entity = Entity;
fn col_type(&self) -> ColumnType { fn def(&self) -> ColumnType {
match self { match self {
Self::Id => ColumnType::Integer(None), Self::Id => ColumnType::Integer(None),
Self::Name => ColumnType::String(None), Self::Name => ColumnType::String(None),
@ -57,7 +57,7 @@ impl ColumnTrait for Column {
} }
impl RelationTrait for Relation { impl RelationTrait for Relation {
fn rel_def(&self) -> RelationDef { fn def(&self) -> RelationDef {
match self { match self {
Self::Fruit => Entity::has_many(super::fruit::Entity) Self::Fruit => Entity::has_many(super::fruit::Entity)
.from(Column::Id) .from(Column::Id)
@ -68,8 +68,8 @@ impl RelationTrait for Relation {
} }
impl Related<super::fruit::Entity> for Entity { impl Related<super::fruit::Entity> for Entity {
fn via() -> RelationDef { fn to() -> RelationDef {
Relation::Fruit.rel_def() Relation::Fruit.def()
} }
} }

View File

@ -49,7 +49,7 @@ impl ModelTrait for Model {
impl ColumnTrait for Column { impl ColumnTrait for Column {
type Entity = Entity; type Entity = Entity;
fn col_type(&self) -> ColumnType { fn def(&self) -> ColumnType {
match self { match self {
Self::Id => ColumnType::Integer(None), Self::Id => ColumnType::Integer(None),
Self::Name => ColumnType::String(None), Self::Name => ColumnType::String(None),
@ -59,7 +59,7 @@ impl ColumnTrait for Column {
} }
impl RelationTrait for Relation { impl RelationTrait for Relation {
fn rel_def(&self) -> RelationDef { fn def(&self) -> RelationDef {
panic!() panic!()
} }
} }

View File

@ -1,4 +1,4 @@
//! Configurations for test cases and examples. Not intended for actual use. //! Configurations for test cases and examples. Not intended for actual use.
pub mod cake; pub mod cake;
pub mod fruit; pub mod fruit;