From f04ef378c6b9478dd2e5291e8c621988787172e7 Mon Sep 17 00:00:00 2001 From: Emile Fugulin Date: Sat, 4 Dec 2021 12:05:24 -0500 Subject: [PATCH 1/4] Add feature to generate table Iden --- Cargo.toml | 1 + sea-orm-macros/Cargo.toml | 3 +++ sea-orm-macros/src/derives/column.rs | 5 +++++ sea-orm-macros/src/derives/entity_model.rs | 13 +++++++++++++ 4 files changed, 22 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index f73cfa511..27380d044 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,6 +67,7 @@ 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"] diff --git a/sea-orm-macros/Cargo.toml b/sea-orm-macros/Cargo.toml index c4c827567..be22fb2f7 100644 --- a/sea-orm-macros/Cargo.toml +++ b/sea-orm-macros/Cargo.toml @@ -25,3 +25,6 @@ proc-macro2 = "^1" [dev-dependencies] sea-orm = { path = "../", features = ["macros"] } serde = { version = "^1.0", features = ["derive"] } + +[features] +with-table-iden = [] diff --git a/sea-orm-macros/src/derives/column.rs b/sea-orm-macros/src/derives/column.rs index 8e60c5608..971912504 100644 --- a/sea-orm-macros/src/derives/column.rs +++ b/sea-orm-macros/src/derives/column.rs @@ -45,6 +45,11 @@ pub fn impl_default_as_str(ident: &Ident, data: &Data) -> syn::Result) -> syn::Res } }); let entity_def = table_name + .clone() .map(|table_name| { quote! { #[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)] @@ -58,6 +59,18 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> 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 let Data::Struct(item_struct) = data { if let Fields::Named(fields) = item_struct.fields { for field in fields.named { From 90122374aa9e60e69d8d7672c91e04e915830529 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 7 Dec 2021 16:13:25 +0800 Subject: [PATCH 2/4] Add test cases [issues] --- .github/workflows/rust.yml | 2 +- issues/356/Cargo.toml | 11 ++++++++++ issues/356/src/main.rs | 3 +++ issues/356/src/model.rs | 42 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 issues/356/Cargo.toml create mode 100644 issues/356/src/main.rs create mode 100644 issues/356/src/model.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index ab5d7663b..4affff662 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -316,7 +316,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest] - path: [86, 249, 262, 319, 324, 352] + path: [86, 249, 262, 319, 324, 352, 356] steps: - uses: actions/checkout@v2 diff --git a/issues/356/Cargo.toml b/issues/356/Cargo.toml new file mode 100644 index 000000000..4e0e64d0f --- /dev/null +++ b/issues/356/Cargo.toml @@ -0,0 +1,11 @@ +[workspace] +# A separate workspace + +[package] +name = "sea-orm-issues-356" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +sea-orm = { path = "../../", features = [ "sqlx-mysql", "runtime-async-std-native-tls", "with-table-iden" ]} diff --git a/issues/356/src/main.rs b/issues/356/src/main.rs new file mode 100644 index 000000000..a9db43c3d --- /dev/null +++ b/issues/356/src/main.rs @@ -0,0 +1,3 @@ +mod model; + +pub fn main() {} diff --git a/issues/356/src/model.rs b/issues/356/src/model.rs new file mode 100644 index 000000000..44325a3a6 --- /dev/null +++ b/issues/356/src/model.rs @@ -0,0 +1,42 @@ +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "model")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub owner: String, + pub name: String, + pub description: String, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} + +#[cfg(test)] +mod tests { + use super::*; + use sea_orm::*; + + #[test] + fn test_columns_1() { + assert_eq!( + Column::iter() + .map(|col| col.to_string()) + .collect::>(), + vec![ + "id".to_owned(), + "owner".to_owned(), + "name".to_owned(), + "description".to_owned(), + ] + ); + assert_eq!(Column::Table.to_string().as_str(), "model"); + assert_eq!(Column::Id.to_string().as_str(), "id"); + assert_eq!(Column::Owner.to_string().as_str(), "owner"); + assert_eq!(Column::Name.to_string().as_str(), "name"); + assert_eq!(Column::Description.to_string().as_str(), "description"); + } +} From 2e26fa3873a75c9e7cebcbed25ea4f84341dc44d Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 7 Dec 2021 22:41:27 +0800 Subject: [PATCH 3/4] Fix clippy warnings --- sea-orm-macros/src/derives/entity_model.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sea-orm-macros/src/derives/entity_model.rs b/sea-orm-macros/src/derives/entity_model.rs index 3a54e1454..e42957a21 100644 --- a/sea-orm-macros/src/derives/entity_model.rs +++ b/sea-orm-macros/src/derives/entity_model.rs @@ -33,7 +33,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res } }); let entity_def = table_name - .clone() + .as_ref() .map(|table_name| { quote! { #[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)] From aaec1bc8456aa2fd378864d16a5db08069a1f4cb Mon Sep 17 00:00:00 2001 From: Emile Fugulin Date: Wed, 8 Dec 2021 13:50:34 -0500 Subject: [PATCH 4/4] Use attribute instead of compilation flag --- Cargo.toml | 1 - issues/356/Cargo.toml | 2 +- issues/356/src/model.rs | 2 +- sea-orm-macros/Cargo.toml | 3 --- sea-orm-macros/src/attributes.rs | 1 + sea-orm-macros/src/derives/entity_model.rs | 30 ++++++++++++++-------- 6 files changed, 22 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 27380d044..f73cfa511 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/issues/356/Cargo.toml b/issues/356/Cargo.toml index 4e0e64d0f..ed64bae46 100644 --- a/issues/356/Cargo.toml +++ b/issues/356/Cargo.toml @@ -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" ]} diff --git a/issues/356/src/model.rs b/issues/356/src/model.rs index 44325a3a6..912a3dcda 100644 --- a/issues/356/src/model.rs +++ b/issues/356/src/model.rs @@ -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, diff --git a/sea-orm-macros/Cargo.toml b/sea-orm-macros/Cargo.toml index be22fb2f7..c4c827567 100644 --- a/sea-orm-macros/Cargo.toml +++ b/sea-orm-macros/Cargo.toml @@ -25,6 +25,3 @@ proc-macro2 = "^1" [dev-dependencies] sea-orm = { path = "../", features = ["macros"] } serde = { version = "^1.0", features = ["derive"] } - -[features] -with-table-iden = [] diff --git a/sea-orm-macros/src/attributes.rs b/sea-orm-macros/src/attributes.rs index 3f479bb9c..cb47d0294 100644 --- a/sea-orm-macros/src/attributes.rs +++ b/sea-orm-macros/src/attributes.rs @@ -11,6 +11,7 @@ pub mod derive_attr { pub relation: Option, pub schema_name: Option, pub table_name: Option, + pub table_iden: Option<()>, } } diff --git a/sea-orm-macros/src/derives/entity_model.rs b/sea-orm-macros/src/derives/entity_model.rs index e42957a21..7309a6c47 100644 --- a/sea-orm-macros/src/derives/entity_model.rs +++ b/sea-orm-macros/src/derives/entity_model.rs @@ -12,6 +12,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> 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) -> 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) -> 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 {