reylo-engine-core 0.9.0

What every database's analysis engine shares: the request protocol and its host, the placeholder lexer, the project file, the type model and its JSON Schema, structured parameters, and the WebAssembly ABI each database's artifact exports.
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
//! Placeholders, read off the raw statement text.
//!
//! One named form works in every dialect, `:name`, which is also the form mysql2's
//! `namedPlaceholders` and SQLite accept; SQLite's own `@name` and `$name` are named placeholders
//! there too. Beside them a statement may use its database's own positional form instead, never
//! both: `$1` on Postgres, `?` on MySQL, `?` and `?NNN` on SQLite. A form the database does not
//! have is refused where the database would misread it: SQLite takes `$1` for a parameter named
//! `$1`, and a Postgres `$name` outside a dollar quote is a syntax error, so each is an error that
//! says what to write; MySQL's `@name` is a user variable and `$` a character of its identifiers,
//! so both stay text there, as `@` (an operator) does on Postgres. A parameter's name drops the
//! prefix: `:id`, `@id` and `$id` are `id`, and `$2` and `?2` are both `2`. SQLite reads `:id` and
//! `@id` as two parameters, so the named placeholders of one statement share one prefix. A bare `?` takes the number after the largest one so far, as SQLite numbers it, so `?, ?`
//! is `1`, `2`. Each parameter appears once in a statement, so that one place types it: a value
//! needed twice is selected once in a `with` and read from there, and every later use of a name
//! is an error at its place ([`repeated`]); a bare `?` is a parameter of its own. A name may be
//! dotted (`:auth.user_id`): each `.segment` starts like a name, so `arr[1:2]` and `:t.*` end
//! where they should. The engine rewrites placeholders to whatever the
//! database binds by position (`$n` on Postgres, `?` on MySQL, `?n` for the SQLite prepare, where
//! SQLite's own `:name` would end at the first dot) and keeps a map back to the author's text, so
//! a server error at a rewritten position lands on the right characters of the `.sql` file.
//!
//! The pass knows what the parser knows about text: single-quoted literals (with `''`), `E''`
//! escape strings (with backslashes), dollar quoting (Postgres's), double-quoted identifiers, line comments,
//! nested block comments, and `::` casts. MySQL reads text its own way, by the session's
//! `sql_mode` ([`MysqlText`]): a backslash in a string escapes the character after it, a
//! double-quoted word is a string, `#` starts a line comment as `-- ` does (`--` followed by
//! anything else is two minus signs), a block comment ends at its first `*/`, and backquotes quote
//! a name. The spans it skipped as text are returned as [`Lexed::opaque`], so a caller that looks
//! for its own construct in the statement does not need a scanner of its own.
//!
//! ClickHouse writes a parameter with its type, `{name:Type}`, and has no other form: a `{` starts
//! nothing else there, and the whole of it, braces included, is the placeholder. `:name` works too
//! where a cast gives it its type, `:name::Type`, which is the only place it is read as one: a `?`
//! is ClickHouse's conditional operator, whose `:` may stand before a name (`c ? a :b`), and a
//! typed JSON path writes one too (`doc.a.:Int64`). Each use of a ClickHouse parameter declares its
//! type, so a name may appear more than once there, and its engine holds the uses to one type. Its
//! names have no dots. Its text is read its way: a backslash escapes in a string and in a name
//! quoted with `"` or backquotes, `#` starts a line comment before a space or `!`, a block comment
//! nests, and `$tag$…$tag$` quotes a string as on Postgres.

use crate::dialect::Dialect;
use serde::Serialize;

/// One placeholder occurrence, by byte offsets in the original text.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Placeholder {
    /// The parameter name: the author's for a named placeholder (dots included, prefix dropped),
    /// and the position's number for a positional one (`$3` and SQLite's `?3` are both `3`; a
    /// bare `?` is one past the largest number before it).
    pub name: String,
    pub start: usize,
    pub end: usize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Style {
    None,
    Named,
    Positional,
    Mixed,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Lexed {
    /// Distinct parameter names in binding order: first appearance for named parameters, and
    /// by number for positional ones, so `params[k]` is the statement's `$(k + 1)`.
    pub params: Vec<String>,
    pub placeholders: Vec<Placeholder>,
    pub style: Style,
    /// Every span the walk read as text rather than code, `(start, end)` in byte offsets, in
    /// source order and never overlapping: string literals (an `E''` string from its `E`), dollar
    /// quotes, quoted identifiers, line comments (up to their newline) and block comments.
    pub opaque: Vec<(usize, usize)>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct LexError {
    pub message: String,
    pub offset: usize,
}

impl std::fmt::Display for LexError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} (at byte {})", self.message, self.offset)
    }
}

impl std::error::Error for LexError {}

/// The placeholder syntax a rewrite emits.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Target {
    /// `$n`, numbered by binding order.
    Dollar,
    /// `?`, one per occurrence, in text order; the binding order repeats a name where it repeats.
    Question,
    /// `?n`, numbered by binding order (the parameter's 1-based index in [`Lexed::params`]), so a
    /// repeated name is one position. An author's `$1 ... $n` keeps its numbers; unlike
    /// [`Target::Dollar`], a gap closes (`$3` alone is `?1`): the binding order is `params` either
    /// way, and SQLite reads `?n` as position `n`.
    Numbered,
    /// `{name}`, ClickHouse's own form less its type, which the driver binds by name: the
    /// ClickHouse engine writes each one's type through [`rewrite_with`]. One entry of the binding
    /// order per distinct name.
    Braced,
}

/// A rewritten statement plus the map back to the author's text.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Rewritten {
    pub sql: String,
    /// The binding order: for `?` one entry per occurrence, for `$n` and `?n` one per distinct name.
    pub order: Vec<String>,
    /// Per placeholder in text order: `(original start, original end, rewritten start, rewritten end)`.
    pub spans: Vec<(usize, usize, usize, usize)>,
    /// Text the rewrite adds that the original does not hold, in text order: `(original offset,
    /// rewritten start, rewritten end)`, as the collation a MySQL statement's own text needs
    /// (the MySQL engine's `mysql_collation`).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub inserted: Vec<(usize, usize, usize)>,
}

impl Rewritten {
    /// The byte offset in the original text that a byte offset in the rewritten text refers to.
    pub fn to_original(&self, pos: usize) -> usize {
        let mut delta: isize = 0;
        let mut regions = self.spans.clone();
        regions.extend(self.inserted.iter().map(|&(at, rs, re)| (at, at, rs, re)));
        regions.sort_by_key(|&(_, _, rs, _)| rs);
        for (os, oe, rs, re) in regions {
            if pos >= re {
                delta += (oe - os) as isize - (re - rs) as isize;
            } else if pos >= rs {
                return os;
            } else {
                break;
            }
        }
        (pos as isize + delta).max(0) as usize
    }
}

fn is_name_start(c: u8) -> bool {
    c.is_ascii_alphabetic() || c == b'_'
}

fn is_name_char(c: u8) -> bool {
    c.is_ascii_alphanumeric() || c == b'_'
}

/// Whether a ClickHouse statement's code from `from`, where a conditional's `?` ends, up to `to`,
/// where a `:` stands, is a whole operand: each parenthesis and bracket it opens is closed, and
/// what it ends with ends an expression, as a name, a number, a string, a placeholder and a
/// closing bracket do, and an operator, a comma, an opening bracket and a word that an operand
/// follows do not. `None` where it closes a bracket it did not open: the `?` stands in brackets
/// that end before the `:`.
fn whole_operand(sql: &str, opaque: &[(usize, usize)], from: usize, to: usize) -> Option<bool> {
    // The words an operand follows.
    const BEFORE_AN_OPERAND: [&str; 21] = ["and", "or", "not", "in", "like", "ilike", "between", "is", "then", "else", "when", "case", "div", "mod", "select", "where", "having", "by", "on", "interval", "distinct"];
    let b = sql.as_bytes();
    let (mut at, mut depth, mut ended) = (from, 0usize, false);
    while at < to {
        // A comment is no part of the operand; a string, a quoted name and a heredoc end one.
        if let Some(&(_, end)) = opaque.iter().find(|&&(start, _)| start == at) {
            ended = ended || !(sql[at..].starts_with("--") || sql[at..].starts_with("/*") || sql[at..].starts_with('#'));
            at = end;
            continue;
        }
        let c = b[at];
        match c {
            _ if c.is_ascii_whitespace() => {}
            _ if is_name_char(c) => {
                // A name or a number, whole: a `$` is a character of a ClickHouse name past its first.
                let end = (at..b.len()).find(|&i| !(is_name_char(b[i]) || b[i] == b'$')).unwrap_or(b.len());
                ended = !BEFORE_AN_OPERAND.iter().any(|word| sql[at..end].eq_ignore_ascii_case(word));
                at = end;
                continue;
            }
            // A placeholder in braces, whole.
            b'{' => match braces_end(b, at + 1) {
                Some(close) if close < to => {
                    ended = true;
                    at = close + 1;
                    continue;
                }
                _ => ended = false,
            },
            b'(' | b'[' => {
                depth += 1;
                ended = false;
            }
            b')' | b']' => {
                depth = depth.checked_sub(1)?;
                ended = true;
            }
            _ => ended = false,
        }
        at += 1;
    }
    Some(depth == 0 && ended)
}

