navree-orm 1.0.0

Navree ORM: active-record models, a typed query builder, dirty tracking and pagination on PostgreSQL/TimescaleDB, MySQL and SQLite.
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
//! Polymorphic relations: `morph_to` ([`MorphTo`]: a comment's
//! `commentable` is a deal or a contact), the per-type loads of its eager
//! loads ([`MorphWith`]), and the existence checks per type (`has_morph`,
//! `where_has_morph`, `where_morphed_to`, ...).

use std::any::TypeId;
use std::borrow::Cow;
use std::fmt;
use std::marker::PhantomData;
use std::sync::Arc;

use navree_kernel::BoxFuture;

use crate::collection::Collection;
use crate::column::Filter;
use crate::error::{OrmError, Result};
use crate::exec::{Access, Executor, Target};
use crate::ir::{Boolean, Expr, Operator, QueryIr, Where, WhereClause};
use crate::model::{Model, ModelMeta};
use crate::query::filters::HasWheres;
use crate::query::{Builder, WhereGroup};
use crate::record::Record;
use crate::relations::access::AccessConstraint;
use crate::relations::any::AnyRecord;
use crate::relations::core::{column_value, qualified};
use crate::relations::eager::{Constraint, ConstraintFn, EagerNode, EagerTree};
use crate::relations::existence::{
    ExistsRequest, ExistsSelect, Refine, build_subquery, columns_equal,
};
use crate::relations::keys::{RelationKey, distinct_keys, in_keys, in_keys_on, key_of};
use crate::relations::morph_map::{MorphMap, type_name_of};
use crate::relations::path::{ParsedPath, RelationPath};
use crate::relations::registry::{ErasedRelation, RelationKind, RelationMeta, find};
use crate::relations::{Relation, private};
use crate::value::{Value, is_null};

/// Which soft-deleted records a `morph_to` loads.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum Trashed {
    /// Not the soft-deleted ones (the default).
    #[default]
    Without,
    /// Soft-deleted ones too.
    With,
    /// Only soft-deleted ones (of models that soft-delete).
    Only,
}

impl Trashed {
    fn apply<M: Model>(self, query: Builder<M>) -> Builder<M> {
        match self {
            Trashed::Without => query,
            Trashed::With => query.with_trashed(),
            Trashed::Only => match M::meta().deleted_at() {
                Some(column) => query.with_trashed().where_(Filter::from(Where::Null {
                    expr: Expr::Column(qualified(M::meta().table(), column)),
                    not: true,
                })),
                None => query,
            },
        }
    }
}

/// What a `morph_to` eager load does for one of its models.
#[derive(Clone, Debug)]
pub struct MorphTypeLoads {
    type_id: TypeId,
    model: &'static str,
    with: EagerTree,
    counts: Vec<String>,
    constraints: Vec<Constraint>,
    errors: Vec<String>,
}

impl MorphTypeLoads {
    fn new<M: Model>() -> Self {
        MorphTypeLoads {
            type_id: TypeId::of::<M>(),
            model: M::meta().name(),
            with: EagerTree::default(),
            counts: Vec::new(),
            constraints: Vec::new(),
            errors: Vec::new(),
        }
    }

    fn merge(&mut self, other: &MorphTypeLoads) {
        self.with.merge(&other.with);
        self.counts.extend(other.counts.iter().cloned());
        self.constraints.extend(other.constraints.iter().cloned());
        self.errors.extend(other.errors.iter().cloned());
    }

    /// `query` with these loads: constraints, eager loads and counts.
    fn apply<M: Model>(&self, mut query: Builder<M>) -> Result<Builder<M>> {
        if let Some(error) = self.errors.first() {
            return Err(OrmError::invalid(error.clone()));
        }
        for constraint in &self.constraints {
            match constraint
                .closure
                .downcast_ref::<ConstraintFn<Builder<M>>>()
            {
                Some(apply) => query = (apply.0)(query),
                None => {
                    return Err(OrmError::invalid(format!(
                        "a morph constraint for {} takes `{}`",
                        self.model, constraint.takes
                    )));
                }
            }
        }
        query.eager.tree.merge(&self.with);
        for count in &self.counts {
            query = query.with_count(count.as_str());
        }
        Ok(query)
    }
}

/// What a `morph_to` eager load loads into each of its models: nested
/// relations ([`on`](MorphWith::on)), relation counts
/// ([`count_on`](MorphWith::count_on)) and constraints
/// ([`constrain`](MorphWith::constrain)), per model.
///
/// ```no_run
/// # use navree_orm::__doc_models::*;
/// use navree_orm::relations::MorphWith;
///
/// # async fn demo(db: Db) -> Result<()> {
/// let notes = Note::query()
///     .with_morph(
///         "notable",
///         MorphWith::new()
///             .on::<Deal>(["company"])
///             .count_on::<Deal>(["activities"])
///             .constrain::<Company>(|q| q.where_(Company::ACTIVE.eq(true))),
///     )
///     .get(&db)
///     .await?;
/// # Ok(()) }
/// ```
#[derive(Clone, Debug, Default)]
pub struct MorphWith {
    types: Vec<MorphTypeLoads>,
}

impl MorphWith {
    /// Nothing per type yet.
    pub fn new() -> Self {
        MorphWith::default()
    }

    fn loads<M: Model>(&mut self) -> &mut MorphTypeLoads {
        let index = match self
            .types
            .iter()
            .position(|loads| loads.type_id == TypeId::of::<M>())
        {
            Some(index) => index,
            None => {
                self.types.push(MorphTypeLoads::new::<M>());
                self.types.len() - 1
            }
        };
        &mut self.types[index]
    }

    /// Eager loads `paths` into the loaded records of model `M`.
    pub fn on<M: Model>(mut self, paths: impl IntoIterator<Item = impl RelationPath<M>>) -> Self {
        let loads = self.loads::<M>();
        for path in paths {
            let text = path.relation_path().into_owned();
            match ParsedPath::parse(&text) {
                Ok(parsed) => {
                    let node = loads.with.entry(&parsed.segments);
                    if parsed.columns.is_some() {
                        node.columns = parsed.columns;
                    }
                }
                Err(error) => loads.errors.push(error.to_string()),
            }
        }
        self
    }

    /// Counts the related records of `paths` on the loaded records of
    /// model `M` (`{relation}_count` extras).
    pub fn count_on<M: Model>(
        mut self,
        paths: impl IntoIterator<Item = impl RelationPath<M>>,
    ) -> Self {
        let loads = self.loads::<M>();
        for path in paths {
            loads.counts.push(path.relation_path().into_owned());
        }
        self
    }

    /// Constrains the query of model `M`'s records.
    pub fn constrain<M: Model>(
        mut self,
        constraint: impl Fn(Builder<M>) -> Builder<M> + Send + Sync + 'static,
    ) -> Self {
        self.loads::<M>()
            .constraints
            .push(Constraint::new(constraint));
        self
    }

