navree-orm 1.2.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
//! The driver-neutral query representation (IR).
//!
//! Every builder ([`Builder`](crate::Builder),
//! [`TableBuilder`](crate::TableBuilder)) produces a [`QueryIr`]; writes
//! produce an [`InsertIr`], [`UpdateIr`] or [`DeleteIr`]. The SQL compiler
//! ([`crate::sql`]) turns them into statements for each dialect, and a
//! [`DocumentDriver`](crate::DocumentDriver) turns them into document
//! commands. Apps rarely touch the IR; drivers read it.
//!
//! The enums are `#[non_exhaustive]`: a driver answers a variant it cannot
//! express with [`OrmError::Unsupported`](crate::OrmError::Unsupported).

use std::borrow::Cow;
use std::fmt;

use crate::value::Value;

/// A column, optionally qualified by its table, optionally followed by a
/// JSON path (`meta->status`, `meta->tags->0`).
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ColumnRef {
    /// The table (or alias) qualifying the column.
    pub table: Option<Cow<'static, str>>,
    /// The column name.
    pub name: Cow<'static, str>,
    /// The JSON path into the column's value; empty for the column itself.
    pub path: Vec<JsonSegment>,
}

/// One step of a JSON path.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum JsonSegment {
    /// An object key.
    Key(String),
    /// An array index (negative counts from the end where the database
    /// supports it).
    Index(i64),
}

impl ColumnRef {
    /// `name` in `table` (or unqualified), without a JSON path.
    pub fn new(
        table: Option<impl Into<Cow<'static, str>>>,
        name: impl Into<Cow<'static, str>>,
    ) -> Self {
        ColumnRef {
            table: table.map(Into::into),
            name: name.into(),
            path: Vec::new(),
        }
    }

    /// Parses `column`, `table.column` and either followed by a JSON path:
    /// `meta->status`, `deals.meta->tags->0`, `meta->items[2]`.
    ///
    /// ```
    /// use navree_orm::ir::{ColumnRef, JsonSegment};
    ///
    /// let column = ColumnRef::parse("deals.meta->tags[1]->name");
    /// assert_eq!(column.table.as_deref(), Some("deals"));
    /// assert_eq!(column.name, "meta");
    /// assert_eq!(
    ///     column.path,
    ///     [
    ///         JsonSegment::Key("tags".into()),
    ///         JsonSegment::Index(1),
    ///         JsonSegment::Key("name".into()),
    ///     ]
    /// );
    /// ```
    pub fn parse(text: impl Into<Cow<'static, str>>) -> Self {
        let text = text.into();
        let (head, path) = match text.split_once("->") {
            Some((head, rest)) => (head.to_owned(), parse_json_path(rest)),
            None if text.trim_end().ends_with(']') && text.contains('[') => {
                // `tags[1]`: an index into the column itself.
                (text.to_string(), Vec::new())
            }
            None => {
                return Self::parse_head(text);
            }
        };
        let (head, mut indexes) = match head.find('[') {
            Some(at) if head.trim_end().ends_with(']') => {
                (head[..at].to_owned(), parse_json_path(&head[at..]))
            }
            _ => (head, Vec::new()),
        };
        let mut column = Self::parse_head(Cow::Owned(head));
        indexes.extend(path);
        column.path = indexes;
        column
    }

    fn parse_head(head: Cow<'static, str>) -> Self {
        match head.rsplit_once('.') {
            Some((table, name)) => ColumnRef {
                table: Some(Cow::Owned(table.trim().to_owned())),
                name: Cow::Owned(name.trim().to_owned()),
                path: Vec::new(),
            },
            None => ColumnRef {
                table: None,
                name: match head {
                    Cow::Borrowed(name) => Cow::Borrowed(name.trim()),
                    Cow::Owned(name) => Cow::Owned(name.trim().to_owned()),
                },
                path: Vec::new(),
            },
        }
    }

    /// The same column without its JSON path.
    pub fn without_path(&self) -> Self {
        ColumnRef {
            table: self.table.clone(),
            name: self.name.clone(),
            path: Vec::new(),
        }
    }
}

/// Parses `a->b[0]->1` into segments: keys, and indexes written as `[n]`
/// or as a whole-number segment.
fn parse_json_path(path: &str) -> Vec<JsonSegment> {
    let mut segments = Vec::new();
    for part in path.split("->") {
        let part = part.trim().trim_matches(|c| c == '\'' || c == '"');
        let (key, indexes) = match part.find('[') {
            Some(at) if part.ends_with(']') => (&part[..at], &part[at..]),
            _ => (part, ""),
        };
        if !key.is_empty() {
            match key.parse::<i64>() {
                Ok(index) => segments.push(JsonSegment::Index(index)),
                Err(_) => segments.push(JsonSegment::Key(key.to_owned())),
            }
        }
        for index in indexes.split(['[', ']']).filter(|piece| !piece.is_empty()) {
            match index.trim().parse::<i64>() {
                Ok(index) => segments.push(JsonSegment::Index(index)),
                Err(_) => segments.push(JsonSegment::Key(index.to_owned())),
            }
        }
    }
    segments
}

impl fmt::Display for ColumnRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(table) = &self.table {
            write!(f, "{table}.")?;
        }
        f.write_str(&self.name)?;
        for segment in &self.path {
            match segment {
                JsonSegment::Key(key) => write!(f, "->{key}")?,
                JsonSegment::Index(index) => write!(f, "->{index}")?,
            }
        }
        Ok(())
    }
}

