Use attribute instead of compilation flag

This commit is contained in:
Emile Fugulin 2021-12-08 13:50:34 -05:00
parent 2e26fa3873
commit aaec1bc845
6 changed files with 22 additions and 17 deletions

View File

@ -67,7 +67,6 @@ 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

@ -8,4 +8,4 @@ edition = "2021"
publish = false publish = false
[dependencies] [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" ]}

View File

@ -1,7 +1,7 @@
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")] #[sea_orm(table_name = "model", table_iden)]
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
pub id: i32, pub id: i32,

View File

@ -25,6 +25,3 @@ 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

@ -11,6 +11,7 @@ pub mod derive_attr {
pub relation: Option<syn::Ident>, pub relation: Option<syn::Ident>,
pub schema_name: Option<syn::Lit>, pub schema_name: Option<syn::Lit>,
pub table_name: Option<syn::Lit>, pub table_name: Option<syn::Lit>,
pub table_iden: Option<()>,
} }
} }

View File

@ -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 // if #[sea_orm(table_name = "foo", schema_name = "bar")] specified, create Entity struct
let mut table_name = None; let mut table_name = None;
let mut schema_name = quote! { None }; let mut schema_name = quote! { None };
let mut table_iden = false;
attrs.iter().for_each(|attr| { attrs.iter().for_each(|attr| {
if attr.path.get_ident().map(|i| i == "sea_orm") != Some(true) { if attr.path.get_ident().map(|i| i == "sea_orm") != Some(true) {
return; return;
@ -28,6 +29,12 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
schema_name = quote! { Some(#name) }; 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_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;
if table_iden {
#[cfg(feature = "with-table-iden")] if let Some(table_name) = table_name {
if let Some(table_name) = table_name { let table_field_name = Ident::new("Table", Span::call_site());
let table_field_name = Ident::new("Table", Span::call_site()); columns_enum.push(quote! {
columns_enum.push(quote! { #[sea_orm(table_name=#table_name)]
#[sea_orm(table_name=#table_name)] #[strum(disabled)]
#[strum(disabled)] #table_field_name
#table_field_name });
}); columns_trait.push(
columns_trait quote! { Self::#table_field_name => panic!("Table cannot be used as a column") },
.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 {