    /// Adds `other`'s loads.
    pub(crate) fn merge(&mut self, other: &MorphWith) {
        for loads in &other.types {
            match self
                .types
                .iter_mut()
                .find(|existing| existing.type_id == loads.type_id)
            {
                Some(existing) => existing.merge(loads),
                None => self.types.push(loads.clone()),
            }
        }
    }

    /// The loads of the model with type id `type_id`.
    pub(crate) fn for_type(&self, type_id: TypeId) -> Option<&MorphTypeLoads> {
        self.types.iter().find(|loads| loads.type_id == type_id)
    }
}

/// What a `morph_to` load asks of one of its models.
pub struct MorphLoad<'a> {
    /// The column the stored ids point at; the model's key when `None`.
    pub(crate) owner_key: Option<&'a str>,
    /// The ids, distinct.
    pub(crate) keys: &'a [(RelationKey, Value)],
    /// The per-type loads.
    pub(crate) loads: Option<&'a MorphTypeLoads>,
    /// Loads that apply to every type (nested paths of the eager load).
    pub(crate) children: &'a EagerTree,
    /// Conditions that apply to every type.
    pub(crate) conditions: &'a [Filter],
    /// Access filters, asked for each type's condition.
    pub(crate) access: &'a [AccessConstraint],
    /// Soft-deleted records.
    pub(crate) trashed: Trashed,
}

/// What a `morph_to` existence check asks of one of its models.
pub struct MorphExists<'a> {
    /// The model the `morph_to` is declared on.
    pub(crate) parent: &'static ModelMeta,
    /// The id column on the parent.
    pub(crate) id_column: &'a str,
    /// The column the ids point at; the model's key when `None`.
    pub(crate) owner_key: Option<&'a str>,
    /// How deep the outer query is nested.
    pub(crate) depth: u32,
    /// What the subquery selects.
    pub(crate) select: ExistsSelect,
    /// A condition on the model's records.
    pub(crate) condition: Option<Filter>,
    /// Soft-deleted records.
    pub(crate) trashed: Trashed,
}

/// One model a `morph_to` can load, with its type erased.
pub trait MorphTarget: Send + Sync + 'static {
    /// The model's type id.
    fn type_id(&self) -> TypeId;

    /// The name the model's records are stored under in a type column.
    fn morph_name(&self) -> Cow<'static, str>;

    /// A stored id as the model's key stores it (an id column of text
    /// holding `"5"` for an integer key), so it binds and matches as one.
    fn normalize_key(&self, value: Value, owner_key: Option<&str>) -> Value;

    /// The records whose owner key is one of `request.keys`, by key.
    fn load<'a, 'b>(
        &'a self,
        target: &'a Target<'b>,
        request: MorphLoad<'a>,
    ) -> BoxFuture<'a, Result<Vec<(RelationKey, AnyRecord)>>>;

    /// Loads `paths` (eager loads, or aggregates when `aggregate` is set)
    /// into `records` (all of this model).
    fn load_into<'a, 'b>(
        &'a self,
        target: &'a Target<'b>,
        records: Vec<AnyRecord>,
        loads: &'a MorphTypeLoads,
        aggregate: Option<&'a crate::relations::aggregates::AggregateSpec>,
    ) -> BoxFuture<'a, Result<Vec<AnyRecord>>>;

    /// Loads what `rest` misses into `records` (all of this model).
    fn load_missing_into<'a, 'b>(
        &'a self,
        target: &'a Target<'b>,
        records: Vec<AnyRecord>,
        rest: &'a EagerTree,
    ) -> BoxFuture<'a, Result<Vec<AnyRecord>>>;

    /// The existence subquery of this model's records.
    fn exists_query(&self, request: MorphExists<'_>) -> Result<QueryIr>;
}

/// `records` as records of `M` (an error names a record of another model).
fn typed_records<M: Model>(records: Vec<AnyRecord>) -> Result<Vec<Record<M>>> {
    records
        .into_iter()
        .map(|record| {
            record.downcast::<M>().map_err(|other| {
                OrmError::invalid(format!(
                    "a {} among the {} records of a morph load",
                    other.model_name(),
                    M::meta().name()
                ))
            })
        })
        .collect()
}

/// The [`MorphTarget`] of model `M`.
pub(crate) struct MorphTargetOf<M: Model>(PhantomData<fn() -> M>);

impl<M: Model> MorphTargetOf<M> {
    pub(crate) fn new() -> Self {
        MorphTargetOf(PhantomData)
    }
}

impl<M: Model> MorphTarget for MorphTargetOf<M> {
    fn type_id(&self) -> TypeId {
        TypeId::of::<M>()
    }