/// An expression: a column, a value, raw SQL, a function call, an
/// aggregate, arithmetic, a subquery, or a TimescaleDB helper.
///
/// Build one with its constructors (`Expr::col`,
/// `Expr::count`, `Expr::time_bucket`, ...) and compare it into a
/// [`Filter`](crate::Filter) with `eq`, `gt`, ...
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Expr {
    /// A column.
    Column(ColumnRef),
    /// A bound value.
    Value(Value),
    /// Raw SQL with `?` placeholders for `values` (`??` is a literal `?`).
    /// SQL drivers only.
    Raw {
        /// The SQL fragment.
        sql: Cow<'static, str>,
        /// The values bound to its placeholders, in order.
        values: Vec<Value>,
    },
    /// A function call, e.g. `lower(name)`.
    Function {
        /// The function's name.
        name: Cow<'static, str>,
        /// Its arguments.
        args: Vec<Expr>,
    },
    /// An aggregate: `count(*)`, `sum(amount)`, `count(DISTINCT owner_id)`.
    Aggregate {
        /// The function.
        function: AggregateFunction,
        /// The argument; `None` is `*`.
        arg: Option<Box<Expr>>,
        /// `DISTINCT` inside the call.
        distinct: bool,
    },
    /// `left op right` with an arithmetic operator.
    Arithmetic {
        /// The left operand.
        left: Box<Expr>,
        /// The operator.
        op: ArithmeticOp,
        /// The right operand.
        right: Box<Expr>,
    },
    /// A subquery returning one value per row.
    Subquery(Box<QueryIr>),
    /// TimescaleDB's `time_bucket(width, expr [, origin])`; fixed widths
    /// are emulated on databases without it.
    TimeBucket {
        /// The bucket width, an interval such as `1 day` or `15 minutes`.
        width: String,
        /// The time column or expression.
        expr: Box<Expr>,
        /// Where buckets start; TimescaleDB's default when `None`.
        origin: Option<Box<Expr>>,
    },
    /// TimescaleDB's `first(value, time)`: the value at the earliest time.
    First {
        /// The value column.
        value: Box<Expr>,
        /// The time column.
        time: Box<Expr>,
    },
    /// TimescaleDB's `last(value, time)`: the value at the latest time.
    Last {
        /// The value column.
        value: Box<Expr>,
        /// The time column.
        time: Box<Expr>,
    },
    /// `*`, or `table.*`.
    All {
        /// The table, for `table.*`.
        table: Option<Cow<'static, str>>,
    },
    /// `EXISTS (subquery)` as a value: true when the subquery has a row
    /// (relation existence flags, `with_exists`).
    Exists(Box<QueryIr>),
    /// `ROW_NUMBER() OVER (PARTITION BY .. ORDER BY ..)`: the row's
    /// position within its partition (the per-parent limits of eager
    /// loads). SQL databases with window functions: PostgreSQL, MySQL 8,
    /// MariaDB 10.2, SQLite 3.25.
    RowNumber {
        /// The partition expressions.
        partition: Vec<Expr>,
        /// The order within each partition.
        order: Vec<Order>,
    },
    /// In an upsert's update ([`OnConflict::Update`]): the value column
    /// `column` would have been inserted with (`excluded.column` on
    /// PostgreSQL and SQLite, `VALUES(column)` on MySQL).
    Excluded {
        /// The column.
        column: Cow<'static, str>,
    },
    /// `left op right` with an operator written by hand (`||`, `-`, `#-`,
    /// `->`, ...), checked against the dialect's list of value operators
    /// when the query compiles. SQL drivers only.
    Binary {
        /// The left operand.
        left: Box<Expr>,
        /// The operator.
        op: Cow<'static, str>,
        /// The right operand.
        right: Box<Expr>,
    },
    /// The distance between the vector in `expr` (an embedding column) and
    /// `vector`: pgvector's `<=>` (cosine), `<->` (Euclidean) or `<#>`
    /// (negative inner product) on PostgreSQL, MariaDB's
    /// `VEC_DISTANCE_COSINE` / `VEC_DISTANCE_EUCLIDEAN` on MySQL;
    /// unsupported on SQLite.
    VectorDistance {
        /// The vector column or expression.
        expr: Box<Expr>,
        /// The vector compared with.
        vector: Vec<f32>,
        /// How distance is measured.
        metric: VectorMetric,
    },
}

/// How a vector distance is measured.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum VectorMetric {
    /// Cosine distance, `1 - cosine similarity` (0 for the same direction,
    /// 2 for opposite ones): the usual measure for text embeddings.
    #[default]
    Cosine,
    /// Euclidean (L2) distance.
    Euclidean,
    /// The negative inner product (PostgreSQL only): smaller is more
    /// similar, for normalized embeddings like cosine but faster.
    InnerProduct,
}

/// An aggregate function.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AggregateFunction {
    /// `COUNT`.
    Count,
    /// `SUM`.
    Sum,
    /// `AVG`.
    Avg,
    /// `MIN`.
    Min,
    /// `MAX`.
    Max,
}

impl AggregateFunction {
    /// The SQL name.
    pub fn sql_name(self) -> &'static str {
        match self {
            AggregateFunction::Count => "COUNT",
            AggregateFunction::Sum => "SUM",
            AggregateFunction::Avg => "AVG",
            AggregateFunction::Min => "MIN",
            AggregateFunction::Max => "MAX",
        }
    }
}

/// An arithmetic operator.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ArithmeticOp {
    /// `+`.
    Add,
    /// `-`.
    Sub,
    /// `*`.
    Mul,
    /// `/`.
    Div,
}

