axumstart_db_macros 0.1.2

Proc macro powering axumstart_db's #[repository], SqlxInsert, and SqlxUpdate
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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use std::collections::{HashMap, HashSet};
use syn::{
    parse::Parse, parse::ParseStream, parse_macro_input, spanned::Spanned, FnArg,
    GenericArgument, ItemTrait, LitStr, PathArguments, ReturnType, Signature, Token, TraitItem,
    TraitItemFn, Type,
};

use crate::dialect;
use crate::dsl::{self, WhereChunk, WhereClause};

struct TableAttr {
    table: String,
    mock: bool,
    component: bool,
}

impl Parse for TableAttr {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let ident: syn::Ident = input.parse()?;
        if ident != "table" {
            return Err(syn::Error::new(ident.span(), "expected `table = \"name\"`"));
        }
        input.parse::<Token![=]>()?;
        let table = input.parse::<LitStr>()?.value();
        let mut mock = false;
        let mut component = false;
        while input.peek(Token![,]) {
            input.parse::<Token![,]>()?;
            if input.is_empty() {
                break;
            }
            let flag: syn::Ident = input.parse()?;
            if flag == "mock" {
                mock = true;
            } else if flag == "component" {
                component = true;
            } else {
                return Err(syn::Error::new(
                    flag.span(),
                    format!("unknown attribute `{flag}`; expected `mock` or `component`"),
                ));
            }
        }
        Ok(TableAttr { table, mock, component })
    }
}

struct Ctx<'a> {
    table: &'a str,
    relations: &'a [dsl::Relation],
}

pub fn expand(attr: TokenStream, item: TokenStream) -> TokenStream {
    let TableAttr { table, mock, component } = parse_macro_input!(attr as TableAttr);
    let mut trait_def = parse_macro_input!(item as ItemTrait);
    let trait_name = trait_def.ident.clone();
    let vis = trait_def.vis.clone();
    let struct_name = format_ident!("Db{}", trait_name);

    let mut errors: Vec<TokenStream2> = Vec::new();
    let relations = collect_relations(&mut trait_def, &table, &mut errors);
    let ctx = Ctx { table: &table, relations: &relations };
    let db = crate::krate::db_path();
    let components = crate::krate::components_path();

    let unique_map = collect_attr_map(&mut trait_def, "unique", &mut errors);
    let created_at_map = collect_attr_map(&mut trait_def, "created_at", &mut errors);
    let unchecked = collect_flag_attr(&mut trait_def, "unchecked_columns");
    let order_by_map = collect_order_by_map(&mut trait_def, &mut errors);
    validate_attr_targets(&unique_map, &created_at_map, &mut errors);

    let tx_parts = process_transactional(
        &mut trait_def,
        &ctx,
        &unique_map,
        &created_at_map,
        &unchecked,
        &order_by_map,
    );
    let (tx_trait_sigs, tx_impls, tx_probes): (Vec<_>, Vec<_>, Vec<_>) = itertools_unzip3(tx_parts);

    let mut method_impls: Vec<TokenStream2> = Vec::new();
    let mut probes: Vec<TokenStream2> = Vec::new();
    for item in &trait_def.items {
        if let TraitItem::Fn(m) = item {
            if m.default.is_none() {
                let (impl_tokens, probe_tokens) =
                    gen_method(m, &ctx, &unique_map, &created_at_map, &unchecked, &order_by_map);
                method_impls.push(impl_tokens);
                probes.push(probe_tokens);
            }
        }
    }

    let trait_attrs = &trait_def.attrs;
    let generics = &trait_def.generics;
    let colon_token = &trait_def.colon_token;
    let supertraits = &trait_def.supertraits;
    let trait_items = &trait_def.items;
    let mock_name = format_ident!("Mock{}", trait_name);
    let mock_attr = if mock {
        quote!(#[cfg_attr(test, ::mockall::automock)])
    } else {
        quote!()
    };
    let mock_repo_impl = if mock {
        quote! {
            #[cfg(test)]
            impl #db::Repository for #mock_name {
                fn pool(&self) -> &#db::DbPool {
                    panic!("pool() called on mock")
                }
            }
        }
    } else {
        quote!()
    };

    let component_impls = if component {
        quote! {
            #[#components::async_trait]
            impl #components::ComponentBlueprint for #struct_name {
                async fn new(ctx: &#components::ComponentProvider) -> Self {
                    Self { pool: ctx.get_cloned::<#db::DbPool>().await }
                }
            }
            impl #components::DynComponentBlueprint for #struct_name {
                type Dyn = dyn #trait_name;
                fn upcast(arc: ::std::sync::Arc<Self>) -> ::std::sync::Arc<dyn #trait_name> {
                    arc
                }
            }
            #components::inventory::submit! {
                #components::ComponentRegistration(|ctx: &#components::ComponentProvider| {
                    #components::RegisterProbe::<#struct_name>::new().register(ctx);
                })
            }
        }
    } else {
        quote!()
    };

    TokenStream::from(quote! {
        #(#errors)*

        #(#trait_attrs)*
        #mock_attr
        #[::async_trait::async_trait]
        #vis trait #trait_name #generics #colon_token #supertraits {
            #(#trait_items)*
            #(#tx_trait_sigs;)*
        }

        #vis struct #struct_name {
            pool: #db::DbPool,
        }

        impl #struct_name {
            pub fn new(pool: #db::DbPool) -> Self {
                Self { pool }
            }
        }

        impl #db::Repository for #struct_name {
            fn pool(&self) -> &#db::DbPool {
                &self.pool
            }
        }

        #[::async_trait::async_trait]
        impl #trait_name for #struct_name {
            #(#method_impls)*
            #(#tx_impls)*
        }

        #component_impls
        #mock_repo_impl

        #(#probes)*
        #(#tx_probes)*
    })
}