    fn morph_name(&self) -> Cow<'static, str> {
        M::morph_name()
    }

    fn normalize_key(&self, value: Value, owner_key: Option<&str>) -> Value {
        match owner_key {
            Some(column) if column != M::meta().key_column() => value,
            _ => match <M::Key as crate::Field>::from_value(value.clone()) {
                Ok(key) => crate::Field::to_value(&key),
                Err(_) => value,
            },
        }
    }

    fn load<'a, 'b>(
        &'a self,
        target: &'a Target<'b>,
        request: MorphLoad<'a>,
    ) -> BoxFuture<'a, Result<Vec<(RelationKey, AnyRecord)>>> {
        Box::pin(async move {
            let meta = M::meta();
            let keys = distinct_keys(
                request
                    .keys
                    .iter()
                    .map(|(_, value)| self.normalize_key(value.clone(), request.owner_key)),
            );
            let owner_key = request.owner_key.unwrap_or(meta.key_column());
            let mut query = request
                .trashed
                .apply(Builder::<M>::new().with_relation_defaults());
            if let Some(loads) = request.loads {
                query = loads.apply(query)?;
            }
            for condition in request.conditions {
                query = query.where_(condition.clone());
            }
            for access in request.access {
                query = access.apply(query)?;
            }
            query.eager.tree.merge(request.children);
            let query = query.where_(Filter::from(in_keys_on(
                target,
                qualified(meta.table(), owner_key),
                &keys,
            )));
            let records = query.fetch(target).await?;
            let mut found = Vec::with_capacity(records.len());
            for record in records {
                let value = column_value(&record.model, owner_key, "morph_to")?;
                if let Some(key) = key_of(&value) {
                    found.push((key, AnyRecord::new(record)));
                }
            }
            Ok(found)
        })
    }

    fn load_into<'a, 'b>(
        &'a self,
        target: &'a Target<'b>,
        records: Vec<AnyRecord>,
        loads: &'a MorphTypeLoads,
        aggregate: Option<&'a crate::relations::aggregates::AggregateSpec>,
    ) -> BoxFuture<'a, Result<Vec<AnyRecord>>> {
        Box::pin(async move {
            let mut typed = typed_records::<M>(records)?;
            match aggregate {
                None => {
                    crate::relations::eager::load_tree(target, &mut typed, &loads.with).await?;
                    if !loads.counts.is_empty() {
                        let count = crate::relations::aggregates::AggregateSpec::count();
                        crate::relations::aggregates::load_aggregate_on(
                            target,
                            &mut typed,
                            &loads.counts,
                            &count,
                        )
                        .await?;
                    }
                }
                Some(spec) => {
                    // The relations listed for the model, with `on` (first
                    // steps) or `count_on`.
                    let mut paths: Vec<String> = loads
                        .with
                        .paths()
                        .into_iter()
                        .filter(|path| !path.contains('.'))
                        .collect();
                    for counted in &loads.counts {
                        if !paths.contains(counted) {
                            paths.push(counted.clone());
                        }
                    }
                    crate::relations::aggregates::load_aggregate_on(
                        target, &mut typed, &paths, spec,
                    )
                    .await?;
                }
            }
            Ok(typed.into_iter().map(AnyRecord::new).collect())
        })
    }

    fn load_missing_into<'a, 'b>(
        &'a self,
        target: &'a Target<'b>,
        records: Vec<AnyRecord>,
        rest: &'a EagerTree,
    ) -> BoxFuture<'a, Result<Vec<AnyRecord>>> {
        Box::pin(async move {
            let mut typed = typed_records::<M>(records)?;
            crate::relations::lazy::load_missing_tree(target, &mut typed, rest).await?;
            Ok(typed.into_iter().map(AnyRecord::new).collect())
        })
    }

    fn exists_query(&self, request: MorphExists<'_>) -> Result<QueryIr> {
        let meta = M::meta();
        let owner_key = request.owner_key.unwrap_or(meta.key_column()).to_owned();
        let aliased = (meta.table() == request.parent.table()).then_some(meta.table());
        let definition = request.trashed.apply(Builder::<M>::new());
        let refine = match request.condition {
            Some(filter) => Refine::Filter(filter),
            None => Refine::None,
        };
        let outer = qualified(request.parent.table(), request.id_column);
        build_subquery::<M>(
            request.parent,
            &definition,
            ExistsRequest {
                depth: request.depth,
                select: request.select,
                refine,
            },
            "morph_to",
            aliased,
            |query, _| query,
            |alias| {
                vec![columns_equal(
                    outer,
                    qualified(alias.unwrap_or(meta.table()), &owner_key),
                )]
            },
        )
    }
}

/// The models a `morph_to` lists itself: tuples of models,
/// `.targets::<(Deal, Contact)>()`.
pub trait MorphTargets {
    #[doc(hidden)]
    fn morph_targets() -> Vec<Arc<dyn MorphTarget>>;
}

macro_rules! morph_targets {
    ($($model:ident),+) => {
        impl<$($model: Model),+> MorphTargets for ($($model,)+) {
            fn morph_targets() -> Vec<Arc<dyn MorphTarget>> {
                vec![$(Arc::new(MorphTargetOf::<$model>::new()) as Arc<dyn MorphTarget>),+]
            }
        }
    };
}

morph_targets!(A);
morph_targets!(A, B);
morph_targets!(A, B, C);
morph_targets!(A, B, C, D);
morph_targets!(A, B, C, D, E);
morph_targets!(A, B, C, D, E, F);
morph_targets!(A, B, C, D, E, F, G);
morph_targets!(A, B, C, D, E, F, G, H);

/// A polymorphic belongs-to: the parent holds the type and the key of one
/// record of any model (`commentable_type` and `commentable_id`). Loaded,
/// it holds an `Option<AnyRecord>`.
///
/// The models it can load come from its own list
/// ([`target`](MorphTo::target), [`targets`](MorphTo::targets)), else from
/// the process-wide [`MorphMap`].
///
/// ```no_run
/// # use navree_orm::__doc_models::*;
/// # async fn demo(db: Db, note: Record<Note>) -> Result<()> {
/// // #[relations] impl Note {
/// //     fn notable(&self) -> MorphTo<Note> { self.morph_to("notable").targets::<(Deal, Company)>() }
/// // }
/// if let Some(notable) = note.notable().get(&db).await? {
///     if let Some(deal) = notable.downcast_ref::<Deal>() {
///         println!("a note on deal {}", deal.title);
///     }
/// }
/// # Ok(()) }
/// ```
pub struct MorphTo<P: Model> {
    pub(crate) name: Cow<'static, str>,
    pub(crate) parent: Option<P>,
    pub(crate) type_column: Cow<'static, str>,
    pub(crate) id_column: Cow<'static, str>,
    pub(crate) owner_key: Option<Cow<'static, str>>,
    pub(crate) targets: Vec<Arc<dyn MorphTarget>>,
    pub(crate) trashed: Trashed,
    pub(crate) with: MorphWith,
}

impl<P: Model> Clone for MorphTo<P> {
    fn clone(&self) -> Self {
        MorphTo {
            name: self.name.clone(),
            parent: self.parent.clone(),
            type_column: self.type_column.clone(),
            id_column: self.id_column.clone(),
            owner_key: self.owner_key.clone(),
            targets: self.targets.clone(),
            trashed: self.trashed,
            with: self.with.clone(),
        }
    }
}

impl<P: Model> fmt::Debug for MorphTo<P> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MorphTo")
            .field("parent", &P::meta().name())
            .field("name", &self.name)
            .field("type_column", &self.type_column)
            .field("id_column", &self.id_column)
            .finish_non_exhaustive()
    }
}

impl<P: Model> MorphTo<P> {
    /// The `morph_to` named `name` of `parent`: columns `{name}_type` and
    /// `{name}_id`.
    pub(crate) fn new(parent: Option<P>, name: &str) -> Self {
        MorphTo {
            name: Cow::Owned(name.to_owned()),
            parent,
            type_column: Cow::Owned(format!("{name}_type")),
            id_column: Cow::Owned(format!("{name}_id")),
            owner_key: None,
            targets: Vec::new(),
            trashed: Trashed::Without,
            with: MorphWith::new(),
        }
    }

    /// Names the relation (what it is loaded under).
    pub fn named(mut self, name: impl Into<Cow<'static, str>>) -> Self {
        self.name = name.into();
        self
    }

    /// The type column, when it is not `{name}_type`.
    pub fn type_column(mut self, column: impl Into<Cow<'static, str>>) -> Self {
        self.type_column = column.into();
        self
    }

    /// The id column, when it is not `{name}_id`.
    pub fn id_column(mut self, column: impl Into<Cow<'static, str>>) -> Self {
        self.id_column = column.into();
        self
    }

    /// The column of the related models the ids point at, when it is not
    /// their key.
    pub fn owner_key(mut self, column: impl Into<Cow<'static, str>>) -> Self {
        self.owner_key = Some(column.into());
        self
    }

    /// Lists model `M` among the models the relation loads.
    pub fn target<M: Model>(mut self) -> Self {
        self.targets
            .retain(|target| target.type_id() != TypeId::of::<M>());
        self.targets.push(Arc::new(MorphTargetOf::<M>::new()));
        self
    }

