Codegen without find_* helper fn

This commit is contained in:
Billy Chan 2021-06-30 23:01:23 +08:00
parent f8f3d128fd
commit dd8d8c3431
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
13 changed files with 19 additions and 109 deletions

View File

@ -71,13 +71,4 @@ impl Related<super::fruit::Entity> for Entity {
} }
} }
impl Model {
pub fn find_cake_filling(&self) -> Select<super::cake_filling::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
pub fn find_fruit(&self) -> Select<super::fruit::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View File

@ -78,13 +78,4 @@ impl Related<super::filling::Entity> for Entity {
} }
} }
impl Model {
pub fn find_cake(&self) -> Select<super::cake::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
pub fn find_filling(&self) -> Select<super::filling::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View File

@ -63,10 +63,4 @@ impl Related<super::cake_filling::Entity> for Entity {
} }
} }
impl Model {
pub fn find_cake_filling(&self) -> Select<super::cake_filling::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View File

@ -39,6 +39,7 @@ impl PrimaryKeyTrait for PrimaryKey {
#[derive(Copy, Clone, Debug, EnumIter)] #[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation { pub enum Relation {
Cake, Cake,
Vendor,
} }
impl ColumnTrait for Column { impl ColumnTrait for Column {
@ -59,6 +60,7 @@ impl RelationTrait for Relation {
.from(Column::CakeId) .from(Column::CakeId)
.to(super::cake::Column::Id) .to(super::cake::Column::Id)
.into(), .into(),
Self::Vendor => Entity::has_many(super::vendor::Entity).into(),
} }
} }
} }
@ -69,9 +71,9 @@ impl Related<super::cake::Entity> for Entity {
} }
} }
impl Model { impl Related<super::vendor::Entity> for Entity {
pub fn find_cake(&self) -> Select<super::cake::Entity> { fn to() -> RelationDef {
Entity::find_related().belongs_to::<Entity>(self) Relation::Vendor.def()
} }
} }

View File

@ -4,3 +4,4 @@ pub mod cake;
pub mod cake_filling; pub mod cake_filling;
pub mod filling; pub mod filling;
pub mod fruit; pub mod fruit;
pub mod vendor;

View File

@ -4,3 +4,4 @@ pub use super::cake::Entity as Cake;
pub use super::cake_filling::Entity as CakeFilling; pub use super::cake_filling::Entity as CakeFilling;
pub use super::filling::Entity as Filling; pub use super::filling::Entity as Filling;
pub use super::fruit::Entity as Fruit; pub use super::fruit::Entity as Fruit;
pub use super::vendor::Entity as Vendor;

View File

