nautilus-orm-codegen 1.0.1

Code generator for Nautilus ORM schema files
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
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
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
---
source: crates/nautilus-codegen/tests/snapshot_tests.rs
assertion_line: 37
expression: code
---
//! User model and associated builders.

use crate::{FromRow, Row};
use futures::stream::StreamExt;

fn all_scalar_value_hints() -> &'static [Option<crate::ValueHint>] {
    &[
        None,
        None,
    ]
}

fn decode_model_row_with_hints(
    row: crate::Row,
    hints: &[Option<crate::ValueHint>],
) -> nautilus_core::Result<User> {
    let row = crate::normalize_row_with_hints(row, hints)
        .map_err(|e| nautilus_core::Error::Other(format!(
            "Failed to normalize User row: {}",
            e
        )))?;
    User::from_row(&row)
}

/// User model.
///
/// PK fields are always present. All other scalar fields are `Option<T>`:
/// they are `Some(value)` when the column was included in the query result
/// (default, or explicitly selected via `select`), and `None` when the
/// column was omitted through projection.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct User {
    pub id: i32,
    pub name: Option<String>,
}


impl User {
    /// Column accessor for id.
    pub fn id() -> nautilus_core::Column<i32> {
        nautilus_core::Column::new("User", "id")
    }
    /// Column accessor for name.
    pub fn name() -> nautilus_core::Column<String> {
        nautilus_core::Column::new("User", "name")
    }
    /// Create a delegate for User queries.
    pub fn nautilus<E>(client: &crate::Client<E>) -> UserDelegate<E>
    where
        E: crate::Executor,
        for<'a> E::Row<'a>: Into<crate::Row>,
    {
        UserDelegate::new(client.clone())
    }
}


impl crate::FromRow for User {
    fn from_row(row: &crate::Row) -> nautilus_core::Result<Self> {
        // PK field — always required
        let id = row
            .get("id")
            .or_else(|| row.get("User__id"))
            .ok_or_else(|| nautilus_core::Error::TypeError(
                "missing required PK column 'id' (id)".to_string()
            ))
            .and_then(nautilus_core::FromValue::from_value)?;
        // Scalar field — None when not selected (projection)
        let name: Option<String> = row
            .get("name")
            .or_else(|| row.get("User__name"))
            .and_then(|v| match v {
                nautilus_core::Value::Null => None,
                other => nautilus_core::FromValue::from_value(other).ok(),
            });

        // Decode relation fields from JSON (if included via .include())

        Ok(User {
            id,
            name,
        })
    }
}


/// Column accessors for User selections.
#[derive(Debug, Clone, Copy)]
pub struct UserColumns;

impl UserColumns {
    pub fn id(&self) -> nautilus_core::Column<i32> {
        nautilus_core::Column::new("User", "id")
    }
    pub fn name(&self) -> nautilus_core::Column<String> {
        nautilus_core::Column::new("User", "name")
    }
}


/// Structured input for creating a User record.
///
/// All fields are optional; unset fields are omitted from the INSERT.
#[derive(Debug, Default, Clone)]
pub struct UserCreateInput {
    /// id column.
    pub id: Option<i32>,
    /// name column.
    pub name: Option<String>,
}

impl UserCreateInput {
    fn to_engine_data(&self) -> serde_json::Value {
        let mut data = serde_json::Map::new();
        if let Some(value) = &self.id {
            data.insert(
                "id".to_string(),
                nautilus_core::Value::from(value.clone()).to_json_plain(),
            );
        }
        if let Some(value) = &self.name {
            data.insert(
                "name".to_string(),
                nautilus_core::Value::from(value.clone()).to_json_plain(),
            );
        }
        serde_json::Value::Object(data)
    }
}

/// Structured input for updating User records.
///
/// All fields are optional; only set fields are included in the UPDATE.
#[derive(Debug, Default, Clone)]
pub struct UserUpdateInput {
    /// id column.
    pub id: Option<i32>,
    /// name column.
    pub name: Option<String>,
}

impl UserUpdateInput {
    fn to_engine_data(&self) -> serde_json::Value {
        let mut data = serde_json::Map::new();
        if let Some(value) = &self.id {
            data.insert(
                "id".to_string(),
                nautilus_core::Value::from(value.clone()).to_json_plain(),
            );
        }
        if let Some(value) = &self.name {
            data.insert(
                "name".to_string(),
                nautilus_core::Value::from(value.clone()).to_json_plain(),
            );
        }
        serde_json::Value::Object(data)
    }
}

/// Structured arguments for updating User records.
#[derive(Debug, Default, Clone)]
pub struct UserUpdateArgs {
    /// Optional WHERE filter — if `None`, all rows are updated.
    pub where_: Option<nautilus_core::Expr>,
    /// Fields to update.
    pub data: UserUpdateInput,
}

/// Structured arguments for deleting User records.
#[derive(Debug, Default, Clone)]
pub struct UserDeleteArgs {
    /// Optional WHERE filter — if `None`, all rows are deleted.
    pub where_: Option<nautilus_core::Expr>,
}

/// Structured arguments for upserting a User record.
#[derive(Debug, Default, Clone)]
pub struct UserUpsertArgs {
    /// WHERE filter used to look up the existing record (should target a unique field).
    pub where_: Option<nautilus_core::Expr>,
    /// Fields used when creating a new record (record not found).
    pub create: UserCreateInput,
    /// Fields applied when updating an existing record.
    pub update: UserUpdateInput,
    /// Whether to return the created/updated record (default: true).
    pub return_data: bool,
}

/// Scalar fields on User that can be referenced in generated aggregate APIs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UserScalarField {
    Id,
    Name,
}

impl UserScalarField {
    fn as_str(&self) -> &'static str {
        match self {
            Self::Id => "id",
            Self::Name => "name",
        }
    }
}

/// Sort direction used by User `group_by()` ordering.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UserSortOrder {
    Asc,
    Desc,
}

impl UserSortOrder {
    fn as_str(&self) -> &'static str {
        match self {
            Self::Asc => "asc",
            Self::Desc => "desc",
        }
    }
}

/// Structured arguments for counting User records.
#[derive(Debug, Default, Clone)]
pub struct UserCountArgs {
    /// Optional WHERE filter.
    pub where_: Option<nautilus_core::Expr>,
    /// Limit the counted window.
    pub take: Option<i32>,
    /// Offset the counted window.
    pub skip: Option<u32>,
    /// Optional keyset cursor for paginated counts.
    pub cursor: Option<std::collections::HashMap<String, nautilus_core::Value>>,
}