    /// Lists the models of tuple `T` (`(Deal, Contact)`).
    pub fn targets<T: MorphTargets>(mut self) -> Self {
        for target in T::morph_targets() {
            self.targets
                .retain(|existing| existing.type_id() != target.type_id());
            self.targets.push(target);
        }
        self
    }

    /// Loads soft-deleted records too, of every model.
    pub fn with_trashed(mut self) -> Self {
        self.trashed = Trashed::With;
        self
    }

    /// Leaves soft-deleted records out (the default).
    pub fn without_trashed(mut self) -> Self {
        self.trashed = Trashed::Without;
        self
    }

    /// Loads only soft-deleted records (of models that soft-delete).
    pub fn only_trashed(mut self) -> Self {
        self.trashed = Trashed::Only;
        self
    }

    /// Per-model loads of every load of this relation.
    pub fn morph_with(mut self, with: MorphWith) -> Self {
        self.with.merge(&with);
        self
    }

    /// Constrains the query of model `M`'s records.
    pub fn constrain<M: Model>(
        mut self,
        constraint: impl Fn(Builder<M>) -> Builder<M> + Send + Sync + 'static,
    ) -> Self {
        self.with = std::mem::take(&mut self.with).constrain(constraint);
        self
    }

    /// The relation's name.
    pub fn relation_name(&self) -> &str {
        &self.name
    }

    /// The type column.
    pub fn morph_type(&self) -> &str {
        &self.type_column
    }

    /// The id column.
    pub fn foreign_key_name(&self) -> &str {
        &self.id_column
    }

    /// The id column qualified by the parent's table.
    pub fn qualified_foreign_key_name(&self) -> String {
        format!("{}.{}", P::meta().table(), self.id_column)
    }

    /// The column the ids point at, when it is not the related models'
    /// key.
    pub fn owner_key_name(&self) -> Option<&str> {
        self.owner_key.as_deref()
    }

    /// The model registered under `name`: the relation's own list first,
    /// then the morph map.
    pub(crate) fn resolve(&self, name: &str) -> Result<Arc<dyn MorphTarget>> {
        if let Some(target) = self
            .targets
            .iter()
            .find(|target| target.morph_name() == name)
        {
            return Ok(Arc::clone(target));
        }
        match MorphMap::current().entry(name) {
            Some(entry) => Ok(Arc::clone(&entry.target)),
            None => Err(OrmError::UnknownMorph {
                name: name.to_owned(),
            }),
        }
    }

    /// Every model the relation can load: its own list, else the morph
    /// map's.
    pub(crate) fn all_targets(&self) -> Vec<Arc<dyn MorphTarget>> {
        if !self.targets.is_empty() {
            return self.targets.clone();
        }
        MorphMap::current()
            .entries()
            .iter()
            .map(|entry| Arc::clone(&entry.target))
            .collect()
    }

    /// The related record of the parent, if any.
    pub async fn get<E: Executor + ?Sized>(self, exec: &E) -> Result<Option<AnyRecord>> {
        let parent = match &self.parent {
            Some(parent) => parent.clone(),
            None => {
                return Err(OrmError::invalid(format!(
                    "the relation `{}` of {} was not made from a record",
                    self.name,
                    P::meta().name()
                )));
            }
        };
        let target = exec.target(Access::Read).await?;
        let mut records = [Record::stored(parent)];
        let node = EagerNode {
            name: self.name.to_string(),
            ..EagerNode::default()
        };
        self.load_into(&target, &mut records, &node).await?;
        let [record] = records;
        Ok(record
            .relations
            .get::<Option<AnyRecord>>(&self.name)
            .cloned()
            .flatten())
    }

    /// Whether `record` is the parent's related record (same type name and
    /// key; no query).
    pub fn is<M: Model>(&self, record: &Record<M>) -> bool {
        let Some(parent) = &self.parent else {
            return false;
        };
        let (Ok(stored_type), Ok(stored_id)) = (
            column_value(parent, &self.type_column, &self.name),
            column_value(parent, &self.id_column, &self.name),
        ) else {
            return false;
        };
        let owner = match &self.owner_key {
            Some(column) => match record.model.value(column) {
                Ok(Some(value)) => value,
                _ => return false,
            },
            None => crate::Field::to_value(&record.key()),
        };
        let type_matches = match &stored_type {
            Value::String(stored) => *stored == M::morph_name(),
            _ => false,
        };
        type_matches
            && key_of(&stored_id).is_some()
            && key_of(&stored_id) == key_of(&owner)
            && M::meta().connection_name() == P::meta().connection_name()
    }

    /// Whether `record` is not the parent's related record.
    pub fn is_not<M: Model>(&self, record: &Record<M>) -> bool {
        !self.is(record)
    }

    /// Loads the relation into `parents`, one query per stored type.
    async fn load_into(
        &self,
        target: &Target<'_>,
        parents: &mut [Record<P>],
        node: &EagerNode,
    ) -> Result<()> {
        let mut relation = self.clone();
        let mut conditions: Vec<Filter> = Vec::new();
        let mut access: Vec<AccessConstraint> = Vec::new();
        for constraint in &node.constraints {
            if let Some(filter) = constraint.filter() {
                conditions.push(filter.clone());
                continue;
            }
            if let Some(constraint) = constraint.access() {
                access.push(constraint.clone());
                continue;
            }
            match constraint
                .closure
                .downcast_ref::<ConstraintFn<MorphTo<P>>>()
            {
                Some(apply) => relation = (apply.0)(relation),
                None => {
                    return Err(OrmError::invalid(format!(
                        "the constraint on morph_to relation `{}` of {} takes `{}`: constrain a MorphTo<{}>, or each model with MorphWith::constrain",
                        node.name,
                        P::meta().name(),
                        constraint.takes,
                        P::meta().name()
                    )));
                }
            }
        }
        let mut with = relation.with.clone();
        if let Some(per_type) = &node.morph {
            with.merge(per_type);
        }
        // The stored type and id of each parent, grouped by type in the
        // order the types first appear.
        let mut groups: Vec<(String, Vec<(usize, Value)>)> = Vec::new();
        for (index, parent) in parents.iter().enumerate() {
            let stored_type = column_value(&parent.model, &relation.type_column, &relation.name)?;
            let id = column_value(&parent.model, &relation.id_column, &relation.name)?;
            let name = match stored_type {
                Value::String(name) if !name.is_empty() => name,
                _ => continue,
            };
            if is_null(&id) {
                continue;
            }
            match groups.iter_mut().find(|(existing, _)| *existing == name) {
                Some((_, members)) => members.push((index, id)),
                None => groups.push((name, vec![(index, id)])),
            }
        }
        let mut found: Vec<Option<AnyRecord>> = vec![None; parents.len()];
        for (name, members) in groups {
            let morph_target = relation.resolve(&name)?;
            let keys = distinct_keys(members.iter().map(|(_, id)| id.clone()));
            let records = morph_target
                .load(
                    target,
                    MorphLoad {
                        owner_key: relation.owner_key.as_deref(),
                        keys: &keys,
                        loads: with.for_type(morph_target.type_id()),
                        children: &node.children,
                        conditions: &conditions,
                        access: &access,
                        trashed: relation.trashed,
                    },
                )
                .await?;
            for (index, id) in members {
                let key = key_of(&morph_target.normalize_key(id, relation.owner_key.as_deref()));
                if let Some((_, record)) = records
                    .iter()
                    .find(|(found_key, _)| Some(found_key) == key.as_ref())
                {
                    found[index] = Some(record.clone());
                }
            }
        }
        for (parent, value) in parents.iter_mut().zip(found) {
            parent.relations.set_relation(node.name.clone(), value);
        }
        Ok(())
    }

