Upstream Pro - 1 (#2415)

* Added `ColumnTrait::enum_type_name()` to signify enum types

* dep

* Added `DbBackend::boolean_value()` for database dependent boolean value

* Bump sea-query

* fmt
This commit is contained in:
Billy Chan 2024-12-02 14:33:21 +08:00 committed by GitHub
parent b83a072123
commit 7b1495d168
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 33 additions and 0 deletions

View File

@ -414,6 +414,10 @@ impl ActiveEnum {
.to_owned() .to_owned()
.into() .into()
} }
fn enum_type_name() -> Option<&'static str> {
Some(stringify!(#ident))
}
} }
#[automatically_derived] #[automatically_derived]

View File

@ -73,6 +73,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
// generate Column enum and it's ColumnTrait impl // generate Column enum and it's ColumnTrait impl
let mut columns_enum: Punctuated<_, Comma> = Punctuated::new(); let mut columns_enum: Punctuated<_, Comma> = Punctuated::new();
let mut columns_trait: Punctuated<_, Comma> = Punctuated::new(); let mut columns_trait: Punctuated<_, Comma> = Punctuated::new();
let mut columns_enum_type_name: Punctuated<_, Comma> = Punctuated::new();
let mut columns_select_as: Punctuated<_, Comma> = Punctuated::new(); let mut columns_select_as: Punctuated<_, Comma> = Punctuated::new();
let mut columns_save_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_keys: Punctuated<_, Comma> = Punctuated::new();
@ -302,6 +303,16 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
} }
// match_row = quote! { #match_row.comment() }; // match_row = quote! { #match_row.comment() };
columns_trait.push(match_row); columns_trait.push(match_row);
let ty: syn::Type = syn::LitStr::new(field_type, field_span)
.parse()
.expect("field type error");
let enum_type_name = quote::quote_spanned! { field_span =>
<#ty as sea_orm::sea_query::ValueType>::enum_type_name()
};
columns_enum_type_name.push(quote! {
Self::#field_name => #enum_type_name
});
} }
} }
} }
@ -358,6 +369,12 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
} }
} }
fn enum_type_name(&self) -> Option<&'static str> {
match self {
#columns_enum_type_name
}
}
fn select_as(&self, expr: sea_orm::sea_query::Expr) -> sea_orm::sea_query::SimpleExpr { fn select_as(&self, expr: sea_orm::sea_query::Expr) -> sea_orm::sea_query::SimpleExpr {
match self { match self {
#columns_select_as #columns_select_as

View File

@ -583,6 +583,13 @@ impl DbBackend {
_ => false, _ => false,
} }
} }
/// A getter for database dependent boolean value
pub fn boolean_value(&self, boolean: bool) -> sea_query::Value {
match self {
Self::MySql | Self::Postgres | Self::Sqlite => boolean.into(),
}
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -75,6 +75,11 @@ pub trait ColumnTrait: IdenStatic + Iterable + FromStr {
/// Define a column for an Entity /// Define a column for an Entity
fn def(&self) -> ColumnDef; fn def(&self) -> ColumnDef;
/// Get the enum name of the column type
fn enum_type_name(&self) -> Option<&'static str> {
None
}
/// Get the name of the entity the column belongs to /// Get the name of the entity the column belongs to
fn entity_name(&self) -> DynIden { fn entity_name(&self) -> DynIden {
SeaRc::new(Self::EntityName::default()) as DynIden SeaRc::new(Self::EntityName::default()) as DynIden