/// A comparison operator.
///
/// Built from the usual spellings with `From<&str>`: `=`, `!=` / `<>`,
/// `<`, `<=`, `>`, `>=`, `like`, `not like`, `ilike`, `not ilike`, `<=>`
/// (null-safe equality), `&`, `|`, `^`, `<<`, `>>`, `regexp`, `not
/// regexp`, and PostgreSQL's `~`, `~*`, `!~`, `!~*`, `similar to`, `not
/// similar to`, `@>`, `<@`, `?`, `?|`, `?&` and `&&`. Anything else is
/// [`Operator::Invalid`], reported when the query runs.
///
/// ```
/// use navree_orm::ir::Operator;
///
/// assert_eq!(Operator::from(">="), Operator::Gte);
/// assert_eq!(Operator::from("<>"), Operator::Ne);
/// assert_eq!(Operator::from("NOT LIKE"), Operator::NotLike);
/// assert!(matches!(Operator::from("=>"), Operator::Invalid(_)));
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Operator {
    /// `=`.
    Eq,
    /// `<>`.
    Ne,
    /// `<`.
    Lt,
    /// `<=`.
    Lte,
    /// `>`.
    Gt,
    /// `>=`.
    Gte,
    /// `LIKE` as the database defines it.
    Like,
    /// `NOT LIKE`.
    NotLike,
    /// Case-insensitive `LIKE` (`ILIKE` on PostgreSQL, `LOWER(..) LIKE
    /// LOWER(..)` elsewhere).
    ILike,
    /// Case-insensitive `NOT LIKE`.
    NotILike,
    /// Null-safe equality (`IS NOT DISTINCT FROM`, `<=>`, `IS`).
    NullSafeEq,
    /// `&`.
    BitAnd,
    /// `|`.
    BitOr,
    /// `^` (`#` on PostgreSQL).
    BitXor,
    /// `<<`.
    ShiftLeft,
    /// `>>`.
    ShiftRight,
    /// Regular-expression match, case-sensitive (`~` on PostgreSQL,
    /// `REGEXP_LIKE(.., 'c')` on MySQL, `REGEXP` on SQLite).
    Regexp,
    /// Regular-expression mismatch, case-sensitive.
    NotRegexp,
    /// Regular-expression match ignoring case (`~*` on PostgreSQL,
    /// `REGEXP_LIKE(.., 'i')` on MySQL, `REGEXP` with `(?i)` on SQLite).
    RegexpIgnoreCase,
    /// Regular-expression mismatch ignoring case.
    NotRegexpIgnoreCase,
    /// Null-safe inequality (`IS DISTINCT FROM`, `NOT <=>`, `IS NOT`): true
    /// when exactly one side is NULL.
    NullSafeNe,
    /// A PostgreSQL operator written as is (`~*`, `@>`, `?|`, ...).
    Postgres(&'static str),
    /// An operator written by hand ([`Filter::op_raw`](crate::Filter::op_raw)),
    /// checked against the dialect's list of condition operators when the
    /// query compiles.
    Raw(String),
    /// An operator that is not recognised; the query fails when it runs.
    Invalid(String),
}

/// The PostgreSQL-only operators [`Operator::from`] recognises.
const POSTGRES_OPERATORS: [&str; 13] = [
    "~*",
    "!~*",
    "~",
    "!~",
    "similar to",
    "not similar to",
    "@>",
    "<@",
    "?",
    "?|",
    "?&",
    "&&",
    "||",
];

impl From<&str> for Operator {
    fn from(text: &str) -> Self {
        let normalized = text
            .split_whitespace()
            .collect::<Vec<_>>()
            .join(" ")
            .to_lowercase();
        match normalized.as_str() {
            "=" | "==" => Operator::Eq,
            "!=" | "<>" => Operator::Ne,
            "<" => Operator::Lt,
            "<=" => Operator::Lte,
            ">" => Operator::Gt,
            ">=" => Operator::Gte,
            "like" => Operator::Like,
            "not like" => Operator::NotLike,
            "ilike" => Operator::ILike,
            "not ilike" => Operator::NotILike,
            "<=>" | "is not distinct from" => Operator::NullSafeEq,
            "is distinct from" => Operator::NullSafeNe,
            "&" => Operator::BitAnd,
            "|" => Operator::BitOr,
            "^" => Operator::BitXor,
            "<<" => Operator::ShiftLeft,
            ">>" => Operator::ShiftRight,
            "regexp" | "rlike" => Operator::Regexp,
            "not regexp" | "not rlike" => Operator::NotRegexp,
            other => match POSTGRES_OPERATORS.iter().find(|op| **op == other) {
                Some(op) => Operator::Postgres(op),
                None => Operator::Invalid(text.to_owned()),
            },
        }
    }
}

impl From<String> for Operator {
    fn from(text: String) -> Self {
        Operator::from(text.as_str())
    }
}

/// A part of a date or date-time, for `where_date` and friends.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DatePart {
    /// The date (`YYYY-MM-DD`).
    Date,
    /// The time of day (`HH:MM:SS`).
    Time,
    /// The day of the month.
    Day,
    /// The month.
    Month,
    /// The year.
    Year,
}

/// How a clause joins the clauses before it.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Boolean {
    /// `AND`.
    And,
    /// `OR`.
    Or,
}

/// One clause of a `WHERE` (or `HAVING`, or join `ON`) list.
///
/// Clauses combine with SQL's precedence: `a AND b OR c AND d` is
/// `(a AND b) OR (c AND d)`.
#[derive(Clone, Debug)]
pub struct WhereClause {
    /// How the clause joins the ones before it (ignored for the first).
    pub boolean: Boolean,
    /// `NOT (...)`.
    pub negated: bool,
    /// The condition.
    pub filter: Where,
}

impl WhereClause {
    /// An `AND` clause.
    pub fn and(filter: Where) -> Self {
        WhereClause {
            boolean: Boolean::And,
            negated: false,
            filter,
        }
    }

    /// An `OR` clause.
    pub fn or(filter: Where) -> Self {
        WhereClause {
            boolean: Boolean::Or,
            negated: false,
            filter,
        }
    }
}