impl UserCountArgs {
    fn to_engine_args(&self) -> nautilus_core::Result<Option<serde_json::Value>> {
        let mut args = serde_json::Map::new();

        if let Some(filter) = self.where_.as_ref() {
            args.insert(
                "where".to_string(),
                nautilus_core::where_expr_to_protocol_json(filter)?,
            );
        }
        if let Some(take) = self.take {
            args.insert("take".to_string(), serde_json::Value::from(take));
        }
        if let Some(skip) = self.skip {
            args.insert("skip".to_string(), serde_json::Value::from(skip));
        }
        if let Some(cursor) = self.cursor.as_ref() {
            args.insert("cursor".to_string(), serialize_cursor_for_engine(cursor));
        }

        Ok((!args.is_empty()).then_some(serde_json::Value::Object(args)))
    }
}

/// Select which COUNT aggregates to return from `UserDelegate::group_by`.
#[derive(Debug, Default, Clone)]
pub struct UserCountAggregateInput {
    pub _all: bool,
    pub id: bool,
    pub name: bool,
}

impl UserCountAggregateInput {
    fn to_json(&self) -> Option<serde_json::Value> {
        let mut obj = serde_json::Map::new();
        if self._all {
            obj.insert("_all".to_string(), serde_json::Value::Bool(true));
        }
        if self.id {
            obj.insert("id".to_string(), serde_json::Value::Bool(true));
        }
        if self.name {
            obj.insert("name".to_string(), serde_json::Value::Bool(true));
        }
        (!obj.is_empty()).then_some(serde_json::Value::Object(obj))
    }
}
/// Select which AVG aggregates to return from `UserDelegate::group_by`.
#[derive(Debug, Default, Clone)]
pub struct UserAvgAggregateInput {
    pub id: bool,
}

impl UserAvgAggregateInput {
    fn to_json(&self) -> Option<serde_json::Value> {
        let mut obj = serde_json::Map::new();
        if self.id {
            obj.insert("id".to_string(), serde_json::Value::Bool(true));
        }
        (!obj.is_empty()).then_some(serde_json::Value::Object(obj))
    }
}

/// Select which SUM aggregates to return from `UserDelegate::group_by`.
#[derive(Debug, Default, Clone)]
pub struct UserSumAggregateInput {
    pub id: bool,
}

impl UserSumAggregateInput {
    fn to_json(&self) -> Option<serde_json::Value> {
        let mut obj = serde_json::Map::new();
        if self.id {
            obj.insert("id".to_string(), serde_json::Value::Bool(true));
        }
        (!obj.is_empty()).then_some(serde_json::Value::Object(obj))
    }
}

/// Select which MIN aggregates to return from `UserDelegate::group_by`.
#[derive(Debug, Default, Clone)]
pub struct UserMinAggregateInput {
    pub id: bool,
    pub name: bool,
}

impl UserMinAggregateInput {
    fn to_json(&self) -> Option<serde_json::Value> {
        let mut obj = serde_json::Map::new();
        if self.id {
            obj.insert("id".to_string(), serde_json::Value::Bool(true));
        }
        if self.name {
            obj.insert("name".to_string(), serde_json::Value::Bool(true));
        }
        (!obj.is_empty()).then_some(serde_json::Value::Object(obj))
    }
}

/// Select which MAX aggregates to return from `UserDelegate::group_by`.
#[derive(Debug, Default, Clone)]
pub struct UserMaxAggregateInput {
    pub id: bool,
    pub name: bool,
}

impl UserMaxAggregateInput {
    fn to_json(&self) -> Option<serde_json::Value> {
        let mut obj = serde_json::Map::new();
        if self.id {
            obj.insert("id".to_string(), serde_json::Value::Bool(true));
        }
        if self.name {
            obj.insert("name".to_string(), serde_json::Value::Bool(true));
        }
        (!obj.is_empty()).then_some(serde_json::Value::Object(obj))
    }
}

/// One ORDER BY item for `UserDelegate::group_by()`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UserGroupByOrderBy {
    Field {
        field: UserScalarField,
        direction: UserSortOrder,
    },
    CountAll(UserSortOrder),
    Count {
        field: UserScalarField,
        direction: UserSortOrder,
    },
    Avg {
        field: UserScalarField,
        direction: UserSortOrder,
    },
    Sum {
        field: UserScalarField,
        direction: UserSortOrder,
    },
    Min {
        field: UserScalarField,
        direction: UserSortOrder,
    },
    Max {
        field: UserScalarField,
        direction: UserSortOrder,
    },
}

impl UserGroupByOrderBy {
    fn to_json(&self) -> serde_json::Value {
        match self {
            Self::Field { field, direction } => {
                let mut obj = serde_json::Map::new();
                obj.insert(
                    field.as_str().to_string(),
                    serde_json::Value::String(direction.as_str().to_string()),
                );
                serde_json::Value::Object(obj)
            }
            Self::CountAll(direction) => {
                let mut inner = serde_json::Map::new();
                inner.insert(
                    "_all".to_string(),
                    serde_json::Value::String(direction.as_str().to_string()),
                );
                let mut outer = serde_json::Map::new();
                outer.insert("_count".to_string(), serde_json::Value::Object(inner));
                serde_json::Value::Object(outer)
            }
            Self::Count { field, direction } => {
                let mut inner = serde_json::Map::new();
                inner.insert(
                    field.as_str().to_string(),
                    serde_json::Value::String(direction.as_str().to_string()),
                );
                let mut outer = serde_json::Map::new();
                outer.insert("_count".to_string(), serde_json::Value::Object(inner));
                serde_json::Value::Object(outer)
            }
            Self::Avg { field, direction } => {
                let mut inner = serde_json::Map::new();
                inner.insert(
                    field.as_str().to_string(),
                    serde_json::Value::String(direction.as_str().to_string()),
                );
                let mut outer = serde_json::Map::new();
                outer.insert("_avg".to_string(), serde_json::Value::Object(inner));
                serde_json::Value::Object(outer)
            }
            Self::Sum { field, direction } => {
                let mut inner = serde_json::Map::new();
                inner.insert(
                    field.as_str().to_string(),
                    serde_json::Value::String(direction.as_str().to_string()),
                );
                let mut outer = serde_json::Map::new();
                outer.insert("_sum".to_string(), serde_json::Value::Object(inner));
                serde_json::Value::Object(outer)
            }
            Self::Min { field, direction } => {
                let mut inner = serde_json::Map::new();
                inner.insert(
                    field.as_str().to_string(),
                    serde_json::Value::String(direction.as_str().to_string()),
                );
                let mut outer = serde_json::Map::new();
                outer.insert("_min".to_string(), serde_json::Value::Object(inner));
                serde_json::Value::Object(outer)
            }
            Self::Max { field, direction } => {
                let mut inner = serde_json::Map::new();
                inner.insert(
                    field.as_str().to_string(),
                    serde_json::Value::String(direction.as_str().to_string()),
                );
                let mut outer = serde_json::Map::new();
                outer.insert("_max".to_string(), serde_json::Value::Object(inner));
                serde_json::Value::Object(outer)
            }
        }
    }
}

