pgorm-derive 0.3.0

Derive macros for pgorm
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
//! Base insert/upsert/returning code generation.
//!
//! This module contains code generation for:
//! - `insert` / `insert_many` methods
//! - `upsert` / `upsert_many` methods
//! - `insert_returning` / `insert_many_returning` methods
//! - `upsert_returning` / `upsert_many_returning` methods
//! - `__pgorm_diff_many_by_fk` helper for diff strategy

use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use crate::common::syn_types::{AutoTimestampKind, option_inner};

use super::attrs::StructAttrs;

/// Information about a field that needs to be bound in SQL.
pub(super) struct BindField {
    pub(super) ident: syn::Ident,
    pub(super) ty: syn::Type,
    pub(super) column: String,
    /// If this field has auto_now_add, store the timestamp kind.
    pub(super) auto_now_add: Option<AutoTimestampKind>,
}

impl BindField {
    /// Get the effective type for batch operations.
    /// For auto_now_add fields, this is the inner type (DateTime<Utc> instead of Option<DateTime<Utc>>).
    pub(super) fn batch_ty(&self) -> &syn::Type {
        if self.auto_now_add.is_some() {
            // For auto_now_add fields, extract the inner type from Option<T>
            option_inner(&self.ty).unwrap_or(&self.ty)
        } else {
            &self.ty
        }
    }
}

/// Generate the INSERT SQL string.
pub(super) fn generate_insert_sql(
    table_name: &str,
    columns: &[String],
    values: &[String],
) -> String {
    if columns.is_empty() {
        format!("INSERT INTO {table_name} DEFAULT VALUES")
    } else {
        format!(
            "INSERT INTO {} ({}) VALUES ({})",
            table_name,
            columns.join(", "),
            values.join(", ")
        )
    }
}