/// A condition.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Where {
    /// `left op right`.
    Compare {
        /// The left side.
        left: Expr,
        /// The operator.
        op: Operator,
        /// The right side.
        right: Expr,
    },
    /// `expr IS [NOT] NULL`.
    Null {
        /// The expression.
        expr: Expr,
        /// `IS NOT NULL`.
        not: bool,
    },
    /// `expr [NOT] IN (...)`.
    In {
        /// The expression.
        expr: Expr,
        /// The list or subquery.
        values: InValues,
        /// `NOT IN`.
        not: bool,
    },
    /// `expr [NOT] BETWEEN low AND high`.
    Between {
        /// The expression.
        expr: Expr,
        /// The lower bound.
        low: Expr,
        /// The upper bound.
        high: Expr,
        /// `NOT BETWEEN`.
        not: bool,
    },
    /// A `LIKE` pattern match (`%` and `_` wildcards).
    Like {
        /// The expression.
        expr: Expr,
        /// The pattern.
        pattern: Expr,
        /// Case-sensitive on every database (`LIKE BINARY` on MySQL, `GLOB`
        /// on SQLite) instead of case-insensitive on every database.
        case_sensitive: bool,
        /// `NOT LIKE`.
        not: bool,
    },
    /// Text compared with `text` ignoring letter case on every database,
    /// non-ASCII letters included, and keeping accents (`équipe` finds
    /// `Équipe`, not `Equipe`). `text` is taken literally: it has no
    /// wildcards. A NULL expression never matches.
    IgnoreCase {
        /// The text expression.
        expr: Expr,
        /// The text compared with.
        text: String,
        /// Whether the expression contains the text or equals it.
        mode: IgnoreCaseMode,
    },
    /// A comparison of part of a date-time: `date(created_at) = ?`,
    /// `month(created_at) = ?`, ...
    DatePart {
        /// The part.
        part: DatePart,
        /// The date or date-time expression.
        expr: Expr,
        /// The operator.
        op: Operator,
        /// The value compared with (a date, a time, or a number).
        value: Expr,
    },
    /// `[NOT] EXISTS (subquery)`.
    Exists {
        /// The subquery.
        query: Box<QueryIr>,
        /// `NOT EXISTS`.
        not: bool,
    },
    /// A parenthesised group of clauses.
    Group(Vec<WhereClause>),
    /// The JSON column (or path) contains `value` (an array element, or an
    /// object's keys and values).
    JsonContains {
        /// The column and path.
        column: ColumnRef,
        /// The value looked for.
        value: serde_json::Value,
        /// Does not contain.
        not: bool,
    },
    /// The JSON at the column (or path) is contained in `value`: every
    /// element (or key and value) it has, `value` has too (`<@` on
    /// PostgreSQL, `json_contains(value, column)` on MySQL).
    JsonContainedBy {
        /// The column and path.
        column: ColumnRef,
        /// The value it must fit in.
        value: serde_json::Value,
        /// Is not contained.
        not: bool,
    },
    /// The JSON array at the column (or path) shares an element with
    /// `value` (an array; a scalar stands for a one-element array).
    JsonOverlaps {
        /// The column and path.
        column: ColumnRef,
        /// The elements looked for.
        value: serde_json::Value,
        /// Shares none.
        not: bool,
    },
    /// The JSON column has the object key (or array index) its path ends
    /// with: `meta->owner` has `owner`, `meta->tags[2]` a third tag.
    JsonContainsKey {
        /// The column, with the path whose last step is looked for.
        column: ColumnRef,
        /// Does not have it.
        not: bool,
    },
    /// The length of the JSON array at the column (or path), compared.
    JsonLength {
        /// The column and path.
        column: ColumnRef,
        /// The operator.
        op: Operator,
        /// The length compared with.
        length: i64,
    },
    /// A full-text match of `query` against `columns`.
    FullText {
        /// The columns searched.
        columns: Vec<Expr>,
        /// The search text.
        query: String,
        /// The mode and language.
        options: FullText,
    },
    /// Raw SQL with `?` placeholders (`??` is a literal `?`): `Filter::raw`,
    /// or a navree-db `Statement::portable` fragment given to
    /// `Filter::from`. SQL drivers only.
    Raw {
        /// The SQL fragment.
        sql: Cow<'static, str>,
        /// Its values, in order.
        values: Vec<Value>,
    },
    /// A condition written as a [render-tree](crate::render::ast)
    /// expression, which the renderer writes for each SQL dialect (the
    /// access-control filters of `Builder::accessible` are built this way).
    /// SQL drivers only.
    Tree(Box<crate::render::ast::Expr>),
    /// A condition in a document store's own query language (a MongoDB
    /// filter document), built by that store's driver crate. Only the
    /// driver that built it can run it; SQL databases and other drivers
    /// answer [`OrmError::Unsupported`](crate::OrmError::Unsupported).
    Native(NativeFilter),
    /// A condition that could not be built (a value its column could not
    /// encode); the query fails with this message when it runs.
    Invalid(String),
}

/// A condition written in a document store's own query language, carried
/// through the IR untouched: [`Where::Native`].
///
/// A driver crate wraps its native value (MongoDB's filter document) with
/// [`NativeFilter::new`] and reads it back with
/// [`downcast_ref`](NativeFilter::downcast_ref); `Debug` shows the value.
///
/// ```
/// use navree_orm::ir::NativeFilter;
///
/// let filter = NativeFilter::new("memory", String::from("status == open"));
/// assert_eq!(filter.driver(), "memory");
/// assert_eq!(filter.downcast_ref::<String>().map(String::as_str), Some("status == open"));
/// assert!(filter.downcast_ref::<i64>().is_none());
/// assert_eq!(format!("{filter:?}"), r#"NativeFilter(memory: "status == open")"#);
/// ```
#[derive(Clone)]
pub struct NativeFilter {
    driver: &'static str,
    value: std::sync::Arc<dyn NativeValue>,
}