fn itertools_unzip3(
    v: Vec<(TokenStream2, TokenStream2, TokenStream2)>,
) -> (Vec<TokenStream2>, Vec<TokenStream2>, Vec<TokenStream2>) {
    let mut a = Vec::with_capacity(v.len());
    let mut b = Vec::with_capacity(v.len());
    let mut c = Vec::with_capacity(v.len());
    for (x, y, z) in v {
        a.push(x);
        b.push(y);
        c.push(z);
    }
    (a, b, c)
}

// Scans all trait methods for #[<attr_name>(col)], strips the attr in place,
// returns method_name → column map. Bad argument shape becomes a compile error.
fn collect_attr_map(
    trait_def: &mut ItemTrait,
    attr_name: &str,
    errors: &mut Vec<TokenStream2>,
) -> HashMap<String, (String, Span)> {
    let mut map = HashMap::new();
    for item in trait_def.items.iter_mut() {
        if let TraitItem::Fn(method) = item {
            if let Some(pos) = method.attrs.iter().position(|a| a.path().is_ident(attr_name)) {
                let attr = method.attrs.remove(pos);
                match attr.parse_args::<syn::Ident>() {
                    Ok(col) => {
                        map.insert(
                            method.sig.ident.to_string(),
                            (col.to_string(), method.sig.ident.span()),
                        );
                    }
                    Err(_) => {
                        let e = syn::Error::new(
                            method.sig.ident.span(),
                            format!("`#[{attr_name}(...)]` expects a single column identifier, e.g. `#[{attr_name}(user_id)]`"),
                        );
                        errors.push(e.to_compile_error());
                    }
                }
            }
        }
    }
    map
}

// Scans all trait methods for #[order_by("col1", "col2", ...)], strips the attr in
// place, and returns method_name → ordered column list. The method name must end in
// `_ordered`; codegen dispatches on the name with that suffix stripped.
fn collect_order_by_map(
    trait_def: &mut ItemTrait,
    errors: &mut Vec<TokenStream2>,
) -> HashMap<String, Vec<String>> {
    let mut map = HashMap::new();
    for item in trait_def.items.iter_mut() {
        let TraitItem::Fn(method) = item else { continue };
        let Some(pos) = method.attrs.iter().position(|a| a.path().is_ident("order_by")) else {
            continue;
        };
        let attr = method.attrs.remove(pos);
        let name = method.sig.ident.to_string();
        let span = method.sig.ident.span();

        if !name.ends_with("_ordered") {
            errors.push(
                syn::Error::new(
                    span,
                    format!(
                        "`#[order_by(...)]` on `{name}` requires the method name to end with `_ordered`"
                    ),
                )
                .to_compile_error(),
            );
            continue;
        }

        match attr.parse_args_with(|input: ParseStream| {
            let mut cols = Vec::new();
            loop {
                cols.push(input.parse::<LitStr>()?.value());
                if input.peek(Token![,]) {
                    input.parse::<Token![,]>()?;
                } else {
                    break;
                }
            }
            Ok(cols)
        }) {
            Ok(cols) => {
                map.insert(name, cols);
            }
            Err(_) => {
                errors.push(
                    syn::Error::new(
                        span,
                        "`#[order_by(...)]` expects one or more string column names, e.g. `#[order_by(\"col1\", \"col2\")]`",
                    )
                    .to_compile_error(),
                );
            }
        }
    }
    map
}

// Strips a bare marker attribute (e.g. #[unchecked_columns]) from methods,
// returning the names of methods that carried it.
fn collect_flag_attr(trait_def: &mut ItemTrait, attr_name: &str) -> HashSet<String> {
    let mut set = HashSet::new();
    for item in trait_def.items.iter_mut() {
        if let TraitItem::Fn(method) = item {
            if let Some(pos) = method.attrs.iter().position(|a| a.path().is_ident(attr_name)) {
                method.attrs.remove(pos);
                set.insert(method.sig.ident.to_string());
            }
        }
    }
    set
}

fn validate_attr_targets(
    unique_map: &HashMap<String, (String, Span)>,
    created_at_map: &HashMap<String, (String, Span)>,
    errors: &mut Vec<TokenStream2>,
) {
    for (method, (_, span)) in unique_map {
        if method != "upsert" {
            errors.push(
                syn::Error::new(
                    *span,
                    format!("`#[unique(...)]` on `{method}` has no effect; it only applies to `upsert`"),
                )
                .to_compile_error(),
            );
        }
    }
    for (method, (_, span)) in created_at_map {
        if !method.contains("_this_week") {
            errors.push(
                syn::Error::new(
                    *span,
                    format!("`#[created_at(...)]` on `{method}` has no effect; it only applies to `*_this_week` methods"),
                )
                .to_compile_error(),
            );
        }
    }
}

// Scans the trait's own attribute list (not per-method) for #[belongs_to(...)],
// #[has_one(...)], #[has_many(...)], #[belongs_to_many(...)], strips each match, and
// returns the parsed relations. A trait may declare several relations of the same or
// different kinds, so this loops until no more matches of a given attribute name remain.
fn collect_relations(
    trait_def: &mut ItemTrait,
    this_table: &str,
    errors: &mut Vec<TokenStream2>,
) -> Vec<dsl::Relation> {
    let mut relations = Vec::new();
    let attr_kinds = [
        ("belongs_to", dsl::RelationKind::BelongsTo),
        ("has_one", dsl::RelationKind::HasOne),
        ("has_many", dsl::RelationKind::HasMany),
        ("belongs_to_many", dsl::RelationKind::BelongsToMany),
    ];
    for (attr_name, kind) in attr_kinds {
        loop {
            let Some(pos) = trait_def.attrs.iter().position(|a| a.path().is_ident(attr_name))
            else {
                break;
            };
            let attr = trait_def.attrs.remove(pos);
            let span = attr.path().span();
            let result = parse_relation_args(&attr)
                .and_then(|args| build_relation(kind, this_table, attr_name, args, span));
            match result {
                Ok(rel) => relations.push(rel),
                Err(e) => errors.push(e.to_compile_error()),
            }
        }
    }
    relations
}