fn prev_is_name_char(b: &[u8], i: usize) -> bool {
    i > 0 && is_name_char(b[i - 1])
}

fn name_end(b: &[u8], start: usize) -> usize {
    let mut j = start;
    while j < b.len() && is_name_char(b[j]) {
        j += 1;
    }
    j
}

/// A placeholder name from `start`: a name, then any `.segment` that starts like one
/// (`:auth.user_id`). `:t.*` stops before the dot.
fn dotted_name_end(b: &[u8], start: usize) -> usize {
    let mut j = name_end(b, start);
    while b.get(j) == Some(&b'.') && b.get(j + 1).is_some_and(|c| is_name_start(*c)) {
        j = name_end(b, j + 1);
    }
    j
}

/// What the walk read last, for the one place it matters: a Postgres subscript's `[` (after an
/// operand) against an array constructor's (after `array`, or nested in one).
#[derive(Clone, Copy, PartialEq, Eq)]
enum Last {
    Other,
    /// A name other than `array`, a number, a quoted identifier, a placeholder, `)` or `]`.
    Operand,
    /// The `[` of a subscript, with nothing but whitespace and comments read since.
    Subscript,
}

fn digits_end(b: &[u8], start: usize) -> usize {
    let mut j = start;
    while j < b.len() && b[j].is_ascii_digit() {
        j += 1;
    }
    j
}

/// `$tag$` at `i`: the offset one past its closing `$`, when `i` starts a dollar-quote opener.
fn dollar_tag_end(b: &[u8], i: usize) -> Option<usize> {
    if b.get(i) != Some(&b'$') {
        return None;
    }
    let mut j = i + 1;
    if j < b.len() && is_name_start(b[j]) {
        j = name_end(b, j);
    }
    (b.get(j) == Some(&b'$')).then_some(j + 1)
}

fn find_bytes(hay: &[u8], needle: &[u8], from: usize) -> Option<usize> {
    if needle.is_empty() || from >= hay.len() {
        return None;
    }
    hay[from..].windows(needle.len()).position(|w| w == needle).map(|p| p + from)
}

/// How a MySQL session reads strings, by its `sql_mode`. The default is the server's default
/// mode, which is also how the MySQL parser reads them by default.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MysqlText {
    /// A backslash in a string escapes the character after it (`'it\'s'`), unless the mode has
    /// NO_BACKSLASH_ESCAPES.
    pub backslash_escapes: bool,
    /// A double-quoted word is a name, not a string, where the mode has ANSI_QUOTES. A name
    /// escapes nothing with a backslash.
    pub ansi_quotes: bool,
}

impl Default for MysqlText {
    fn default() -> Self {
        MysqlText { backslash_escapes: true, ansi_quotes: false }
    }
}

impl MysqlText {
    /// Every reading a session may have.
    pub const ALL: [MysqlText; 4] = [
        MysqlText { backslash_escapes: true, ansi_quotes: false },
        MysqlText { backslash_escapes: false, ansi_quotes: false },
        MysqlText { backslash_escapes: true, ansi_quotes: true },
        MysqlText { backslash_escapes: false, ansi_quotes: true },
    ];

    /// The reading of a session whose `@@sql_mode` is `mode`. ANSI (and the modes that imply it)
    /// is expanded by the server when it is set, so the two flags are read as they are listed.
    pub fn of_mode(mode: &str) -> MysqlText {
        let set = |name: &str| mode.split(',').any(|m| m.trim().eq_ignore_ascii_case(name));
        MysqlText { backslash_escapes: !set("NO_BACKSLASH_ESCAPES"), ansi_quotes: set("ANSI_QUOTES") }
    }
}

/// From the quote at `i`, the offset one past the closing one, or `None` if the text ends first.
/// A doubled quote is one character; where `escapes`, so is a backslash and what follows it.
fn quoted_end(b: &[u8], i: usize, escapes: bool) -> Option<usize> {
    let quote = b[i];
    let mut j = i + 1;
    loop {
        match b.get(j)? {
            b'\\' if escapes => j += 2,
            &q if q == quote && b.get(j + 1) == Some(&quote) => j += 2,
            &q if q == quote => return Some(j + 1),
            _ => j += 1,
        }
    }
}

/// Which named placeholders a statement may use.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Named {
    /// `:name` everywhere, and SQLite's own `@name` and `$name` there: the loader's rule.
    Dialect,
    /// `:name` alone; SQLite's `@name` and `$name` are errors: the portable spelling alone.
    Colon,
}

pub fn lex(sql: &str, dialect: Dialect) -> Result<Lexed, LexError> {
    lex_with(sql, dialect, Named::Dialect)
}

/// MySQL reads its text by the session's mode; [`lex_with`] reads it as the default mode does.
pub fn lex_with(sql: &str, dialect: Dialect, named: Named) -> Result<Lexed, LexError> {
    lex_text(sql, dialect, named, MysqlText::default())
}

/// A MySQL statement as a session of this reading reads it.
pub fn lex_mysql(sql: &str, named: Named, text: MysqlText) -> Result<Lexed, LexError> {
    lex_text(sql, Dialect::Mysql, named, text)
}