/// What a [`NativeFilter`] holds: any `Debug` value that can cross threads.
trait NativeValue: std::any::Any + fmt::Debug + Send + Sync {
    fn as_any(&self) -> &dyn std::any::Any;
}

impl<T: std::any::Any + fmt::Debug + Send + Sync> NativeValue for T {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

impl NativeFilter {
    /// `value`, a condition only driver `driver` (its name, e.g.
    /// `mongodb`) understands.
    pub fn new<T: std::any::Any + fmt::Debug + Send + Sync>(
        driver: &'static str,
        value: T,
    ) -> Self {
        NativeFilter {
            driver,
            value: std::sync::Arc::new(value),
        }
    }

    /// The driver that understands the condition.
    pub fn driver(&self) -> &'static str {
        self.driver
    }

    /// The native value, when it is a `T`.
    pub fn downcast_ref<T: std::any::Any>(&self) -> Option<&T> {
        // Through the trait object: the `Arc` itself is a `NativeValue`
        // too, and would answer for itself.
        let value: &dyn NativeValue = &*self.value;
        value.as_any().downcast_ref::<T>()
    }
}

impl fmt::Debug for NativeFilter {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "NativeFilter({}: {:?})", self.driver, self.value)
    }
}

/// How a [`Where::IgnoreCase`] condition compares its text.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum IgnoreCaseMode {
    /// The expression contains the text (`Column::contains_ignore_case`).
    Contains,
    /// The expression equals the text (`Column::eq_ignore_case`).
    Equals,
}

/// How a full-text condition matches.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FullTextMode {
    /// Words, in any order (`plainto_tsquery`; MySQL's natural language
    /// mode).
    #[default]
    Natural,
    /// The words as a phrase (`phraseto_tsquery`; natural language mode
    /// on MySQL).
    Phrase,
    /// Search-engine syntax: quotes, `or`, `-word` (`websearch_to_tsquery`;
    /// boolean mode on MySQL).
    Websearch,
    /// MySQL's boolean mode (`+word -word "phrase"`); websearch syntax on
    /// PostgreSQL.
    Boolean,
    /// The text search query syntax itself (`to_tsquery`: `'fat & (rat |
    /// cat)'`); boolean mode on MySQL. The text must be a valid query.
    Raw,
}

/// The options of a full-text condition
/// ([`where_full_text_with`](crate::Builder::where_full_text_with)).
///
/// ```
/// use navree_orm::ir::{FullText, FullTextMode};
///
/// let options = FullText::websearch().language("german");
/// assert_eq!(options.mode, FullTextMode::Websearch);
/// assert_eq!(options.language.as_deref(), Some("german"));
/// ```
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct FullText {
    /// How the text matches.
    pub mode: FullTextMode,
    /// PostgreSQL's text search configuration (`english` by default); one
    /// of PostgreSQL's built-in languages.
    pub language: Option<String>,
    /// MySQL's `WITH QUERY EXPANSION` (natural language mode).
    pub expanded: bool,
    /// PostgreSQL: the columns hold `tsvector`s already (a generated
    /// column, indexed), so they are matched as they are instead of
    /// through `to_tsvector`.
    pub vectors: bool,
}

impl FullText {
    /// Words in English (the default language), in any order.
    pub fn english() -> Self {
        FullText::default().language("english")
    }

    /// PostgreSQL's text search syntax (`to_tsquery`), boolean mode on
    /// MySQL.
    pub fn raw() -> Self {
        FullText {
            mode: FullTextMode::Raw,
            ..FullText::default()
        }
    }

    /// The same options matching `tsvector` columns as they are
    /// (PostgreSQL).
    ///
    /// ```
    /// use navree_orm::ir::FullText;
    ///
    /// let options = FullText::english().websearch_mode().vectors();
    /// assert!(options.vectors);
    /// ```
    pub fn vectors(mut self) -> Self {
        self.vectors = true;
        self
    }

    /// The same options in search-engine syntax (`websearch_to_tsquery`).
    pub fn websearch_mode(mut self) -> Self {
        self.mode = FullTextMode::Websearch;
        self
    }

    /// The same options matching the words as a phrase.
    pub fn phrase_mode(mut self) -> Self {
        self.mode = FullTextMode::Phrase;
        self
    }

    /// The words as a phrase.
    pub fn phrase() -> Self {
        FullText {
            mode: FullTextMode::Phrase,
            ..FullText::default()
        }
    }

    /// Search-engine syntax.
    pub fn websearch() -> Self {
        FullText {
            mode: FullTextMode::Websearch,
            ..FullText::default()
        }
    }

    /// MySQL's boolean mode.
    pub fn boolean() -> Self {
        FullText {
            mode: FullTextMode::Boolean,
            ..FullText::default()
        }
    }

    /// PostgreSQL's text search language.
    pub fn language(mut self, language: impl Into<String>) -> Self {
        self.language = Some(language.into());
        self
    }

    /// MySQL's query expansion.
    pub fn expanded(mut self) -> Self {
        self.expanded = true;
        self
    }
}

/// The right side of `IN`.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum InValues {
    /// A list (empty: `IN` matches nothing, `NOT IN` everything).
    List(Vec<Expr>),
    /// A subquery returning one column.
    Subquery(Box<QueryIr>),
}