@ -47,7 +47,7 @@ impl ColumnTrait for Column {
match self { match self {
Self::Id => ColumnType::Integer.def(), Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(Some(255u32)).def(), Self::Name => ColumnType::String(Some(255u32)).def(),
Self::FruitId => ColumnType::Integer.def(), Self::FruitId => ColumnType::Integer.def().null(),
} }
} }
} }
@ -55,7 +55,7 @@ impl ColumnTrait for Column {
impl RelationTrait for Relation { impl RelationTrait for Relation {
fn def(&self) -> RelationDef { fn def(&self) -> RelationDef {
match self { match self {
Self::Fruit => Entity::has_one(super::fruit::Entity) Self::Fruit => Entity::belongs_to(super::fruit::Entity)
.from(Column::FruitId) .from(Column::FruitId)
.to(super::fruit::Column::Id) .to(super::fruit::Column::Id)
.into(), .into(),
@ -69,10 +69,4 @@ impl Related<super::fruit::Entity> for Entity {
} }
} }
impl Model {
pub fn find_fruit(&self) -> Select<super::fruit::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View File

@ -53,14 +53,8 @@ impl ColumnTrait for Column {
impl RelationTrait for Relation { impl RelationTrait for Relation {
fn def(&self) -> RelationDef { fn def(&self) -> RelationDef {
match self { match self {
Self::CakeFilling => Entity::has_many(super::cake_filling::Entity) Self::CakeFilling => Entity::has_many(super::cake_filling::Entity).into(),
.from(Column::Id) Self::Fruit => Entity::has_many(super::fruit::Entity).into(),
.to(super::cake_filling::Column::CakeId)
.into(),
Self::Fruit => Entity::has_many(super::fruit::Entity)
.from(Column::Id)
.to(super::fruit::Column::CakeId)
.into(),
} }
} }
} }
@ -77,13 +71,4 @@ impl Related<super::fruit::Entity> for Entity {
} }
} }
impl Model {
pub fn find_cake_filling(&self) -> Select<super::cake_filling::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
pub fn find_fruit(&self) -> Select<super::fruit::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View File

@ -54,11 +54,11 @@ impl ColumnTrait for Column {
impl RelationTrait for Relation { impl RelationTrait for Relation {
fn def(&self) -> RelationDef { fn def(&self) -> RelationDef {
match self { match self {
Self::Cake => Entity::has_one(super::cake::Entity) Self::Cake => Entity::belongs_to(super::cake::Entity)
.from(Column::CakeId) .from(Column::CakeId)
.to(super::cake::Column::Id) .to(super::cake::Column::Id)
.into(), .into(),
Self::Filling => Entity::has_one(super::filling::Entity) Self::Filling => Entity::belongs_to(super::filling::Entity)
.from(Column::FillingId) .from(Column::FillingId)
.to(super::filling::Column::Id) .to(super::filling::Column::Id)
.into(), .into(),
@ -78,13 +78,4 @@ impl Related<super::filling::Entity> for Entity {
} }
} }
impl Model {
pub fn find_cake(&self) -> Select<super::cake::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
pub fn find_filling(&self) -> Select<super::filling::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View File

@ -52,10 +52,7 @@ impl ColumnTrait for Column {
impl RelationTrait for Relation { impl RelationTrait for Relation {
fn def(&self) -> RelationDef { fn def(&self) -> RelationDef {
match self { match self {
Self::CakeFilling => Entity::has_many(super::cake_filling::Entity) Self::CakeFilling => Entity::has_many(super::cake_filling::Entity).into(),
.from(Column::Id)
.to(super::cake_filling::Column::FillingId)
.into(),
} }
} }
} }
@ -66,10 +63,4 @@ impl Related<super::cake_filling::Entity> for Entity {
} }
} }
impl Model {
pub fn find_cake_filling(&self) -> Select<super::cake_filling::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View File

@ -48,7 +48,7 @@ impl ColumnTrait for Column {
match self { match self {
Self::Id => ColumnType::Integer.def(), Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(Some(255u32)).def(), Self::Name => ColumnType::String(Some(255u32)).def(),
Self::CakeId => ColumnType::Integer.def(), Self::CakeId => ColumnType::Integer.def().null(),
} }
} }
} }
@ -56,14 +56,11 @@ impl ColumnTrait for Column {
impl RelationTrait for Relation { impl RelationTrait for Relation {
fn def(&self) -> RelationDef { fn def(&self) -> RelationDef {
match self { match self {
Self::Cake => Entity::has_one(super::cake::Entity) Self::Cake => Entity::belongs_to(super::cake::Entity)
.from(Column::CakeId) .from(Column::CakeId)
.to(super::cake::Column::Id) .to(super::cake::Column::Id)
.into(), .into(),
Self::Vendor => Entity::has_many(super::vendor::Entity) Self::Vendor => Entity::has_many(super::vendor::Entity).into(),
.from(Column::Id)
.to(super::vendor::Column::FruitId)
.into(),
} }
} }
} }
@ -80,13 +77,4 @@ impl Related<super::vendor::Entity> for Entity {
} }
} }
impl Model {
pub fn find_cake(&self) -> Select<super::cake::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
pub fn find_vendor(&self) -> Select<super::vendor::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View File

@ -47,7 +47,7 @@ impl ColumnTrait for Column {
match self { match self {
Self::Id => ColumnType::Integer.def(), Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(Some(255u32)).def(), Self::Name => ColumnType::String(Some(255u32)).def(),
Self::FruitId => ColumnType::Integer.def(), Self::FruitId => ColumnType::Integer.def().null(),
} }
} }
} }
@ -55,7 +55,7 @@ impl ColumnTrait for Column {
impl RelationTrait for Relation { impl RelationTrait for Relation {
fn def(&self) -> RelationDef { fn def(&self) -> RelationDef {
match self { match self {
Self::Fruit => Entity::has_one(super::fruit::Entity) Self::Fruit => Entity::belongs_to(super::fruit::Entity)
.from(Column::FruitId) .from(Column::FruitId)
.to(super::fruit::Column::Id) .to(super::fruit::Column::Id)
.into(), .into(),
@ -69,10 +69,4 @@ impl Related<super::fruit::Entity> for Entity {
} }
} }
impl Model {
pub fn find_fruit(&self) -> Select<super::fruit::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
}
}
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}

View File

@ -117,7 +117,6 @@ impl EntityWriter {
]; ];
code_blocks.extend(Self::gen_impl_related(entity)); code_blocks.extend(Self::gen_impl_related(entity));
code_blocks.extend(vec![ code_blocks.extend(vec![
Self::gen_impl_model(entity),
Self::gen_impl_active_model_behavior(), Self::gen_impl_active_model_behavior(),
]); ]);
code_blocks code_blocks
@ -256,18 +255,6 @@ impl EntityWriter {
.collect() .collect()
} }
pub fn gen_impl_model(entity: &Entity) -> TokenStream {
let relation_ref_tables_snake_case = entity.get_relation_ref_tables_snake_case();
let relation_rel_find_helpers = entity.get_relation_rel_find_helpers();
quote! {
impl Model {
#(pub fn #relation_rel_find_helpers(&self) -> Select<super::#relation_ref_tables_snake_case::Entity> {
Entity::find_related().belongs_to::<Entity>(self)
})*
}
}
}
pub fn gen_impl_active_model_behavior() -> TokenStream { pub fn gen_impl_active_model_behavior() -> TokenStream {
quote! { quote! {
impl ActiveModelBehavior for ActiveModel {} impl ActiveModelBehavior for ActiveModel {}