fn lex_text(sql: &str, dialect: Dialect, named: Named, text: MysqlText) -> Result<Lexed, LexError> {
    let sqlite_named = dialect == Dialect::Sqlite && named == Named::Dialect;
    let mysql = dialect == Dialect::Mysql;
    let clickhouse = dialect == Dialect::Clickhouse;
    let b = sql.as_bytes();
    let mut i = 0usize;
    let mut last = Last::Other;
    let mut params: Vec<String> = Vec::new();
    let mut placeholders: Vec<Placeholder> = Vec::new();
    let mut opaque: Vec<(usize, usize)> = Vec::new();
    // The largest position a positional placeholder has named so far; a bare `?` takes the next.
    let mut largest = 0u64;
    // The span of the first named placeholder, whose prefix every later one repeats.
    let mut first_named: Option<(usize, usize)> = None;
    // Where each `?` of a ClickHouse conditional that has not met its `:` ends.
    let mut conditionals: Vec<usize> = Vec::new();
    let mut push = |name: String, start: usize, end: usize, params: &mut Vec<String>| {
        if !params.contains(&name) {
            params.push(name.clone());
        }
        placeholders.push(Placeholder { name, start, end });
    };
    let position = |digits: &str, largest: &mut u64| {
        let n = digits.trim_start_matches('0');
        let n = if n.is_empty() { "0" } else { n };
        *largest = (*largest).max(n.parse().unwrap_or(u64::MAX));
        n.to_string()
    };
    while i < b.len() {
        let c = b[i];
        match c {
            // A MySQL string, unless ANSI_QUOTES makes `"` a name's quote.
            b'\'' | b'"' if c == b'\'' || (mysql && !text.ansi_quotes) => {
                let Some(end) = quoted_end(b, i, (mysql && text.backslash_escapes) || clickhouse) else {
                    return Err(LexError { message: "unterminated string literal".into(), offset: i });
                };
                opaque.push((i, end));
                i = end;
                last = Last::Other;
            }
            b'E' | b'e' if b.get(i + 1) == Some(&b'\'') && !prev_is_name_char(b, i) && !mysql && !clickhouse => {
                let start = i;
                i += 2;
                loop {
                    match b.get(i) {
                        None => return Err(LexError { message: "unterminated escape string".into(), offset: start }),
                        Some(b'\\') => i += 2,
                        Some(b'\'') if b.get(i + 1) == Some(&b'\'') => i += 2,
                        Some(b'\'') => {
                            i += 1;
                            break;
                        }
                        Some(_) => i += 1,
                    }
                }
                opaque.push((start, i));
                last = Last::Other;
            }
            b'"' | b'`' => {
                let Some(end) = quoted_end(b, i, clickhouse) else {
                    return Err(LexError { message: "unterminated quoted identifier".into(), offset: i });
                };
                opaque.push((i, end));
                i = end;
                last = Last::Operand;
            }
            // MySQL's `--` starts a comment only before a space or a control character, or at the
            // end of the text: `:a --:b` is `:a - (-:b)`.
            b'-' if b.get(i + 1) == Some(&b'-') && !(mysql && b.get(i + 2).is_some_and(|&n| n > b' ' && n != 0x7f)) => {
                let start = i;
                while i < b.len() && b[i] != b'\n' {
                    i += 1;
                }
                opaque.push((start, i));
            }
            // ClickHouse's `#` starts a comment only before a space or a `!`.
            b'#' if mysql || (clickhouse && matches!(b.get(i + 1), Some(b' ' | b'!'))) => {
                let start = i;
                while i < b.len() && b[i] != b'\n' {
                    i += 1;
                }
                opaque.push((start, i));
            }
            // A MySQL block comment does not nest: it ends at the first `*/`.
            b'/' if b.get(i + 1) == Some(&b'*') && mysql => {
                let Some(close) = find_bytes(b, b"*/", i + 2) else {
                    return Err(LexError { message: "unterminated block comment".into(), offset: i });
                };
                opaque.push((i, close + 2));
                i = close + 2;
            }
            b'/' if b.get(i + 1) == Some(&b'*') => {
                let start = i;
                let mut depth = 1usize;
                i += 2;
                while depth > 0 {
                    match (b.get(i), b.get(i + 1)) {
                        (Some(b'/'), Some(b'*')) => {
                            depth += 1;
                            i += 2;
                        }
                        (Some(b'*'), Some(b'/')) => {
                            depth -= 1;
                            i += 2;
                        }
                        (Some(_), _) => i += 1,
                        (None, _) => return Err(LexError { message: "unterminated block comment".into(), offset: start }),
                    }
                }
                opaque.push((start, i));
            }
            b':' => {
                if b.get(i + 1) == Some(&b':') {
                    // A `::` cast: the type name after it is not a parameter.
                    i += 2;
                    if i < b.len() && is_name_start(b[i]) {
                        i = name_end(b, i);
                    }
                    last = Last::Operand;
                    continue;
                }
                // The `:` of a ClickHouse conditional, `c ? a : b`: the first after its `?` that
                // a whole operand stands before. A name and a cast may follow it, and are no
                // parameter (`c ? a :b::String`); a `:` inside the operand opens one
                // (`c ? abs(:n::Int32) : 0`, `c ? 1 + :n::Int32 : 0`, `c ? :n::Int32 : 0`).
                let mut separates = false;
                while let Some(&after) = conditionals.last() {
                    match whole_operand(sql, &opaque, after, i) {
                        // The parentheses the `?` stands in closed before any `:`: it awaits none.
                        None => {
                            conditionals.pop();
                        }
                        Some(whole) => {
                            separates = whole;
                            break;
                        }
                    }
                }
                if separates {
                    conditionals.pop();
                    i += 1;
                    last = Last::Other;
                    continue;
                }
                match b.get(i + 1) {
                    // A colon that opens a Postgres subscript is a slice with no lower bound
                    // (`tags[:n]` is `tags[1:n]`), since no expression starts with a colon there,
                    // so the name after it is a column's; a parameter as the subscript is written
                    // `tags[(:n)]`. An array constructor's `[` (`array[:a, :b]`) follows `array`
                    // or another `[`, not an operand.
                    _ if last == Last::Subscript && dialect == Dialect::Postgres => {
                        i += 1;
                        last = Last::Other;
                    }
                    // On ClickHouse a `:name` is a parameter only where a cast types it
                    // (`:name::Type`): the `:` of its conditional operator may stand before a
                    // name (`c ? a :b`), and so does a typed JSON path's (`doc.a.:Int64`).
                    Some(&n) if is_name_start(n) && !prev_is_name_char(b, i) && clickhouse => {
                        let end = name_end(b, i + 1);
                        let cast = b.get(end) == Some(&b':') && b.get(end + 1) == Some(&b':') && b.get(end + 2).is_some_and(|&c| is_name_start(c));
                        if cast && (i == 0 || b[i - 1] != b'.') {
                            one_prefix(sql, &mut first_named, i, end)?;
                            push(sql[i + 1..end].to_string(), i, end, &mut params);
                            i = end;
                            last = Last::Operand;
                        } else {
                            i += 1;
                            last = Last::Other;
                        }
                    }
                    Some(&n) if is_name_start(n) && !prev_is_name_char(b, i) => {
                        let end = dotted_name_end(b, i + 1);
                        one_prefix(sql, &mut first_named, i, end)?;
                        push(sql[i + 1..end].to_string(), i, end, &mut params);
                        i = end;
                        last = Last::Operand;
                    }
                    _ => {
                        i += 1;
                        last = Last::Other;
                    }
                }
            }
            // ClickHouse's `{name:Type}`, the only thing a `{` starts there: the placeholder is the
            // whole of it, to the `}` that closes it, past the parentheses and strings of its type
            // (`{at:DateTime64(3, 'UTC')}`). One with no name, colon or type is left to the server,
            // whose error says what it expects.
            b'{' if clickhouse => {
                let name_start = skip_spaces(b, i + 1);
                // A ClickHouse name may hold a `$` past its first character.
                let name_stop = if b.get(name_start).is_some_and(|&c| is_name_start(c)) { (name_start..b.len()).find(|&at| !(is_name_char(b[at]) || b[at] == b'$')).unwrap_or(b.len()) } else { name_start };
                let colon = skip_spaces(b, name_stop);
                let close = if name_stop > name_start && b.get(colon) == Some(&b':') && b.get(colon + 1) != Some(&b':') { braces_end(b, colon + 1) } else { None };
                match close {
                    Some(close) if !sql[colon + 1..close].trim().is_empty() => {
                        one_prefix(sql, &mut first_named, i, close + 1)?;
                        push(sql[name_start..name_stop].to_string(), i, close + 1, &mut params);
                        i = close + 1;
                        last = Last::Operand;
                    }
                    _ => {
                        i += 1;
                        last = Last::Other;
                    }
                }
            }
            b'$' => {
                // A `$` inside a ClickHouse name is the name's (`a$b$`), and opens no heredoc.
                let named = clickhouse && (prev_is_name_char(b, i) || (i > 0 && b[i - 1] == b'$'));
                // ClickHouse's heredoc: its tag is whatever stands up to the next `$` (`$1$hi$1$`),
                // and it is one only where the tag closes it later.
                if clickhouse && !named {
                    let tag = find_bytes(b, b"$", i + 1).map(|stop| &b[i..=stop]);
                    if let Some((tag, close)) = tag.and_then(|tag| find_bytes(b, tag, i + tag.len()).map(|close| (tag, close))) {
                        opaque.push((i, close + tag.len()));
                        i = close + tag.len();
                        last = Last::Other;
                        continue;
                    }
                }
                if dialect == Dialect::Postgres {
                    if let Some(tag_end) = dollar_tag_end(b, i) {
                        let tag = &b[i..tag_end];
                        match find_bytes(b, tag, tag_end) {
                            Some(close) => {
                                opaque.push((i, close + tag.len()));
                                i = close + tag.len();
                                last = Last::Other;
                                continue;
                            }
                            None => {
                                return Err(LexError {
                                    message: format!("unterminated dollar-quoted string {}", sql[i..tag_end].to_string()),
                                    offset: i,
                                })
                            }
                        }
                    }
                }
                match b.get(i + 1) {
                    // `a$1` and `a$b` are identifiers.
                    _ if prev_is_name_char(b, i) => {
                        i += 1;
                        last = Last::Operand;
                    }
                    Some(&n) if n.is_ascii_digit() && dialect == Dialect::Postgres => {
                        let end = digits_end(b, i + 1);
                        push(position(&sql[i + 1..end], &mut largest), i, end, &mut params);
                        i = end;
                        last = Last::Operand;
                    }
                    Some(&n) if is_name_start(n) && sqlite_named => {
                        let end = dotted_name_end(b, i + 1);
                        one_prefix(sql, &mut first_named, i, end)?;
                        push(sql[i + 1..end].to_string(), i, end, &mut params);
                        i = end;
                        last = Last::Operand;
                    }
                    Some(&n) if (is_name_start(n) || n.is_ascii_digit()) && dialect != Dialect::Mysql => {
                        return Err(foreign(sql, i, dialect));
                    }
                    _ => {
                        i += 1;
                        last = Last::Other;
                    }
                }
            }
            b'@' => match b.get(i + 1) {
                Some(&n) if is_name_start(n) && !prev_is_name_char(b, i) && sqlite_named => {
                    let end = dotted_name_end(b, i + 1);
                    one_prefix(sql, &mut first_named, i, end)?;
                    push(sql[i + 1..end].to_string(), i, end, &mut params);
                    i = end;
                    last = Last::Operand;
                }
                Some(&n) if is_name_start(n) && !prev_is_name_char(b, i) && dialect == Dialect::Sqlite => {
                    return Err(foreign(sql, i, dialect));
                }
                _ => {
                    i += 1;
                    last = Last::Other;
                }
            },
            // ClickHouse's `?` is its conditional operator (`c ? a : b`), and it binds by name alone.
            b'?' if clickhouse => {
                i += 1;
                conditionals.push(i);
                last = Last::Other;
            }
            b'?' if dialect != Dialect::Postgres => {
                if b.get(i + 1).is_some_and(u8::is_ascii_digit) && dialect == Dialect::Mysql {
                    let end = digits_end(b, i + 1);
                    return Err(LexError { message: format!("MySQL has no numbered placeholder {}; write ? for each value, or :name", &sql[i..end]), offset: i });
                }
                if b.get(i + 1).is_some_and(u8::is_ascii_digit) {
                    // SQLite's `?NNN` names position NNN, as `$NNN` does, so it takes that name.
                    let end = digits_end(b, i + 1);
                    push(position(&sql[i + 1..end], &mut largest), i, end, &mut params);
                    i = end;
                } else {
                    largest = largest.saturating_add(1);
                    push(largest.to_string(), i, i + 1, &mut params);
                    i += 1;
                }
                last = Last::Operand;
            }
            // Whitespace leaves what was read last as it was, as a comment does.
            _ if c.is_ascii_whitespace() => i += 1,
            // A name or a number, whole: `array` opens a constructor, anything else is an operand.
            _ if is_name_char(c) => {
                let end = name_end(b, i);
                last = if sql[i..end].eq_ignore_ascii_case("array") { Last::Other } else { Last::Operand };
                i = end;
            }
            b')' | b']' => {
                i += 1;
                last = Last::Operand;
            }
            // SQLite's `[name]` is a quoted identifier, which ends at the first `]`: a quote or a
            // `?` inside it is part of the name.
            b'[' if dialect == Dialect::Sqlite => {
                let Some(close) = b[i..].iter().position(|&c| c == b']') else {
                    return Err(LexError { message: "unterminated quoted identifier".into(), offset: i });
                };
                opaque.push((i, i + close + 1));
                i += close + 1;
                last = Last::Operand;
            }
            b'[' => {
                i += 1;
                last = if last == Last::Operand { Last::Subscript } else { Last::Other };
            }
            _ => {
                i += 1;
                last = Last::Other;
            }
        }
    }
    let positional = params.iter().filter(|p| is_positional(p)).count();
    let style = match (positional, params.len()) {
        (_, 0) => Style::None,
        (0, _) => Style::Named,
        (p, n) if p == n => Style::Positional,
        _ => Style::Mixed,
    };
    if style == Style::Positional {
        // `$2 ... $1` binds `$1` first: the describe reports the parameters by number.
        params.sort_by_key(|p| p.parse::<u64>().unwrap_or(u64::MAX));
    }
    Ok(Lexed { params, placeholders, style, opaque })
}

