From 7b1495d16872102a8b91926f6bcb3baece0d04e6 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 2 Dec 2024 14:33:21 +0800 Subject: [PATCH] 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 --- sea-orm-macros/src/derives/active_enum.rs | 4 ++++ sea-orm-macros/src/derives/entity_model.rs | 17 +++++++++++++++++ src/database/db_connection.rs | 7 +++++++ src/entity/column.rs | 5 +++++ 4 files changed, 33 insertions(+) diff --git a/sea-orm-macros/src/derives/active_enum.rs b/sea-orm-macros/src/derives/active_enum.rs index b3be14733..2a5e42b7b 100644 --- a/sea-orm-macros/src/derives/active_enum.rs +++ b/sea-orm-macros/src/derives/active_enum.rs @@ -414,6 +414,10 @@ impl ActiveEnum { .to_owned() .into() } + + fn enum_type_name() -> Option<&'static str> { + Some(stringify!(#ident)) + } } #[automatically_derived] diff --git a/sea-orm-macros/src/derives/entity_model.rs b/sea-orm-macros/src/derives/entity_model.rs index 7070ac9d4..ec12a5259 100644 --- a/sea-orm-macros/src/derives/entity_model.rs +++ b/sea-orm-macros/src/derives/entity_model.rs @@ -73,6 +73,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> 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_enum_type_name: 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(); @@ -302,6 +303,16 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res } // match_row = quote! { #match_row.comment() }; 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) -> 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 { match self { #columns_select_as diff --git a/src/database/db_connection.rs b/src/database/db_connection.rs index a06381532..f49a43254 100644 --- a/src/database/db_connection.rs +++ b/src/database/db_connection.rs @@ -583,6 +583,13 @@ impl DbBackend { _ => 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)] diff --git a/src/entity/column.rs b/src/entity/column.rs index 7b60f95a5..599d87d70 100644 --- a/src/entity/column.rs +++ b/src/entity/column.rs @@ -75,6 +75,11 @@ pub trait ColumnTrait: IdenStatic + Iterable + FromStr { /// Define a column for an Entity 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 fn entity_name(&self) -> DynIden { SeaRc::new(Self::EntityName::default()) as DynIden