    /// The condition of a polymorphic existence check: per type, the type
    /// column holds the type's name and its records exist (or not); for
    /// "none" over every type, a NULL type too.
    pub(crate) fn has_morph_where(
        &self,
        depth: u32,
        types: Option<&[String]>,
        op: Operator,
        count: u64,
        refine: &mut dyn FnMut(&str) -> Option<Filter>,
    ) -> Result<Where> {
        let targets = match types {
            Some(names) => names
                .iter()
                .map(|name| self.resolve(name))
                .collect::<Result<Vec<_>>>()?,
            None => self.all_targets(),
        };
        let count_i = i64::try_from(count).unwrap_or(i64::MAX);
        let check_null = types.is_none()
            && match op {
                Operator::Lt => count >= 1,
                Operator::Lte => true,
                Operator::Eq => count == 0,
                Operator::Ne => count >= 1,
                _ => false,
            };
        if targets.is_empty() {
            // No model to check: compare a constant, as a count of zero.
            return Ok(Where::Compare {
                left: Expr::Value(Value::I64(0)),
                op,
                right: Expr::Value(Value::I64(count_i)),
            });
        }
        let exists = count == 1 && matches!(op, Operator::Gte | Operator::Lt);
        let parent = P::meta();
        let mut alternatives = Vec::with_capacity(targets.len() + 1);
        for target in targets {
            let name = target.morph_name();
            let query = target.exists_query(MorphExists {
                parent,
                id_column: &self.id_column,
                owner_key: self.owner_key.as_deref(),
                depth,
                select: if exists {
                    ExistsSelect::One
                } else {
                    ExistsSelect::Count
                },
                condition: refine(&name),
                trashed: self.trashed,
            })?;
            let check = if exists {
                Where::Exists {
                    query: Box::new(query),
                    not: op == Operator::Lt,
                }
            } else {
                Where::Compare {
                    left: Expr::Subquery(Box::new(query)),
                    op: op.clone(),
                    right: Expr::Value(Value::I64(count_i)),
                }
            };
            alternatives.push(WhereClause::or(Where::Group(vec![
                WhereClause::and(Where::Compare {
                    left: Expr::Column(qualified(parent.table(), &self.type_column)),
                    op: Operator::Eq,
                    right: Expr::Value(Value::from(name.into_owned())),
                }),
                WhereClause::and(check),
            ])));
        }
        if check_null {
            alternatives.push(WhereClause::or(Where::Null {
                expr: Expr::Column(qualified(parent.table(), &self.type_column)),
                not: false,
            }));
        }
        Ok(Where::Group(alternatives))
    }
}

/// The polymorphic operations of a `morph_to`, for relation names.
pub trait MorphToOps<P: Model>: Send + Sync {
    /// The condition of a polymorphic existence check over `types` (every
    /// known type when `None`), with a condition per type from `refine`.
    fn has_morph(
        &self,
        depth: u32,
        types: Option<&[String]>,
        op: Operator,
        count: u64,
        refine: &mut dyn FnMut(&str) -> Option<Filter>,
    ) -> Result<Where>;

    /// The model registered under morph name `name`.
    #[doc(hidden)]
    fn target_for(&self, name: &str) -> Result<Arc<dyn MorphTarget>>;
}

impl<P: Model> MorphToOps<P> for MorphTo<P> {
    fn has_morph(
        &self,
        depth: u32,
        types: Option<&[String]>,
        op: Operator,
        count: u64,
        refine: &mut dyn FnMut(&str) -> Option<Filter>,
    ) -> Result<Where> {
        self.has_morph_where(depth, types, op, count, refine)
    }

    fn target_for(&self, name: &str) -> Result<Arc<dyn MorphTarget>> {
        self.resolve(name)
    }
}

impl<P: Model> private::Erase<P> for MorphTo<P> {
    fn erase(self) -> Arc<dyn ErasedRelation<P>> {
        let mut unbound = self;
        unbound.parent = None;
        Arc::new(unbound)
    }

    fn rename(&mut self, name: Cow<'static, str>) {
        self.name = name;
    }
}

impl<P: Model> Relation<P> for MorphTo<P> {
    type Value = Option<AnyRecord>;

    fn name(&self) -> &str {
        &self.name
    }

    fn kind(&self) -> RelationKind {
        RelationKind::MorphTo
    }

    fn meta(&self) -> RelationMeta {
        let mut meta = RelationMeta::new::<P>(&self.name, RelationKind::MorphTo);
        meta.foreign_key = Some(self.id_column.to_string());
        meta.owner_key = self.owner_key.as_ref().map(ToString::to_string);
        meta.morph_type = Some(self.type_column.to_string());
        meta
    }
}

impl<P: Model> ErasedRelation<P> for MorphTo<P> {
    fn name(&self) -> &str {
        &self.name
    }

    fn meta(&self) -> RelationMeta {
        Relation::meta(self)
    }

    fn related_type(&self) -> Option<TypeId> {
        None
    }