/// Generate the `insert` method.
pub(super) fn generate_insert_method(
    insert_sql: &str,
    batch_bind_fields: &[BindField],
) -> TokenStream {
    let bind_field_idents: Vec<_> = batch_bind_fields.iter().map(|f| f.ident.clone()).collect();

    let destructure = if bind_field_idents.is_empty() {
        quote! { let _ = self; }
    } else {
        quote! { let Self { #(#bind_field_idents),*, .. } = self; }
    };

    // Check if any field has auto_now_add
    let has_auto_now_add = batch_bind_fields.iter().any(|f| f.auto_now_add.is_some());

    let now_init = if has_auto_now_add {
        quote! { let __pgorm_now = ::chrono::Utc::now(); }
    } else {
        quote! {}
    };

    // Generate bind expressions, handling auto_now_add fields
    let bind_exprs: Vec<TokenStream> = batch_bind_fields
        .iter()
        .map(|f| {
            let ident = &f.ident;
            match f.auto_now_add {
                Some(AutoTimestampKind::DateTimeUtc) => {
                    quote! { #ident.unwrap_or(__pgorm_now) }
                }
                Some(AutoTimestampKind::NaiveDateTime) => {
                    quote! { #ident.unwrap_or_else(|| __pgorm_now.naive_utc()) }
                }
                None => {
                    quote! { #ident }
                }
            }
        })
        .collect();

    let insert_query_expr = bind_exprs.iter().fold(
        quote! { pgorm::query(#insert_sql) },
        |acc, expr| quote! { #acc.bind(#expr) },
    );

    quote! {
        /// Insert a new row into the target table.
        pub async fn insert(self, conn: &impl pgorm::GenericClient) -> pgorm::OrmResult<u64> {
            #destructure
            #now_init
            #insert_query_expr.execute(conn).await
        }
    }
}

/// Generate the `insert_many` method.
pub(super) fn generate_insert_many_method(
    table_name: &str,
    batch_bind_fields: &[BindField],
) -> TokenStream {
    if batch_bind_fields.is_empty() {
        quote! {
            /// Insert multiple rows.
            ///
            /// Falls back to per-row inserts when bulk insert isn't applicable.
            pub async fn insert_many(
                conn: &impl pgorm::GenericClient,
                rows: ::std::vec::Vec<Self>,
            ) -> pgorm::OrmResult<u64> {
                if rows.is_empty() {
                    return ::std::result::Result::Ok(0);
                }

                let mut affected = 0_u64;
                for row in rows {
                    affected += row.insert(conn).await?;
                }
                ::std::result::Result::Ok(affected)
            }
        }
    } else {
        let batch_columns: Vec<String> =
            batch_bind_fields.iter().map(|f| f.column.clone()).collect();
        let batch_columns_str = batch_columns.join(", ");
        let list_idents: Vec<syn::Ident> = batch_bind_fields
            .iter()
            .map(|f| format_ident!("__pgorm_insert_{}_list", f.ident))
            .collect();
        let field_idents: Vec<syn::Ident> =
            batch_bind_fields.iter().map(|f| f.ident.clone()).collect();

        // For batch inserts, use the batch_ty which extracts inner type for auto_now_add fields
        let batch_tys: Vec<&syn::Type> = batch_bind_fields.iter().map(|f| f.batch_ty()).collect();

        // Check if any field has auto_now_add
        let has_auto_now_add = batch_bind_fields.iter().any(|f| f.auto_now_add.is_some());

        let now_init = if has_auto_now_add {
            quote! { let __pgorm_now = ::chrono::Utc::now(); }
        } else {
            quote! {}
        };

        let init_lists: Vec<TokenStream> = list_idents
            .iter()
            .zip(batch_tys.iter())
            .map(|(list_ident, ty)| {
                quote! { let mut #list_ident: ::std::vec::Vec<#ty> = ::std::vec::Vec::with_capacity(rows.len()); }
            })
            .collect();

        // Generate push expressions, handling auto_now_add fields
        let push_lists: Vec<TokenStream> = list_idents
            .iter()
            .zip(batch_bind_fields.iter())
            .map(|(list_ident, f)| {
                let field_ident = &f.ident;
                let value_expr = match f.auto_now_add {
                    Some(AutoTimestampKind::DateTimeUtc) => {
                        quote! { #field_ident.unwrap_or(__pgorm_now) }
                    }
                    Some(AutoTimestampKind::NaiveDateTime) => {
                        quote! { #field_ident.unwrap_or_else(|| __pgorm_now.naive_utc()) }
                    }
                    None => {
                        quote! { #field_ident }
                    }
                };
                quote! { #list_ident.push(#value_expr); }
            })
            .collect();

        // Generate type casts using PgType trait at runtime (use batch_ty for correct array type)
        let type_cast_exprs: Vec<TokenStream> = batch_tys
            .iter()
            .enumerate()
            .map(|(i, ty)| {
                let idx = i + 1;
                quote! { ::std::format!("${}::{}", #idx, <#ty as pgorm::PgType>::pg_array_type()) }
            })
            .collect();

        let bind_lists_expr = list_idents.iter().fold(
            quote! { pgorm::query(sql) },
            |acc, list_ident| quote! { #acc.bind(#list_ident) },
        );

        quote! {
            /// Insert multiple rows using a single statement (UNNEST bulk insert).
            pub async fn insert_many(
                conn: &impl pgorm::GenericClient,
                rows: ::std::vec::Vec<Self>,
            ) -> pgorm::OrmResult<u64> {
                if rows.is_empty() {
                    return ::std::result::Result::Ok(0);
                }

                #now_init
                #(#init_lists)*

                for row in rows {
                    let Self { #(#field_idents),*, .. } = row;
                    #(#push_lists)*
                }

                static __PGORM_INSERT_MANY_SQL: ::std::sync::OnceLock<::std::string::String> =
                    ::std::sync::OnceLock::new();
                let sql = __PGORM_INSERT_MANY_SQL.get_or_init(|| {
                    let type_casts: ::std::vec::Vec<::std::string::String> =
                        ::std::vec![#(#type_cast_exprs),*];
                    ::std::format!(
                        "INSERT INTO {} ({}) SELECT * FROM UNNEST({})",
                        #table_name,
                        #batch_columns_str,
                        type_casts.join(", ")
                    )
                });

                #bind_lists_expr.execute(conn).await
            }
        }
    }
}

/// Conflict specification for UPSERT operations.
pub(super) enum ConflictSpec {
    Constraint(String),
    Columns(Vec<String>),
}

/// Determine conflict specification from struct attributes.
pub(super) fn determine_conflict_spec(
    struct_attrs: &StructAttrs,
    id_field: Option<&BindField>,
) -> Option<ConflictSpec> {
    if let Some(constraint) = struct_attrs.conflict_constraint.clone() {
        Some(ConflictSpec::Constraint(constraint))
    } else if let Some(cols) = struct_attrs.conflict_target.clone() {
        Some(ConflictSpec::Columns(cols))
    } else {
        id_field.map(|f| ConflictSpec::Columns(vec![f.column.clone()]))
    }
}

/// Generate upsert methods.
pub(super) fn generate_upsert_methods(
    table_name: &str,
    struct_attrs: &StructAttrs,
    conflict_spec: &ConflictSpec,
    batch_bind_fields: &[BindField],
    id_field: Option<&BindField>,
) -> TokenStream {
    // Build the ON CONFLICT clause
    let (on_conflict_clause, conflict_cols_for_exclusion): (String, Vec<String>) =
        match conflict_spec {
            ConflictSpec::Constraint(name) => (format!("ON CONFLICT ON CONSTRAINT {name}"), vec![]),
            ConflictSpec::Columns(cols) => {
                (format!("ON CONFLICT ({})", cols.join(", ")), cols.clone())
            }
        };

    // For upsert, we need all fields that should be in the INSERT (including conflict columns)
    let (upsert_columns, upsert_bind_idents, upsert_bind_field_tys): (
        Vec<String>,
        Vec<syn::Ident>,
        Vec<syn::Type>,
    ) = match conflict_spec {
        ConflictSpec::Constraint(_) => {
            // With constraint-based conflict, include all bind fields
            (
                batch_bind_fields.iter().map(|f| f.column.clone()).collect(),
                batch_bind_fields.iter().map(|f| f.ident.clone()).collect(),
                batch_bind_fields.iter().map(|f| f.ty.clone()).collect(),
            )
        }
        ConflictSpec::Columns(conflict_cols) => {
            if struct_attrs.conflict_target.is_some() {
                // With explicit conflict_target, include all insert columns (bind fields)
                let mut columns: Vec<String> =
                    batch_bind_fields.iter().map(|f| f.column.clone()).collect();
                let mut idents: Vec<syn::Ident> =
                    batch_bind_fields.iter().map(|f| f.ident.clone()).collect();
                let mut tys: Vec<syn::Type> =
                    batch_bind_fields.iter().map(|f| f.ty.clone()).collect();

                // If we have an id field and it's in the conflict columns, add it
                if let Some(id_f) = id_field {
                    if conflict_cols.contains(&id_f.column) && !columns.contains(&id_f.column) {
                        columns.insert(0, id_f.column.clone());
                        idents.insert(0, id_f.ident.clone());
                        tys.insert(0, id_f.ty.clone());
                    }
                }

                (columns, idents, tys)
            } else if let Some(id_f) = id_field {
                // Using id field as conflict target (original behavior)
                let columns: Vec<String> = std::iter::once(id_f.column.clone())
                    .chain(batch_bind_fields.iter().map(|f| f.column.clone()))
                    .collect();
                let idents: Vec<syn::Ident> = std::iter::once(id_f.ident.clone())
                    .chain(batch_bind_fields.iter().map(|f| f.ident.clone()))
                    .collect();
                let tys: Vec<syn::Type> = std::iter::once(id_f.ty.clone())
                    .chain(batch_bind_fields.iter().map(|f| f.ty.clone()))
                    .collect();
                (columns, idents, tys)
            } else {
                // Should not happen since we have conflict_cols
                (vec![], vec![], vec![])
            }
        }
    };

    let placeholders: Vec<String> = (1..=upsert_bind_idents.len())
        .map(|i| format!("${i}"))
        .collect();

    // Update assignments
    let mut update_assignments: Vec<String> =
        if let Some(update_cols) = &struct_attrs.conflict_update {
            update_cols
                .iter()
                .map(|col| format!("{col} = EXCLUDED.{col}"))
                .collect()
        } else {
            upsert_columns
                .iter()
                .filter(|col| !conflict_cols_for_exclusion.contains(col))
                .map(|col| format!("{col} = EXCLUDED.{col}"))
                .collect()
        };
    if update_assignments.is_empty() {
        if let Some(first_col) = upsert_columns.first() {
            update_assignments.push(format!("{first_col} = EXCLUDED.{first_col}"));
        }
    }

    let upsert_sql = format!(
        "INSERT INTO {} ({}) VALUES ({}) {} DO UPDATE SET {}",
        table_name,
        upsert_columns.join(", "),
        placeholders.join(", "),
        on_conflict_clause,
        update_assignments.join(", ")
    );

    let upsert_destructure = quote! { let Self { #(#upsert_bind_idents),*, .. } = self; };
    let upsert_query_expr = upsert_bind_idents.iter().fold(
        quote! { ::pgorm::query(#upsert_sql) },
        |acc, ident| quote! { #acc.bind(#ident) },
    );

    let upsert_method = quote! {
        /// Insert a row, or update it if a conflict occurs on the conflict target (Postgres UPSERT).
        pub async fn upsert(self, conn: &impl ::pgorm::GenericClient) -> ::pgorm::OrmResult<u64> {
            #upsert_destructure
            #upsert_query_expr.execute(conn).await
        }
    };

    let upsert_batch_list_idents: Vec<syn::Ident> = upsert_bind_idents
        .iter()
        .map(|ident| format_ident!("__pgorm_upsert_{}_list", ident))
        .collect();

    let upsert_batch_init_lists: Vec<TokenStream> = upsert_batch_list_idents
        .iter()
        .zip(upsert_bind_field_tys.iter())
        .map(|(list_ident, ty)| {
            quote! { let mut #list_ident: ::std::vec::Vec<#ty> = ::std::vec::Vec::with_capacity(rows.len()); }
        })
        .collect();

    let upsert_batch_push_lists: Vec<TokenStream> = upsert_batch_list_idents
        .iter()
        .zip(upsert_bind_idents.iter())
        .map(|(list_ident, field_ident)| quote! { #list_ident.push(#field_ident); })
        .collect();

    // Generate type casts for upsert batch using PgType trait
    let upsert_type_cast_exprs: Vec<TokenStream> = upsert_bind_field_tys
        .iter()
        .enumerate()
        .map(|(i, ty)| {
            let idx = i + 1;
            quote! { ::std::format!("${}::{}", #idx, <#ty as ::pgorm::PgType>::pg_array_type()) }
        })
        .collect();

    let upsert_many_query_expr = upsert_batch_list_idents.iter().fold(
        quote! { ::pgorm::query(&upsert_batch_sql) },
        |acc, list_ident| quote! { #acc.bind(#list_ident) },
    );

    let upsert_columns_str = upsert_columns.join(", ");
    let update_assignments_str = update_assignments.join(", ");

    let upsert_many_method = quote! {
        /// Insert or update multiple rows using a single statement (UNNEST + ON CONFLICT).
        pub async fn upsert_many(
            conn: &impl ::pgorm::GenericClient,
            rows: ::std::vec::Vec<Self>,
        ) -> ::pgorm::OrmResult<u64> {
            if rows.is_empty() {
                return ::std::result::Result::Ok(0);
            }

            #(#upsert_batch_init_lists)*

            for row in rows {
                let Self { #(#upsert_bind_idents),*, .. } = row;
                #(#upsert_batch_push_lists)*
            }

            let type_casts: ::std::vec::Vec<::std::string::String> = ::std::vec![#(#upsert_type_cast_exprs),*];
            let upsert_batch_sql = ::std::format!(
                "INSERT INTO {} ({}) SELECT * FROM UNNEST({}) {} DO UPDATE SET {}",
                #table_name,
                #upsert_columns_str,
                type_casts.join(", "),
                #on_conflict_clause,
                #update_assignments_str
            );

            #upsert_many_query_expr.execute(conn).await
        }
    };

    let upsert_returning_methods = if let Some(returning_ty) = struct_attrs.returning.as_ref() {
        let upsert_returning_query_expr = upsert_bind_idents.iter().fold(
            quote! { ::pgorm::query(sql) },
            |acc, ident| quote! { #acc.bind(#ident) },
        );
        let upsert_many_returning_query_expr = upsert_batch_list_idents.iter().fold(
            quote! { ::pgorm::query(sql) },
            |acc, list_ident| quote! { #acc.bind(#list_ident) },
        );

        quote! {
            /// UPSERT and return the resulting row mapped as the configured returning type.
            pub async fn upsert_returning(
                self,
                conn: &impl ::pgorm::GenericClient,
            ) -> ::pgorm::OrmResult<#returning_ty>
            where
                #returning_ty: ::pgorm::FromRow,
            {
                #upsert_destructure
                let sql = ::std::format!(
                    "WITH {table} AS ({upsert} RETURNING *) SELECT {} FROM {table} {}",
                    #returning_ty::SELECT_LIST,
                    #returning_ty::JOIN_CLAUSE,
                    table = #table_name,
                    upsert = #upsert_sql,
                );
                #upsert_returning_query_expr.fetch_one_as::<#returning_ty>(conn).await
            }

            /// UPSERT multiple rows and return resulting rows mapped as the configured returning type.
            pub async fn upsert_many_returning(
                conn: &impl ::pgorm::GenericClient,
                rows: ::std::vec::Vec<Self>,
            ) -> ::pgorm::OrmResult<::std::vec::Vec<#returning_ty>>
            where
                #returning_ty: ::pgorm::FromRow,
            {
                if rows.is_empty() {
                    return ::std::result::Result::Ok(::std::vec::Vec::new());
                }

                #(#upsert_batch_init_lists)*

                for row in rows {
                    let Self { #(#upsert_bind_idents),*, .. } = row;
                    #(#upsert_batch_push_lists)*
                }

                let type_casts: ::std::vec::Vec<::std::string::String> = ::std::vec![#(#upsert_type_cast_exprs),*];
                let upsert_batch_sql = ::std::format!(
                    "INSERT INTO {} ({}) SELECT * FROM UNNEST({}) {} DO UPDATE SET {}",
                    #table_name,
                    #upsert_columns_str,
                    type_casts.join(", "),
                    #on_conflict_clause,
                    #update_assignments_str
                );

                let sql = ::std::format!(
                    "WITH {table} AS ({upsert} RETURNING *) SELECT {} FROM {table} {}",
                    #returning_ty::SELECT_LIST,
                    #returning_ty::JOIN_CLAUSE,
                    table = #table_name,
                    upsert = upsert_batch_sql,
                );

                #upsert_many_returning_query_expr.fetch_all_as::<#returning_ty>(conn).await
            }
        }
    } else {
        quote! {}
    };

    // Generate __pgorm_diff_many_by_fk helper for diff strategy
    let diff_helper = generate_diff_helper(
        table_name,
        &upsert_columns_str,
        &on_conflict_clause,
        &update_assignments_str,
        &upsert_bind_idents,
        &upsert_bind_field_tys,
    );

    quote! {
        #upsert_method
        #upsert_many_method
        #upsert_returning_methods
        #diff_helper
    }
}

/// Generate the `__pgorm_diff_many_by_fk` helper for diff strategy.
fn generate_diff_helper(
    table_name: &str,
    upsert_columns_str: &str,
    on_conflict_clause: &str,
    update_assignments_str: &str,
    upsert_bind_idents: &[syn::Ident],
    upsert_bind_field_tys: &[syn::Type],
) -> TokenStream {
    let field_idents: Vec<syn::Ident> = upsert_bind_idents.to_vec();
    let field_tys: Vec<syn::Type> = upsert_bind_field_tys.to_vec();
    let upsert_list_idents: Vec<syn::Ident> = field_idents
        .iter()
        .map(|ident| format_ident!("__pgorm_diff_{}_list", ident))
        .collect();

    let init_lists: Vec<TokenStream> = upsert_list_idents
        .iter()
        .zip(field_tys.iter())
        .map(|(list_ident, ty)| {
            quote! { let mut #list_ident: ::std::vec::Vec<#ty> = ::std::vec::Vec::with_capacity(rows.len()); }
        })
        .collect();

    let push_lists: Vec<TokenStream> = upsert_list_idents
        .iter()
        .zip(field_idents.iter())
        .map(|(list_ident, field_ident)| quote! { #list_ident.push(#field_ident); })
        .collect();

    // Generate type casts using PgType trait
    let type_cast_exprs: Vec<TokenStream> = field_tys
        .iter()
        .enumerate()
        .map(|(i, ty)| {
            // fk_value is $1, so columns start at $2
            let idx = i + 2;
            quote! { ::std::format!("${}::{}", #idx, <#ty as ::pgorm::PgType>::pg_array_type()) }
        })
        .collect();

    let bind_lists_expr = upsert_list_idents.iter().fold(
        quote! { ::pgorm::query(&sql).bind(fk_value) },
        |acc, list_ident| quote! { #acc.bind(#list_ident) },
    );

    quote! {
        /// Internal helper for diff strategy. Upserts rows and deletes rows not in the new list.
        ///
        /// This method is used by UpdateModel's has_many_update with strategy = "diff".
        #[doc(hidden)]
        pub async fn __pgorm_diff_many_by_fk<I>(
            conn: &impl ::pgorm::GenericClient,
            fk_column: &'static str,
            fk_value: I,
            key_columns: &'static [&'static str],
            rows: ::std::vec::Vec<Self>,
        ) -> ::pgorm::OrmResult<u64>
        where
            I: ::pgorm::tokio_postgres::types::ToSql + ::core::marker::Sync + ::core::marker::Send + 'static,
        {
            let rows_count = rows.len() as u64;

            if rows.is_empty() {
                // Empty list means delete all children with this fk
                let delete_sql = ::std::format!(
                    "DELETE FROM {} WHERE {} = $1",
                    #table_name,
                    fk_column
                );
                return ::pgorm::query(delete_sql).bind(fk_value).execute(conn).await;
            }

            // Collect columns into arrays
            #(#init_lists)*

            for row in rows {
                let Self { #(#field_idents),*, .. } = row;
                #(#push_lists)*
            }

            // Build the CTE query
            let type_casts: ::std::vec::Vec<::std::string::String> = ::std::vec![#(#type_cast_exprs),*];

            // Build key column equality conditions for NOT EXISTS
            let key_conditions: ::std::string::String = key_columns
                .iter()
                .map(|col| ::std::format!("u.{} = c.{}", col, col))
                .collect::<::std::vec::Vec<_>>()
                .join(" AND ");

            // Build RETURNING clause for key columns
            let returning_keys = key_columns.join(", ");

            let sql = ::std::format!(
                "WITH upserted AS (\
                    INSERT INTO {} ({}) \
                    SELECT * FROM UNNEST({}) AS t({}) \
                    {} DO UPDATE SET {} \
                    RETURNING {}\
                ), \
                deleted AS (\
                    DELETE FROM {} c \
                    WHERE c.{} = $1 \
                    AND NOT EXISTS (\
                        SELECT 1 FROM upserted u WHERE {}\
                    ) \
                    RETURNING 1\
                ) \
                SELECT (SELECT COUNT(*) FROM deleted) AS deleted_count",
                #table_name,
                #upsert_columns_str,
                type_casts.join(", "),
                #upsert_columns_str,
                #on_conflict_clause,
                #update_assignments_str,
                returning_keys,
                #table_name,
                fk_column,
                key_conditions
            );

            let row = #bind_lists_expr.fetch_one(conn).await?;
            let deleted_count: i64 = row.get(0);

            ::std::result::Result::Ok(rows_count + deleted_count as u64)
        }
    }
}

/// Generate the `insert_returning` and `insert_many_returning` methods.
pub(super) fn generate_returning_methods(
    table_name: &str,
    struct_attrs: &StructAttrs,
    insert_sql: &str,
    batch_bind_fields: &[BindField],
) -> TokenStream {
    let returning_ty = match struct_attrs.returning.as_ref() {
        Some(ty) => ty,
        None => return quote! {},
    };

    let bind_field_idents: Vec<_> = batch_bind_fields.iter().map(|f| f.ident.clone()).collect();

    let destructure = if bind_field_idents.is_empty() {
        quote! { let _ = self; }
    } else {
        quote! { let Self { #(#bind_field_idents),*, .. } = self; }
    };

    // Check if any field has auto_now_add
    let has_auto_now_add = batch_bind_fields.iter().any(|f| f.auto_now_add.is_some());

    let now_init = if has_auto_now_add {
        quote! { let __pgorm_now = ::chrono::Utc::now(); }
    } else {
        quote! {}
    };

    // Generate bind expressions, handling auto_now_add fields
    let bind_exprs: Vec<TokenStream> = batch_bind_fields
        .iter()
        .map(|f| {
            let ident = &f.ident;
            match f.auto_now_add {
                Some(AutoTimestampKind::DateTimeUtc) => {
                    quote! { #ident.unwrap_or(__pgorm_now) }
                }
                Some(AutoTimestampKind::NaiveDateTime) => {
                    quote! { #ident.unwrap_or_else(|| __pgorm_now.naive_utc()) }
                }
                None => {
                    quote! { #ident }
                }
            }
        })
        .collect();

    let returning_query_expr = bind_exprs
        .iter()
        .fold(quote! { pgorm::query(sql) }, |acc, expr| {
            quote! { #acc.bind(#expr) }
        });

    let insert_many_returning_method = if batch_bind_fields.is_empty() {
        quote! {
            /// Insert multiple rows and return created rows mapped as the configured returning type.
            ///
            /// Falls back to per-row inserts when bulk insert isn't applicable.
            pub async fn insert_many_returning(
                conn: &impl pgorm::GenericClient,
                rows: ::std::vec::Vec<Self>,
            ) -> pgorm::OrmResult<::std::vec::Vec<#returning_ty>>
            where
                #returning_ty: pgorm::FromRow,
            {
                if rows.is_empty() {
                    return ::std::result::Result::Ok(::std::vec::Vec::new());
                }

                let mut out = ::std::vec::Vec::with_capacity(rows.len());
                for row in rows {
                    out.push(row.insert_returning(conn).await?);
                }
                ::std::result::Result::Ok(out)
            }
        }
    } else {
        let batch_columns: Vec<String> =
            batch_bind_fields.iter().map(|f| f.column.clone()).collect();

        let list_idents: Vec<syn::Ident> = batch_bind_fields
            .iter()
            .map(|f| format_ident!("__pgorm_insert_{}_list", f.ident))
            .collect();
        let field_idents: Vec<syn::Ident> =
            batch_bind_fields.iter().map(|f| f.ident.clone()).collect();

        // For batch inserts, use the batch_ty which extracts inner type for auto_now_add fields
        let batch_tys: Vec<&syn::Type> = batch_bind_fields.iter().map(|f| f.batch_ty()).collect();

        let batch_now_init = if has_auto_now_add {
            quote! { let __pgorm_now = ::chrono::Utc::now(); }
        } else {
            quote! {}
        };

        let init_lists: Vec<TokenStream> = list_idents
            .iter()
            .zip(batch_tys.iter())
            .map(|(list_ident, ty)| {
                quote! { let mut #list_ident: ::std::vec::Vec<#ty> = ::std::vec::Vec::with_capacity(rows.len()); }
            })
            .collect();

        // Generate push expressions, handling auto_now_add fields
        let push_lists: Vec<TokenStream> = list_idents
            .iter()
            .zip(batch_bind_fields.iter())
            .map(|(list_ident, f)| {
                let field_ident = &f.ident;
                let value_expr = match f.auto_now_add {
                    Some(AutoTimestampKind::DateTimeUtc) => {
                        quote! { #field_ident.unwrap_or(__pgorm_now) }
                    }
                    Some(AutoTimestampKind::NaiveDateTime) => {
                        quote! { #field_ident.unwrap_or_else(|| __pgorm_now.naive_utc()) }
                    }
                    None => {
                        quote! { #field_ident }
                    }
                };
                quote! { #list_ident.push(#value_expr); }
            })
            .collect();

        let batch_returning_query_expr = list_idents.iter().fold(
            quote! { pgorm::query(sql) },
            |acc, list_ident| quote! { #acc.bind(#list_ident) },
        );

        // Generate type casts for batch insert returning using PgType trait (use batch_ty)
        let batch_type_cast_exprs: Vec<TokenStream> = batch_tys
            .iter()
            .enumerate()
            .map(|(i, ty)| {
                let idx = i + 1;
                quote! { ::std::format!("${}::{}", #idx, <#ty as pgorm::PgType>::pg_array_type()) }
            })
            .collect();

        let batch_columns_str = batch_columns.join(", ");

        quote! {
            /// Insert multiple rows and return created rows mapped as the configured returning type.
            pub async fn insert_many_returning(
                conn: &impl pgorm::GenericClient,
                rows: ::std::vec::Vec<Self>,
            ) -> pgorm::OrmResult<::std::vec::Vec<#returning_ty>>
            where
                #returning_ty: pgorm::FromRow,
            {
                if rows.is_empty() {
                    return ::std::result::Result::Ok(::std::vec::Vec::new());
                }

                #batch_now_init
                #(#init_lists)*

                for row in rows {
                    let Self { #(#field_idents),*, .. } = row;
                    #(#push_lists)*
                }

                static __PGORM_INSERT_MANY_RETURNING_SQL: ::std::sync::OnceLock<::std::string::String> =
                    ::std::sync::OnceLock::new();
                let sql = __PGORM_INSERT_MANY_RETURNING_SQL.get_or_init(|| {
                    let type_casts: ::std::vec::Vec<::std::string::String> =
                        ::std::vec![#(#batch_type_cast_exprs),*];
                    let batch_insert_sql = ::std::format!(
                        "INSERT INTO {} ({}) SELECT * FROM UNNEST({})",
                        #table_name,
                        #batch_columns_str,
                        type_casts.join(", ")
                    );
                    ::std::format!(
                        "WITH {table} AS ({insert} RETURNING *) SELECT {} FROM {table} {}",
                        #returning_ty::SELECT_LIST,
                        #returning_ty::JOIN_CLAUSE,
                        table = #table_name,
                        insert = batch_insert_sql,
                    )
                });

                #batch_returning_query_expr.fetch_all_as::<#returning_ty>(conn).await
            }
        }
    };

    quote! {
        /// Insert and return the created row mapped as the configured returning type.
        pub async fn insert_returning(
            self,
            conn: &impl pgorm::GenericClient,
        ) -> pgorm::OrmResult<#returning_ty>
        where
            #returning_ty: pgorm::FromRow,
        {
            #destructure
            #now_init
            let sql = ::std::format!(
                "WITH {table} AS ({insert} RETURNING *) SELECT {} FROM {table} {}",
                #returning_ty::SELECT_LIST,
                #returning_ty::JOIN_CLAUSE,
                table = #table_name,
                insert = #insert_sql,
            );
            #returning_query_expr.fetch_one_as::<#returning_ty>(conn).await
        }

        #insert_many_returning_method
    }
}