Skip to content

Commit

Permalink
Casting columns as a different data type on select, insert and update (
Browse files Browse the repository at this point in the history
…SeaQL#1304)

* Cast select and value

* Refactoring

* Test casting Postgres citext

* Fixup

* Revert

* Add test cases

* Fixup

* Rename methods
  • Loading branch information
billy1624 authored and denwong47 committed Jan 20, 2023
1 parent edf9607 commit 2ea6d3f
Show file tree
Hide file tree
Showing 13 changed files with 569 additions and 75 deletions.
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

0 comments on commit 2ea6d3f

Please sign in to comment.