/// Structured arguments for grouping User rows and computing aggregates.
#[derive(Debug, Default, Clone)]
pub struct UserGroupByArgs {
    /// Logical field names to group by.
    pub by: Vec<UserScalarField>,
    /// Optional pre-group WHERE filter.
    pub where_: Option<nautilus_core::Expr>,
    /// Optional HAVING JSON object using the protocol aggregate shape.
    pub having: Option<serde_json::Value>,
    /// Limit the number of returned groups.
    pub take: Option<i32>,
    /// Offset the returned groups.
    pub skip: Option<u32>,
    /// COUNT aggregation selection.
    pub count: Option<UserCountAggregateInput>,
    /// AVG aggregation selection.
    pub avg: Option<UserAvgAggregateInput>,
    /// SUM aggregation selection.
    pub sum: Option<UserSumAggregateInput>,
    /// MIN aggregation selection.
    pub min: Option<UserMinAggregateInput>,
    /// MAX aggregation selection.
    pub max: Option<UserMaxAggregateInput>,
    /// ORDER BY items applied after grouping.
    pub order_by: Vec<UserGroupByOrderBy>,
}

impl UserGroupByArgs {
    fn to_engine_args(&self) -> nautilus_core::Result<serde_json::Value> {
        let mut args = serde_json::Map::new();
        args.insert(
            "by".to_string(),
            serde_json::Value::Array(
                self.by
                    .iter()
                    .map(|field| serde_json::Value::String(field.as_str().to_string()))
                    .collect(),
            ),
        );

        if let Some(filter) = self.where_.as_ref() {
            args.insert(
                "where".to_string(),
                nautilus_core::where_expr_to_protocol_json(filter)?,
            );
        }
        if let Some(having) = self.having.as_ref() {
            args.insert("having".to_string(), having.clone());
        }
        if let Some(take) = self.take {
            args.insert("take".to_string(), serde_json::Value::from(take));
        }
        if let Some(skip) = self.skip {
            args.insert("skip".to_string(), serde_json::Value::from(skip));
        }
        if let Some(count) = self.count.as_ref().and_then(UserCountAggregateInput::to_json) {
            args.insert("count".to_string(), count);
        }
        if let Some(avg) = self.avg.as_ref().and_then(UserAvgAggregateInput::to_json) {
            args.insert("avg".to_string(), avg);
        }
        if let Some(sum) = self.sum.as_ref().and_then(UserSumAggregateInput::to_json) {
            args.insert("sum".to_string(), sum);
        }
        if let Some(min) = self.min.as_ref().and_then(UserMinAggregateInput::to_json) {
            args.insert("min".to_string(), min);
        }
        if let Some(max) = self.max.as_ref().and_then(UserMaxAggregateInput::to_json) {
            args.insert("max".to_string(), max);
        }
        if !self.order_by.is_empty() {
            args.insert(
                "orderBy".to_string(),
                serde_json::Value::Array(
                    self.order_by
                        .iter()
                        .map(UserGroupByOrderBy::to_json)
                        .collect(),
                ),
            );
        }

        Ok(serde_json::Value::Object(args))
    }
}

/// Typed COUNT aggregates returned by `UserDelegate::group_by()`.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct UserCountAggregateOutput {
    pub _all: Option<i64>,
    pub id: Option<i64>,
    pub name: Option<i64>,
}

impl UserCountAggregateOutput {
    fn from_json_object(
        obj: &serde_json::Map<String, serde_json::Value>,
    ) -> nautilus_core::Result<Self> {
        Ok(Self {
            _all: decode_optional_group_by_json_value(obj, "_all")?,
            id: decode_optional_group_by_json_value(obj, "id")?,
            name: decode_optional_group_by_json_value(obj, "name")?,
        })
    }
}
/// Typed AVG aggregates returned by `UserDelegate::group_by()`.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct UserAvgAggregateOutput {
    pub id: Option<f64>,
}

impl UserAvgAggregateOutput {
    fn from_json_object(
        obj: &serde_json::Map<String, serde_json::Value>,
    ) -> nautilus_core::Result<Self> {
        Ok(Self {
            id: decode_optional_group_by_json_value(obj, "id")?,
        })
    }
}

/// Typed SUM aggregates returned by `UserDelegate::group_by()`.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct UserSumAggregateOutput {
    pub id: Option<i64>,
}

impl UserSumAggregateOutput {
    fn from_json_object(
        obj: &serde_json::Map<String, serde_json::Value>,
    ) -> nautilus_core::Result<Self> {
        Ok(Self {
            id: decode_optional_group_by_json_value(obj, "id")?,
        })
    }
}

/// Typed MIN aggregates returned by `UserDelegate::group_by()`.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct UserMinAggregateOutput {
    pub id: Option<i32>,
    pub name: Option<String>,
}

impl UserMinAggregateOutput {
    fn from_json_object(
        obj: &serde_json::Map<String, serde_json::Value>,
    ) -> nautilus_core::Result<Self> {
        Ok(Self {
            id: decode_optional_group_by_json_value(obj, "id")?,
            name: decode_optional_group_by_json_value(obj, "name")?,
        })
    }
}

/// Typed MAX aggregates returned by `UserDelegate::group_by()`.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct UserMaxAggregateOutput {
    pub id: Option<i32>,
    pub name: Option<String>,
}

impl UserMaxAggregateOutput {
    fn from_json_object(
        obj: &serde_json::Map<String, serde_json::Value>,
    ) -> nautilus_core::Result<Self> {
        Ok(Self {
            id: decode_optional_group_by_json_value(obj, "id")?,
            name: decode_optional_group_by_json_value(obj, "name")?,
        })
    }
}

/// One typed row returned by `UserDelegate::group_by()`.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct UserGroupByOutput {
    pub id: Option<i32>,
    pub name: Option<String>,
    pub _count: Option<UserCountAggregateOutput>,
    pub _avg: Option<UserAvgAggregateOutput>,
    pub _sum: Option<UserSumAggregateOutput>,
    pub _min: Option<UserMinAggregateOutput>,
    pub _max: Option<UserMaxAggregateOutput>,
}

