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

Casting columns as a different data type on select, insert and update #1304

Merged
merged 10 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
53 changes: 53 additions & 0 deletions sea-orm-macros/src/derives/entity_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
// generate Column enum and it's ColumnTrait impl
let mut columns_enum: Punctuated<_, Comma> = Punctuated::new();
let mut columns_trait: Punctuated<_, Comma> = Punctuated::new();
let mut columns_select_as: Punctuated<_, Comma> = Punctuated::new();
let mut columns_save_as: Punctuated<_, Comma> = Punctuated::new();
let mut primary_keys: Punctuated<_, Comma> = Punctuated::new();
let mut primary_key_types: Punctuated<_, Comma> = Punctuated::new();
let mut auto_increment = true;
Expand Down Expand Up @@ -90,6 +92,8 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
let mut nullable = false;
let mut default_value = None;
let mut default_expr = None;
let mut select_as = None;
let mut save_as = None;
let mut indexed = false;
let mut ignore = false;
let mut unique = false;
Expand Down Expand Up @@ -169,6 +173,24 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
format!("Invalid enum_name {:?}", nv.lit),
));
}
} else if name == "select_as" {
if let Lit::Str(litstr) = &nv.lit {
select_as = Some(litstr.value());
} else {
return Err(Error::new(
field.span(),
format!("Invalid select_as {:?}", nv.lit),
));
}
} else if name == "save_as" {
if let Lit::Str(litstr) = &nv.lit {
save_as = Some(litstr.value());
} else {
return Err(Error::new(
field.span(),
format!("Invalid save_as {:?}", nv.lit),
));
}
}
}
}
Expand Down Expand Up @@ -224,6 +246,23 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
});
}

if let Some(select_as) = select_as {
columns_select_as.push(quote! {
Self::#field_name => sea_orm::sea_query::SimpleExpr::cast_as(
Into::<sea_orm::sea_query::SimpleExpr>::into(expr),
sea_orm::sea_query::Alias::new(&#select_as),
),
});
}
if let Some(save_as) = save_as {
columns_save_as.push(quote! {
Self::#field_name => sea_orm::sea_query::SimpleExpr::cast_as(
Into::<sea_orm::sea_query::SimpleExpr>::into(val),
sea_orm::sea_query::Alias::new(&#save_as),
),
});
}

let field_type = &field.ty;
let field_type = quote! { #field_type }
.to_string() //E.g.: "Option < String >"
Expand Down Expand Up @@ -347,6 +386,20 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
#columns_trait
}
}

fn select_as(&self, expr: sea_orm::sea_query::Expr) -> sea_orm::sea_query::SimpleExpr {
match self {
#columns_select_as
_ => sea_orm::prelude::ColumnTrait::select_enum_as(self, expr),
}
}

fn save_as(&self, val: sea_orm::sea_query::Expr) -> sea_orm::sea_query::SimpleExpr {
match self {
#columns_save_as
_ => sea_orm::prelude::ColumnTrait::save_enum_as(self, val),
}
}
}

#entity_def
Expand Down
Loading