/// The offset of the first byte at or after `i` that is no whitespace.
fn skip_spaces(b: &[u8], mut i: usize) -> usize {
    while i < b.len() && b[i].is_ascii_whitespace() {
        i += 1;
    }
    i
}

/// The offset of the `}` that closes a ClickHouse placeholder whose type starts at `from`: the
/// first one outside the type's parentheses and its strings, in which a backslash escapes.
fn braces_end(b: &[u8], from: usize) -> Option<usize> {
    let mut depth = 0usize;
    let mut j = from;
    while j < b.len() {
        match b[j] {
            b'\'' | b'"' | b'`' => j = quoted_end(b, j, true)?,
            b'(' => {
                depth += 1;
                j += 1;
            }
            b')' => {
                depth = depth.checked_sub(1)?;
                j += 1;
            }
            b'}' if depth == 0 => return Some(j),
            b'{' | b'}' | b';' => return None,
            _ => j += 1,
        }
    }
    None
}

/// SQLite reads `:a`, `@a` and `$a` as three parameters, and a name here drops its prefix, so every
/// named placeholder of a statement repeats the prefix of the first.
fn one_prefix(sql: &str, first: &mut Option<(usize, usize)>, start: usize, end: usize) -> Result<(), LexError> {
    match *first {
        None => {
            *first = Some((start, end));
            Ok(())
        }
        Some((s, _)) if sql.as_bytes()[s] == sql.as_bytes()[start] => Ok(()),
        // ClickHouse's two forms, of which neither is the other with another prefix.
        Some((s, e)) if sql.as_bytes()[s] == b'{' || sql.as_bytes()[start] == b'{' => Err(LexError {
            message: format!("{} and {} are written two ways; write every parameter of a statement one way, as {{name:Type}} or as :name::Type", &sql[start..end], &sql[s..e]),
            offset: start,
        }),
        Some((s, e)) => Err(LexError {
            message: format!(
                "{} and {} have different prefixes; write every named parameter of a statement with one prefix, here {}{}",
                &sql[start..end],
                &sql[s..e],
                &sql[s..s + 1],
                &sql[start + 1..end]
            ),
            offset: start,
        }),
    }
}

/// The error for a `$name`, `$1` or `@name` the dialect would misread: Reylo's named placeholder is
/// `:name` (and, outside [`Named::Colon`], SQLite's own `@name` and `$name`), and SQLite's
/// numbered one `?1`.
fn foreign(sql: &str, i: usize, dialect: Dialect) -> LexError {
    let b = sql.as_bytes();
    let digits = b.get(i + 1).is_some_and(u8::is_ascii_digit);
    let end = if digits { digits_end(b, i + 1) } else { dotted_name_end(b, i + 1) };
    let written = &sql[i..end];
    let fix = if digits { format!("?{}", &sql[i + 1..end]) } else { format!(":{}", &sql[i + 1..end]) };
    let message = match dialect {
        Dialect::Sqlite => format!("SQLite reads {written} as a parameter that Reylo does not bind; write {fix}"),
        Dialect::Clickhouse => format!("{written} is not a placeholder on ClickHouse, which writes a parameter with its type; write {{{}:Type}}", if digits { format!("p{}", &sql[i + 1..end]) } else { sql[i + 1..end].to_string() }),
        _ => format!("{written} is not a placeholder here; Reylo's named placeholder is {fix}"),
    };
    LexError { message, offset: i }
}

/// One use of a parameter after its first, by byte offsets in the original text.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Repeated {
    pub message: String,
    pub start: usize,
    pub end: usize,
}

impl From<Repeated> for LexError {
    fn from(r: Repeated) -> LexError {
        LexError { message: r.message, offset: r.start }
    }
}

/// Every use of a parameter after its first, each with the error that says so. Each parameter
/// appears once in a statement, so that one place types it:
/// a value read twice is selected once in a `with` and read from there. The rule holds for every
/// form a parameter is named by: `:name` (a dotted global such as `:auth.user_id` too), SQLite's
/// `@name` and `$name`, and the positional `$n` and `?n`, whose name is the number, so `?1` after
/// a bare `?` that took 1 repeats it. A bare `?` is a parameter of its own and never repeats.
/// Reylo's analysis reports each one where it is written, and a host's build may fail on it.
pub fn repeated(sql: &str, lexed: &Lexed) -> Vec<Repeated> {
    let mut seen: Vec<&str> = Vec::new();
    let mut out = Vec::new();
    for p in &lexed.placeholders {
        if !seen.contains(&p.name.as_str()) {
            seen.push(&p.name);
            continue;
        }
        let written = &sql[p.start..p.end];
        // The name the example selects the value as: the last segment of a dotted name, and `pn`
        // for a position.
        let column = if is_positional(&p.name) { format!("p{}", p.name) } else { p.name.rsplit('.').next().unwrap_or(&p.name).to_string() };
        // The example casts the value, since nothing else types a parameter a select list reads:
        // Postgres takes it as text, MySQL as a string, and SQLite leaves it untyped.
        out.push(Repeated {
            message: format!(
                "The parameter {written} appears more than once; use each parameter once, and select it in a with to read it twice, cast to its type, as in: with input as (select {written}::<type> as {column}) select … from input"
            ),
            start: p.start,
            end: p.end,
        });
    }
    out
}

/// Whether a parameter name is one the lexer minted for a positional placeholder: a number,
/// where a named one starts with a letter or `_`.
pub fn is_positional(name: &str) -> bool {
    !name.is_empty() && name.bytes().all(|c| c.is_ascii_digit())
}

/// Rewrite every placeholder to the target's syntax.
pub fn rewrite(sql: &str, lexed: &Lexed, target: Target) -> Rewritten {
    rewrite_with(sql, lexed, target, &|_, rendered| rendered.to_string())
}