    fn eager_load<'a, 'b>(
        &'a self,
        target: &'a Target<'b>,
        parents: &'a mut [Record<P>],
        node: &'a EagerNode,
    ) -> BoxFuture<'a, Result<()>> {
        Box::pin(async move { self.load_into(target, parents, node).await })
    }

    fn exists_query(&self, _request: ExistsRequest) -> Result<QueryIr> {
        Err(OrmError::invalid(format!(
            "`{}` of {} is a morph_to relation whose model varies: count or aggregate each type through its own relations",
            self.name,
            P::meta().name()
        )))
    }

    fn touch<'a, 'b>(
        &'a self,
        target: &'a Target<'b>,
        parent: &'a Record<P>,
    ) -> BoxFuture<'a, Result<()>> {
        Box::pin(async move {
            let stored_type = column_value(&parent.model, &self.type_column, &self.name)?;
            let id = column_value(&parent.model, &self.id_column, &self.name)?;
            let (Value::String(name), false) = (&stored_type, is_null(&id)) else {
                return Ok(());
            };
            let morph_target = self.resolve(name)?;
            let keys = distinct_keys([id]);
            let records = morph_target
                .load(
                    target,
                    MorphLoad {
                        owner_key: self.owner_key.as_deref(),
                        keys: &keys,
                        loads: None,
                        children: &EagerTree::default(),
                        conditions: &[],
                        access: &[],
                        trashed: Trashed::With,
                    },
                )
                .await?;
            for (_, mut record) in records {
                crate::relations::touch::touch_any(target, &mut record).await?;
            }
            Ok(())
        })
    }

    fn load_nested_missing<'a, 'b>(
        &'a self,
        target: &'a Target<'b>,
        parents: &'a mut [Record<P>],
        rest: &'a EagerTree,
    ) -> BoxFuture<'a, Result<()>> {
        Box::pin(async move {
            // Gather the loaded records by model, load what each misses,
            // and put them back in place.
            let mut taken: Vec<Option<AnyRecord>> = parents
                .iter_mut()
                .map(|parent| {
                    parent
                        .relations
                        .take::<Option<AnyRecord>>(&self.name)
                        .flatten()
                })
                .collect();
            let mut model_types: Vec<TypeId> = Vec::new();
            let mut targets: Vec<Arc<dyn MorphTarget>> = Vec::new();
            for record in taken.iter().flatten() {
                let name = record.morph_name();
                let morph_target = self.resolve(&name)?;
                if !model_types.contains(&morph_target.type_id()) {
                    model_types.push(morph_target.type_id());
                    targets.push(morph_target);
                }
            }
            for morph_target in targets {
                let indexes: Vec<usize> = taken
                    .iter()
                    .enumerate()
                    .filter(|(_, record)| {
                        record
                            .as_ref()
                            .is_some_and(|record| record.morph_name() == morph_target.morph_name())
                    })
                    .map(|(index, _)| index)
                    .collect();
                let records: Vec<AnyRecord> = indexes
                    .iter()
                    .filter_map(|index| taken[*index].take())
                    .collect();
                let loaded = crate::relations::lazy::load_missing_any(
                    target,
                    morph_target.as_ref(),
                    records,
                    rest,
                )
                .await?;
                for (index, record) in indexes.into_iter().zip(loaded) {
                    taken[index] = Some(record);
                }
            }
            for (parent, record) in parents.iter_mut().zip(taken) {
                parent.relations.set_relation(self.name.clone(), record);
            }
            Ok(())
        })
    }

    fn morph_to(&self) -> Option<&dyn MorphToOps<P>> {
        Some(self)
    }
}

/// The models a polymorphic existence check covers: listed by name,
/// listed by model, or every known model.
///
/// ```
/// # use navree_orm::__doc_models::*;
/// use navree_orm::relations::MorphTypes;
///
/// let deals_or_companies = MorphTypes::of::<Deal>().and::<Company>();
/// let by_name: MorphTypes = ["deal", "company"].into();
/// let notes = Note::query().has_morph("notable", deals_or_companies);
/// # let _ = (by_name, notes, MorphTypes::all());
/// ```
#[derive(Clone, Debug, Default)]
pub struct MorphTypes {
    names: Option<Vec<String>>,
}

impl MorphTypes {
    /// Every model the relation knows (its list, else the morph map).
    pub fn all() -> Self {
        MorphTypes { names: None }
    }

    /// The models registered under `names`.
    pub fn named(names: impl IntoIterator<Item = impl Into<String>>) -> Self {
        MorphTypes {
            names: Some(names.into_iter().map(Into::into).collect()),
        }
    }

    /// Model `M` alone.
    pub fn of<M: Model>() -> Self {
        MorphTypes::named([M::morph_name().into_owned()])
    }

    /// These models and model `M`.
    pub fn and<M: Model>(mut self) -> Self {
        self.names
            .get_or_insert_with(Vec::new)
            .push(M::morph_name().into_owned());
        self
    }
}

impl From<&[&str]> for MorphTypes {
    fn from(names: &[&str]) -> Self {
        MorphTypes::named(names.iter().copied())
    }
}

impl<const N: usize> From<[&str; N]> for MorphTypes {
    fn from(names: [&str; N]) -> Self {
        MorphTypes::named(names)
    }
}

impl From<Vec<String>> for MorphTypes {
    fn from(names: Vec<String>) -> Self {
        MorphTypes { names: Some(names) }
    }
}

/// What a `morph_to` points at, for [`where_morphed_to`](Builder::where_morphed_to):
/// a record (`&deal`), several records of one model (`&deals`), any record
/// of a model ([`MorphRef::type_of`]), or nothing ([`MorphRef::null`]).
///
/// ```
/// # use navree_orm::__doc_models::*;
/// use navree_orm::relations::MorphRef;
///
/// let sql = Note::query()
///     .where_morphed_to("notable", MorphRef::type_of::<Deal>())
///     .to_sql(Dialect::Sqlite)
///     .unwrap();
/// assert!(sql.sql.contains(r#"WHERE "notes"."notable_type" = ?"#));
/// let loose = Note::query().where_morphed_to("notable", MorphRef::null());
/// # let _ = loose;
/// ```
#[derive(Clone, Debug)]
pub struct MorphRef {
    kind: MorphRefKind,
}

#[derive(Clone, Debug)]
enum MorphRefKind {
    Records(Vec<AnyRecord>),
    Type(String),
    Null,
}

impl MorphRef {
    /// Any record of model `M`.
    pub fn type_of<M: Model>() -> Self {
        MorphRef {
            kind: MorphRefKind::Type(M::morph_name().into_owned()),
        }
    }

    /// Any record of the model registered under `name`.
    pub fn named(name: impl Into<String>) -> Self {
        MorphRef {
            kind: MorphRefKind::Type(name.into()),
        }
    }

    /// No record: the type column is NULL.
    pub fn null() -> Self {
        MorphRef {
            kind: MorphRefKind::Null,
        }
    }
}

impl<M: Model> From<&Record<M>> for MorphRef {
    fn from(record: &Record<M>) -> Self {
        MorphRef {
            kind: MorphRefKind::Records(vec![AnyRecord::new(record.without_relations())]),
        }
    }
}

impl<M: Model> From<&Collection<M>> for MorphRef {
    fn from(records: &Collection<M>) -> Self {
        MorphRef {
            kind: MorphRefKind::Records(
                records
                    .iter()
                    .map(|record| AnyRecord::new(record.without_relations()))
                    .collect(),
            ),
        }
    }
}

impl From<&AnyRecord> for MorphRef {
    fn from(record: &AnyRecord) -> Self {
        MorphRef {
            kind: MorphRefKind::Records(vec![record.clone()]),
        }
    }
}