fn parse_relation_args(attr: &syn::Attribute) -> syn::Result<Vec<(syn::Ident, LitStr)>> {
    attr.parse_args_with(|input: ParseStream| {
        let mut pairs = Vec::new();
        loop {
            if input.is_empty() {
                break;
            }
            let key: syn::Ident = input.parse()?;
            input.parse::<Token![=]>()?;
            let val: LitStr = input.parse()?;
            pairs.push((key, val));
            if input.peek(Token![,]) {
                input.parse::<Token![,]>()?;
            } else {
                break;
            }
        }
        Ok(pairs)
    })
}

fn build_relation(
    kind: dsl::RelationKind,
    this_table: &str,
    attr_name: &str,
    args: Vec<(syn::Ident, LitStr)>,
    span: Span,
) -> syn::Result<dsl::Relation> {
    let mut table: Option<String> = None;
    let mut fk_raw: Option<String> = None;
    let mut other_fk_raw: Option<String> = None;
    let mut through_raw: Option<String> = None;

    for (key, val) in &args {
        match key.to_string().as_str() {
            "table" => table = Some(val.value()),
            "fk" => fk_raw = Some(val.value()),
            "other_fk" => other_fk_raw = Some(val.value()),
            "through" => through_raw = Some(val.value()),
            other => {
                return Err(syn::Error::new(
                    key.span(),
                    format!("unknown key `{other}` in `#[{attr_name}(...)]`"),
                ));
            }
        }
    }

    let table = table.ok_or_else(|| {
        syn::Error::new(span, format!("`#[{attr_name}(...)]` requires `table = \"...\"`"))
    })?;

    let is_many = matches!(kind, dsl::RelationKind::BelongsToMany);
    if is_many && through_raw.is_none() {
        return Err(syn::Error::new(
            span,
            "`#[belongs_to_many(...)]` requires `through = \"...\"`",
        ));
    }
    if !is_many {
        if through_raw.is_some() {
            return Err(syn::Error::new(
                span,
                format!("`through` is only valid on `belongs_to_many`, not `#[{attr_name}(...)]`"),
            ));
        }
        if other_fk_raw.is_some() {
            return Err(syn::Error::new(
                span,
                format!(
                    "`other_fk` is only valid on `belongs_to_many`, not `#[{attr_name}(...)]`"
                ),
            ));
        }
    }

    let fk = fk_raw.unwrap_or_else(|| match kind {
        dsl::RelationKind::BelongsTo => format!("{table}_id"),
        _ => format!("{this_table}_id"),
    });
    let other_fk = is_many.then(|| other_fk_raw.unwrap_or_else(|| format!("{table}_id")));

    Ok(dsl::Relation { table, kind, fk, other_fk, through: through_raw })
}