impl UserGroupByOutput {
    fn from_row(row: &crate::Row) -> nautilus_core::Result<Self> {
        Ok(Self {
            id: decode_optional_group_by_row_value(row, "id")?,
            name: decode_optional_group_by_row_value(row, "name")?,
            _count: decode_optional_group_by_object(row, "_count")?
                .map(UserCountAggregateOutput::from_json_object)
                .transpose()?,
            _avg: decode_optional_group_by_object(row, "_avg")?
                .map(UserAvgAggregateOutput::from_json_object)
                .transpose()?,
            _sum: decode_optional_group_by_object(row, "_sum")?
                .map(UserSumAggregateOutput::from_json_object)
                .transpose()?,
            _min: decode_optional_group_by_object(row, "_min")?
                .map(UserMinAggregateOutput::from_json_object)
                .transpose()?,
            _max: decode_optional_group_by_object(row, "_max")?
                .map(UserMaxAggregateOutput::from_json_object)
                .transpose()?,
        })
    }
}

fn serialize_cursor_for_engine(
    cursor: &std::collections::HashMap<String, nautilus_core::Value>,
) -> serde_json::Value {
    serde_json::Value::Object(
        cursor
            .iter()
            .map(|(key, value)| (key.clone(), value.to_json_plain()))
            .collect(),
    )
}

fn decode_optional_group_by_row_value<T>(
    row: &crate::Row,
    key: &str,
) -> nautilus_core::Result<Option<T>>
where
    T: nautilus_core::FromValue,
{
    match row.get(key) {
        Some(nautilus_core::Value::Null) | None => Ok(None),
        Some(value) => <T as nautilus_core::FromValue>::from_value(value).map(Some),
    }
}

fn decode_optional_group_by_object<'a>(
    row: &'a crate::Row,
    key: &str,
) -> nautilus_core::Result<Option<&'a serde_json::Map<String, serde_json::Value>>> {
    match row.get(key) {
        Some(nautilus_core::Value::Json(serde_json::Value::Object(obj))) => Ok(Some(obj)),
        Some(nautilus_core::Value::Null) | None => Ok(None),
        Some(other) => Err(nautilus_core::Error::TypeError(format!(
            "expected object-valued aggregate '{}' in groupBy output, got {:?}",
            key, other
        ))),
    }
}

fn decode_optional_group_by_json_value<T>(
    obj: &serde_json::Map<String, serde_json::Value>,
    key: &str,
) -> nautilus_core::Result<Option<T>>
where
    T: nautilus_core::FromValue,
{
    match obj.get(key) {
        Some(serde_json::Value::Null) | None => Ok(None),
        Some(value) => <T as nautilus_core::FromValue>::from_value_owned(
            crate::runtime::wire_value_to_core_value(key, value),
        )
        .map(Some),
    }
}

fn serialize_update_filter_for_engine(
    filter: Option<&nautilus_core::Expr>,
) -> nautilus_core::Result<Option<serde_json::Value>> {
    let filter_json = match filter {
        Some(expr) => match nautilus_core::where_expr_to_protocol_json(expr) {
            Ok(value) => value,
            Err(nautilus_core::Error::InvalidQuery(_)) => return Ok(None),
            Err(err) => return Err(err),
        },
        None => return Ok(None),
    };

    Ok(Some(filter_json))
}

/// Delegate for User queries.
///
/// Obtained via `client.User` (or the model accessor). Each method
/// accepts a structured args object and executes the query immediately — no
/// extra `.exec()` call is needed.
pub struct UserDelegate<E: crate::Executor> {
    client: crate::Client<E>,
}