impl<M: Model> Builder<M> {
    /// Adds a polymorphic existence check of `morph_to` relation `path`.
    #[allow(clippy::too_many_arguments)]
    fn push_has_morph(
        mut self,
        boolean: Boolean,
        path: &str,
        types: MorphTypes,
        op: Operator,
        count: u64,
        refine: &mut dyn FnMut(&str) -> Option<Filter>,
    ) -> Self {
        let relation = match find::<M>(path) {
            Ok(relation) => relation,
            Err(error) => return self.with_error(error.to_string()),
        };
        let Some(morph) = relation.inner.morph_to() else {
            return self.with_error(format!(
                "`{path}` of {} is a {}, not a morph_to relation",
                M::meta().name(),
                relation.kind()
            ));
        };
        match morph.has_morph(self.eager.depth, types.names.as_deref(), op, count, refine) {
            Ok(filter) => {
                self.push_where(boolean, false, filter);
                self
            }
            Err(error) => self.with_error(error.to_string()),
        }
    }

    /// Only records whose `morph_to` relation `path` points at an existing
    /// record of one of `types` (`["deal", "company"]`,
    /// `MorphTypes::of::<Deal>()`, or `MorphTypes::all()`).
    pub fn has_morph(self, path: impl RelationPath<M>, types: impl Into<MorphTypes>) -> Self {
        let text = path.relation_path().into_owned();
        self.push_has_morph(
            Boolean::And,
            &text,
            types.into(),
            Operator::Gte,
            1,
            &mut |_| None,
        )
    }

    /// `OR` records whose `morph_to` points at an existing record of one
    /// of `types`.
    pub fn or_has_morph(self, path: impl RelationPath<M>, types: impl Into<MorphTypes>) -> Self {
        let text = path.relation_path().into_owned();
        self.push_has_morph(
            Boolean::Or,
            &text,
            types.into(),
            Operator::Gte,
            1,
            &mut |_| None,
        )
    }

    /// Only records whose `morph_to` relation `path` is of one of `types`
    /// but points at no existing record (a note about a deleted deal).
    /// Records of other types are left out; over every type
    /// (`MorphTypes::all()`), records with a NULL type are included.
    pub fn doesnt_have_morph(
        self,
        path: impl RelationPath<M>,
        types: impl Into<MorphTypes>,
    ) -> Self {
        let text = path.relation_path().into_owned();
        self.push_has_morph(
            Boolean::And,
            &text,
            types.into(),
            Operator::Lt,
            1,
            &mut |_| None,
        )
    }

    /// `OR` records whose `morph_to` is of one of `types` but points at no
    /// existing record.
    pub fn or_doesnt_have_morph(
        self,
        path: impl RelationPath<M>,
        types: impl Into<MorphTypes>,
    ) -> Self {
        let text = path.relation_path().into_owned();
        self.push_has_morph(
            Boolean::Or,
            &text,
            types.into(),
            Operator::Lt,
            1,
            &mut |_| None,
        )
    }

    /// Only records whose `morph_to` points at a record of one of `types`
    /// matching `constraint`, which gets the conditions to fill and the
    /// type's name.
    ///
    /// ```
    /// # use navree_orm::__doc_models::*;
    /// let notes = Note::query().where_has_morph("notable", ["deal", "company"], |q, ty| match ty {
    ///     "deal" => q.where_(Deal::STAGE.eq(Stage::Won)),
    ///     _ => q.where_(Company::ACTIVE.eq(true)),
    /// });
    /// # let _ = notes;
    /// ```
    pub fn where_has_morph(
        self,
        path: impl RelationPath<M>,
        types: impl Into<MorphTypes>,
        mut constraint: impl FnMut(WhereGroup, &str) -> WhereGroup,
    ) -> Self {
        let text = path.relation_path().into_owned();
        self.push_has_morph(
            Boolean::And,
            &text,
            types.into(),
            Operator::Gte,
            1,
            &mut |ty| group_filter(constraint(WhereGroup::new(), ty)),
        )
    }

    /// `OR` records whose `morph_to` points at a record matching
    /// `constraint`.
    pub fn or_where_has_morph(
        self,
        path: impl RelationPath<M>,
        types: impl Into<MorphTypes>,
        mut constraint: impl FnMut(WhereGroup, &str) -> WhereGroup,
    ) -> Self {
        let text = path.relation_path().into_owned();
        self.push_has_morph(
            Boolean::Or,
            &text,
            types.into(),
            Operator::Gte,
            1,
            &mut |ty| group_filter(constraint(WhereGroup::new(), ty)),
        )
    }

    /// Only records whose `morph_to` is of one of `types` but points at no
    /// record matching `constraint` (over every type, NULL types too).
    pub fn where_doesnt_have_morph(
        self,
        path: impl RelationPath<M>,
        types: impl Into<MorphTypes>,
        mut constraint: impl FnMut(WhereGroup, &str) -> WhereGroup,
    ) -> Self {
        let text = path.relation_path().into_owned();
        self.push_has_morph(
            Boolean::And,
            &text,
            types.into(),
            Operator::Lt,
            1,
            &mut |ty| group_filter(constraint(WhereGroup::new(), ty)),
        )
    }

    /// `OR` records whose `morph_to` is of one of `types` but points at no
    /// record matching `constraint`.
    pub fn or_where_doesnt_have_morph(
        self,
        path: impl RelationPath<M>,
        types: impl Into<MorphTypes>,
        mut constraint: impl FnMut(WhereGroup, &str) -> WhereGroup,
    ) -> Self {
        let text = path.relation_path().into_owned();
        self.push_has_morph(
            Boolean::Or,
            &text,
            types.into(),
            Operator::Lt,
            1,
            &mut |ty| group_filter(constraint(WhereGroup::new(), ty)),
        )
    }

    /// Only records whose `morph_to` points at a record of one of `types`
    /// matching `filter`.
    pub fn where_morph_relation(
        self,
        path: impl RelationPath<M>,
        types: impl Into<MorphTypes>,
        filter: impl Into<Filter>,
    ) -> Self {
        let text = path.relation_path().into_owned();
        let filter = filter.into();
        self.push_has_morph(
            Boolean::And,
            &text,
            types.into(),
            Operator::Gte,
            1,
            &mut |_| Some(filter.clone()),
        )
    }

    /// `OR` records whose `morph_to` points at a record matching `filter`.
    pub fn or_where_morph_relation(
        self,
        path: impl RelationPath<M>,
        types: impl Into<MorphTypes>,
        filter: impl Into<Filter>,
    ) -> Self {
        let text = path.relation_path().into_owned();
        let filter = filter.into();
        self.push_has_morph(
            Boolean::Or,
            &text,
            types.into(),
            Operator::Gte,
            1,
            &mut |_| Some(filter.clone()),
        )
    }

    /// Only records whose `morph_to` is of one of `types` but points at no
    /// record matching `filter`.
    pub fn where_morph_doesnt_have_relation(
        self,
        path: impl RelationPath<M>,
        types: impl Into<MorphTypes>,
        filter: impl Into<Filter>,
    ) -> Self {
        let text = path.relation_path().into_owned();
        let filter = filter.into();
        self.push_has_morph(
            Boolean::And,
            &text,
            types.into(),
            Operator::Lt,
            1,
            &mut |_| Some(filter.clone()),
        )
    }