/// Rewrite with a hook over each rendered placeholder — a probe pins a cast on it.
pub fn rewrite_with(sql: &str, lexed: &Lexed, target: Target, decorate: &dyn Fn(&str, &str) -> String) -> Rewritten {
    let mut out = String::with_capacity(sql.len());
    let mut spans = Vec::with_capacity(lexed.placeholders.len());
    let mut order: Vec<String> = Vec::new();
    let mut last = 0;
    for p in &lexed.placeholders {
        let rendered = match target {
            Target::Dollar => {
                if is_positional(&p.name) {
                    format!("${}", p.name)
                } else {
                    format!("${}", lexed.params.iter().position(|q| q == &p.name).map_or(0, |i| i + 1))
                }
            }
            Target::Question => "?".to_string(),
            Target::Numbered => format!("?{}", lexed.params.iter().position(|q| q == &p.name).map_or(0, |i| i + 1)),
            Target::Braced => format!("{{{}}}", p.name),
        };
        let rendered = decorate(&p.name, &rendered);
        out.push_str(&sql[last..p.start]);
        let rs = out.len();
        out.push_str(&rendered);
        spans.push((p.start, p.end, rs, out.len()));
        last = p.end;
        if target == Target::Question {
            order.push(p.name.clone());
        }
    }
    out.push_str(&sql[last..]);
    if target != Target::Question {
        order = lexed.params.clone();
    }
    Rewritten { sql: out, order, spans, inserted: Vec::new() }
}

/// The first keywords of the statements SQLite and MySQL know that are not queries: DDL,
/// transaction control, session settings and the like. A first word outside this list and
/// outside the typed kinds is left to the database, whose syntax error names it (`selec`).
const NOT_QUERIES: &[&str] = &[
    "alter", "analyze", "attach", "begin", "binlog", "cache", "call", "change", "check", "checksum", "clone", "commit", "create", "deallocate",
    "describe", "desc", "detach", "do", "drop", "end", "execute", "explain", "flush", "get", "grant", "handler", "help", "import", "install",
    "kill", "load", "lock", "optimize", "pragma", "prepare", "purge", "reindex", "release", "rename", "repair", "reset", "resignal", "restart",
    "revoke", "rollback", "savepoint", "set", "show", "shutdown", "signal", "start", "stop", "truncate", "uninstall", "unlock", "use",
    "vacuum", "xa",
];

/// How a file the type check refuses (a migration or a schema file, which nothing imports, but
/// which tsconfig.json's `include` hands to the content mapper all the same) is left out of it.
const LEAVE_OUT: &str = "so if it is a migration or a schema file, leave it out of the type check with \"exclude\" in tsconfig.json, as in \"exclude\": [\"db/**\"]";

/// The error of a file that holds several statements, on every database.
pub fn several_statements() -> String {
    format!("A typed .sql file holds one statement and this one holds several, {LEAVE_OUT}.")
}

/// The database's error on a command ([`Statement::Command`]), with how to leave the file out of
/// the type check: a migration's `CREATE TABLE` prepares against a schema that already has the
/// table on SQLite, and fails there.
pub fn refused_command(message: &str) -> String {
    format!("{}, {LEAVE_OUT}.", message.trim_end().trim_end_matches('.'))
}

/// What the one statement of a typed file is.
#[derive(Debug, PartialEq, Eq)]
pub enum Statement {
    /// One of the kinds the database's analysis reads, or a first word it leaves to the database.
    Query,
    /// Any other statement, by its first keyword, lowercased: DDL, a session setting, a
    /// transaction's control, `EXPLAIN`. It is typed from the database's describe alone.
    Command(String),
}

/// The first word of the text past whitespace and comments, lowercased.
pub fn first_word(sql: &str, lexed: &Lexed) -> String {
    let start = past_comments(sql, lexed, 0);
    sql[start..].chars().take_while(|c| c.is_ascii_alphabetic()).collect::<String>().to_ascii_lowercase()
}

/// The first byte at or after `i` that is neither whitespace nor inside a comment.
fn past_comments(sql: &str, lexed: &Lexed, mut i: usize) -> usize {
    let bytes = sql.as_bytes();
    let is_comment = |start: usize| sql[start..].starts_with("--") || sql[start..].starts_with("/*") || sql[start..].starts_with('#');
    loop {
        while i < bytes.len() && bytes[i].is_ascii_whitespace() {
            i += 1;
        }
        match lexed.opaque.iter().find(|&&(start, _)| start == i && is_comment(start)) {
            Some(&(_, end)) => i = end,
            None => return i,
        }
    }
}

