MCPcopy Create free account
hub / github.com/SeaQL/sea-orm / impl_partial_model

Method impl_partial_model

sea-orm-macros/src/derives/partial_model.rs:252–356  ·  view source on GitHub ↗
(&self)

Source from the content-addressed store, hash-verified

250 }
251
252 fn impl_partial_model(&self) -> TokenStream {
253 let select_ident = format_ident!("select");
254 let DerivePartialModel {
255 entity,
256 alias,
257 ident,
258 fields,
259 ..
260 } = self;
261 let select_col_code_gen = fields.iter().map(|col_as| match col_as {
262 ColumnAs::Col { col, field } => {
263 let field = field.unraw().to_string();
264 let entity = entity.as_ref().unwrap();
265
266 let variant_name = if let Some(col) = col {
267 col
268 } else {
269 &format_ident!("{}", field.to_upper_camel_case())
270 };
271
272 // variant of the entity column
273 let column = quote! {
274 <#entity as sea_orm::EntityTrait>::Column::#variant_name
275 };
276
277 // We cast enum as text in select_as if the backend is postgres
278
279 let non_nested = match alias {
280 Some(alias) => quote! {
281 let col_expr = sea_orm::sea_query::Expr::col((#alias, #column));
282 let casted = sea_orm::ColumnTrait::select_as(&#column, col_expr);
283 sea_orm::SelectColumns::select_column_as(#select_ident, casted, col_alias)
284 },
285 None => quote! {
286 sea_orm::SelectColumns::select_column_as(#select_ident, #column, col_alias)
287 },
288 };
289
290 quote! {
291 let #select_ident = {
292 let col_alias = pre.map_or(#field.to_string(), |pre| format!("{pre}{}", #field));
293 if let Some(nested_alias) = nested_alias {
294 // TODO: Replace this with the new iden after updating to sea-query 1.0
295 let alias = sea_orm::sea_query::Alias::new(nested_alias);
296 let alias_iden = sea_orm::sea_query::DynIden::new(alias);
297 let col_expr = sea_orm::sea_query::Expr::col(
298 (alias_iden, #column)
299 );
300
301 let casted = sea_orm::ColumnTrait::select_as(&#column, col_expr);
302 sea_orm::SelectColumns::select_column_as(#select_ident, casted, col_alias)
303 } else {
304 #non_nested
305 }
306 };
307 }
308 }
309

Callers 1

expandMethod · 0.80

Calls 2

unwrapMethod · 0.80
as_refMethod · 0.80

Tested by

no test coverage detected