/// A table, optionally aliased, a subquery used as a table (`FROM
/// (SELECT ...) AS alias`, `JOIN (SELECT ...) AS alias`), or the rows of a
/// set-returning function (`FROM generate_series(1, 10) AS n`).
#[derive(Clone, Debug)]
pub struct TableRef {
    /// The table name (for a subquery or a function, its alias).
    pub name: Cow<'static, str>,
    /// The alias.
    pub alias: Option<Cow<'static, str>>,
    /// The subquery the rows come from, when the table is a subquery.
    pub subquery: Option<Box<QueryIr>>,
    /// The function call (an [`Expr::Function`]) the rows come from, when
    /// the table is a set-returning function. SQL drivers only.
    pub function: Option<Box<Expr>>,
}

impl TableRef {
    /// The table `name`.
    pub fn new(name: impl Into<Cow<'static, str>>) -> Self {
        TableRef {
            name: name.into(),
            alias: None,
            subquery: None,
            function: None,
        }
    }

    /// The table `name` as `alias`.
    pub fn aliased(
        name: impl Into<Cow<'static, str>>,
        alias: impl Into<Cow<'static, str>>,
    ) -> Self {
        TableRef {
            name: name.into(),
            alias: Some(alias.into()),
            subquery: None,
            function: None,
        }
    }

    /// The rows of `query`, named `alias`.
    pub fn subquery(query: QueryIr, alias: impl Into<Cow<'static, str>>) -> Self {
        let alias = alias.into();
        TableRef {
            name: alias.clone(),
            alias: Some(alias),
            subquery: Some(Box::new(query)),
            function: None,
        }
    }

    /// The rows the set-returning function call `call` (an
    /// [`Expr::Function`]) returns, named `alias`.
    pub fn function(call: Expr, alias: impl Into<Cow<'static, str>>) -> Self {
        let alias = alias.into();
        TableRef {
            name: alias.clone(),
            alias: Some(alias),
            subquery: None,
            function: Some(Box::new(call)),
        }
    }

    /// The name columns of this table are qualified with: the alias, else
    /// the name.
    pub fn qualifier(&self) -> &str {
        self.alias.as_deref().unwrap_or(&self.name)
    }
}

/// `DISTINCT`.
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub enum Distinct {
    /// No `DISTINCT`.
    #[default]
    No,
    /// `SELECT DISTINCT`.
    Yes,
    /// PostgreSQL's `SELECT DISTINCT ON (...)`.
    On(Vec<Expr>),
}

/// One item of a `SELECT` list.
#[derive(Clone, Debug)]
pub struct SelectItem {
    /// The expression.
    pub expr: Expr,
    /// `AS alias`.
    pub alias: Option<Cow<'static, str>>,
}

/// The kind of a join.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum JoinKind {
    /// `INNER JOIN`.
    Inner,
    /// `LEFT JOIN`.
    Left,
    /// `RIGHT JOIN`.
    Right,
    /// `CROSS JOIN`.
    Cross,
    /// `INNER JOIN LATERAL (subquery) AS alias ON true`: the subquery
    /// runs once per row and may use its columns (PostgreSQL, MySQL
    /// 8.0.14+).
    Lateral,
    /// `LEFT JOIN LATERAL (subquery) AS alias ON true`.
    LeftLateral,
}

/// At most [`limit`](GroupLimit::limit) rows per value of
/// [`column`](GroupLimit::column) (top N per group), in the query's order:
/// `ROW_NUMBER() OVER (PARTITION BY column ORDER BY ..)` in a derived
/// table. The query's offset skips rows per group; its limit caps the whole
/// result.
#[derive(Clone, Debug)]
pub struct GroupLimit {
    /// The most rows per group.
    pub limit: u64,
    /// The column (or expression) whose values form the groups.
    pub column: Expr,
}

/// A join.
#[derive(Clone, Debug)]
pub struct Join {
    /// The kind.
    pub kind: JoinKind,
    /// The joined table.
    pub table: TableRef,
    /// The `ON` conditions (empty for cross joins).
    pub on: Vec<WhereClause>,
}

/// A sort direction.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Direction {
    /// Ascending.
    Asc,
    /// Descending.
    Desc,
}

impl Direction {
    /// The opposite direction.
    pub fn reversed(self) -> Self {
        match self {
            Direction::Asc => Direction::Desc,
            Direction::Desc => Direction::Asc,
        }
    }
}

/// Where NULLs sort.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Nulls {
    /// NULLs first.
    First,
    /// NULLs last.
    Last,
}

/// One `ORDER BY` item.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Order {
    /// An expression and a direction.
    By {
        /// The expression.
        expr: Expr,
        /// The direction.
        direction: Direction,
        /// Where NULLs sort; the database's default when `None`.
        nulls: Option<Nulls>,
    },
    /// Random order.
    Random,
    /// Raw SQL with `?` placeholders (`??` is a literal `?`), direction
    /// included. SQL drivers only.
    Raw {
        /// The SQL fragment.
        sql: Cow<'static, str>,
        /// Its values, in order.
        values: Vec<Value>,
    },
}

impl Order {
    /// Raw SQL with `?` placeholders for `values`: `Order::raw("field(stage, ?, ?)", ["won", "lost"])`.
    pub fn raw(
        sql: impl Into<Cow<'static, str>>,
        values: impl IntoIterator<Item = impl Into<Value>>,
    ) -> Self {
        Order::Raw {
            sql: sql.into(),
            values: values.into_iter().map(Into::into).collect(),
        }
    }

    /// `expr ASC`.
    pub fn asc(expr: impl Into<Expr>) -> Self {
        Order::By {
            expr: expr.into(),
            direction: Direction::Asc,
            nulls: None,
        }
    }

    /// `expr DESC`.
    pub fn desc(expr: impl Into<Expr>) -> Self {
        Order::By {
            expr: expr.into(),
            direction: Direction::Desc,
            nulls: None,
        }
    }