impl<E> UserDelegate<E>
where
    E: crate::Executor,
    for<'a> E::Row<'a>: Into<crate::Row>,
{
    pub(crate) fn new(client: crate::Client<E>) -> Self {
        UserDelegate { client }
    }

    /// Find all User records matching `args`.
    ///
    /// Executes immediately and returns a `Vec<User>`.
    pub fn find_many(
        &self,
        args: nautilus_core::FindManyArgs,
    ) -> nautilus_core::Result<Vec<User>> {
        let _c = self.client.clone();
        tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(async move {
        if let Some(rows) =
            crate::runtime::try_find_many_via_engine::<_, User>(
                &_c,
                "User",
                &args,
            )
            .await?
        {
            return Ok(rows);
        }

        if !args.include.is_empty() {
            return Err(nautilus_core::Error::InvalidQuery(
                "include queries require the embedded engine path in the generated Rust client"
                    .to_string(),
            ));
        }

        let mut builder = UserFindMany::new(_c);
        if let Some(filter) = args.where_ {
            builder = builder.where_(filter);
        }
        for order in args.order_by {
            builder = builder.order_by(order);
        }
        if let Some(take) = args.take {
            builder = builder.take(take);
        }
        if let Some(skip) = args.skip {
            builder = builder.skip(skip);
        }
        if !args.select.is_empty() {
            let set: std::collections::HashSet<String> = args.select
                .into_iter()
                .filter_map(|(k, v)| if v { Some(k) } else { None })
                .collect();
            if !set.is_empty() {
                builder = builder.select_fields(set);
            }
        }
        if let Some(cursor) = args.cursor {
            builder = builder.cursor(cursor);
        }
        if !args.distinct.is_empty() {
            builder = builder.distinct(args.distinct);
        }
        builder.exec().await
        }))
    }

    /// Find the first User record matching `args`, or `None`.
    pub fn find_first(
        &self,
        args: nautilus_core::FindManyArgs,
    ) -> nautilus_core::Result<Option<User>> {
        let args = nautilus_core::FindManyArgs { take: Some(1), ..args };
        Ok(self.find_many(args)?.into_iter().next())
    }

    /// Find a single User record by a unique filter, or `None` if not found.
    ///
    /// `args.where_` should reference a `@id` or `@unique` field for correct semantics.
    pub fn find_unique(
        &self,
        args: nautilus_core::FindUniqueArgs,
    ) -> nautilus_core::Result<Option<User>> {
        let find_args = nautilus_core::FindManyArgs {
            where_: Some(args.where_),
            take: Some(1),
            select: args.select,
            include: args.include,
            ..Default::default()
        };
        Ok(self.find_many(find_args)?.into_iter().next())
    }

    /// Find a single User record by a unique filter, or return `Error::NotFound`.
    pub fn find_unique_or_throw(
        &self,
        args: nautilus_core::FindUniqueArgs,
    ) -> nautilus_core::Result<User> {
        self.find_unique(args)?.ok_or_else(|| {
            nautilus_core::Error::NotFound(
                format!("No {} record found matching the given unique filter", "User")
            )
        })
    }

    /// Find the first User record matching `args`, or return `Error::NotFound`.
    pub fn find_first_or_throw(
        &self,
        args: nautilus_core::FindManyArgs,
    ) -> nautilus_core::Result<User> {
        self.find_first(args)?.ok_or_else(|| {
            nautilus_core::Error::NotFound(
                format!("No {} record found matching the given filter", "User")
            )
        })
    }

    /// Create a new User record.
    ///
    /// Executes immediately and returns the created `User`.
    pub fn create(
        &self,
        data: UserCreateInput,
    ) -> nautilus_core::Result<User> {
        let _c = self.client.clone();
        tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(async move {
        if let Some(record) =
            crate::runtime::try_create_via_engine::<_, User>(
                &_c,
                "User",
                data.to_engine_data(),
            )
            .await?
        {
            return Ok(record);
        }

        let mut builder = UserCreate::new(_c);
        if let Some(value) = data.id {
            builder = builder.id(value);
        }
        if let Some(value) = data.name {
            builder = builder.name(value);
        }
        builder.exec().await
        }))
    }

    /// Create multiple User records in a single batch.
    ///
    /// Executes immediately and returns the created records.
    pub fn create_many(
        &self,
        entries: Vec<UserCreateEntry>,
    ) -> nautilus_core::Result<Vec<User>> {
        let _c = self.client.clone();
        tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(async move {
        if entries.is_empty() {
            return Ok(vec![]);
        }

        if let Some(rows) =
            crate::runtime::try_create_many_via_engine::<_, User>(
                &_c,
                "User",
                entries.iter().map(UserCreateEntry::to_engine_data).collect(),
            )
            .await?
        {
            return Ok(rows);
        }

        let mut builder = UserCreateMany::new(_c);
        for entry in entries {
            builder = builder.entry(entry);
        }
        builder.exec().await
        }))
    }

    /// Update User records matching `args.where_`.
    ///
    /// If `args.where_` is `None`, all rows are updated.
    /// Executes immediately and returns the updated records.
    pub fn update(
        &self,
        args: UserUpdateArgs,
    ) -> nautilus_core::Result<Vec<User>> {
        let _c = self.client.clone();
        tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(async move {
        let engine_data = args.data.to_engine_data();
        let no_assignments = engine_data
            .as_object()
            .is_some_and(|data| data.is_empty());
        if no_assignments {
            return Ok(vec![]);
        }

        if let Some(filter_json) = serialize_update_filter_for_engine(args.where_.as_ref())? {
            if let Some(rows) =
                crate::runtime::try_update_via_engine::<_, User>(
                    &_c,
                    "User",
                    filter_json,
                    engine_data,
                )
                .await?
            {
                return Ok(rows);
            }
        }

        let mut builder = UserUpdate::new(_c).set_data(args.data);
        if let Some(filter) = args.where_ {
            builder = builder.where_(filter);
        } else {
            builder = builder.all();
        }
        builder.exec().await
        }))
    }

    /// Delete the first User record matching `args.where_`.
    ///
    /// finds the first matching record and
    /// deletes it by its primary key. Returns `Ok(None)` if no record matches.
    pub fn delete(
        &self,
        args: UserDeleteArgs,
    ) -> nautilus_core::Result<Option<User>> {
        // Find the first matching record to get its primary key
        let record = self.find_first(nautilus_core::FindManyArgs {
            where_: args.where_,
            ..Default::default()
        })?;

        let Some(record) = record else {
            return Ok(None);
        };

        // Build a primary-key filter to delete exactly that one record
        let pk_filter = nautilus_core::Expr::column("User__id").eq(nautilus_core::Expr::param(record.id.clone()));
        let _c = self.client.clone();
        tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(async move {
            UserDelete::new(_c).where_(pk_filter).exec().await?;
            nautilus_core::Result::<()>::Ok(())
        }))?;
        Ok(Some(record))
    }

    /// Delete all User records matching `args.where_`.
    ///
    /// If `args.where_` is `None`, all rows are deleted.
    /// Executes immediately and returns the deleted records.
    pub fn delete_many(
        &self,
        args: UserDeleteArgs,
    ) -> nautilus_core::Result<Vec<User>> {
        let _c = self.client.clone();
        tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(async move {
        let mut builder = UserDelete::new(_c);
        if let Some(expr) = args.where_ {
            builder = builder.where_(expr);
        } else {
            builder = builder.all();
        }
        builder.exec().await
        }))
    }

    /// Count User records matching `args`.
    pub fn count(
        &self,
        args: UserCountArgs,
    ) -> nautilus_core::Result<i64> {
        let _c = self.client.clone();
        tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(async move {
        if let Some(count) =
            crate::runtime::try_count_via_engine(
                &_c,
                "User",
                args.to_engine_args()?,
            )
            .await?
        {
            return Ok(count);
        }

        Err(nautilus_core::Error::InvalidQuery(
            "count queries require the embedded engine path in the generated Rust client"
                .to_string(),
        ))
        }))
    }

    /// Group User rows and compute aggregates.
    pub fn group_by(
        &self,
        args: UserGroupByArgs,
    ) -> nautilus_core::Result<Vec<UserGroupByOutput>> {
        let _c = self.client.clone();
        tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(async move {
        let engine_args = args.to_engine_args()?;
        if let Some(rows) =
            crate::runtime::try_group_by_rows_via_engine(
                &_c,
                "User",
                engine_args,
            )
            .await?
        {
            let mut decoded = Vec::with_capacity(rows.len());
            for row in &rows {
                decoded.push(UserGroupByOutput::from_row(row)?);
            }
            return Ok(decoded);
        }

        Err(nautilus_core::Error::InvalidQuery(
            "groupBy queries require the embedded engine path in the generated Rust client"
                .to_string(),
        ))
        }))
    }

    /// Execute a raw SQL string and return the result rows as generic maps.
    ///
    /// The SQL is sent to the database as-is with no parameter binding.
    /// Prefer `raw_stmt_query` when user-supplied values are involved to avoid
    /// SQL injection.
    pub fn raw_query(
        &self,
        sql: impl Into<String>,
    ) -> nautilus_core::Result<Vec<std::collections::HashMap<String, serde_json::Value>>> {
        let _c = self.client.clone();
        tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(async move {
        let sql_obj = nautilus_dialect::Sql { text: sql.into(), params: vec![] };
        let stream = _c.executor().execute(&sql_obj);
        futures::pin_mut!(stream);
        let mut results: Vec<std::collections::HashMap<String, serde_json::Value>> = Vec::new();
        while let Some(result) = stream.next().await {
            let row: crate::Row = result
                .map_err(|e| nautilus_core::Error::Other(e.to_string()))?
                .into();
            let map = row
                .iter()
                .map(|(name, val)| {
                    let json_val = val.to_json_plain();
                    (name.to_string(), json_val)
                })
                .collect();
            results.push(map);
        }
        Ok(results)
        }))
    }

    /// Execute a raw prepared-statement query with bound parameters.
    ///
    /// Use `$1`, `$2`, … (PostgreSQL) or `?` (MySQL / SQLite) as placeholders.
    /// Parameters are bound in the order they appear in the `params` slice.
    pub fn raw_stmt_query(
        &self,
        sql: impl Into<String>,
        params: Vec<nautilus_core::Value>,
    ) -> nautilus_core::Result<Vec<std::collections::HashMap<String, serde_json::Value>>> {
        let _c = self.client.clone();
        tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(async move {
        let sql_obj = nautilus_dialect::Sql { text: sql.into(), params };
        let stream = _c.executor().execute(&sql_obj);
        futures::pin_mut!(stream);
        let mut results: Vec<std::collections::HashMap<String, serde_json::Value>> = Vec::new();
        while let Some(result) = stream.next().await {
            let row: crate::Row = result
                .map_err(|e| nautilus_core::Error::Other(e.to_string()))?
                .into();
            let map = row
                .iter()
                .map(|(name, val)| {
                    let json_val = val.to_json_plain();
                    (name.to_string(), json_val)
                })
                .collect();
            results.push(map);
        }
        Ok(results)
        }))
    }

    /// Create or update a User record.
    ///
    /// Looks up an existing record using `args.where_`. If found, updates it
    /// with `args.data`; otherwise creates a new record with `args.data`.
    /// Executes immediately and returns the created or updated `User`
    /// (or `None` when `args.return_data` is `false`).
    pub fn upsert(
        &self,
        args: UserUpsertArgs,
    ) -> nautilus_core::Result<Option<User>> {
        let existing = self.find_first(nautilus_core::FindManyArgs {
            where_: args.where_.clone(),
            ..Default::default()
        })?;

        if let Some(record) = existing {
            let updated = self.update(UserUpdateArgs {
                where_: args.where_,
                data: args.update,
            })?;
            if !args.return_data {
                return Ok(None);
            }
            Ok(Some(updated.into_iter().next().unwrap_or(record)))
        } else {
            let created = self.create(args.create)?;
            if !args.return_data {
                return Ok(None);
            }
            Ok(Some(created))
        }
    }
}