    /// `OR` records whose `morph_to` is of one of `types` but points at no
    /// record matching `filter`.
    pub fn or_where_morph_doesnt_have_relation(
        self,
        path: impl RelationPath<M>,
        types: impl Into<MorphTypes>,
        filter: impl Into<Filter>,
    ) -> Self {
        let text = path.relation_path().into_owned();
        let filter = filter.into();
        self.push_has_morph(
            Boolean::Or,
            &text,
            types.into(),
            Operator::Lt,
            1,
            &mut |_| Some(filter.clone()),
        )
    }

    /// Only records whose `morph_to` relation `path` points at `target`: a
    /// record, the records of a collection, any record of a model
    /// ([`MorphRef::type_of`]), or none ([`MorphRef::null`]). No query of
    /// the related table: the type and id columns are compared.
    ///
    /// ```
    /// # use navree_orm::__doc_models::*;
    /// use navree_orm::relations::MorphRef;
    ///
    /// let on_deals = Note::query().where_morphed_to("notable", MorphRef::type_of::<Deal>());
    /// # let _ = on_deals;
    /// ```
    pub fn where_morphed_to(self, path: impl RelationPath<M>, target: impl Into<MorphRef>) -> Self {
        let text = path.relation_path().into_owned();
        self.push_morphed_to(Boolean::And, &text, target.into(), false)
    }

    /// Only records whose `morph_to` does not point at `target`.
    pub fn where_not_morphed_to(
        self,
        path: impl RelationPath<M>,
        target: impl Into<MorphRef>,
    ) -> Self {
        let text = path.relation_path().into_owned();
        self.push_morphed_to(Boolean::And, &text, target.into(), true)
    }

    /// `OR` records whose `morph_to` points at `target`.
    pub fn or_where_morphed_to(
        self,
        path: impl RelationPath<M>,
        target: impl Into<MorphRef>,
    ) -> Self {
        let text = path.relation_path().into_owned();
        self.push_morphed_to(Boolean::Or, &text, target.into(), false)
    }

    /// `OR` records whose `morph_to` does not point at `target`.
    pub fn or_where_not_morphed_to(
        self,
        path: impl RelationPath<M>,
        target: impl Into<MorphRef>,
    ) -> Self {
        let text = path.relation_path().into_owned();
        self.push_morphed_to(Boolean::Or, &text, target.into(), true)
    }

    fn push_morphed_to(
        mut self,
        boolean: Boolean,
        path: &str,
        target: MorphRef,
        not: bool,
    ) -> Self {
        match morphed_to_filter::<M>(path, target, not) {
            Ok(filter) => {
                self.push_where(boolean, false, filter);
                self
            }
            Err(error) => self.with_error(error.to_string()),
        }
    }

    /// Eager loads `morph_to` relation `path` with per-model loads.
    pub fn with_morph(mut self, path: impl RelationPath<M>, with: MorphWith) -> Self {
        let text = path.relation_path().into_owned();
        match ParsedPath::parse(&text) {
            Ok(parsed) => {
                let node = self.eager.tree.entry(&parsed.segments);
                match &mut node.morph {
                    Some(existing) => existing.merge(&with),
                    None => node.morph = Some(with),
                }
                self
            }
            Err(error) => self.with_error(error.to_string()),
        }
    }
}

/// The conditions of a group, as one filter (`None` when it has none).
fn group_filter(group: WhereGroup) -> Option<Filter> {
    let clauses = group.into_clauses();
    (!clauses.is_empty()).then(|| Filter::from(Where::Group(clauses)))
}

/// The condition of `where_morphed_to`: per model, the type column holds
/// its name and the id column one of its records' keys.
fn morphed_to_filter<M: Model>(path: &str, target: MorphRef, not: bool) -> Result<Where> {
    let relation = find::<M>(path)?;
    let meta = relation.meta();
    if meta.kind != RelationKind::MorphTo {
        return Err(OrmError::invalid(format!(
            "`{path}` of {} is a {}, not a morph_to relation",
            M::meta().name(),
            meta.kind
        )));
    }
    let table = M::meta().table();
    let (Some(type_column), Some(id_column)) = (meta.morph_type, meta.foreign_key) else {
        return Err(OrmError::invalid(format!(
            "the morph_to relation `{path}` of {} has no columns",
            M::meta().name()
        )));
    };
    let type_op = if not {
        Operator::NullSafeEq
    } else {
        Operator::Eq
    };
    let condition = match target.kind {
        MorphRefKind::Null => Where::Null {
            expr: Expr::Column(qualified(table, &type_column)),
            not,
        },
        MorphRefKind::Type(name) => {
            let check = Where::Compare {
                left: Expr::Column(qualified(table, &type_column)),
                op: type_op,
                right: Expr::Value(Value::from(name)),
            };
            if not {
                Where::Group(vec![WhereClause {
                    boolean: Boolean::And,
                    negated: true,
                    filter: check,
                }])
            } else {
                check
            }
        }
        MorphRefKind::Records(records) => {
            if records.is_empty() {
                return Err(OrmError::invalid(format!(
                    "where_morphed_to(`{path}`) was given no records"
                )));
            }
            // By model, in the order the models first appear.
            let mut by_type: Vec<(String, Vec<Value>)> = Vec::new();
            for record in &records {
                let name = record.morph_name().into_owned();
                let key = match &meta.owner_key {
                    Some(column) => record.value(column)?.ok_or_else(|| {
                        OrmError::invalid(format!(
                            "{} has no column `{column}`, which `{path}` points at",
                            record.model_name()
                        ))
                    })?,
                    None => record.key(),
                };
                match by_type.iter_mut().find(|(existing, _)| *existing == name) {
                    Some((_, keys)) => keys.push(key),
                    None => by_type.push((name, vec![key])),
                }
            }
            let alternatives = by_type
                .into_iter()
                .map(|(name, keys)| {
                    let keys = distinct_keys(keys);
                    WhereClause::or(Where::Group(vec![
                        WhereClause::and(Where::Compare {
                            left: Expr::Column(qualified(table, &type_column)),
                            op: type_op.clone(),
                            right: Expr::Value(Value::from(name)),
                        }),
                        WhereClause::and(in_keys(qualified(table, &id_column), &keys)),
                    ]))
                })
                .collect();
            let any = Where::Group(alternatives);
            if not {
                Where::Group(vec![WhereClause {
                    boolean: Boolean::And,
                    negated: true,
                    filter: any,
                }])
            } else {
                any
            }
        }
    };
    Ok(condition)
}

/// The value a `morph_to` stores for `record`'s model: its morph name
/// (checked against a required morph map).
pub(crate) fn stored_type_of<M: Model>() -> Result<Value> {
    Ok(Value::from(type_name_of::<M>()?.into_owned()))
}