    /// The same order with NULLs first.
    pub fn nulls_first(self) -> Self {
        self.with_nulls(Nulls::First)
    }

    /// The same order with NULLs last.
    pub fn nulls_last(self) -> Self {
        self.with_nulls(Nulls::Last)
    }

    fn with_nulls(self, which: Nulls) -> Self {
        match self {
            Order::By {
                expr, direction, ..
            } => Order::By {
                expr,
                direction,
                nulls: Some(which),
            },
            other => other,
        }
    }
}

/// A row lock on the rows a query reads, held until its transaction ends:
/// a [`LockMode`] and what to do about rows another transaction holds
/// ([`LockWait`]).
///
/// | | PostgreSQL | MySQL | SQLite |
/// |---|---|---|---|
/// | [`Update`](LockMode::Update) | `FOR UPDATE` | `FOR UPDATE` | none |
/// | [`NoKeyUpdate`](LockMode::NoKeyUpdate) | `FOR NO KEY UPDATE` | `FOR UPDATE` | none |
/// | [`Share`](LockMode::Share) | `FOR SHARE` | `LOCK IN SHARE MODE` (`FOR SHARE` with `NOWAIT` or `SKIP LOCKED`) | none |
/// | [`KeyShare`](LockMode::KeyShare) | `FOR KEY SHARE` | as `Share` | none |
///
/// SQLite has no row locks: its write transactions are serialised
/// (navree-db begins them `IMMEDIATE`), so the clause is dropped there.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Lock {
    /// What the lock keeps other transactions from doing.
    pub mode: LockMode,
    /// What happens to rows another transaction has locked.
    pub wait: LockWait,
}

impl Lock {
    /// A lock of `mode` that waits for rows other transactions hold.
    pub const fn new(mode: LockMode) -> Self {
        Lock {
            mode,
            wait: LockWait::Wait,
        }
    }
}

/// What a row lock keeps other transactions from doing.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum LockMode {
    /// Changing or locking the rows (`FOR UPDATE`): read them, then update
    /// them in the same transaction.
    Update,
    /// Like [`Update`](LockMode::Update), without blocking `KeyShare`
    /// locks (foreign keys pointing at the rows); for updates that leave
    /// the key alone (`FOR NO KEY UPDATE`, PostgreSQL; `FOR UPDATE`
    /// elsewhere).
    NoKeyUpdate,
    /// Changing the rows; other shared locks are allowed (`FOR SHARE`).
    Share,
    /// Deleting the rows or changing their key (`FOR KEY SHARE`,
    /// PostgreSQL; a shared lock elsewhere).
    KeyShare,
}

/// What a locking read does with rows another transaction has locked.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum LockWait {
    /// Wait until they are released (the database's lock timeout applies).
    #[default]
    Wait,
    /// Fail at once (`NOWAIT`) with the database's lock error.
    NoWait,
    /// Leave them out of the result (`SKIP LOCKED`): workers claiming jobs
    /// or outbox entries never wait for each other.
    SkipLocked,
}

/// A `UNION`.
#[derive(Clone, Debug)]
pub struct Union {
    /// The other query.
    pub query: Box<QueryIr>,
    /// `UNION ALL`.
    pub all: bool,
}

/// An aggregate over a query's rows: `count(*)`, `sum(amount)`, ...
#[derive(Clone, Debug)]
pub struct Aggregate {
    /// The function.
    pub function: AggregateFunction,
    /// The argument; `None` is `*`.
    pub arg: Option<Expr>,
    /// `DISTINCT` inside the call.
    pub distinct: bool,
}

/// A read query.
#[derive(Clone, Debug)]
pub struct QueryIr {
    /// The table (a collection for document drivers).
    pub table: TableRef,
    /// `DISTINCT`.
    pub distinct: Distinct,
    /// The selected items; empty selects every column.
    pub columns: Vec<SelectItem>,
    /// Joins, in order.
    pub joins: Vec<Join>,
    /// `WHERE` clauses.
    pub wheres: Vec<WhereClause>,
    /// `GROUP BY` expressions.
    pub groups: Vec<Expr>,
    /// `HAVING` clauses.
    pub havings: Vec<WhereClause>,
    /// `ORDER BY` items.
    pub orders: Vec<Order>,
    /// `LIMIT`.
    pub limit: Option<u64>,
    /// `OFFSET`.
    pub offset: Option<u64>,
    /// A row lock.
    pub lock: Option<Lock>,
    /// `UNION`s.
    pub unions: Vec<Union>,
    /// At most so many rows per group (top N per parent).
    pub group_limit: Option<GroupLimit>,
    /// The connection the query runs on (`on_connection`), whatever its
    /// model's or the executor's (see [`Route`](crate::Route)).
    pub connection: Option<Cow<'static, str>>,
    /// Reads go to the writer even where read replicas exist
    /// (`use_write`).
    pub use_writer: bool,
    /// The longest the database may run the query's reads (`timeout`).
    pub timeout: Option<std::time::Duration>,
    /// Problems found while building the query; it fails with them when
    /// it runs.
    pub errors: Vec<String>,
}

impl QueryIr {
    /// A query of every row of `table`.
    pub fn new(table: impl Into<Cow<'static, str>>) -> Self {
        QueryIr {
            table: TableRef::new(table),
            distinct: Distinct::No,
            columns: Vec::new(),
            joins: Vec::new(),
            wheres: Vec::new(),
            groups: Vec::new(),
            havings: Vec::new(),
            orders: Vec::new(),
            limit: None,
            offset: None,
            lock: None,
            unions: Vec::new(),
            group_limit: None,
            connection: None,
            use_writer: false,
            timeout: None,
            errors: Vec::new(),
        }
    }