/// Internal FindMany builder for User - use `UserDelegate::find_many(args)` instead.
pub(crate) struct UserFindMany<E: crate::Executor, S = ()> {
    client: crate::Client<E>,
    filter: Option<nautilus_core::Expr>,
    order_by: Vec<nautilus_core::OrderBy>,
    /// Positive = forward pagination, negative = backward pagination.
    take: Option<i32>,
    skip: Option<u32>,
    select: std::collections::HashSet<String>,
    /// Cursor map: PK field name -> value for stable keyset pagination.
    cursor: Option<std::collections::HashMap<String, nautilus_core::Value>>,
    /// True when `take` was negative (backward pagination).
    backward: bool,
    /// Columns to deduplicate on (SELECT DISTINCT / DISTINCT ON).
    distinct: Vec<String>,
    _selection: std::marker::PhantomData<S>,
}

impl<E, S> UserFindMany<E, S>
where
    E: crate::Executor,
    for<'a> E::Row<'a>: Into<crate::Row>,
{
    pub(crate) fn new(client: crate::Client<E>) -> Self {
        UserFindMany {
            client,
            filter: None,
            order_by: vec![],
            take: None,
            skip: None,
            select: std::collections::HashSet::new(),
            cursor: None,
            backward: false,
            distinct: vec![],
            _selection: std::marker::PhantomData,
        }
    }

    /// Add a WHERE filter.
    #[must_use]
    pub(crate) fn where_(mut self, expr: nautilus_core::Expr) -> Self {
        self.filter = Some(expr);
        self
    }

    /// Add ORDER BY clause.
    #[must_use]
    pub(crate) fn order_by(mut self, order: nautilus_core::OrderBy) -> Self {
        self.order_by.push(order);
        self
    }

    /// Limit the number of results.
    ///
    /// Positive `n` paginates **forward**; negative `n` paginates **backward**
    /// (flips ORDER BY and reverses the result slice in memory).
    #[must_use]
    pub(crate) fn take(mut self, n: i32) -> Self {
        self.backward = n < 0;
        self.take = Some(n);
        self
    }

    /// Skip a number of results (OFFSET).
    #[must_use]
    pub(crate) fn skip(mut self, n: u32) -> Self {
        self.skip = Some(n);
        self
    }

    /// Set the cursor for stable keyset pagination.
    ///
    /// The map must contain all primary-key fields of the model.
    /// The cursor record is included in the result.
    #[must_use]
    pub(crate) fn cursor(
        mut self,
        map: std::collections::HashMap<String, nautilus_core::Value>,
    ) -> Self {
        self.cursor = Some(map);
        self
    }

    /// Specify columns to deduplicate on (SELECT DISTINCT / DISTINCT ON).
    ///
    /// - **Postgres**: emits `DISTINCT ON (cols)` - the columns are automatically
    ///   prepended to `ORDER BY` before query execution.
    /// - **SQLite / MySQL**: emits plain `SELECT DISTINCT` (full-row dedup;
    ///   combine with `select_fields` for column-level effect).
    #[must_use]
    pub(crate) fn distinct(mut self, columns: Vec<String>) -> Self {
        self.distinct = columns;
        self
    }

    /// Specify a field projection (only return the listed columns).
    ///
    /// PK fields are always included regardless.
    #[must_use]
    pub(crate) fn select_fields(mut self, fields: std::collections::HashSet<String>) -> Self {
        self.select = fields;
        self
    }
}