fn process_transactional(
    trait_def: &mut ItemTrait,
    ctx: &Ctx,
    unique_map: &HashMap<String, (String, Span)>,
    created_at_map: &HashMap<String, (String, Span)>,
    unchecked: &HashSet<String>,
    order_by_map: &HashMap<String, Vec<String>>,
) -> Vec<(TokenStream2, TokenStream2, TokenStream2)> {
    let mut orig_sigs: Vec<Signature> = Vec::new();

    for item in trait_def.items.iter_mut() {
        if let TraitItem::Fn(method) = item {
            if let Some(pos) = method.attrs.iter().position(|a| a.path().is_ident("transactional")) {
                method.attrs.remove(pos);
                orig_sigs.push(method.sig.clone());
            }
        }
    }

    orig_sigs
        .into_iter()
        .map(|sig| {
            let tx_sig = make_tx_sig(&sig);
            let name = sig.ident.to_string();
            let unique_col = unique_map.get(&name).map(|(c, _)| c.as_str());
            let date_col = created_at_map.get(&name).map(|(c, _)| c.as_str());
            let order_cols = order_by_map.get(&name).map(|c| c.as_slice());
            match gen_body(&name, &sig, ctx, quote!(conn), unique_col, date_col, order_cols) {
                Ok((body, probe)) => {
                    let probe = if unchecked.contains(&name) { quote!() } else { probe };
                    (quote!(#tx_sig), quote!(#tx_sig { #body }), probe)
                }
                Err(e) => {
                    let err = e.to_compile_error();
                    (quote!(#tx_sig), quote!(#tx_sig { #err }), quote!())
                }
            }
        })
        .collect()
}

fn make_tx_sig(sig: &Signature) -> Signature {
    let db = crate::krate::db_path();
    let mut tx_sig = sig.clone();
    tx_sig.ident = format_ident!("{}_tx", sig.ident);
    let conn: FnArg = syn::parse_quote!(conn: &mut #db::DbConnection);
    tx_sig.inputs.insert(1, conn);
    tx_sig
}

fn gen_method(
    m: &TraitItemFn,
    ctx: &Ctx,
    unique_map: &HashMap<String, (String, Span)>,
    created_at_map: &HashMap<String, (String, Span)>,
    unchecked: &HashSet<String>,
    order_by_map: &HashMap<String, Vec<String>>,
) -> (TokenStream2, TokenStream2) {
    let sig = &m.sig;
    let name = sig.ident.to_string();
    let db = crate::krate::db_path();
    let unique_col = unique_map.get(&name).map(|(c, _)| c.as_str());
    let date_col = created_at_map.get(&name).map(|(c, _)| c.as_str());
    let order_cols = order_by_map.get(&name).map(|c| c.as_slice());
    match gen_body(&name, sig, ctx, quote!(#db::Repository::pool(self)), unique_col, date_col, order_cols) {
        Ok((body, probe)) => {
            let probe = if unchecked.contains(&name) { quote!() } else { probe };
            (quote! { #sig { #body } }, probe)
        }
        Err(e) => {
            let err = e.to_compile_error();
            (quote! { #sig { #err } }, quote!())
        }
    }
}

fn no_order_by(sig: &Signature, order_cols: Option<&[String]>, name: &str) -> syn::Result<()> {
    if order_cols.is_some() {
        return Err(syn::Error::new(
            sig.ident.span(),
            format!(
                "`#[order_by(...)]` has no effect on `{name}`; only supported on find_by_*, \
                 find_all_by_*, find_all, and bare boolean find_all_<field> methods"
            ),
        ));
    }
    Ok(())
}

fn gen_body(
    name: &str,
    sig: &Signature,
    ctx: &Ctx,
    exec: TokenStream2,
    unique_col: Option<&str>,
    date_col: Option<&str>,
    order_cols: Option<&[String]>,
) -> syn::Result<(TokenStream2, TokenStream2)> {
    let db = crate::krate::db_path();
    let dispatch_name = match order_cols {
        Some(_) => name.strip_suffix("_ordered").unwrap_or(name),
        None => name,
    };

    if let Some(f) = dispatch_name.strip_prefix("find_all_by_") {
        gen_select_all(sig, ctx, f, exec, date_col, order_cols)
    } else if let Some(f) = dispatch_name.strip_prefix("find_random_by_") {
        gen_filtered(sig, ctx, f, exec, Kind::Random, order_cols)
    } else if let Some(f) = dispatch_name.strip_prefix("find_by_") {
        gen_filtered(sig, ctx, f, exec, Kind::One, order_cols)
    } else if let Some(f) = dispatch_name.strip_prefix("count_by_") {
        gen_filtered(sig, ctx, f, exec, Kind::Count, order_cols)
    } else if let Some(f) = dispatch_name.strip_prefix("exists_by_") {
        gen_filtered(sig, ctx, f, exec, Kind::Exists, order_cols)
    } else if let Some(f) = dispatch_name.strip_prefix("delete_by_") {
        gen_filtered(sig, ctx, f, exec, Kind::Delete, order_cols)
    } else if dispatch_name == "find_all" {
        gen_select_all_no_filter(sig, ctx, exec, None, order_cols)
    } else if let Some(order_str) = dispatch_name.strip_prefix("find_all_order_by_") {
        no_order_by(sig, order_cols, name)?;
        gen_select_all_no_filter(sig, ctx, exec, Some(order_str), None)
    } else if let Some(field) = dispatch_name.strip_prefix("find_all_") {
        gen_select_all_bool_flag(sig, ctx, field, exec, order_cols)
    } else if dispatch_name == "upsert" {
        no_order_by(sig, order_cols, name)?;
        gen_upsert(sig, ctx, exec, unique_col)
    } else if dispatch_name == "update" {
        no_order_by(sig, order_cols, name)?;
        gen_delegated(sig, ctx, exec, "update", quote!(__sqlx_update_in))
    } else if dispatch_name == "insert" {
        no_order_by(sig, order_cols, name)?;
        let method = if returns_unit(&sig.output) {
            quote!(__sqlx_insert_into_void)
        } else {
            quote!(__sqlx_insert_into)
        };
        gen_delegated(sig, ctx, exec, "insert", method)
    } else if dispatch_name == "insert_or_ignore" {
        no_order_by(sig, order_cols, name)?;
        gen_delegated(sig, ctx, exec, "insert_or_ignore", quote!(__sqlx_insert_ignore_into))
    } else if dispatch_name == "insert_all" {
        no_order_by(sig, order_cols, name)?;
        gen_insert_all(sig, ctx, exec)
    } else if let Some(rest) = dispatch_name.strip_prefix("set_") {
        no_order_by(sig, order_cols, name)?;
        gen_set(sig, ctx, rest, exec)
    } else if dispatch_name == "pool" {
        no_order_by(sig, order_cols, name)?;
        Ok((quote! { #db::Repository::pool(self) }, quote!()))
    } else {
        Err(syn::Error::new(
            sig.ident.span(),
            format!(
                "no codegen rule for `{name}`; expected one of: find_by_*, find_all, \
                 find_all_by_*, find_all_order_by_*, find_random_by_*, find_all_<bool_field>, \
                 count_by_*, exists_by_*, delete_by_*, set_*_by_*, insert, insert_all, \
                 insert_or_ignore, upsert, update, pool — or provide a method body"
            ),
        ))
    }
}

// ---------- parameter handling ----------

struct SplitParams {
    /// Non-Page value parameters, in declaration order.
    binds: Vec<TokenStream2>,
    /// Pattern of the `Page`-typed parameter, if any.
    page: Option<TokenStream2>,
}

fn split_params(sig: &Signature) -> syn::Result<SplitParams> {
    let mut binds = Vec::new();
    let mut page = None;
    for arg in &sig.inputs {
        if let FnArg::Typed(pt) = arg {
            if is_page_type(&pt.ty) {
                if page.is_some() {
                    return Err(syn::Error::new(
                        sig.ident.span(),
                        "at most one `Page` parameter is allowed",
                    ));
                }
                page = Some(pt.pat.to_token_stream());
            } else {
                binds.push(pt.pat.to_token_stream());
            }
        }
    }
    Ok(SplitParams { binds, page })
}

fn is_page_type(ty: &Type) -> bool {
    matches!(ty, Type::Path(tp) if tp.path.segments.last().is_some_and(|s| s.ident == "Page"))
}

fn check_arity(sig: &Signature, actual: usize, expected: usize) -> syn::Result<()> {
    if actual != expected {
        return Err(syn::Error::new(
            sig.ident.span(),
            format!(
                "`{}` takes {actual} bindable parameter(s) but its name implies {expected} \
                 (excluding `&self` and any `Page` parameter)",
                sig.ident
            ),
        ));
    }
    Ok(())
}

fn no_page(sig: &Signature, sp: &SplitParams) -> syn::Result<()> {
    if sp.page.is_some() {
        return Err(syn::Error::new(
            sig.ident.span(),
            "`Page` parameter is only supported on find_all* methods",
        ));
    }
    Ok(())
}

// ---------- return type analysis ----------

fn generic_arg(ty: &Type, seg_name: &str) -> Option<Type> {
    let Type::Path(tp) = ty else { return None };
    let seg = tp.path.segments.last()?;
    if seg.ident != seg_name {
        return None;
    }
    let PathArguments::AngleBracketed(ab) = &seg.arguments else { return None };
    ab.args.iter().find_map(|a| match a {
        GenericArgument::Type(t) => Some(t.clone()),
        _ => None,
    })
}

fn result_ok_type(ret: &ReturnType) -> Option<Type> {
    let ReturnType::Type(_, ty) = ret else { return None };
    generic_arg(ty, "Result")
}

/// True for `sqlx::Result<()>` — used by `insert`/`insert_all` to skip RETURNING (or, on
/// MySQL, the write-then-select-back emulation entirely) when the caller doesn't want the
/// row back.
fn returns_unit(ret: &ReturnType) -> bool {
    matches!(result_ok_type(ret), Some(Type::Tuple(t)) if t.elems.is_empty())
}

fn fetch_method(ret: &ReturnType) -> TokenStream2 {
    if let Some(ok) = result_ok_type(ret) {
        if generic_arg(&ok, "Option").is_some() {
            return quote!(fetch_optional);
        }
        if generic_arg(&ok, "Vec").is_some() {
            return quote!(fetch_all);
        }
    }
    quote!(fetch_one)
}

/// Row type for column probing: Result<Row>, Result<Option<Row>>, Result<Vec<Row>>.
/// Only path types qualify — tuples and scalars are skipped by the callers' kind gating.
fn infer_row_type(ret: &ReturnType) -> Option<Type> {
    let ok = result_ok_type(ret)?;
    let inner = generic_arg(&ok, "Option")
        .or_else(|| generic_arg(&ok, "Vec"))
        .unwrap_or(ok);
    matches!(inner, Type::Path(_)).then_some(inner)
}

// Generates a compile-time check that every DSL column exists as a field on the
// row/values struct. Spanned to the method name so field typos point at the method.
fn make_probe(span: Span, ty: &Type, cols: &[String]) -> TokenStream2 {
    let fields: Vec<syn::Ident> = cols
        .iter()
        .filter_map(|c| syn::parse_str::<syn::Ident>(c).ok())
        .map(|mut id| {
            id.set_span(span);
            id
        })
        .collect();
    if fields.is_empty() {
        return quote!();
    }
    quote_spanned! {span=>
        const _: () = {
            #[allow(dead_code)]
            fn _column_check(r: &#ty) {
                #( let _ = &r.#fields; )*
            }
        };
    }
}

fn row_probe(sig: &Signature, cols: &[String]) -> TokenStream2 {
    if cols.is_empty() {
        return quote!();
    }
    match infer_row_type(&sig.output) {
        Some(ty) => make_probe(sig.ident.span(), &ty, cols),
        None => quote!(),
    }
}

// Generates a compile-time check that `field` on the row struct is actually `bool`-typed,
// not just present. Used for the `find_all_<field>` bare boolean filter, where a
// non-bool column would otherwise only fail at query time as a type mismatch.
fn make_bool_probe(span: Span, ty: &Type, field: &str) -> TokenStream2 {
    let Ok(mut ident) = syn::parse_str::<syn::Ident>(field) else { return quote!() };
    ident.set_span(span);
    quote_spanned! {span=>
        const _: () = {
            #[allow(dead_code)]
            fn _bool_flag_check(r: &#ty) {
                let _: bool = r.#ident;
            }
        };
    }
}

// ---------- QueryBuilder codegen for the `_in` DSL suffix ----------

/// Renders each `WhereChunk` as a statement pushing onto a live `__qb: QueryBuilder`.
/// `binds` must be the same bind-expression slice whose indices `chunks` was built
/// against (see `dsl::build_where`'s `bind_offset` parameter).
fn chunk_statements(chunks: &[WhereChunk], binds: &[TokenStream2]) -> Vec<TokenStream2> {
    let db = crate::krate::db_path();
    chunks
        .iter()
        .map(|c| match c {
            WhereChunk::Literal(s) => quote!(__qb.push(#s);),
            WhereChunk::Bind(i) => {
                let b = &binds[*i];
                quote!(__qb.push_bind(#b);)
            }
            WhereChunk::InList(i) => {
                let b = &binds[*i];
                quote!(#db::push_in_list(&mut __qb, #b);)
            }
        })
        .collect()
}

// ---------- query generators ----------

enum Kind {
    One,
    Random,
    Count,
    Exists,
    Delete,
}

fn gen_filtered(
    sig: &Signature,
    ctx: &Ctx,
    field_str: &str,
    exec: TokenStream2,
    kind: Kind,
    order_cols: Option<&[String]>,
) -> syn::Result<(TokenStream2, TokenStream2)> {
    let db = crate::krate::db_path();
    if order_cols.is_some() && !matches!(kind, Kind::One) {
        return Err(syn::Error::new(
            sig.ident.span(),
            "`#[order_by(...)]` is only supported on `find_by_*` (not find_random_by_/count_by_/exists_by_/delete_by_)",
        ));
    }
    let conds = dsl::parse_conditions(field_str)
        .map_err(|e| syn::Error::new(sig.ident.span(), e))?;
    let wc: WhereClause = dsl::build_where(&conds, ctx.relations, 0, 0, &dialect::placeholder);
    if wc.fan_out {
        return Err(syn::Error::new(
            sig.ident.span(),
            "this filter is reached through a has_many/belongs_to_many relation and may match \
             multiple rows; only find_all_by_* may filter through it",
        ));
    }
    let sp = split_params(sig)?;
    no_page(sig, &sp)?;
    check_arity(sig, sp.binds.len(), wc.params)?;

    let table = ctx.table;
    let join_sql = if wc.joins_needed.is_empty() {
        String::new()
    } else {
        format!(" {}", dsl::join_clauses(table, &wc.joins_needed, ctx.relations))
    };
    let qualified = if wc.joins_needed.is_empty() { "*".to_string() } else { format!("\"{table}\".*") };
    let mut probe_cols = wc.probe_cols.clone();
    if let (Kind::One, Some(cols)) = (&kind, order_cols) {
        probe_cols.extend(cols.iter().cloned());
    }

    let binds = &sp.binds;
    let stmts = chunk_statements(&wc.chunks, binds);

    let body = match kind {
        Kind::One => {
            let sql_prefix = format!("SELECT {qualified} FROM \"{table}\"{join_sql} WHERE ");
            let fetch = fetch_method(&sig.output);
            if wc.has_in {
                let order_stmt = order_cols.map(|cols| {
                    let s = format!(" ORDER BY {}", cols.join(", "));
                    quote!(__qb.push(#s);)
                });
                quote! {
                    {
                        let mut __qb = ::sqlx::QueryBuilder::<#db::Db>::new(#sql_prefix);
                        #(#stmts)*
                        #order_stmt
                        __qb.build_query_as().#fetch(#exec).await
                    }
                }
            } else {
                let mut sql = format!("{sql_prefix}{}", wc.sql);
                if let Some(cols) = order_cols {
                    sql.push_str(" ORDER BY ");
                    sql.push_str(&cols.join(", "));
                }
                quote! {
                    ::sqlx::query_as(#sql)
                        #(.bind(#binds))*
                        .#fetch(#exec)
                        .await
                }
            }
        }
        Kind::Random => {
            let sql_prefix = format!("SELECT {qualified} FROM \"{table}\"{join_sql} WHERE ");
            let fetch = fetch_method(&sig.output);
            let random_sql = dialect::random_order_sql();
            if wc.has_in {
                quote! {
                    {
                        let mut __qb = ::sqlx::QueryBuilder::<#db::Db>::new(#sql_prefix);
                        #(#stmts)*
                        __qb.push(#random_sql);
                        __qb.push(" LIMIT 1");
                        __qb.build_query_as().#fetch(#exec).await
                    }
                }
            } else {
                let sql = format!("{sql_prefix}{} {random_sql} LIMIT 1", wc.sql);
                quote! {
                    ::sqlx::query_as(#sql)
                        #(.bind(#binds))*
                        .#fetch(#exec)
                        .await
                }
            }
        }
        Kind::Count => {
            let sql_prefix = format!("SELECT COUNT(*) FROM \"{table}\"{join_sql} WHERE ");
            if wc.has_in {
                quote! {
                    {
                        let mut __qb = ::sqlx::QueryBuilder::<#db::Db>::new(#sql_prefix);
                        #(#stmts)*
                        __qb.build_query_scalar().fetch_one(#exec).await
                    }
                }
            } else {
                let sql = format!("{sql_prefix}{}", wc.sql);
                quote! {
                    ::sqlx::query_scalar(#sql)
                        #(.bind(#binds))*
                        .fetch_one(#exec)
                        .await
                }
            }
        }
        Kind::Exists => {
            let sql_prefix = format!("SELECT EXISTS(SELECT 1 FROM \"{table}\"{join_sql} WHERE ");
            if wc.has_in {
                quote! {
                    {
                        let mut __qb = ::sqlx::QueryBuilder::<#db::Db>::new(#sql_prefix);
                        #(#stmts)*
                        __qb.push(")");
                        __qb.build_query_scalar().fetch_one(#exec).await
                    }
                }
            } else {
                let sql = format!("{sql_prefix}{})", wc.sql);
                quote! {
                    ::sqlx::query_scalar(#sql)
                        #(.bind(#binds))*
                        .fetch_one(#exec)
                        .await
                }
            }
        }
        Kind::Delete => {
            if !wc.joins_needed.is_empty() {
                return Err(syn::Error::new(
                    sig.ident.span(),
                    "`delete_by_*` cannot filter through joined tables; use the FK column or a method body",
                ));
            }
            let sql_prefix = format!("DELETE FROM \"{table}\" WHERE ");
            if wc.has_in {
                quote! {
                    {
                        let mut __qb = ::sqlx::QueryBuilder::<#db::Db>::new(#sql_prefix);
                        #(#stmts)*
                        __qb.build().execute(#exec).await.map(|_| ())
                    }
                }
            } else {
                let sql = format!("{sql_prefix}{}", wc.sql);
                quote! {
                    ::sqlx::query(#sql)
                        #(.bind(#binds))*
                        .execute(#exec)
                        .await
                        .map(|_| ())
                }
            }
        }
    };

    // Only find* methods return the row type — probing count/exists/delete would
    // wrongly probe i64/bool/().
    let probe = match kind {
        Kind::One | Kind::Random => row_probe(sig, &probe_cols),
        _ => quote!(),
    };
    Ok((body, probe))
}

fn gen_select_all(
    sig: &Signature,
    ctx: &Ctx,
    field_str: &str,
    exec: TokenStream2,
    date_col: Option<&str>,
    order_cols: Option<&[String]>,
) -> syn::Result<(TokenStream2, TokenStream2)> {
    let db = crate::krate::db_path();
    let (after_order, order) = dsl::split_order(field_str);
    if order.is_some() && order_cols.is_some() {
        return Err(syn::Error::new(
            sig.ident.span(),
            "cannot combine an embedded `_order_by_` in the method name with `#[order_by(...)]`; use one or the other",
        ));
    }
    let (filter_str, this_week) = dsl::split_this_week(after_order);
    let conds = dsl::parse_conditions(filter_str)
        .map_err(|e| syn::Error::new(sig.ident.span(), e))?;
    let wc: WhereClause = dsl::build_where(&conds, ctx.relations, 0, 0, &dialect::placeholder);
    let sp = split_params(sig)?;
    check_arity(sig, sp.binds.len(), wc.params)?;

    let table = ctx.table;
    let mut probe_cols = wc.probe_cols.clone();
    let date_col_name = date_col.unwrap_or("created_at").to_string();
    if this_week {
        probe_cols.push(date_col_name.clone());
    }

    let select_prefix = if wc.joins_needed.is_empty() {
        format!("SELECT * FROM \"{table}\" WHERE ")
    } else {
        format!(
            "SELECT \"{table}\".* FROM \"{table}\" {} WHERE ",
            dsl::join_clauses(table, &wc.joins_needed, ctx.relations)
        )
    };

    let order_sql: Option<String> = if let Some((order_sql, order_col)) = &order {
        probe_cols.push(order_col.clone());
        Some(format!(" {order_sql}"))
    } else if let Some(cols) = order_cols {
        probe_cols.extend(cols.iter().cloned());
        Some(format!(" ORDER BY {}", cols.join(", ")))
    } else {
        None
    };

    let binds = &sp.binds;

    let body = if wc.has_in {
        let stmts = chunk_statements(&wc.chunks, binds);
        let week_stmt = this_week.then(|| {
            let lit = format!(" AND {date_col_name} >= ");
            quote!(__qb.push(#lit); __qb.push(#db::WEEK_START_SQL);)
        });
        let order_stmt = order_sql.as_ref().map(|o| quote!(__qb.push(#o);));
        let page_stmt = sp.page.as_ref().map(|page| {
            quote! {
                __qb.push(" LIMIT ");
                __qb.push_bind(#page.limit);
                __qb.push(" OFFSET ");
                __qb.push_bind(#page.offset);
            }
        });
        quote! {
            {
                let mut __qb = ::sqlx::QueryBuilder::<#db::Db>::new(#select_prefix);
                #(#stmts)*
                #week_stmt
                #order_stmt
                #page_stmt
                __qb.build_query_as().fetch_all(#exec).await
            }
        }
    } else if this_week {
        let date_filter_template = format!(" AND {date_col_name} >= {{}}");
        let mut sql_template = format!("{select_prefix}{}{date_filter_template}", wc.sql);
        if let Some(o) = &order_sql {
            sql_template.push_str(o);
        }
        let page_binds = append_page(&mut sql_template, &sp, wc.params);
        quote! {
            {
                static __SQL: ::std::sync::OnceLock<::std::string::String> = ::std::sync::OnceLock::new();
                let sql: &str = __SQL
                    .get_or_init(|| ::std::format!(#sql_template, #db::WEEK_START_SQL))
                    .as_str();
                ::sqlx::query_as(sql)
                    #(.bind(#binds))*
                    #page_binds
                    .fetch_all(#exec)
                    .await
            }
        }
    } else {
        let mut sql = format!("{select_prefix}{}", wc.sql);
        if let Some(o) = &order_sql {
            sql.push_str(o);
        }
        let page_binds = append_page(&mut sql, &sp, wc.params);
        quote! {
            ::sqlx::query_as(#sql)
                #(.bind(#binds))*
                #page_binds
                .fetch_all(#exec)
                .await
        }
    };

    let probe = row_probe(sig, &probe_cols);
    Ok((body, probe))
}

fn gen_select_all_no_filter(
    sig: &Signature,
    ctx: &Ctx,
    exec: TokenStream2,
    order_str: Option<&str>,
    order_cols: Option<&[String]>,
) -> syn::Result<(TokenStream2, TokenStream2)> {
    let sp = split_params(sig)?;
    check_arity(sig, sp.binds.len(), 0)?;
    let table = ctx.table;
    let mut probe_cols: Vec<String> = Vec::new();
    let mut sql = match order_str {
        Some(o) => {
            let (rendered, col) = dsl::format_order_col(o);
            probe_cols.push(col);
            format!("SELECT * FROM \"{table}\" ORDER BY {rendered}")
        }
        None => format!("SELECT * FROM \"{table}\""),
    };
    if let Some(cols) = order_cols {
        sql.push_str(" ORDER BY ");
        sql.push_str(&cols.join(", "));
        probe_cols.extend(cols.iter().cloned());
    }
    let page_binds = append_page(&mut sql, &sp, 0);
    let fetch = fetch_method(&sig.output);
    let probe = row_probe(sig, &probe_cols);
    Ok((
        quote! {
            ::sqlx::query_as(#sql)
                #page_binds
                .#fetch(#exec)
                .await
        },
        probe,
    ))
}

// find_all_<field> (no `_by_`) — bare boolean flag filter, no bound parameter: `WHERE field = TRUE`.
// The extra bool-typed probe catches the case where `field` exists on the Row but isn't a bool
// column, which `row_probe`'s existence-only check wouldn't.
fn gen_select_all_bool_flag(
    sig: &Signature,
    ctx: &Ctx,
    field: &str,
    exec: TokenStream2,
    order_cols: Option<&[String]>,
) -> syn::Result<(TokenStream2, TokenStream2)> {
    let sp = split_params(sig)?;
    check_arity(sig, sp.binds.len(), 0)?;
    let table = ctx.table;
    let mut probe_cols = vec![field.to_string()];
    let mut sql = format!("SELECT * FROM \"{table}\" WHERE {field} = TRUE");
    if let Some(cols) = order_cols {
        sql.push_str(" ORDER BY ");
        sql.push_str(&cols.join(", "));
        probe_cols.extend(cols.iter().cloned());
    }
    let page_binds = append_page(&mut sql, &sp, 0);
    let fetch = fetch_method(&sig.output);

    let mut probe = row_probe(sig, &probe_cols);
    if let Some(ty) = infer_row_type(&sig.output) {
        let bool_probe = make_bool_probe(sig.ident.span(), &ty, field);
        probe = quote! { #probe #bool_probe };
    }

    Ok((
        quote! {
            ::sqlx::query_as(#sql)
                #page_binds
                .#fetch(#exec)
                .await
        },
        probe,
    ))
}

/// Appends `LIMIT $n OFFSET $m` (dialect-correct placeholders) and returns the extra
/// bind calls, if a Page param exists.
fn append_page(sql: &mut String, sp: &SplitParams, params_before: usize) -> TokenStream2 {
    match &sp.page {
        Some(page) => {
            let limit_idx = params_before + 1;
            let offset_idx = params_before + 2;
            let limit_ph = dialect::placeholder(limit_idx);
            let offset_ph = dialect::placeholder(offset_idx);
            sql.push_str(&format!(" LIMIT {limit_ph} OFFSET {offset_ph}"));
            quote!(.bind(#page.limit).bind(#page.offset))
        }
        None => quote!(),
    }
}

// set_{field}_by_{filter} — first (n - filter_count) params are SET values, rest are WHERE values.
// e.g. set_email_verified_at_by_id(&self, email_verified_at: DateTime, id: i32)
//      → UPDATE "{table}" SET email_verified_at = $1 WHERE id = $2
fn gen_set(
    sig: &Signature,
    ctx: &Ctx,
    rest: &str,
    exec: TokenStream2,
) -> syn::Result<(TokenStream2, TokenStream2)> {
    let db = crate::krate::db_path();
    let by_pos = rest.find("_by_").ok_or_else(|| {
        syn::Error::new(
            sig.ident.span(),
            "`set_*` methods require a `_by_` filter (e.g. `set_email_by_id`) — or provide a method body",
        )
    })?;
    let field = &rest[..by_pos];
    let filter_str = &rest[by_pos + 4..];

    let sp = split_params(sig)?;
    no_page(sig, &sp)?;
    let set_cols: Vec<&str> = field.split("_and_").collect();
    let filter_conds = dsl::parse_conditions(filter_str)
        .map_err(|e| syn::Error::new(sig.ident.span(), e))?;
    let wc: WhereClause = dsl::build_where(
        &filter_conds,
        ctx.relations,
        set_cols.len(),
        set_cols.len(),
        &dialect::placeholder,
    );
    if wc.fan_out {
        return Err(syn::Error::new(
            sig.ident.span(),
            "this filter is reached through a has_many/belongs_to_many relation and may match \
             multiple rows; only find_all_by_* may filter through it",
        ));
    }
    check_arity(sig, sp.binds.len(), set_cols.len() + wc.params)?;

    let binds = &sp.binds;
    let set_params = &sp.binds[..set_cols.len()];
    let filter_params = &sp.binds[set_cols.len()..];
    let table = ctx.table;

    let body = if wc.has_in {
        let where_stmts = chunk_statements(&wc.chunks, binds);
        let mut set_stmts: Vec<TokenStream2> = Vec::new();
        for (i, col) in set_cols.iter().enumerate() {
            if i > 0 {
                set_stmts.push(quote!(__qb.push(", ");));
            }
            let lit = format!("{col} = ");
            let b = &set_params[i];
            set_stmts.push(quote!(__qb.push(#lit); __qb.push_bind(#b);));
        }
        let set_prefix = format!("UPDATE \"{table}\" SET ");
        quote! {
            {
                let mut __qb = ::sqlx::QueryBuilder::<#db::Db>::new(#set_prefix);
                #(#set_stmts)*
                __qb.push(" WHERE ");
                #(#where_stmts)*
                __qb.build().execute(#exec).await.map(|_| ())
            }
        }
    } else {
        let set_clause = set_cols
            .iter()
            .enumerate()
            .map(|(i, col)| format!("{col} = {}", dialect::placeholder(i + 1)))
            .collect::<Vec<_>>()
            .join(", ");
        let sql = format!("UPDATE \"{table}\" SET {set_clause} WHERE {}", wc.sql);
        quote! {
            ::sqlx::query(#sql)
                #(.bind(#set_params))*
                #(.bind(#filter_params))*
                .execute(#exec)
                .await
                .map(|_| ())
        }
    };

    Ok((body, quote!()))
}

/// First non-receiver parameter (pattern and type) — errors if absent.
fn first_value_param(sig: &Signature, what: &str) -> syn::Result<(TokenStream2, Type)> {
    sig.inputs
        .iter()
        .find_map(|arg| match arg {
            FnArg::Typed(pt) => {
                let p = &pt.pat;
                Some((quote!(#p), (*pt.ty).clone()))
            }
            FnArg::Receiver(_) => None,
        })
        .ok_or_else(|| {
            syn::Error::new(sig.ident.span(), format!("`{what}` requires a values parameter"))
        })
}

fn gen_upsert(
    sig: &Signature,
    ctx: &Ctx,
    exec: TokenStream2,
    unique_col: Option<&str>,
) -> syn::Result<(TokenStream2, TokenStream2)> {
    let conflict_col = unique_col.ok_or_else(|| {
        syn::Error::new(
            sig.ident.span(),
            "`upsert` requires `#[unique(col)]` on the method to name the conflict column",
        )
    })?;
    let (values, values_ty) = first_value_param(sig, "upsert")?;
    let table = ctx.table;
    // Conflict column must be a field of the values struct — probe it too.
    let probe = make_probe(sig.ident.span(), &values_ty, &[conflict_col.to_string()]);
    Ok((
        quote! { #values.__sqlx_upsert_into(#table, #conflict_col, #exec).await },
        probe,
    ))
}

fn gen_delegated(
    sig: &Signature,
    ctx: &Ctx,
    exec: TokenStream2,
    what: &str,
    method: TokenStream2,
) -> syn::Result<(TokenStream2, TokenStream2)> {
    let (values, _) = first_value_param(sig, what)?;
    let table = ctx.table;
    Ok((quote! { #values.#method(#table, #exec).await }, quote!()))
}

fn gen_insert_all(
    sig: &Signature,
    ctx: &Ctx,
    exec: TokenStream2,
) -> syn::Result<(TokenStream2, TokenStream2)> {
    let (values, values_ty) = first_value_param(sig, "insert_all")?;
    let elem_ty = generic_arg(&values_ty, "Vec").ok_or_else(|| {
        syn::Error::new(
            sig.ident.span(),
            "`insert_all` requires a `Vec<T>` values parameter where T derives SqlxInsert",
        )
    })?;
    let table = ctx.table;
    let method = if returns_unit(&sig.output) {
        quote!(__sqlx_insert_all_into_void)
    } else {
        quote!(__sqlx_insert_all_into)
    };
    Ok((
        quote! { <#elem_ty>::#method(#values, #table, #exec).await },
        quote!(),
    ))
}