    /// Where the query asks to run: its connection (`on_connection`),
    /// `model`'s connection, and whether reads go to the writer.
    pub fn route(&self, model: Option<&'static str>) -> crate::Route<'_> {
        crate::Route::new()
            .on(self.connection.as_deref())
            .for_model(model)
            .writer(self.use_writer)
    }

    /// The first problem found while building, as an error.
    pub fn check(&self) -> Result<(), crate::OrmError> {
        match self.errors.first() {
            Some(error) => Err(crate::OrmError::invalid(error.clone())),
            None => Ok(()),
        }
    }
}

/// An `INSERT` of one or more rows, or of the rows of a query.
#[derive(Clone, Debug)]
pub struct InsertIr {
    /// The table.
    pub table: Cow<'static, str>,
    /// The columns, in the order of each row's values.
    pub columns: Vec<Cow<'static, str>>,
    /// The rows (empty when [`select`](InsertIr::select) provides them).
    pub rows: Vec<Vec<Expr>>,
    /// Columns to read back from the inserted rows (generated keys).
    pub returning: Vec<Cow<'static, str>>,
    /// A query whose rows are inserted (`INSERT … SELECT`) instead of
    /// [`rows`](InsertIr::rows); it selects one value per column.
    pub select: Option<Box<QueryIr>>,
    /// What happens to a row that conflicts with a unique index; `None`
    /// fails the statement.
    pub on_conflict: Option<OnConflict>,
}

impl InsertIr {
    /// An insert of `rows` (each with one value per column) into `table`,
    /// returning nothing and failing on conflicts.
    pub fn new(
        table: impl Into<Cow<'static, str>>,
        columns: Vec<Cow<'static, str>>,
        rows: Vec<Vec<Expr>>,
    ) -> Self {
        InsertIr {
            table: table.into(),
            columns,
            rows,
            returning: Vec::new(),
            select: None,
            on_conflict: None,
        }
    }

    /// The same insert, reading `columns` back from the inserted rows.
    pub fn returning(mut self, columns: Vec<Cow<'static, str>>) -> Self {
        self.returning = columns;
        self
    }

    /// The same insert, handling conflicting rows with `on_conflict`.
    pub fn on_conflict(mut self, on_conflict: OnConflict) -> Self {
        self.on_conflict = Some(on_conflict);
        self
    }
}

/// What an insert does with a row that conflicts with a unique index
/// (an "upsert" when it updates).
///
/// | | PostgreSQL, SQLite | MySQL |
/// |---|---|---|
/// | [`Ignore`](OnConflict::Ignore) | `ON CONFLICT [(target)] DO NOTHING` | `INSERT IGNORE` |
/// | [`Update`](OnConflict::Update) | `ON CONFLICT (target) DO UPDATE SET ..` | `ON DUPLICATE KEY UPDATE ..` |
///
/// PostgreSQL and SQLite only skip or update rows that conflict on a
/// unique index (a target names its columns); MySQL looks at every unique
/// index, and its `INSERT IGNORE` also turns other errors (a value that
/// does not fit its column) into warnings.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum OnConflict {
    /// Skip the conflicting rows.
    Ignore {
        /// The unique columns whose conflicts are skipped; empty for any
        /// unique index.
        target: Vec<Cow<'static, str>>,
    },
    /// Update the existing row instead.
    Update {
        /// The unique columns the conflict is on (required on PostgreSQL
        /// and SQLite; MySQL uses every unique index).
        target: Vec<Cow<'static, str>>,
        /// `SET column = expr` on the existing row; [`Expr::Excluded`] is
        /// the value the row would have been inserted with.
        sets: Vec<(Cow<'static, str>, Expr)>,
    },
}

/// An `UPDATE`.
#[derive(Clone, Debug)]
pub struct UpdateIr {
    /// The table.
    pub table: TableRef,
    /// `SET column = expr`, in order.
    pub sets: Vec<(Cow<'static, str>, Expr)>,
    /// `WHERE` clauses.
    pub wheres: Vec<WhereClause>,
}

/// A `DELETE`.
#[derive(Clone, Debug)]
pub struct DeleteIr {
    /// The table.
    pub table: TableRef,
    /// `WHERE` clauses.
    pub wheres: Vec<WhereClause>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn column_refs_parse_tables_and_json_paths() {
        assert_eq!(
            ColumnRef::parse("title"),
            ColumnRef::new(None::<&str>, "title")
        );
        assert_eq!(
            ColumnRef::parse("deals.title"),
            ColumnRef::new(Some("deals"), "title")
        );
        let column = ColumnRef::parse("meta->a->0->b");
        assert_eq!(
            column.path,
            [
                JsonSegment::Key("a".into()),
                JsonSegment::Index(0),
                JsonSegment::Key("b".into())
            ]
        );
        let indexed = ColumnRef::parse("deals.tags[1]");
        assert_eq!(
            (indexed.table.as_deref(), indexed.name.as_ref()),
            (Some("deals"), "tags")
        );
        assert_eq!(indexed.path, [JsonSegment::Index(1)]);
        let nested = ColumnRef::parse("tags[-1]->name");
        assert_eq!(
            nested.path,
            [JsonSegment::Index(-1), JsonSegment::Key("name".into())]
        );
        assert_eq!(column.to_string(), "meta->a->0->b");
        assert_eq!(column.without_path().to_string(), "meta");
    }

    #[test]
    fn operators_parse_case_and_whitespace_insensitively() {
        assert_eq!(Operator::from("  not   LIKE "), Operator::NotLike);
        assert_eq!(Operator::from("@>"), Operator::Postgres("@>"));
        assert_eq!(Operator::from("rlike"), Operator::Regexp);
        assert_eq!(Operator::from("bogus"), Operator::Invalid("bogus".into()));
    }
}