impl<E> UserFindMany<E, ()>
where
    E: crate::Executor,
    for<'a> E::Row<'a>: Into<crate::Row>,
{
    /// Execute the query and return all rows.
    pub(crate) async fn exec(self) -> nautilus_core::Result<Vec<User>> {
        use nautilus_core::*;

        let client = self.client;
        let mut builder = Select::from_table("User");
        let mut row_hints: Vec<Option<crate::ValueHint>> = Vec::new();

        // Add scalar columns (PK columns always included; others filtered by select if active)
        builder = builder.item(SelectItem::column(ColumnMarker::new("User", "id")));
        row_hints.push(None);
        if self.select.is_empty() || self.select.contains("name") {
            builder = builder.item(SelectItem::column(ColumnMarker::new("User", "name")));
            row_hints.push(None);
        }

        // Extract fields from self so we can mix borrows and moves freely.
        let backward = self.backward;
        let cursor = self.cursor;
        let filter = self.filter;
        let order_by = self.order_by;
        let take = self.take;
        let skip = self.skip;
        let distinct = self.distinct;

        // Auto-add distinct columns to ORDER BY so they appear first (required for
        // Postgres DISTINCT ON; harmless for SQLite / MySQL which use plain DISTINCT).
        if !distinct.is_empty() {
            let existing_cols: std::collections::HashSet<&str> =
                order_by.iter().map(|o| o.column.as_str()).collect();
            for col in &distinct {
                if !existing_cols.contains(col.as_str()) {
                    let dir = if backward { OrderDir::Desc } else { OrderDir::Asc };
                    builder = builder.order_by(col.clone(), dir);
                }
            }
        }

        // Cursor predicate
        // Build an inclusive keyset WHERE clause from the cursor map and merge
        // it with any caller-supplied filter.
        let effective_filter = if let Some(ref cursor_map) = cursor {
            // Static PK field (cursor-map-key, table__db_col) pairs - generated
            // at code-generation time from the model's primary-key definition.
            let pk_pairs: &[(&str, &str)] = &[
                ("id", "User__id"),
            ];
            let cursor_pred =
                nautilus_core::build_cursor_predicate(pk_pairs, cursor_map, backward)?;

            // Auto-add any PK column that is not yet in order_by so that the
            // keyset predicate is unambiguous.
            let existing_cols: std::collections::HashSet<&str> =
                order_by.iter().map(|o| o.column.as_str()).collect();
            if !existing_cols.contains("id") {
                let dir = if backward { OrderDir::Desc } else { OrderDir::Asc };
                builder = builder.order_by("id".to_string(), dir);
            }

            Some(match filter {
                Some(f) => f.and(cursor_pred),
                None => cursor_pred,
            })
        } else {
            filter
        };

        if let Some(f) = effective_filter {
            builder = builder.filter(f);
        }

        // Apply ordering, flipping direction for backward pagination.
        for order in order_by {
            let dir = if backward {
                match order.direction {
                    OrderDir::Asc => OrderDir::Desc,
                    OrderDir::Desc => OrderDir::Asc,
                }
            } else {
                order.direction
            };
            builder = builder.order_by(order.column, dir);
        }

        // Apply take (absolute value; direction is handled by ORDER BY flip).
        if let Some(n) = take {
            builder = builder.take(n.saturating_abs());
        }
        if let Some(offset) = skip {
            builder = builder.skip(offset);
        }

        // Apply distinct (SELECT DISTINCT / DISTINCT ON).
        if !distinct.is_empty() {
            builder = builder.distinct(distinct);
        }

        let select = builder.build()?;
        let sql = client.dialect().render_select(&select)?;

        let stream = client.executor().execute(&sql);
        futures::pin_mut!(stream);

        let mut results = Vec::new();
        while let Some(result) = stream.next().await {
            let row: crate::Row = result.map_err(|e| Error::Other(e.to_string()))?.into();
            let decoded = decode_model_row_with_hints(row, &row_hints)?;
            results.push(decoded);
        }

        // Backward pagination: DB returned rows in reversed order - restore natural order.
        if backward {
            results.reverse();
        }

        Ok(results)
    }
}


/// Internal Create builder for User — use `UserDelegate::create(data)` instead.
pub(crate) struct UserCreate<E: crate::Executor> {
    client: crate::Client<E>,
    id: Option<i32>,
    name: Option<String>,
}

impl<E> UserCreate<E>
where
    E: crate::Executor,
    for<'a> E::Row<'a>: Into<crate::Row>,
{
    pub(crate) fn new(client: crate::Client<E>) -> Self {
        UserCreate {
            client,
            id: None,
            name: None,
        }
    }
    /// Set id.
    #[must_use]
    pub(crate) fn id(mut self, value: impl Into<i32>) -> Self {
        self.id = Some(value.into());
        self
    }
    /// Set name.
    #[must_use]
    pub(crate) fn name(mut self, value: impl Into<String>) -> Self {
        self.name = Some(value.into());
        self
    }
    /// Execute the create query.
    pub(crate) async fn exec(self) -> nautilus_core::Result<User> {
        use nautilus_core::*;

        let mut columns = Vec::new();
        let mut values = Vec::new();
        if let Some(value) = self.id {
            columns.push(ColumnMarker::new("User", "id"));
            values.push(Value::from(value));
        }
        if let Some(value) = self.name {
            columns.push(ColumnMarker::new("User", "name"));
            values.push(Value::from(value));
        }

        let mut builder = Insert::into_table("User")
            .columns(columns)
            .values(values);

        let supports_returning = self.client.dialect().supports_returning();
        if supports_returning {
            let returning_cols = vec![
                ColumnMarker::new("User", "id"),
                ColumnMarker::new("User", "name"),
            ];
            builder = builder.returning(returning_cols);
        }

        let insert = builder.build()?;
        let sql = self.client.dialect().render_insert(&insert)?;

        let stream = self.client.executor().execute(&sql);
        futures::pin_mut!(stream);

        let row: Row = stream
            .next()
            .await
            .ok_or_else(|| Error::Other("INSERT returned no rows".to_string()))?
            .map_err(|e| Error::Other(e.to_string()))?
            .into();

        decode_model_row_with_hints(row, all_scalar_value_hints())
    }
}


/// Entry for creating User.
#[derive(Debug, Clone)]
pub struct UserCreateEntry {
    pub id: i32,
    pub name: String,
}

impl UserCreateEntry {
    pub(crate) fn to_engine_data(&self) -> serde_json::Value {
        let mut data = serde_json::Map::new();
        data.insert(
            "id".to_string(),
            nautilus_core::Value::from(self.id.clone()).to_json_plain(),
        );
        data.insert(
            "name".to_string(),
            nautilus_core::Value::from(self.name.clone()).to_json_plain(),
        );
        serde_json::Value::Object(data)
    }
}

/// Internal CreateMany builder for User — use `UserDelegate::create_many(entries)` instead.
pub(crate) struct UserCreateMany<E: crate::Executor> {
    client: crate::Client<E>,
    entries: Vec<UserCreateEntry>,
}

impl<E> UserCreateMany<E>
where
    E: crate::Executor,
    for<'a> E::Row<'a>: Into<crate::Row>,
{
    pub(crate) fn new(client: crate::Client<E>) -> Self {
        UserCreateMany {
            client,
            entries: vec![],
        }
    }

    /// Add an entry to the batch.
    #[must_use]
    pub(crate) fn entry(mut self, entry: UserCreateEntry) -> Self {
        self.entries.push(entry);
        self
    }

    /// Execute the batch create using a single multi-row `INSERT`.
    pub(crate) async fn exec(self) -> nautilus_core::Result<Vec<User>> {
        use nautilus_core::*;

        if self.entries.is_empty() {
            return Ok(vec![]);
        }

        // MySQL does not support RETURNING — fall back to per-row inserts.
        if !self.client.dialect().supports_returning() {
            let mut results = Vec::with_capacity(self.entries.len());
            for entry in self.entries {
                let result = UserCreate::new(self.client.clone())
                    .id(entry.id)
                    .name(entry.name)
                    .exec().await?;
                results.push(result);
            }
            return Ok(results);
        }

        let columns = vec![
            ColumnMarker::new("User", "id"),
            ColumnMarker::new("User", "name"),
        ];

        let mut builder = Insert::into_table("User")
            .columns(columns);

        for entry in &self.entries {
            let row = vec![
                Value::from(entry.id.clone()),
                Value::from(entry.name.clone()),
            ];
            builder = builder.values(row);
        }

        let returning_cols = vec![
            ColumnMarker::new("User", "id"),
            ColumnMarker::new("User", "name"),
        ];
        builder = builder.returning(returning_cols);

        let insert = builder.build()?;
        let sql = self.client.dialect().render_insert(&insert)?;

        let stream = self.client.executor().execute(&sql);
        futures::pin_mut!(stream);

        let mut results = Vec::with_capacity(self.entries.len());
        while let Some(result) = stream.next().await {
            let row: crate::Row = result.map_err(|e| Error::Other(e.to_string()))?.into();
            let decoded = decode_model_row_with_hints(row, all_scalar_value_hints())?;
            results.push(decoded);
        }

        Ok(results)
    }
}


