Refactor EntityName
This commit is contained in:
parent
6a26b8cc17
commit
93daf55cda
@ -17,7 +17,7 @@ fn get_entity_attr(attrs: &[Attribute]) -> Option<syn::Lit> {
|
||||
}
|
||||
|
||||
pub fn expand_derive_entity(ident: Ident, attrs: Vec<Attribute>) -> syn::Result<TokenStream> {
|
||||
let entity_name = match get_entity_attr(&attrs) {
|
||||
let _entity_name = match get_entity_attr(&attrs) {
|
||||
Some(lit) => quote! { #lit },
|
||||
None => {
|
||||
let normalized = ident.to_string().to_snake_case();
|
||||
@ -26,20 +26,18 @@ pub fn expand_derive_entity(ident: Ident, attrs: Vec<Attribute>) -> syn::Result<
|
||||
};
|
||||
|
||||
Ok(quote!(
|
||||
impl sea_orm::EntityName for #ident {}
|
||||
|
||||
impl sea_orm::IdenStatic for #ident {
|
||||
fn as_str(&self) -> &str {
|
||||
#entity_name
|
||||
}
|
||||
}
|
||||
|
||||
impl sea_orm::Iden for #ident {
|
||||
fn unquoted(&self, s: &mut dyn std::fmt::Write) {
|
||||
write!(s, "{}", self.as_str()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl sea_orm::IdenStatic for #ident {
|
||||
fn as_str(&self) -> &str {
|
||||
<Self as sea_orm::EntityName>::table_name(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl EntityTrait for #ident {
|
||||
type Model = Model;
|
||||
|
||||
|
@ -11,7 +11,13 @@ pub trait IdenStatic: Iden + Copy + Debug + 'static {
|
||||
fn as_str(&self) -> &str;
|
||||
}
|
||||
|
||||
pub trait EntityName: IdenStatic + Default {}
|
||||
pub trait EntityName: IdenStatic + Default {
|
||||
fn table_name(&self) -> &str;
|
||||
|
||||
fn module_name(&self) -> &str {
|
||||
Self::table_name(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait EntityTrait: EntityName {
|
||||
type Model: ModelTrait<Entity = Self> + FromQueryResult;
|
||||
|
@ -2,9 +2,14 @@ use crate as sea_orm;
|
||||
use crate::entity::prelude::*;
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
|
||||
#[table = "cake"]
|
||||
pub struct Entity;
|
||||
|
||||
impl EntityName for Entity {
|
||||
fn table_name(&self) -> &str {
|
||||
"cake"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub id: i32,
|
||||
|
@ -2,9 +2,14 @@ use crate as sea_orm;
|
||||
use crate::entity::prelude::*;
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
|
||||
#[table = "cake_filling"]
|
||||
pub struct Entity;
|
||||
|
||||
impl EntityName for Entity {
|
||||
fn table_name(&self) -> &str {
|
||||
"cake_filling"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub cake_id: i32,
|
||||
|
@ -2,9 +2,14 @@ use crate as sea_orm;
|
||||
use crate::entity::prelude::*;
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
|
||||
#[table = "filling"]
|
||||
pub struct Entity;
|
||||
|
||||
impl EntityName for Entity {
|
||||
fn table_name(&self) -> &str {
|
||||
"filling"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub id: i32,
|
||||
|
@ -2,9 +2,14 @@ use crate as sea_orm;
|
||||
use crate::entity::prelude::*;
|
||||
|
||||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
|
||||
#[table = "fruit"]
|
||||
pub struct Entity;
|
||||
|
||||
impl EntityName for Entity {
|
||||
fn table_name(&self) -> &str {
|
||||
"fruit"
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
||||
pub struct Model {
|
||||
pub id: i32,
|
||||
|
Loading…
x
Reference in New Issue
Block a user