Add feature to generate table Iden

This commit is contained in:
Emile Fugulin 2021-12-04 12:05:24 -05:00
parent b5bdb683ee
commit f04ef378c6
4 changed files with 22 additions and 0 deletions

View File

@ -67,6 +67,7 @@ with-json = ["serde_json", "sea-query/with-json", "chrono/serde"]
with-chrono = ["chrono", "sea-query/with-chrono"] with-chrono = ["chrono", "sea-query/with-chrono"]
with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal"] with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal"]
with-uuid = ["uuid", "sea-query/with-uuid"] with-uuid = ["uuid", "sea-query/with-uuid"]
with-table-iden = ["sea-orm-macros/with-table-iden"]
sqlx-all = ["sqlx-mysql", "sqlx-postgres", "sqlx-sqlite"] sqlx-all = ["sqlx-mysql", "sqlx-postgres", "sqlx-sqlite"]
sqlx-dep = ["sqlx-json", "sqlx-chrono", "sqlx-decimal", "sqlx-uuid"] sqlx-dep = ["sqlx-json", "sqlx-chrono", "sqlx-decimal", "sqlx-uuid"]
sqlx-json = ["sqlx/json", "with-json"] sqlx-json = ["sqlx/json", "with-json"]

View File

@ -25,3 +25,6 @@ proc-macro2 = "^1"
[dev-dependencies] [dev-dependencies]
sea-orm = { path = "../", features = ["macros"] } sea-orm = { path = "../", features = ["macros"] }
serde = { version = "^1.0", features = ["derive"] } serde = { version = "^1.0", features = ["derive"] }
[features]
with-table-iden = []

View File

@ -45,6 +45,11 @@ pub fn impl_default_as_str(ident: &Ident, data: &Data) -> syn::Result<TokenStrea
column_name = litstr.value(); column_name = litstr.value();
} }
} }
if name == "table_name" {
if let Lit::Str(litstr) = &nv.lit {
column_name = litstr.value();
}
}
} }
} }
} }

View File

@ -33,6 +33,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
} }
}); });
let entity_def = table_name let entity_def = table_name
.clone()
.map(|table_name| { .map(|table_name| {
quote! { quote! {
#[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)] #[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)]
@ -58,6 +59,18 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
let mut primary_keys: Punctuated<_, Comma> = Punctuated::new(); let mut primary_keys: Punctuated<_, Comma> = Punctuated::new();
let mut primary_key_types: Punctuated<_, Comma> = Punctuated::new(); let mut primary_key_types: Punctuated<_, Comma> = Punctuated::new();
let mut auto_increment = true; let mut auto_increment = true;
#[cfg(feature = "with-table-iden")]
if let Some(table_name) = table_name {
let table_field_name = Ident::new("Table", Span::call_site());
columns_enum.push(quote! {
#[sea_orm(table_name=#table_name)]
#[strum(disabled)]
#table_field_name
});
columns_trait
.push(quote! { Self::#table_field_name => panic!("Table cannot be used as a column") });
}
if let Data::Struct(item_struct) = data { if let Data::Struct(item_struct) = data {
if let Fields::Named(fields) = item_struct.fields { if let Fields::Named(fields) = item_struct.fields {
for field in fields.named { for field in fields.named {