Codegen column_name
proc_macro attribute (#433)
* feat: codegen `column_name` proc_macro attribute * test: codegen `column_name`
This commit is contained in:
parent
3cde517d2a
commit
21216f3a97
@ -22,6 +22,10 @@ impl Column {
|
|||||||
format_ident!("{}", escape_rust_keyword(self.name.to_camel_case()))
|
format_ident!("{}", escape_rust_keyword(self.name.to_camel_case()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_snake_case_name(&self) -> bool {
|
||||||
|
self.name.to_snake_case() == self.name
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_rs_type(&self) -> TokenStream {
|
pub fn get_rs_type(&self) -> TokenStream {
|
||||||
#[allow(unreachable_patterns)]
|
#[allow(unreachable_patterns)]
|
||||||
let ident: TokenStream = match &self.col_type {
|
let ident: TokenStream = match &self.col_type {
|
||||||
|
@ -306,11 +306,22 @@ impl EntityWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn gen_column_enum(entity: &Entity) -> TokenStream {
|
pub fn gen_column_enum(entity: &Entity) -> TokenStream {
|
||||||
let column_names_camel_case = entity.get_column_names_camel_case();
|
let column_variants = entity.columns.iter().map(|col| {
|
||||||
|
let variant = col.get_name_camel_case();
|
||||||
|
let mut variant = quote! { #variant };
|
||||||
|
if !col.is_snake_case_name() {
|
||||||
|
let column_name = &col.name;
|
||||||
|
variant = quote! {
|
||||||
|
#[sea_orm(column_name = #column_name)]
|
||||||
|
#variant
|
||||||
|
};
|
||||||
|
}
|
||||||
|
variant
|
||||||
|
});
|
||||||
quote! {
|
quote! {
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
||||||
pub enum Column {
|
pub enum Column {
|
||||||
#(#column_names_camel_case,)*
|
#(#column_variants,)*
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -474,6 +485,10 @@ impl EntityWriter {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|col| {
|
.map(|col| {
|
||||||
let mut attrs: Punctuated<_, Comma> = Punctuated::new();
|
let mut attrs: Punctuated<_, Comma> = Punctuated::new();
|
||||||
|
if !col.is_snake_case_name() {
|
||||||
|
let column_name = &col.name;
|
||||||
|
attrs.push(quote! { column_name = #column_name });
|
||||||
|
}
|
||||||
if primary_keys.contains(&col.name) {
|
if primary_keys.contains(&col.name) {
|
||||||
attrs.push(quote! { primary_key });
|
attrs.push(quote! { primary_key });
|
||||||
if !col.auto_increment {
|
if !col.auto_increment {
|
||||||
@ -724,14 +739,14 @@ mod tests {
|
|||||||
unique: false,
|
unique: false,
|
||||||
},
|
},
|
||||||
Column {
|
Column {
|
||||||
name: "name".to_owned(),
|
name: "_name_".to_owned(),
|
||||||
col_type: ColumnType::String(Some(255)),
|
col_type: ColumnType::String(Some(255)),
|
||||||
auto_increment: false,
|
auto_increment: false,
|
||||||
not_null: true,
|
not_null: true,
|
||||||
unique: false,
|
unique: false,
|
||||||
},
|
},
|
||||||
Column {
|
Column {
|
||||||
name: "fruit_id".to_owned(),
|
name: "fruitId".to_owned(),
|
||||||
col_type: ColumnType::Integer(Some(11)),
|
col_type: ColumnType::Integer(Some(11)),
|
||||||
auto_increment: false,
|
auto_increment: false,
|
||||||
not_null: false,
|
not_null: false,
|
||||||
@ -740,7 +755,7 @@ mod tests {
|
|||||||
],
|
],
|
||||||
relations: vec![Relation {
|
relations: vec![Relation {
|
||||||
ref_table: "fruit".to_owned(),
|
ref_table: "fruit".to_owned(),
|
||||||
columns: vec!["fruit_id".to_owned()],
|
columns: vec!["fruitId".to_owned()],
|
||||||
ref_columns: vec!["id".to_owned()],
|
ref_columns: vec!["id".to_owned()],
|
||||||
rel_type: RelationType::BelongsTo,
|
rel_type: RelationType::BelongsTo,
|
||||||
on_delete: None,
|
on_delete: None,
|
||||||
|
@ -7,7 +7,9 @@ use sea_orm::entity::prelude::*;
|
|||||||
pub struct Model {
|
pub struct Model {
|
||||||
#[sea_orm(primary_key)]
|
#[sea_orm(primary_key)]
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
|
#[sea_orm(column_name = "_name_")]
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
#[sea_orm(column_name = "fruitId")]
|
||||||
pub fruit_id: Option<i32> ,
|
pub fruit_id: Option<i32> ,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,9 @@ pub struct Model {
|
|||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
||||||
pub enum Column {
|
pub enum Column {
|
||||||
Id,
|
Id,
|
||||||
|
#[sea_orm(column_name = "_name_")]
|
||||||
Name,
|
Name,
|
||||||
|
#[sea_orm(column_name = "fruitId")]
|
||||||
FruitId,
|
FruitId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user