Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to generate table Iden #360

Merged
merged 4 commits into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
11 changes: 11 additions & 0 deletions issues/356/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" ]}
3 changes: 3 additions & 0 deletions issues/356/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod model;

pub fn main() {}
42 changes: 42 additions & 0 deletions issues/356/src/model.rs
Original file line number Diff line number Diff line change
@@ -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<_>>(),
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");
}
}
3 changes: 3 additions & 0 deletions sea-orm-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ proc-macro2 = "^1"
[dev-dependencies]
sea-orm = { path = "../", features = ["macros"] }
serde = { version = "^1.0", features = ["derive"] }

[features]
with-table-iden = []
Sytten marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions sea-orm-macros/src/derives/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ pub fn impl_default_as_str(ident: &Ident, data: &Data) -> syn::Result<TokenStrea
column_name = litstr.value();
}
}
if name == "table_name" {
if let Lit::Str(litstr) = &nv.lit {
column_name = litstr.value();
}
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions sea-orm-macros/src/derives/entity_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
}
});
let entity_def = table_name
.as_ref()
.map(|table_name| {
quote! {
#[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)]
Expand All @@ -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_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") });
Sytten marked this conversation as resolved.
Show resolved Hide resolved
}
if let Data::Struct(item_struct) = data {
if let Fields::Named(fields) = item_struct.fields {
for field in fields.named {
Expand Down