/// Internal Update builder for User — use `UserDelegate::update(args)` instead.
pub(crate) struct UserUpdate<E: crate::Executor> {
    client: crate::Client<E>,
    filter: Option<nautilus_core::Expr>,
    require_filter: bool,
    /// Column-value assignments collected from `set_data`.
    assignments: Vec<(nautilus_core::ColumnMarker, nautilus_core::Value)>,
}

impl<E> UserUpdate<E>
where
    E: crate::Executor,
    for<'a> E::Row<'a>: Into<crate::Row>,
{
    pub(crate) fn new(client: crate::Client<E>) -> Self {
        UserUpdate {
            client,
            filter: None,
            require_filter: true,
            assignments: Vec::new(),
        }
    }

    /// Set WHERE filter.
    #[must_use]
    pub(crate) fn where_(mut self, expr: nautilus_core::Expr) -> Self {
        self.filter = Some(expr);
        self
    }

    /// Update all rows (removes the safety guard that requires a filter).
    #[must_use]
    pub(crate) fn all(mut self) -> Self {
        self.require_filter = false;
        self
    }

    /// Load field assignments from a structured update-input value.
    ///
    /// Only fields that are `Some(…)` in `data` are included in the SET
    /// clause; `None` fields are intentionally skipped (left unchanged).
    /// For nullable fields a `Some(None)` sets the column to NULL.
    #[must_use]
    pub(crate) fn set_data(mut self, data: UserUpdateInput) -> Self {
        if let Some(outer) = data.id {
            let val = nautilus_core::Value::from(outer);
            self.assignments.push((
                nautilus_core::ColumnMarker::new("User", "id"),
                val,
            ));
        }
        if let Some(outer) = data.name {
            let val = nautilus_core::Value::from(outer);
            self.assignments.push((
                nautilus_core::ColumnMarker::new("User", "name"),
                val,
            ));
        }
        self
    }

    /// Execute the update, returning affected rows via `RETURNING`.
    ///
    /// Returns an empty `Vec` for databases that do not support `RETURNING`
    /// (e.g. MySQL).  Returns an empty `Vec` without touching the database
    /// when no assignments have been loaded via `set_data`.
    pub(crate) async fn exec(self) -> nautilus_core::Result<Vec<User>> {
        use nautilus_core::*;
        use futures::StreamExt;

        if self.assignments.is_empty() {
            return Ok(vec![]);
        }

        if self.require_filter && self.filter.is_none() {
            return Err(Error::InvalidQuery(
                "Update requires a WHERE filter — call `.all()` to update every row.".to_string(),
            ));
        }

        let mut builder = Update::table("User");
        for (col, val) in self.assignments {
            builder = builder.set(col, val);
        }
        if let Some(filter) = self.filter {
            builder = builder.filter(filter);
        }

        let supports_returning = self.client.dialect().supports_returning();
        if supports_returning {
            let returning_cols = vec![
                ColumnMarker::new("User", "id"),
                ColumnMarker::new("User", "name"),
            ];
            builder = builder.returning(returning_cols);
        }

        let update = builder.build()?;
        let sql = self.client.dialect().render_update(&update)?;

        let stream = self.client.executor().execute(&sql);
        futures::pin_mut!(stream);

        let mut results = Vec::new();
        while let Some(result) = stream.next().await {
            let row: crate::Row = result.map_err(|e| Error::Other(e.to_string()))?.into();
            results.push(decode_model_row_with_hints(row, all_scalar_value_hints())?);
        }

        Ok(results)
    }
}


/// Internal Delete builder for User — use `UserDelegate::delete(filter)` instead.
pub(crate) struct UserDelete<E: crate::Executor> {
    client: crate::Client<E>,
    filter: Option<nautilus_core::Expr>,
    require_filter: bool,
}

impl<E> UserDelete<E>
where
    E: crate::Executor,
    for<'a> E::Row<'a>: Into<crate::Row>,
{
    pub(crate) fn new(client: crate::Client<E>) -> Self {
        UserDelete {
            client,
            filter: None,
            require_filter: true,
        }
    }

    /// Set WHERE filter.
    #[must_use]
    pub(crate) fn where_(mut self, expr: nautilus_core::Expr) -> Self {
        self.filter = Some(expr);
        self
    }

    /// Delete all rows (removes the safety guard that requires a filter).
    #[must_use]
    pub(crate) fn all(mut self) -> Self {
        self.require_filter = false;
        self
    }

    /// Execute the delete, returning deleted rows via `RETURNING`.
    ///
    /// Returns an empty `Vec` for databases that do not support `RETURNING`
    /// (e.g. MySQL).
    pub(crate) async fn exec(self) -> nautilus_core::Result<Vec<User>> {
        use nautilus_core::*;
        use futures::StreamExt;

        if self.require_filter && self.filter.is_none() {
            return Err(Error::InvalidQuery(
                "Delete requires a WHERE filter — call `.all()` to delete every row.".to_string(),
            ));
        }

        let mut builder = Delete::from_table("User");
        if let Some(filter) = self.filter {
            builder = builder.filter(filter);
        }

        let supports_returning = self.client.dialect().supports_returning();
        if supports_returning {
            let returning_cols = vec![
                ColumnMarker::new("User", "id"),
                ColumnMarker::new("User", "name"),
            ];
            builder = builder.returning(returning_cols);
        }

        let delete = builder.build()?;
        let sql = self.client.dialect().render_delete(&delete)?;

        let stream = self.client.executor().execute(&sql);
        futures::pin_mut!(stream);

        let mut results = Vec::new();
        while let Some(result) = stream.next().await {
            let row: crate::Row = result.map_err(|e| Error::Other(e.to_string()))?.into();
            results.push(decode_model_row_with_hints(row, all_scalar_value_hints())?);
        }

        Ok(results)
    }
}