Use attribute instead of compilation flag
This commit is contained in:
parent
2e26fa3873
commit
aaec1bc845
@ -67,7 +67,6 @@ with-json = ["serde_json", "sea-query/with-json", "chrono/serde"]
|
||||
with-chrono = ["chrono", "sea-query/with-chrono"]
|
||||
with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal"]
|
||||
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-dep = ["sqlx-json", "sqlx-chrono", "sqlx-decimal", "sqlx-uuid"]
|
||||
sqlx-json = ["sqlx/json", "with-json"]
|
||||
|
@ -8,4 +8,4 @@ edition = "2021"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
sea-orm = { path = "../../", features = [ "sqlx-mysql", "runtime-async-std-native-tls", "with-table-iden" ]}
|
||||
sea-orm = { path = "../../", features = [ "sqlx-mysql", "runtime-async-std-native-tls" ]}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "model")]
|
||||
#[sea_orm(table_name = "model", table_iden)]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
|
@ -25,6 +25,3 @@ proc-macro2 = "^1"
|
||||
[dev-dependencies]
|
||||
sea-orm = { path = "../", features = ["macros"] }
|
||||
serde = { version = "^1.0", features = ["derive"] }
|
||||
|
||||
[features]
|
||||
with-table-iden = []
|
||||
|
@ -11,6 +11,7 @@ pub mod derive_attr {
|
||||
pub relation: Option<syn::Ident>,
|
||||
pub schema_name: Option<syn::Lit>,
|
||||
pub table_name: Option<syn::Lit>,
|
||||
pub table_iden: Option<()>,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
|
||||
// if #[sea_orm(table_name = "foo", schema_name = "bar")] specified, create Entity struct
|
||||
let mut table_name = None;
|
||||
let mut schema_name = quote! { None };
|
||||
let mut table_iden = false;
|
||||
attrs.iter().for_each(|attr| {
|
||||
if attr.path.get_ident().map(|i| i == "sea_orm") != Some(true) {
|
||||
return;
|
||||
@ -28,6 +29,12 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
|
||||
schema_name = quote! { Some(#name) };
|
||||
}
|
||||
}
|
||||
} else if let Meta::Path(path) = meta {
|
||||
if let Some(ident) = path.get_ident() {
|
||||
if ident == "table_iden" {
|
||||
table_iden = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -59,17 +66,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_key_types: Punctuated<_, Comma> = Punctuated::new();
|
||||
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 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 Fields::Named(fields) = item_struct.fields {
|
||||
|
Loading…
x
Reference in New Issue
Block a user