Skip to content

Commit

Permalink
Upstream Pro - 1 (#2415)
Browse files Browse the repository at this point in the history
* Added `ColumnTrait::enum_type_name()` to signify enum types

* dep

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

* Bump sea-query

* fmt
  • Loading branch information
billy1624 authored Dec 2, 2024
1 parent b83a072 commit 7b1495d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sea-orm-macros/src/derives/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,10 @@ impl ActiveEnum {
.to_owned()
.into()
}

fn enum_type_name() -> Option<&'static str> {
Some(stringify!(#ident))
}
}

#[automatically_derived]
Expand Down
17 changes: 17 additions & 0 deletions sea-orm-macros/src/derives/entity_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
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();
Expand Down Expand Up @@ -302,6 +303,16 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> 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
});
}
}
}
Expand Down Expand Up @@ -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 {
match self {
#columns_select_as
Expand Down
7 changes: 7 additions & 0 deletions src/database/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 5 additions & 0 deletions src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7b1495d

Please sign in to comment.