/// Whether the text is one statement, and of which kind: a query when its first keyword (past
/// whitespace and comments) is one of `kinds`, or an opening parenthesis, and a command when it is
/// another statement's. Nothing but whitespace, semicolons and comments may follow its first
/// semicolon outside the text spans: SQLite prepares only the first statement of the text, so a
/// migration file would otherwise come back typed from its first statement alone; Postgres's
/// parse refuses several (`AnalyzeError` in `analyze.rs`).
pub fn one_statement(sql: &str, lexed: &Lexed, kinds: &[&str]) -> Result<Statement, String> {
    let bytes = sql.as_bytes();
    // A `;` inside a placeholder is its type's, as in ClickHouse's `{e:Enum8('a;b' = 1)}`.
    let in_text = |i: usize| lexed.opaque.iter().any(|&(start, end)| start <= i && i < end) || lexed.placeholders.iter().any(|p| p.start <= i && i < p.end);
    if let Some(semicolon) = (0..bytes.len()).find(|&i| bytes[i] == b';' && !in_text(i)) {
        let mut i = semicolon + 1;
        loop {
            i = past_comments(sql, lexed, i);
            if i < bytes.len() && bytes[i] == b';' {
                i += 1;
            } else {
                break;
            }
        }
        if i < bytes.len() {
            return Err(several_statements());
        }
    }
    let word = first_word(sql, lexed);
    if kinds.contains(&word.as_str()) || sql[past_comments(sql, lexed, 0)..].starts_with('(') || !NOT_QUERIES.contains(&word.as_str()) {
        return Ok(Statement::Query);
    }
    Ok(Statement::Command(word))
}

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

    fn names(sql: &str, d: Dialect) -> Vec<String> {
        lex(sql, d).unwrap().params
    }

    #[test]
    fn styles() {
        assert_eq!(names("select :a, :b, :a", Dialect::Postgres), ["a", "b"]);
        assert_eq!(names("select $1, $2, $1", Dialect::Postgres), ["1", "2"]);
        assert_eq!(names("select ?, ?", Dialect::Sqlite), ["1", "2"]);
        assert_eq!(names("select ?, ?", Dialect::Mysql), ["1", "2"]);
        assert_eq!(lex("select :a, $1", Dialect::Postgres).unwrap().style, Style::Mixed);
        assert_eq!(lex("select 1", Dialect::Postgres).unwrap().style, Style::None);
    }

    #[test]
    fn skips_text_and_casts() {
        let sql = "select ':x' as a, \":y\" as b, e'\\':z' as c, $$:w$$ as d, x::int, arr[1:2], tags ? 'k' -- :c\n /* :d /* :e */ */ from t where id = :id";
        assert_eq!(names(sql, Dialect::Postgres), ["id"]);
        assert_eq!(names("select json_extract(j, '$.a') = ? and x = :n", Dialect::Sqlite), ["1", "n"]);
        // SQLite's bracketed name is text, whatever it holds; elsewhere a `[` opens a subscript.
        assert_eq!(names("select [a'b] as x, :p as p, [c'd?] as y, [n:m] from t", Dialect::Sqlite), ["p"]);
        assert_eq!(lex("select [a from t", Dialect::Sqlite).unwrap_err().offset, 7);
        assert_eq!(names("select `a:b`, 'x' from t where a = :a", Dialect::Mysql), ["a"]);
    }

    #[test]
    fn one_named_form_and_the_native_positional_one() {
        // A form the dialect would misread is an error that says what to write.
        let err = |sql: &str, d: Dialect| lex(sql, d).unwrap_err();
        assert_eq!(err("select $name", Dialect::Postgres).message, "$name is not a placeholder here; Reylo's named placeholder is :name");
        assert_eq!(err("select 1 where x = $2", Dialect::Sqlite).message, "SQLite reads $2 as a parameter that Reylo does not bind; write ?2");
        assert_eq!(err("select ?2", Dialect::Mysql).message, "MySQL has no numbered placeholder ?2; write ? for each value, or :name");
        assert_eq!(err("select $name", Dialect::Postgres).offset, 7);
        // Where the dialect reads the form as something else, it is that: MySQL's user variable and
        // `$` identifier, Postgres's `@` operator and `a$1` identifier.
        assert!(names("select @x := 1, $col, $1 from t", Dialect::Mysql).is_empty());
        assert!(names("select @ -5, a$1, b$c from t", Dialect::Postgres).is_empty());
        // A `-- @returns one` line is a comment like any other.
        assert!(names("-- @returns one\nselect 1", Dialect::Postgres).is_empty());
    }

    #[test]
    fn clickhouse_writes_a_parameter_with_its_type() {
        let l = |sql: &str| lex(sql, Dialect::Clickhouse).unwrap();
        // The placeholder is the whole of `{name:Type}`, past its type's parentheses and strings.
        let sql = "SELECT {id:UInt64}, { since : DateTime64(3, 'UTC') }, {e:Enum8('}' = 1, 'it\\'s' = 2)} FROM t";
        let found = l(sql);
        assert_eq!(found.params, ["id", "since", "e"]);
        assert_eq!(found.placeholders.iter().map(|p| &sql[p.start..p.end]).collect::<Vec<_>>(), ["{id:UInt64}", "{ since : DateTime64(3, 'UTC') }", "{e:Enum8('}' = 1, 'it\\'s' = 2)}"]);
        assert_eq!(found.style, Style::Named);
        // A name may be used again: each use carries its type.
        assert_eq!(l("SELECT {a:UInt8} + {a:UInt8}").params, ["a"]);
        assert_eq!(rewrite("SELECT {a:UInt8} + 1", &l("SELECT {a:UInt8} + 1"), Target::Braced).sql, "SELECT {a} + 1");
        // `:name` is one only where a cast gives it its type.
        assert_eq!(l("SELECT :id::UInt64, x::String FROM t WHERE n IN :ns::Array(UInt8)").params, ["id", "ns"]);
        assert!(l("SELECT c ? a :b, doc.a.:Int64, m[:k], :bare FROM t").params.is_empty());
        // Its `?` is an operator, and a `{` that opens no placeholder is the server's to refuse.
        assert!(l("SELECT a ? 1 : 2, {x}, {1:UInt8}, {y:} FROM t").params.is_empty());
        // The operator's `:` may stand before a name and a cast, which is no parameter; a
        // parameter that is its operand has no operand before it.
        assert!(l("SELECT c ? a :b::String, c ? 'x' /* or */ :d::String, c ? (d ? 1 :e::UInt8) :f::UInt8, c ? f(a) :g::String, c ? m['k'] :h::String FROM t").params.is_empty());
        assert_eq!(l("SELECT c ? :a::UInt8 : 0, c ? 1 : :b::UInt8, :c::UInt8 FROM t").params, ["a", "b", "c"]);
        // A parameter inside the operand is one: in a call, after an operator or a word that takes an operand, and in brackets.
        assert_eq!(l("SELECT 1 ? abs(:n::Int32) : 0 AS v, c ? 1 + :m::Int32 : 0, c ? a AND :o::UInt8 : 0, c ? [:p::UInt8, 2] : [], (c ? 1 : 2) + :q::UInt8").params, ["n", "m", "o", "p", "q"]);
        assert_eq!(l("SELECT c ? {r:UInt8} :s::UInt8, c ? {t:Decimal(9, 2)} + 1 : 0").params, ["r", "t"]);
        // A name with a `$` in it is one name, whatever word it ends with.
        assert!(l("SELECT 1 ? a$and :b::UInt8, 1 ? a$b$ :c::UInt8 FROM (SELECT 1 AS a$and, 2 AS b, 3 AS a$b$, 4 AS c)").params.is_empty());
        // A `$` is a character of a name, a parameter's too, and opens a heredoc only where none stands before it.
        assert_eq!(l("SELECT 1 AS a$b$, {p:UInt8} AS q, 2 AS c$b$, {x$y:UInt8}").params, ["p", "x$y"]);
        // Its text: a backslash escapes in a string and a quoted name, `#` comments before a space
        // or a `!`, block comments nest, and a heredoc quotes.
        assert!(l("SELECT 'it\\'s {a:UInt8}', `{b:UInt8}\\``, \"{c:UInt8}\\\"\" # {d:UInt8}\n #! {e:UInt8}\n /* {f:UInt8} /* {g:UInt8} */ {h:UInt8} */, $q${i:UInt8}$q$ -- {j:UInt8}").params.is_empty());
        assert_eq!(l("SELECT 1 #{k:UInt8}").params, ["k"]);
        // A heredoc's tag is whatever stands up to the next `$`, digits and spaces too, and a `$` that no tag closes opens none.
        assert!(l("SELECT $1$ {a:UInt8} $1$, $ x ${b:UInt8}$ x $, $${c:UInt8}$$").params.is_empty());
        assert_eq!(l("SELECT 1 $ {a:UInt8} $, {b:UInt8}").params, ["a", "b"]);
        // A `;` in a placeholder's type is the type's: the statement is one.
        let labelled = "SELECT {e:Enum8('a;b' = 1)} AS e;";
        assert_eq!(one_statement(labelled, &l(labelled), &["select"]), Ok(Statement::Query));
        // One statement writes its parameters one way.
        let e = lex("SELECT {a:UInt8}, :b::UInt8", Dialect::Clickhouse).unwrap_err();
        assert_eq!(e.message, ":b and {a:UInt8} are written two ways; write every parameter of a statement one way, as {name:Type} or as :name::Type");
        assert_eq!(lex("SELECT $1", Dialect::Clickhouse).unwrap_err().message, "$1 is not a placeholder on ClickHouse, which writes a parameter with its type; write {p1:Type}");
        assert_eq!(lex("SELECT 'open", Dialect::Clickhouse).unwrap_err().message, "unterminated string literal");
    }

    #[test]
    fn sqlite_named_prefixes() {
        // SQLite's `@name` and `$name` are named placeholders there, dotted like `:name`, and the
        // name drops the prefix.
        for sql in ["select @a.b, @c, @a.b", "select $a.b, $c, $a.b"] {
            let l = lex(sql, Dialect::Sqlite).unwrap();
            assert_eq!(l.params, ["a.b", "c"]);
            assert_eq!(l.style, Style::Named);
            assert_eq!(rewrite(sql, &l, Target::Numbered).sql, "select ?1, ?2, ?1");
        }
        // SQLite reads `:a` and `@a` as two parameters, so one statement keeps one prefix.
        let e = lex("select :a, @b where x = $a", Dialect::Sqlite).unwrap_err();
        assert_eq!(e.message, "@b and :a have different prefixes; write every named parameter of a statement with one prefix, here :b");
        assert_eq!(e.offset, 11);
        assert_eq!(lex("select @a, :a", Dialect::Sqlite).unwrap_err().offset, 11);
        // Named and positional still do not mix, and `a$b` and `a@b` are not placeholders.
        assert_eq!(lex("select @a, ?", Dialect::Sqlite).unwrap().style, Style::Mixed);
        assert!(names("select a$b, a@b from t", Dialect::Sqlite).is_empty());
        // Elsewhere the forms keep their meaning: Postgres `@` is an operator, MySQL `@x` a variable.
        assert!(names("select @a from t", Dialect::Postgres).is_empty());
        assert!(names("select @a, $b from t", Dialect::Mysql).is_empty());
        // `Named::Colon` takes `:name` alone.
        let colon = |sql: &str| lex_with(sql, Dialect::Sqlite, Named::Colon).unwrap_err().message;
        assert_eq!(colon("select @a.b from t"), "SQLite reads @a.b as a parameter that Reylo does not bind; write :a.b");
        assert_eq!(colon("select :x, $y"), "SQLite reads $y as a parameter that Reylo does not bind; write :y");
    }

    #[test]
    fn rewrites_and_maps_back() {
        let sql = "select * from t where id = :id and name = :name and id = :id";
        let l = lex(sql, Dialect::Postgres).unwrap();
        let r = rewrite(sql, &l, Target::Dollar);
        assert_eq!(r.sql, "select * from t where id = $1 and name = $2 and id = $1");
        assert_eq!(r.order, ["id", "name"]);
        // "name" starts at the same place in both; the third placeholder shifted.
        let third = r.sql.rfind("$1").unwrap();
        assert_eq!(r.to_original(third), sql.rfind(":id").unwrap());
        assert_eq!(r.to_original(r.sql.len()), sql.len());
        let q = rewrite(sql, &l, Target::Question);
        assert_eq!(q.sql, "select * from t where id = ? and name = ? and id = ?");
        assert_eq!(q.order, ["id", "name", "id"]);
    }

    #[test]
    fn each_parameter_once() {
        let at = |sql: &str, d: Dialect| repeated(sql, &lex(sql, d).unwrap()).into_iter().map(|r| (r.start, r.end)).collect::<Vec<_>>();
        // Every use after the first is an error at its place, in every form a dialect names.
        let sql = "select id, name from users where name like :q or email like :q";
        let r = repeated(sql, &lex(sql, Dialect::Postgres).unwrap());
        assert_eq!(r.len(), 1);
        assert_eq!(&sql[r[0].start..r[0].end], ":q");
        assert_eq!(r[0].start, sql.rfind(":q").unwrap());
        assert_eq!(
            r[0].message,
            "The parameter :q appears more than once; use each parameter once, and select it in a with to read it twice, cast to its type, as in: with input as (select :q::<type> as q) select … from input"
        );
        assert_eq!(at("select :a, :b, :a, :a, :b", Dialect::Postgres), [(15, 17), (19, 21), (23, 25)]);
        assert_eq!(at("select $1, $2, $1", Dialect::Postgres), [(15, 17)]);
        assert_eq!(at("select ?3, ?3", Dialect::Sqlite), [(11, 13)]);
        assert_eq!(at("select @a, @a", Dialect::Sqlite), [(11, 13)]);
        assert_eq!(at("select $a, $a", Dialect::Sqlite), [(11, 13)]);
        assert_eq!(at("select :a, :a", Dialect::Mysql), [(11, 13)]);
        // A bare `?` takes 1 here, so `?1` names the same parameter again.
        assert_eq!(at("select ?, ?1", Dialect::Sqlite), [(10, 12)]);
        // A dotted global is one name; the example selects it by its last segment, and a
        // position by `p` and its number.
        let sql = "select * from t where owner = :auth.user_id or author = :auth.user_id";
        assert!(repeated(sql, &lex(sql, Dialect::Postgres).unwrap())[0].message.contains("with input as (select :auth.user_id::<type> as user_id)"));
        let sql = "select $1::int + $1";
        assert!(repeated(sql, &lex(sql, Dialect::Postgres).unwrap())[0].message.starts_with("The parameter $1 appears more than once;"));
        assert!(repeated(sql, &lex(sql, Dialect::Postgres).unwrap())[0].message.contains("(select $1::<type> as p1)"));
        // A bare `?` never repeats; distinct names, text and casts are not uses.
        assert!(at("select ?, ?, ?", Dialect::Mysql).is_empty());
        assert!(at("select ?, ?", Dialect::Sqlite).is_empty());
        assert!(at("select :a, :b, ':a', x::a -- :a", Dialect::Postgres).is_empty());
        assert!(at("with input as (select :q::text as q) select 1 from t, input where a = input.q or b = input.q", Dialect::Postgres).is_empty());
    }

    #[test]
    fn errors() {
        assert!(lex("select 'abc", Dialect::Postgres).is_err());
        assert!(lex("select $x$abc", Dialect::Postgres).is_err());
        assert!(lex("select /* a", Dialect::Postgres).is_err());
        assert_eq!(lex("select 'abc", Dialect::Postgres).unwrap_err().to_string(), "unterminated string literal (at byte 7)");
        // Each error points at what is left open, the quote or the comment's start, which lies
        // inside the text: an offset at its end placed the error past the end of the file.
        let at = |sql: &str, dialect| lex(sql, dialect).unwrap_err().offset;
        assert_eq!(at("select 1, 'abc", Dialect::Sqlite), 10);
        assert_eq!(at("select 1, \"abc", Dialect::Postgres), 10);
        assert_eq!(at("select 1, `abc\n", Dialect::Sqlite), 10);
        assert_eq!(at("select 1, `abc", Dialect::Mysql), 10);
        assert_eq!(at("select 1, E'abc", Dialect::Postgres), 10);
        assert_eq!(at("select 1, $x$abc", Dialect::Postgres), 10);
        assert_eq!(at("select 1, /* a /* b */", Dialect::Postgres), 10);
        assert_eq!(at("select 1, /* a", Dialect::Sqlite), 10);
        assert_eq!(at("select 1, /* a", Dialect::Mysql), 10);
    }

    #[test]
    fn dotted_names() {
        let sql = "select :a.b";
        let l = lex(sql, Dialect::Postgres).unwrap();
        assert_eq!(l.params, ["a.b"]);
        assert_eq!(l.placeholders, [Placeholder { name: "a.b".into(), start: 7, end: 11 }]);
        assert_eq!(l.style, Style::Named);
        assert_eq!(rewrite(sql, &l, Target::Dollar).sql, "select $1");
        assert_eq!(rewrite(sql, &l, Target::Numbered).sql, "select ?1");
        assert_eq!(rewrite(sql, &l, Target::Question).sql, "select ?");
        // Every segment starts like a name; the named forms agree; a cast after one is a cast.
        assert_eq!(names("select :auth.user_id, :org.name::text, :t.*, :x.1", Dialect::Postgres), ["auth.user_id", "org.name", "t", "x"]);
        // A dotted name survives the SQLite prepare only through the rewrite, and maps back.
        let sql = "select :a.b, :c";
        let l = lex(sql, Dialect::Sqlite).unwrap();
        let r = rewrite(sql, &l, Target::Numbered);
        assert_eq!(r.sql, "select ?1, ?2");
        assert_eq!(r.to_original(r.sql.find("?2").unwrap()), sql.find(":c").unwrap());
        assert_eq!(r.to_original(r.sql.len()), sql.len());
    }

    #[test]
    fn numbered_question_is_a_position() {
        let l = lex("select ?3, ?3", Dialect::Sqlite).unwrap();
        assert_eq!(l.params, ["3"]);
        assert_eq!(l.placeholders.iter().map(|p| (p.start, p.end)).collect::<Vec<_>>(), [(7, 9), (11, 13)]);
        assert_eq!(l.style, Style::Positional);
        assert_eq!(lex("select ?3, :x", Dialect::Sqlite).unwrap().style, Style::Mixed);
        // On Postgres `?` is still the operator, digits or not.
        assert!(lex("select j ? 'k', 1 ?2", Dialect::Postgres).unwrap().params.is_empty());
    }

    #[test]
    fn array_slices_are_not_placeholders() {
        for d in [Dialect::Postgres, Dialect::Sqlite, Dialect::Mysql] {
            assert!(lex("select arr[1:2], arr[2:], arr[:3], arr[1:2].x, arr[lo:hi], arr[1:n] from t", d).unwrap().params.is_empty());
        }
        // On Postgres a colon that opens a subscript is a slice with no lower bound, whatever
        // follows it: no expression starts with a colon there. Whitespace and comments may stand
        // between, and the subscript may follow a quoted name, a parenthesis, another subscript or
        // a placeholder.
        let slices = "select arr[:hi], arr[ :hi], arr[/* c */:hi], arr[:t.n], \"a\"[:hi], (f(x))[:pg_t.tgnargs], m[1][:hi], arr[1:hi], arr[:hi][:lo] from t where x = :x and y = $1";
        let l = lex(slices, Dialect::Postgres).unwrap();
        assert_eq!(l.params, ["x", "1"]);
        assert_eq!(l.style, Style::Mixed);
        assert_eq!(names("select :a[:hi], arr[:hi] from t", Dialect::Postgres), ["a"]);
        // A parameter is the subscript, or a slice's bound after its colon, as written.
        assert_eq!(names("select arr[(:i)], arr[ : :hi], arr[1 : :b], arr[:i + 0] from t", Dialect::Postgres), ["i", "hi", "b"]);
        // An array constructor's bracket follows `array` or another bracket, and holds parameters.
        assert_eq!(names("select array[:a, :b], ARRAY[[:c], [:d]], x = any(array[:e]) from t", Dialect::Postgres), ["a", "b", "c", "d", "e"]);
        // The statement the runtime's lexer is checked against reads the same here.
        let l = lex("select tags[:n], tags[ :m], (f(x))[:pg_t.tgnargs], \"q\"[:k], array[:a, :b], tags[(:c)], tags[1::int:d] from t where y = :y", Dialect::Postgres).unwrap();
        assert_eq!(l.params, ["a", "b", "c", "y"]);
        assert_eq!(l.placeholders.iter().map(|p| (p.start, p.end)).collect::<Vec<_>>(), [(66, 68), (70, 72), (81, 83), (119, 121)]);
        // MySQL keeps reading a colon after no name character as a parameter. SQLite reads `[:lo]`
        // as a quoted name, the alias of `arr`.
        assert_eq!(names("select arr[:lo] from t", Dialect::Mysql), ["lo"]);
        assert!(names("select arr[:lo] from t", Dialect::Sqlite).is_empty());
    }

    #[test]
    fn colons_that_open_nothing() {
        // `:{` and MySQL's `:=` open nothing; a quote doubled inside a string is one quote.
        assert!(names("select '{}'::jsonb, :{a}", Dialect::Postgres).is_empty());
        assert!(names("select @x := 1", Dialect::Mysql).is_empty());
        assert_eq!(names("select 'it''s :not', :yes", Dialect::Postgres), ["yes"]);
        // An upper-case `E''` string and a tagged dollar quote hide what is inside them.
        assert_eq!(names("select E'\\':nope', $tag$ :nope $tag$, :real", Dialect::Postgres), ["real"]);
    }

    #[test]
    fn opaque_spans() {
        let sql = "select 'a''b', \"q\"\"r\", $t$ :x $t$, e'\\'' -- c :y\n/* d /* :z */ */ :w";
        let l = lex(sql, Dialect::Postgres).unwrap();
        assert_eq!(l.params, ["w"]);
        assert_eq!(l.opaque, [(7, 13), (15, 21), (23, 33), (35, 40), (41, 48), (49, 65)]);
        let text: Vec<&str> = l.opaque.iter().map(|&(s, e)| &sql[s..e]).collect();
        assert_eq!(text, ["'a''b'", "\"q\"\"r\"", "$t$ :x $t$", "e'\\''", "-- c :y", "/* d /* :z */ */"]);
        // A `-- @` line is a comment like any other; code between the spans is not listed.
        let l = lex("-- @returns one\nselect `a`, 1", Dialect::Mysql).unwrap();
        assert_eq!(l.opaque, [(0, 15), (23, 26)]);
    }

    #[test]
    fn numbered_target() {
        let sql = "select :a, :b, :a";
        let l = lex(sql, Dialect::Sqlite).unwrap();
        let r = rewrite(sql, &l, Target::Numbered);
        assert_eq!(r.sql, "select ?1, ?2, ?1");
        assert_eq!(r.order, ["a", "b"]);
        assert_eq!(r.spans, [(7, 9, 7, 9), (11, 13, 11, 13), (15, 17, 15, 17)]);
        // Positional parameters bind by number, whatever order they appear in: `?n` binds
        // `params[n - 1]`, so an author's numbering survives.
        let sql = "select ?2, ?1, ?2";
        let l = lex(sql, Dialect::Sqlite).unwrap();
        let r = rewrite(sql, &l, Target::Numbered);
        assert_eq!(r.sql, "select ?2, ?1, ?2");
        assert_eq!(r.order, ["1", "2"]);
    }

    #[test]
    fn positional_names_are_numbers() {
        // A bare `?` takes the number after the largest one so far, as SQLite numbers it.
        assert_eq!(names("select ?1, ?2, ?", Dialect::Sqlite), ["1", "2", "3"]);
        assert_eq!(names("select ?5, ?, ?2", Dialect::Sqlite), ["2", "5", "6"]);
        assert_eq!(names("select $01, $1", Dialect::Postgres), ["1"]);
        assert!(is_positional("12") && !is_positional("a1") && !is_positional(""));
        // `$2 ... $1` binds `$1` first, and the rewrite keeps the author's numbers.
        let sql = "select $2::int, $1::text";
        let l = lex(sql, Dialect::Postgres).unwrap();
        assert_eq!(l.params, ["1", "2"]);
        let r = rewrite(sql, &l, Target::Dollar);
        assert_eq!(r.sql, sql);
        assert_eq!(r.order, ["1", "2"]);
        // MySQL binds one value per `?`, in text order.
        let sql = "select ?, ?";
        let l = lex(sql, Dialect::Mysql).unwrap();
        assert_eq!(rewrite(sql, &l, Target::Question).order, ["1", "2"]);
    }

    #[test]
    fn mysql_text() {
        // By default a backslash escapes the character after it, in either quote, and a
        // double-quoted word is a string; a placeholder inside one is text.
        let sql = r#"select 'it\'s :not', 'a\\' as b, :x, "say \":no", 'a\nb' from t where id = :id"#;
        assert_eq!(names(sql, Dialect::Mysql), ["x", "id"]);
        // Postgres and SQLite read a backslash as a character, so there `'a\'` ends at its quote.
        assert_eq!(names(r"select 'C:\', :x", Dialect::Postgres), ["x"]);
        assert_eq!(names(r"select 'C:\', :x", Dialect::Sqlite), ["x"]);
        assert_eq!(lex(r"select 'C:\', :x", Dialect::Mysql).unwrap_err().message, "unterminated string literal");
        // NO_BACKSLASH_ESCAPES makes the backslash a character there too.
        let plain = MysqlText { backslash_escapes: false, ansi_quotes: false };
        assert_eq!(lex_mysql(r"select 'C:\', :x", Named::Dialect, plain).unwrap().params, ["x"]);
        assert_eq!(lex_mysql(r"select 'a\' as b, :x, 'it''s :not'", Named::Dialect, plain).unwrap().params, ["x"]);
        // Under ANSI_QUOTES a double-quoted word is a name, which escapes nothing.
        let ansi = MysqlText { backslash_escapes: true, ansi_quotes: true };
        assert_eq!(lex_mysql(r#"select "a\" from t where id = :id"#, Named::Dialect, ansi).unwrap().params, ["id"]);
        assert!(lex(r#"select "a\" from t where id = :id"#, Dialect::Mysql).is_err());
        // `#` starts a line comment; `--` does only before a space or a control character, or at the end.
        assert_eq!(names("select :a # :b\n, :c", Dialect::Mysql), ["a", "c"]);
        assert_eq!(names("select :a --:b", Dialect::Mysql), ["a", "b"]);
        assert_eq!(names("select :a -- :b\n, :c --\t:d\n, 1--", Dialect::Mysql), ["a", "c"]);
        assert_eq!(names("select :a --:b", Dialect::Postgres), ["a"]);
        assert_eq!(names("select 1 # :b", Dialect::Postgres), ["b"]);
        // A block comment ends at its first `*/`; backquotes quote a name and escape nothing.
        assert_eq!(names("select /* a /* b */ :x", Dialect::Mysql), ["x"]);
        assert_eq!(names(r"select `a\`, :x", Dialect::Mysql), ["x"]);
        // MySQL has no `E''` string: `e'\''` is an escaped quote by the default reading alone.
        assert_eq!(lex_mysql(r"select e'\', :x", Named::Dialect, plain).unwrap().params, ["x"]);
        // The spans are listed as the reading has them.
        let sql = "select 'a\\'b'':c', \"c\\\"\", `d`, :e # f\n/* g /* */";
        let l = lex(sql, Dialect::Mysql).unwrap();
        let text: Vec<&str> = l.opaque.iter().map(|&(s, e)| &sql[s..e]).collect();
        assert_eq!(text, ["'a\\'b'':c'", "\"c\\\"\"", "`d`", "# f", "/* g /* */"]);
        assert_eq!(l.params, ["e"]);
        // A comment that starts with `#` is a comment to the statement check too.
        assert_eq!(one_statement("select 1; # done", &lex("select 1; # done", Dialect::Mysql).unwrap(), &["select"]), Ok(Statement::Query));
        assert_eq!(MysqlText::of_mode("STRICT_TRANS_TABLES,NO_BACKSLASH_ESCAPES"), plain);
        assert_eq!(MysqlText::of_mode("REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI"), ansi);
        assert_eq!(MysqlText::of_mode(""), MysqlText::default());
    }

    #[test]
    fn one_statement_of_any_kind() {
        const SQLITE: &[&str] = &["select", "with", "values", "insert", "replace", "update", "delete"];
        let check = |sql: &str| one_statement(sql, &lex(sql, Dialect::Sqlite).unwrap(), SQLITE);
        assert_eq!(check("select 1"), Ok(Statement::Query));
        assert_eq!(check("-- the user\n/* by id */ SELECT id from users where id = :id;"), Ok(Statement::Query));
        assert_eq!(check("select 1;\n-- a trailing comment\n;\n"), Ok(Statement::Query));
        assert_eq!(check("select ';' as a"), Ok(Statement::Query));
        assert_eq!(check("with x as (select 1) delete from t where id in (select * from x)"), Ok(Statement::Query));
        // A statement of another kind is a command, named by its first keyword.
        assert_eq!(check("create table t (id integer primary key)"), Ok(Statement::Command("create".into())));
        assert_eq!(check("/* the schema */ PRAGMA table_info(t);"), Ok(Statement::Command("pragma".into())));
        // A migration or a schema file of several statements: the error says what a typed file
        // holds, and how to leave such a file out of the type check.
        let several = "A typed .sql file holds one statement and this one holds several, so if it is a migration or a schema file, leave it out of the type check with \"exclude\" in tsconfig.json, as in \"exclude\": [\"db/**\"].";
        assert_eq!(check("select 1; select 2").unwrap_err(), several);
        assert_eq!(check("create table t (id integer primary key);\ncreate index i on t (id);").unwrap_err(), several);
        // A misspelt keyword is a syntax error the database reports, not a statement of another kind.
        assert_eq!(check("selec id from users"), Ok(Statement::Query));
        assert_eq!(refused_command("table t already exists"), "table t already exists, so if it is a migration or a schema file, leave it out of the type check with \"exclude\" in tsconfig.json, as in \"exclude\": [\"db/**\"].");
    }
}