diff --git a/sea-orm-macros/Cargo.toml b/sea-orm-macros/Cargo.toml index cb6df780c..34086a674 100644 --- a/sea-orm-macros/Cargo.toml +++ b/sea-orm-macros/Cargo.toml @@ -18,7 +18,7 @@ path = "src/lib.rs" proc-macro = true [dependencies] -bae = { version = "0.2", package = "sea-bae", default-features = false, optional = true } +darling = { version = "0.23", optional = true } heck = { version = "0.5", default-features = false } itertools = "0.14" pluralizer = { version = "0.5" } @@ -44,7 +44,7 @@ serde = { version = "1.0", features = ["derive"] } [features] async = [] default = ["derive"] -derive = ["bae"] +derive = ["darling"] entity-registry = [] postgres-array = [] seaography = ["proc-macro-crate"] diff --git a/sea-orm-macros/src/derives/active_model_ex.rs b/sea-orm-macros/src/derives/active_model_ex.rs index b095a2836..94c11f60b 100644 --- a/sea-orm-macros/src/derives/active_model_ex.rs +++ b/sea-orm-macros/src/derives/active_model_ex.rs @@ -6,7 +6,7 @@ use super::util::{ await_token, consume_meta, escape_rust_keyword, is_self_entity, trim_starting_raw_identifier, }; use heck::ToUpperCamelCase; -use proc_macro2::{Ident, Span, TokenStream}; +use proc_macro2::{Ident, TokenStream}; use quote::{format_ident, quote}; use syn::{Attribute, Data, LitStr, PathArguments, Type, TypePath, Visibility}; @@ -620,10 +620,10 @@ impl ActiveModelActionTokens { if is_unique_relation_target { RelationLookup::ByRelatedEntity } else { - RelationLookup::ByRelationVariant(Ident::new( - &infer_relation_name_from_entity(&compound_type.entity) - .to_upper_camel_case(), - Span::call_site(), + RelationLookup::ByRelationVariant(format_ident!( + "{}", + infer_relation_name_from_entity(&compound_type.entity) + .to_upper_camel_case() )) } } diff --git a/sea-orm-macros/src/derives/attributes.rs b/sea-orm-macros/src/derives/attributes.rs index 5bc821266..bb07473d7 100644 --- a/sea-orm-macros/src/derives/attributes.rs +++ b/sea-orm-macros/src/derives/attributes.rs @@ -1,92 +1,166 @@ +use darling::{FromAttributes, FromMeta, Result}; +use syn::{Attribute, Expr, Ident, Lit}; + +/// An optional `syn::Ident` attribute value. +/// +/// Darling has no `from_word` for `syn::Ident`, so a bare-word usage such as +/// `#[sea_orm(model_ex)]` would be rejected for an `Option` field. Several +/// `sea_orm` attributes are emitted both as bare words (e.g. `#[sea_orm(model_ex)]`) +/// and as name-value paths (e.g. `#[sea_orm(model_ex = ModelEx)]`); this wrapper +/// treats the bare-word form as absent so both usages parse cleanly. +#[derive(Debug, Clone, Default)] +pub struct OptionalIdent(pub Option); + +impl FromMeta for OptionalIdent { + fn from_word() -> Result { + Ok(Self(None)) + } + fn from_value(value: &Lit) -> Result { + Ident::from_value(value).map(|id| Self(Some(id))) + } + fn from_expr(expr: &Expr) -> Result { + Ident::from_expr(expr).map(|id| Self(Some(id))) + } +} + +fn try_from_attributes(attrs: &[Attribute]) -> syn::Result> { + if attrs.iter().any(|a| a.path().is_ident("sea_orm")) { + T::from_attributes(attrs) + .map(Some) + .map_err(syn::Error::from) + } else { + Ok(None) + } +} + +fn from_attributes(attrs: &[Attribute]) -> syn::Result { + T::from_attributes(attrs).map_err(syn::Error::from) +} + pub mod derive_attr { - use bae::FromAttributes; + use super::*; + use syn::{Ident, LitStr}; /// Attributes for Models and ActiveModels #[derive(Default, FromAttributes)] + #[darling(attributes(sea_orm), allow_unknown_fields)] #[allow(dead_code)] pub struct SeaOrm { - pub column: Option, - pub entity: Option, - pub model: Option, - pub model_ex: Option, - pub active_model: Option, - pub active_model_ex: Option, - pub primary_key: Option, - pub relation: Option, - pub schema_name: Option, - pub table_name: Option, - pub comment: Option, + pub column: Option, + pub entity: Option, + pub model: Option, + #[darling(default)] + pub model_ex: OptionalIdent, + pub active_model: Option, + pub active_model_ex: Option, + pub primary_key: Option, + pub relation: Option, + pub schema_name: Option, + pub table_name: Option, + pub comment: Option, pub table_iden: Option<()>, - pub rename_all: Option, + pub rename_all: Option, + } + + impl SeaOrm { + pub fn try_from_attributes(attrs: &[Attribute]) -> syn::Result> { + super::try_from_attributes(attrs) + } } } pub mod relation_attr { - use bae::FromAttributes; + use super::*; + use syn::Lit; /// Attributes for Relation enum #[derive(Default, FromAttributes)] + #[darling(attributes(sea_orm), allow_unknown_fields)] pub struct SeaOrm { - pub belongs_to: Option, - pub has_one: Option, - pub has_many: Option, - pub via_rel: Option, - pub on_update: Option, - pub on_delete: Option, - pub on_condition: Option, - pub from: Option, - pub to: Option, - pub fk_name: Option, + pub belongs_to: Option, + pub has_one: Option, + pub has_many: Option, + pub via_rel: Option, + pub on_update: Option, + pub on_delete: Option, + pub on_condition: Option, + pub from: Option, + pub to: Option, + pub fk_name: Option, pub skip_fk: Option<()>, - pub condition_type: Option, + pub condition_type: Option, + } + + impl SeaOrm { + pub fn from_attributes(attrs: &[Attribute]) -> syn::Result { + super::from_attributes(attrs) + } } } pub mod compound_attr { - use bae::FromAttributes; + use super::*; + use syn::LitStr; /// Attributes for compound model fields #[derive(Default, FromAttributes)] + #[darling(attributes(sea_orm), allow_unknown_fields)] pub struct SeaOrm { pub has_one: Option<()>, pub has_many: Option<()>, pub belongs_to: Option<()>, pub self_ref: Option<()>, pub skip_fk: Option<()>, - pub via: Option, - pub via_rel: Option, - pub from: Option, - pub to: Option, - pub relation_enum: Option, - pub relation_reverse: Option, + pub via: Option, + pub via_rel: Option, + pub from: Option, + pub to: Option, + pub relation_enum: Option, + pub relation_reverse: Option, pub reverse: Option<()>, - pub on_update: Option, - pub on_delete: Option, + pub on_update: Option, + pub on_delete: Option, + } + + impl SeaOrm { + pub fn try_from_attributes(attrs: &[Attribute]) -> syn::Result> { + super::try_from_attributes(attrs) + } } } pub mod value_type_attr { - use bae::FromAttributes; + use super::*; + use syn::LitStr; /// Attributes for compound model fields #[derive(Default, FromAttributes)] + #[darling(attributes(sea_orm), allow_unknown_fields)] pub struct SeaOrm { - pub column_type: Option, - pub array_type: Option, - pub value_type: Option, - pub from_str: Option, - pub to_str: Option, + pub column_type: Option, + pub array_type: Option, + pub value_type: Option, + pub from_str: Option, + pub to_str: Option, pub try_from_u64: Option<()>, pub try_getable_array: Option<()>, } + + impl SeaOrm { + pub fn try_from_attributes(attrs: &[Attribute]) -> syn::Result> { + super::try_from_attributes(attrs) + } + } } #[cfg(feature = "seaography")] pub mod related_attr { - use bae::FromAttributes; + use super::*; + use syn::Lit; /// Attributes for RelatedEntity enum #[derive(Default, FromAttributes)] + #[darling(attributes(sea_orm), allow_unknown_fields)] pub struct SeaOrm { /// /// Allows to modify target entity @@ -96,7 +170,7 @@ pub mod related_attr { /// If used on enumeration attributes /// it allows to specify different /// Entity ident - pub entity: Option, + pub entity: Option, /// /// Allows to specify RelationDef /// @@ -104,6 +178,15 @@ pub mod related_attr { /// /// If not supplied the generated code /// will utilize `impl Related` trait - pub def: Option, + pub def: Option, + } + + impl SeaOrm { + pub fn try_from_attributes(attrs: &[Attribute]) -> syn::Result> { + super::try_from_attributes(attrs) + } + pub fn from_attributes(attrs: &[Attribute]) -> syn::Result { + super::from_attributes(attrs) + } } } diff --git a/sea-orm-macros/src/derives/case_style.rs b/sea-orm-macros/src/derives/case_style.rs index 917dda05a..e2510a11a 100644 --- a/sea-orm-macros/src/derives/case_style.rs +++ b/sea-orm-macros/src/derives/case_style.rs @@ -127,7 +127,7 @@ impl TryFrom<&ParseNestedMeta<'_>> for CaseStyle { #[test] fn test_convert_case() { - let id = Ident::new("test_me", proc_macro2::Span::call_site()); + let id = quote::format_ident!("test_me"); assert_eq!("testMe", id.convert_case(Some(CaseStyle::CamelCase))); assert_eq!("TestMe", id.convert_case(Some(CaseStyle::PascalCase))); } diff --git a/sea-orm-macros/src/derives/entity.rs b/sea-orm-macros/src/derives/entity.rs index bf896bf81..20b1c85af 100644 --- a/sea-orm-macros/src/derives/entity.rs +++ b/sea-orm-macros/src/derives/entity.rs @@ -25,7 +25,10 @@ impl DeriveEntity { let ident = input.ident; let column_ident = sea_attr.column.unwrap_or_else(|| format_ident!("Column")); let model_ident = sea_attr.model.unwrap_or_else(|| format_ident!("Model")); - let model_ex_ident = sea_attr.model_ex.unwrap_or_else(|| format_ident!("Model")); + let model_ex_ident = sea_attr + .model_ex + .0 + .unwrap_or_else(|| format_ident!("Model")); let active_model_ident = sea_attr .active_model .unwrap_or_else(|| format_ident!("ActiveModel")); diff --git a/sea-orm-macros/src/derives/entity_loader.rs b/sea-orm-macros/src/derives/entity_loader.rs index a9bc84592..9080cf591 100644 --- a/sea-orm-macros/src/derives/entity_loader.rs +++ b/sea-orm-macros/src/derives/entity_loader.rs @@ -1,3 +1,4 @@ +use proc_macro2::Span; use proc_macro2::TokenStream; use quote::quote; use std::collections::{HashMap, HashSet}; @@ -20,6 +21,16 @@ pub enum EntityLoaderFieldKind { }, } +/// Ident for the `LoadTarget` variant used by a self-referential many-to-many +/// junction: `TableRef` for the forward direction, `TableRefRev` for the reverse. +fn table_ref_ident(reverse: bool, span: Span) -> Ident { + if reverse { + Ident::new("TableRefRev", span) + } else { + Ident::new("TableRef", span) + } +} + pub struct EntityLoaderField { pub field: Ident, /// super::bakery::Entity @@ -115,11 +126,7 @@ impl EntityLoaderField { junction_module: &Ident, reverse: bool, ) { - let target_type = if !reverse { - Ident::new("TableRef", junction_module.span()) - } else { - Ident::new("TableRefRev", junction_module.span()) - }; + let target_type = table_ref_ident(reverse, junction_module.span()); let target_entity = if !reverse { quote!(super::#junction_module::Entity) @@ -195,11 +202,7 @@ impl EntityLoaderField { reverse, } = &self.kind { - let target_type = if !reverse { - Ident::new("TableRef", junction_module.span()) - } else { - Ident::new("TableRefRev", junction_module.span()) - }; + let target_type = table_ref_ident(*reverse, junction_module.span()); output.loader_with_set_impl.extend(quote! { if target == sea_orm::compound::LoadTarget::#target_type(super::#junction_module::Entity.table_ref()) { @@ -248,11 +251,7 @@ impl EntityLoaderField { junction_module, reverse, } => { - let target_type = if !reverse { - Ident::new("TableRef", junction_module.span()) - } else { - Ident::new("TableRefRev", junction_module.span()) - }; + let target_type = table_ref_ident(*reverse, junction_module.span()); output.loader_with_2_impl.extend(quote! { if left == sea_orm::compound::LoadTarget::#target_type(super::#junction_module::Entity.table_ref()) { diff --git a/sea-orm-macros/src/derives/entity_model.rs b/sea-orm-macros/src/derives/entity_model.rs index 8a877fa61..dd7926704 100644 --- a/sea-orm-macros/src/derives/entity_model.rs +++ b/sea-orm-macros/src/derives/entity_model.rs @@ -3,8 +3,8 @@ use super::util::{consume_meta, escape_rust_keyword, trim_starting_raw_identifie use heck::{ ToKebabCase, ToLowerCamelCase, ToShoutySnakeCase, ToSnakeCase, ToTitleCase, ToUpperCamelCase, }; -use proc_macro2::{Ident, Span, TokenStream}; -use quote::quote; +use proc_macro2::{Ident, TokenStream}; +use quote::{format_ident, quote}; use std::str::FromStr; use syn::{ Attribute, Data, Fields, Lit, LitStr, Visibility, punctuated::Punctuated, spanned::Spanned, @@ -172,7 +172,7 @@ pub fn expand_derive_entity_model( if table_iden { if let Some(table_name) = &table_name { - let table_field_name = Ident::new("Table", Span::call_site()); + let table_field_name = format_ident!("Table"); columns_enum.push(quote! { #[doc = " Generated by sea-orm-macros"] #[sea_orm(table_name=#table_name)] diff --git a/sea-orm-macros/src/derives/into_active_model.rs b/sea-orm-macros/src/derives/into_active_model.rs index 4a0a6f2c0..19f34b93d 100644 --- a/sea-orm-macros/src/derives/into_active_model.rs +++ b/sea-orm-macros/src/derives/into_active_model.rs @@ -141,7 +141,7 @@ impl DeriveIntoActiveModel { let mut active_model_ident = active_model .clone() - .unwrap_or_else(|| syn::parse_str::("ActiveModel").unwrap()); + .unwrap_or_else(|| syn::parse_quote!(ActiveModel)); // Create a type alias for qualified types let type_alias_definition = if is_qualified_type(&active_model_ident) { diff --git a/sea-orm-macros/src/derives/model_ex.rs b/sea-orm-macros/src/derives/model_ex.rs index b1dc78d54..0b795844d 100644 --- a/sea-orm-macros/src/derives/model_ex.rs +++ b/sea-orm-macros/src/derives/model_ex.rs @@ -10,7 +10,7 @@ use super::util::{ }; use super::{expand_typed_column, model::DeriveModel}; use heck::ToUpperCamelCase; -use proc_macro2::{Ident, Span, TokenStream}; +use proc_macro2::{Ident, TokenStream}; use quote::{format_ident, quote}; use std::collections::{BTreeMap, HashMap}; use syn::{ @@ -274,9 +274,9 @@ impl RelationVariants { if let Some(name) = self.explicit_name() { Ident::new(&name.value().to_upper_camel_case(), name.span()) } else { - Ident::new( - &infer_relation_name_from_entity(entity).to_upper_camel_case(), - Span::call_site(), + format_ident!( + "{}", + infer_relation_name_from_entity(entity).to_upper_camel_case() ) } } @@ -823,7 +823,7 @@ fn expand_belongs_to_into( .map(|segment| segment.ident.to_string()) .collect::>() .join("::"); - let belongs_to = Ident::new("belongs_to", Span::call_site()); + let belongs_to = format_ident!("belongs_to"); let format_columns = |columns: &RelationColumns, prefix: &str| { let columns = columns .columns @@ -994,7 +994,7 @@ fn expand_has_one_into( .map(|segment| segment.ident.to_string()) .collect::>() .join("::"); - let has_one = Ident::new("has_one", Span::call_site()); + let has_one = format_ident!("has_one"); let mut extra: Punctuated<_, Comma> = Punctuated::new(); if let Some(via_rel) = &attr.via_rel { let tag = Ident::new("via_rel", via_rel.span()); @@ -1185,7 +1185,7 @@ fn expand_has_many_into( .. } = relation_variants { - let has_many = Ident::new("has_many", Span::call_site()); + let has_many = format_ident!("has_many"); let via_rel = format!("Relation::{}", relation_reverse.value()); output.relation_enum_variants.push(quote! { #[doc = " Generated by sea-orm-macros"] @@ -1200,10 +1200,10 @@ fn expand_has_many_into( .map(|segment| segment.ident.to_string()) .collect::>() .join("::"); - let has_many = Ident::new("has_many", Span::call_site()); + let has_many = format_ident!("has_many"); let mut extra: Punctuated<_, Comma> = Punctuated::new(); if let Some(via_rel) = via_rel { - let tag = Ident::new("via_rel", via_rel.span()); + let tag = format_ident!("via_rel"); let via_rel = format!("Relation::{}", via_rel.value()); extra.push(quote!(#tag = #via_rel)); } diff --git a/sea-orm-macros/src/derives/partial_model.rs b/sea-orm-macros/src/derives/partial_model.rs index c72882de4..dcd109b4b 100644 --- a/sea-orm-macros/src/derives/partial_model.rs +++ b/sea-orm-macros/src/derives/partial_model.rs @@ -69,7 +69,6 @@ impl DerivePartialModel { }; let mut entity = None; - let mut entity_string = String::new(); let mut active_model = None; let mut model_alias = None; let mut from_query_result = true; @@ -84,7 +83,6 @@ impl DerivePartialModel { for meta in list { if let Some(s) = meta.get_as_kv("entity") { entity = Some(syn::parse_str::(&s).map_err(Error::Syn)?); - entity_string = s; } else if let Some(s) = meta.get_as_kv("alias") { model_alias = Some(s); } else if let Some(s) = meta.get_as_kv("from_query_result") { @@ -99,12 +97,8 @@ impl DerivePartialModel { } if into_active_model { - active_model = Some( - syn::parse_str::(&format!( - "<{entity_string} as EntityTrait>::ActiveModel" - )) - .map_err(Error::Syn)?, - ); + let entity = entity.clone().ok_or(Error::EntityNotSpecified)?; + active_model = Some(syn::parse_quote!(<#entity as EntityTrait>::ActiveModel)); } let mut column_as_list = Vec::with_capacity(fields.len()); @@ -565,6 +559,21 @@ mod test { assert!(DerivePartialModel::new(input).is_err()); } + #[test] + fn test_into_active_model_requires_entity() { + let input: DeriveInput = parse_str( + r#" + #[sea_orm(into_active_model)] + struct PartialModel { + #[sea_orm(from_expr = "Expr::val(1).add(1)")] + total: i32, + } + "#, + ) + .unwrap(); + assert!(DerivePartialModel::new(input).is_err()); + } + const CODE_SNIPPET_5: &str = r#" struct PartialModel { #[sea_orm(nested)] diff --git a/sea-orm-macros/src/derives/related_entity.rs b/sea-orm-macros/src/derives/related_entity.rs index 558b776b4..e5c8ad1a4 100644 --- a/sea-orm-macros/src/derives/related_entity.rs +++ b/sea-orm-macros/src/derives/related_entity.rs @@ -2,8 +2,8 @@ mod private { use heck::ToLowerCamelCase; use proc_macro_crate::{FoundCrate, crate_name}; - use proc_macro2::{Ident, Span, TokenStream}; - use quote::{quote, quote_spanned}; + use proc_macro2::TokenStream; + use quote::{format_ident, quote, quote_spanned}; use crate::derives::attributes::related_attr; @@ -94,7 +94,7 @@ mod private { let async_graphql_crate = match crate_name("async-graphql") { // if found, use application's `async-graphql` Ok(FoundCrate::Name(name)) => { - let ident = Ident::new(&name, Span::call_site()); + let ident = format_ident!("{}", name); quote! { #ident } } Ok(FoundCrate::Itself) => quote! { async_graphql }, diff --git a/sea-orm-macros/src/derives/typed_column.rs b/sea-orm-macros/src/derives/typed_column.rs index 34a1726ec..a9a44ab11 100644 --- a/sea-orm-macros/src/derives/typed_column.rs +++ b/sea-orm-macros/src/derives/typed_column.rs @@ -4,7 +4,7 @@ use super::util::{CompoundType, escape_rust_keyword, trim_starting_raw_identifie use heck::ToUpperCamelCase; use proc_macro2::{Ident, TokenStream}; use quote::quote; -use syn::{Data, Fields, Lit, Visibility, spanned::Spanned}; +use syn::{Data, Fields, Lit, Visibility}; /// First is `struct TypedColumn`, second is the `const COLUMN` pub fn expand_typed_column( @@ -72,7 +72,7 @@ pub fn expand_typed_column( column_fields.push(ident.clone()); let wrapper = - super::value_type_match::column_type_wrapper(&column_type, field_ty, field.span()); + super::value_type_match::column_type_wrapper(&column_type, field_ty, ident.span()); column_types.push(if let Some(wrapper) = &wrapper { quote!(sea_orm::#wrapper) } else { diff --git a/sea-orm-macros/src/derives/util.rs b/sea-orm-macros/src/derives/util.rs index 19a72c535..252e04de1 100644 --- a/sea-orm-macros/src/derives/util.rs +++ b/sea-orm-macros/src/derives/util.rs @@ -1,6 +1,6 @@ use heck::ToUpperCamelCase; use proc_macro2::{Ident, Span, TokenStream}; -use quote::quote; +use quote::{format_ident, quote}; use syn::{ Field, GenericArgument, LitStr, Meta, MetaNameValue, PathArguments, Type, TypePath, meta::ParseNestedMeta, punctuated::Punctuated, token::Comma, @@ -54,9 +54,9 @@ impl RelationColumns { let Some(segment) = path.segments.last() else { return Err(syn::Error::new_spanned(path, "expected column path")); }; - Ok(Ident::new( - &escape_rust_keyword(segment.ident.to_string().to_upper_camel_case()), - segment.ident.span(), + Ok(format_ident!( + "{}", + escape_rust_keyword(segment.ident.to_string().to_upper_camel_case()) )) }) .collect::>>()?; @@ -125,8 +125,8 @@ impl CompoundType { pub(crate) fn matches_type(type_path: &TypePath) -> bool { last_path_segment(type_path).is_ok_and(|segment| { matches!( - segment.ident.to_string().as_str(), - "BelongsTo" | "HasOne" | "HasMany" + &segment.ident, + id if *id == "BelongsTo" || *id == "HasOne" || *id == "HasMany" ) }) } @@ -135,8 +135,8 @@ impl CompoundType { pub(crate) fn from_type(type_path: &TypePath) -> syn::Result> { let segment = last_path_segment(type_path)?; - match segment.ident.to_string().as_str() { - "BelongsTo" => { + match &segment.ident { + id if *id == "BelongsTo" => { let PathArguments::AngleBracketed(args) = &segment.arguments else { return Err(syn::Error::new_spanned( type_path, @@ -163,15 +163,12 @@ impl CompoundType { )); }; let target_segment = last_path_segment(ty_path)?; - match ( - target_segment.ident.to_string().as_str(), - &target_segment.arguments, - ) { - ("Entity", _) => Ok(Some(Self { + match (&target_segment.ident, &target_segment.arguments) { + (id, _) if *id == "Entity" => Ok(Some(Self { kind: CompoundKind::BelongsTo(CardinalityKind::Required), entity: ty_path.clone(), })), - ("Option", PathArguments::AngleBracketed(args)) => { + (id, PathArguments::AngleBracketed(args)) if *id == "Option" => { let Some(entity) = entity_generic_arg(&args.args) else { return Err(syn::Error::new_spanned( ty, @@ -189,7 +186,7 @@ impl CompoundType { )), } } - "HasOne" => { + id if *id == "HasOne" => { let PathArguments::AngleBracketed(args) = &segment.arguments else { return Err(syn::Error::new_spanned( type_path, @@ -207,7 +204,7 @@ impl CompoundType { entity, })) } - "HasMany" => { + id if *id == "HasMany" => { let PathArguments::AngleBracketed(args) = &segment.arguments else { return Err(syn::Error::new_spanned( type_path, diff --git a/sea-orm-macros/src/derives/value_type_match.rs b/sea-orm-macros/src/derives/value_type_match.rs index b09af6e07..c1843faf2 100644 --- a/sea-orm-macros/src/derives/value_type_match.rs +++ b/sea-orm-macros/src/derives/value_type_match.rs @@ -1,6 +1,6 @@ -use proc_macro2::{Span, TokenStream}; +use proc_macro2::{Ident, Span, TokenStream}; use quote::quote_spanned; -use syn::{GenericArgument, Ident, LitStr, PathArguments, Type, TypePath}; +use syn::{GenericArgument, LitStr, PathArguments, Type, TypePath}; pub fn column_type_expr( column_type: Option, diff --git a/sea-orm-macros/src/raw_sql.rs b/sea-orm-macros/src/raw_sql.rs index 5a7762287..217840a27 100644 --- a/sea-orm-macros/src/raw_sql.rs +++ b/sea-orm-macros/src/raw_sql.rs @@ -28,11 +28,11 @@ pub fn expand(input: proc_macro::TokenStream) -> syn::Result { .. } = syn::parse(input)?; - let builder = match backend.to_string().as_str() { - "MySql" => quote!(MysqlQueryBuilder), - "Postgres" => quote!(PostgresQueryBuilder), - "Sqlite" => quote!(SqliteQueryBuilder), - backend => panic!("Unsupported backend {backend}"), + let builder = match &backend { + id if *id == "MySql" => quote!(MysqlQueryBuilder), + id if *id == "Postgres" => quote!(PostgresQueryBuilder), + id if *id == "Sqlite" => quote!(SqliteQueryBuilder), + _ => panic!("Unsupported backend {backend}"), }; Ok(quote! {{