rustango 0.61.13

Batteries-included web framework for Rust: ORM + migrations + auto-admin + multi-tenancy + audit log + auth (sessions, JWT, OAuth2/OIDC, HMAC) + APIs (ViewSet, OpenAPI auto-derive, JSON:API) + jobs (in-mem + Postgres) + email + media (S3 / R2 / B2 / MinIO + presigned uploads + collections + tags) + production middleware (CSRF, CSP, rate-limiting, compression, idempotency, etc.).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
//! The MySQL 8.4+ dialect: backtick-quoted identifiers, `?`
//! placeholders, `BIGINT AUTO_INCREMENT` for an `Auto<T>` PK, `1` and
//! `0` for booleans, and `GET_LOCK` for advisory locks. MySQL has no
//! `RETURNING`.
//!
//! An operator with no MySQL translation gets
//! [`SqlError::OperatorNotSupportedInDialect`] from the writers.

use crate::core::{
    AggregateQuery, BulkInsertQuery, BulkUpdateQuery, ConflictClause, CountQuery, DeleteQuery,
    FieldSchema, FieldType, InsertQuery, ModelSchema, SelectQuery, UpdateQuery,
};

use super::server::{AdminRight, Collation, DatabaseName, Login, LoginPassword, ServerAccount};
use super::writers::{
    write_aggregate, write_bulk_insert, write_count, write_delete, write_insert, write_select,
    write_update, Sql,
};
use super::{CompiledStatement, Dialect, SqlError};

/// The insert id a skipped `DoNothing` insert reports. No auto-increment
/// column reaches it: a signed BIGINT stops at 2^63 - 1.
pub(crate) const SKIPPED_INSERT_ID: u64 = u64::MAX;

/// SQL that MySQL and MariaDB spell differently, picked by the server
/// itself: MySQL 8 runs `/*!80000 …*/`, which MariaDB skips, and MariaDB
/// runs `/*M! …*/`, which MySQL skips. Both sides bind the same
/// placeholders in the same order.
#[derive(Clone, Copy)]
pub(crate) struct ByServer<'a> {
    pub mysql: &'a str,
    pub mariadb: &'a str,
}

impl ByServer<'_> {
    pub(crate) fn write(self, sql: &mut String) {
        debug_assert!(!self.mysql.contains("*/") && !self.mariadb.contains("*/"));
        sql.push_str("/*!80000 ");
        sql.push_str(self.mysql);
        sql.push_str(" */ /*M! ");
        sql.push_str(self.mariadb);
        sql.push_str(" */");
    }
}

/// The `MySQL` 8.4+ dialect. Stateless; construct with `MySql`.
#[derive(Debug, Default, Clone, Copy)]
pub struct MySql;

/// The singleton [`MySql`] dialect, which
/// [`crate::sql::Pool::dialect`] hands back for a MySQL pool.
#[cfg(feature = "mysql")]
pub static DIALECT: &MySql = &MySql;

impl MySql {
    /// `user`@`host`, both identifier-quoted.
    fn account(&self, login: &Login) -> String {
        format!(
            "{}@{}",
            self.quote_ident(login.name().as_str()),
            self.quote_ident(login.host())
        )
    }
}

#[cfg(feature = "mysql")]
impl MySql {
    /// The INSERT without `RETURNING`; the id comes from the reply, so an
    /// INSERT naming no column is still a write (#2416).
    pub(crate) fn compile_insert_auto_id(
        &self,
        query: &InsertQuery,
    ) -> Result<CompiledStatement, SqlError> {
        let mut b = Sql::with_capacity(self, query.values.len());
        super::writers::write_insert_rows(&mut b, query)?;
        b.finish()
    }
}

impl Dialect for MySql {
    fn name(&self) -> &'static str {
        "mysql"
    }

    fn binds_pg_only_values(&self) -> bool {
        false
    }

    fn default_values_clause(&self) -> &'static str {
        " () VALUES ()"
    }

    /// MySQL has no `NULLS FIRST` or `NULLS LAST`, so the writer
    /// sorts on `<col> IS NULL` first instead.
    fn supports_nulls_order(&self) -> bool {
        false
    }

    /// MySQL spells the random function `RAND()`.
    fn random_fn(&self) -> &'static str {
        "RAND"
    }

    /// MySQL rejects an `OFFSET` with no `LIMIT`, so pair it with
    /// the largest `u64` and the limit does nothing.
    fn offset_without_limit_clause(&self) -> Option<&'static str> {
        Some(" LIMIT 18446744073709551615")
    }

    /// A materialized derived table also lifts error 1093, so the
    /// subquery of an UPDATE/DELETE may read the target table.
    fn in_subquery_limit_needs_derived_table(&self) -> bool {
        true
    }

    fn plain_reads_see_snapshot(&self) -> bool {
        true
    }

    /// MySQL quotes with backticks, and an embedded backtick is
    /// doubled, so any name comes out valid.
    fn quote_ident(&self, name: &str) -> String {
        super::dialect::quote_with(name, '`')
    }

    /// MySQL reads `\` as an escape inside a string literal under the
    /// default `sql_mode`, so it is doubled along with `'` (#2232).
    /// On a `NO_BACKSLASH_ESCAPES` server the `\` is stored doubled.
    fn quote_literal(&self, text: &str) -> String {
        format!("'{}'", text.replace('\\', "\\\\").replace('\'', "''"))
    }

    /// utf8mb4: older MariaDB defaults to latin1. Without `collation` the
    /// server picks utf8mb4's default, which ignores `collation_server`.
    fn create_database_sql(
        &self,
        db: &DatabaseName,
        collation: Option<&Collation>,
    ) -> Option<String> {
        let collate = collation.map_or_else(String::new, |c| format!(" COLLATE {}", c.as_str()));
        Some(format!(
            "CREATE DATABASE {} CHARACTER SET utf8mb4{collate}",
            self.quote_ident(db.as_str())
        ))
    }

    fn drop_database_sql(&self, db: &DatabaseName) -> Option<String> {
        Some(format!(
            "DROP DATABASE IF EXISTS {}",
            self.quote_ident(db.as_str())
        ))
    }

    /// The account is identifier-quoted, so only the password is a
    /// literal; [`Self::admin_session_sql`] pins its escaping.
    fn create_login_sql(&self, login: &Login, password: &LoginPassword) -> Option<String> {
        Some(format!(
            "CREATE USER {} IDENTIFIED BY {}",
            self.account(login),
            self.quote_literal(password.expose())
        ))
    }

    fn drop_login_sql(&self, login: &Login) -> Option<String> {
        Some(format!("DROP USER IF EXISTS {}", self.account(login)))
    }

    fn collation_exists_sql(&self, collation: &Collation) -> Option<CompiledStatement> {
        Some(CompiledStatement {
            sql: "SELECT count(*) FROM information_schema.COLLATIONS WHERE COLLATION_NAME = ?"
                .to_owned(),
            params: vec![crate::core::SqlValue::String(collation.as_str().to_owned())],
        })
    }

    /// `retain_current` needs MySQL 8.0.14+; MariaDB has no dual passwords.
    fn alter_login_password_sql(
        &self,
        login: &Login,
        password: &LoginPassword,
        retain_current: bool,
    ) -> Option<String> {
        let retain = if retain_current {
            " RETAIN CURRENT PASSWORD"
        } else {
            ""
        };
        Some(format!(
            "ALTER USER {} IDENTIFIED BY {}{retain}",
            self.account(login),
            self.quote_literal(password.expose())
        ))
    }

    fn discard_old_password_sql(&self, login: &Login) -> Option<String> {
        Some(format!(
            "ALTER USER {} DISCARD OLD PASSWORD",
            self.account(login)
        ))
    }

    fn database_exists_sql(&self, db: &DatabaseName) -> Option<CompiledStatement> {
        Some(CompiledStatement {
            sql: "SELECT count(*) FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ?"
                .to_owned(),
            params: vec![crate::core::SqlValue::String(db.as_str().to_owned())],
        })
    }

    /// Needs SELECT on `mysql.user`; callers treat a refusal as unknown.
    fn login_exists_sql(&self, login: &Login) -> Option<CompiledStatement> {
        Some(CompiledStatement {
            sql: "SELECT count(*) FROM mysql.user WHERE User = ? AND Host = ?".to_owned(),
            params: vec![
                crate::core::SqlValue::String(login.name().as_str().to_owned()),
                crate::core::SqlValue::String(login.host().to_owned()),
            ],
        })
    }

    /// `DatabaseName` holds no `_` or `%`, the wildcards of `db.*`.
    fn grant_database_sql(&self, db: &DatabaseName, login: &Login) -> Vec<String> {
        vec![format!(
            "GRANT ALL PRIVILEGES ON {}.* TO {}",
            self.quote_ident(db.as_str()),
            self.account(login)
        )]
    }

    fn server_version_sql(&self) -> Option<&'static str> {
        Some("SELECT VERSION()")
    }

    /// `@@server_uuid`; MariaDB has none, so host, port and server id.
    fn database_identity_sql(&self, version: &str, db: Option<&str>) -> Option<CompiledStatement> {
        let key = if version.contains("MariaDB") {
            "CONCAT(@@hostname, ':', @@port, ':', @@server_id)"
        } else {
            "@@server_uuid"
        };
        let (name, params) = match db {
            Some(db) => {
                let v = crate::core::SqlValue::String(db.to_owned());
                ("?", vec![v.clone(), v])
            }
            None => ("DATABASE()", Vec::new()),
        };
        Some(CompiledStatement {
            sql: format!(
                "SELECT CAST({key} AS CHAR), \
                 CAST(IF(@@lower_case_table_names = 0, {name}, LOWER({name})) AS CHAR)"
            ),
            params,
        })
    }

    /// Rows of (`PRIVILEGE_TYPE`, `IS_GRANTABLE`): the current account's
    /// global privileges. MariaDB has the same view.
    fn login_privileges_sql(&self) -> Option<&'static str> {
        Some(
            "SELECT PRIVILEGE_TYPE, IS_GRANTABLE FROM information_schema.USER_PRIVILEGES \
             WHERE GRANTEE = CONCAT('''', SUBSTRING_INDEX(CURRENT_USER(), '@', 1), \
             '''@''', SUBSTRING_INDEX(CURRENT_USER(), '@', -1), '''')",
        )
    }

    /// MySQL folds active roles' rights into the user's line; MariaDB
    /// lists each role, nested ones too.
    fn login_grants_sql(&self) -> Option<&'static str> {
        Some("SHOW GRANTS")
    }

    /// `user@host`; the host follows the last `@`.
    fn current_account_sql(&self) -> Option<&'static str> {
        Some("SELECT CAST(CURRENT_USER() AS CHAR)")
    }

    fn grant_admin_rights_sql(
        &self,
        account: &ServerAccount,
        _database: Option<&str>,
        rights: &[AdminRight],
    ) -> Vec<String> {
        let to = format!(
            "{}@{}",
            self.quote_literal(&account.user),
            self.quote_literal(account.host.as_deref().unwrap_or("%"))
        );
        let mut privs = Vec::new();
        if rights.contains(&AdminRight::CreateDatabase) {
            privs.push("CREATE, DROP");
        }
        if rights.contains(&AdminRight::CreateLogin) {
            privs.push("CREATE USER");
        }
        let mut out = Vec::new();
        if !privs.is_empty() {
            out.push(format!("GRANT {} ON *.* TO {to}", privs.join(", ")));
        }
        // GRANT ALL ON db.* needs every database privilege grantable.
        if rights.contains(&AdminRight::Grant) {
            out.push(format!(
                "GRANT ALL PRIVILEGES ON *.* TO {to} WITH GRANT OPTION"
            ));
        }
        out
    }

    /// sqlx appends to the server's `sql_mode`, so a server-wide
    /// `NO_BACKSLASH_ESCAPES` would make [`Self::quote_literal`] wrong.
    fn admin_session_sql(&self) -> Option<&'static str> {
        Some(
            "SET SESSION sql_mode = TRIM(BOTH ',' FROM REPLACE(\
             CONCAT(',', @@SESSION.sql_mode, ','), ',NO_BACKSLASH_ESCAPES,', ','))",
        )
    }

    /// MySQL writes a column comment inline, after the rest of the
    /// column definition.
    fn write_inline_column_comment(&self, comment: &str) -> Option<String> {
        Some(format!(" COMMENT {}", self.quote_literal(comment)))
    }

    /// MySQL writes a table comment as a `COMMENT='…'` trailer after
    /// the closing paren.
    fn write_inline_table_comment(&self, comment: &str) -> Option<String> {
        Some(format!(" COMMENT={}", self.quote_literal(comment)))
    }

    // `?` placeholders are the trait default.

    fn serial_type(&self, field_type: FieldType) -> &'static str {
        match field_type {
            FieldType::I32 => "INT AUTO_INCREMENT",
            _ => "BIGINT AUTO_INCREMENT",
        }
    }

    fn cast_aggregate_to_int(&self, expr: &str) -> String {
        // MySQL has no `BIGINT` cast target; it is `SIGNED`.
        format!("CAST({expr} AS SIGNED)")
    }

    fn cast_aggregate_to_float(&self, expr: &str) -> String {
        // MySQL has no `DOUBLE PRECISION`; it is `DOUBLE`.
        format!("CAST({expr} AS DOUBLE)")
    }

    /// MySQL's CAST targets differ from the ANSI names: integers
    /// cast to `SIGNED`, floats to `FLOAT` or `DOUBLE`, booleans to
    /// `UNSIGNED`, strings to `CHAR`, and timestamps to `DATETIME(6)`
    /// (bare `DATETIME` / `TIME` drop the microseconds).
    ///
    /// UUID has no CAST target, so it returns `None`; cast a
    /// string-shaped value to `CHAR` yourself.
    fn cast_type(&self, ty: FieldType) -> Option<&'static str> {
        Some(match ty {
            FieldType::I16 | FieldType::I32 | FieldType::I64 => "SIGNED",
            FieldType::F32 => "FLOAT",
            FieldType::F64 => "DOUBLE",
            FieldType::Bool => "UNSIGNED",
            FieldType::String => "CHAR",
            FieldType::DateTime => "DATETIME(6)",
            FieldType::Date => "DATE",
            FieldType::Time => "TIME(6)",
            FieldType::Decimal => "DECIMAL(65, 28)",
            FieldType::Binary => "BINARY",
            FieldType::Json => "JSON",
            // UUID has no target, and the array, range and other
            // Postgres-only types have nothing to cast to.
            FieldType::Uuid
            | FieldType::Array(_)
            | FieldType::Range(_)
            | FieldType::HStore
            | FieldType::Vector(_)
            | FieldType::Geometry(_) => return None,
        })
    }

    /// InnoDB caps a key at 3072 bytes (1071); utf8mb4 counts 4 per char.
    fn index_key_size(&self, cols: &[(FieldType, Option<u32>)]) -> Option<(u32, u32)> {
        let bytes = cols
            .iter()
            .map(|(ty, len)| match (ty, len) {
                (FieldType::String, Some(n)) => n.saturating_mul(4),
                (FieldType::Uuid, _) => 36 * 4,
                (FieldType::Decimal, _) => 30,
                // LONGTEXT / LONGBLOB / JSON take no key without a prefix (ERROR 1170 / 3152).
                (FieldType::String, None) | (FieldType::Binary | FieldType::Json, _) => u32::MAX,
                // Every other key-able type here is at most 8 bytes.
                _ => 8,
            })
            .fold(0u32, u32::saturating_add);
        Some((bytes, 3072))
    }

    /// MySQL's column types differ from the Postgres ones in
    /// several ways:
    /// - no `BOOLEAN`; it is an alias for `TINYINT(1)`
    /// - no `TIMESTAMPTZ`; `DATETIME(6)` holds microseconds but no
    ///   timezone, and sqlx binds a `DateTime<Utc>` into it correctly
    /// - no `JSONB`; `JSON` validates on write and stores binary
    /// - no `UUID`; `CHAR(36)` is the usual form
    /// - `VARCHAR` needs a length, so an unbounded `String` becomes
    ///   `LONGTEXT`; `TEXT` caps at 64 KiB where PG/SQLite have no cap
    /// - no `DOUBLE PRECISION`; `DOUBLE`, and `REAL` is an alias for
    ///   `FLOAT`
    fn column_type(&self, ty: FieldType, max_length: Option<u32>) -> String {
        match ty {
            FieldType::I16 => "SMALLINT".into(),
            FieldType::I32 => "INT".into(),
            FieldType::I64 => "BIGINT".into(),
            FieldType::F32 => "FLOAT".into(),
            FieldType::F64 => "DOUBLE".into(),
            FieldType::Bool => "TINYINT(1)".into(),
            FieldType::String => match max_length {
                Some(n) => format!("VARCHAR({n})"),
                None => "LONGTEXT".into(),
            },
            FieldType::DateTime => "DATETIME(6)".into(),
            FieldType::Date => "DATE".into(),
            FieldType::Uuid => "CHAR(36)".into(),
            FieldType::Json => "JSON".into(),
            // A bare `DECIMAL` is `(10, 0)`, which has no fraction.
            // `rust_decimal` holds 29 integer and 28 fractional digits;
            // `(65, 28)` stores all of them.
            FieldType::Decimal => "DECIMAL(65, 28)".into(),
            // `BLOB` caps at 64 KiB, too small here; `LONGBLOB` at
            // 4 GiB.
            FieldType::Binary => "LONGBLOB".into(),
            // Microsecond precision, to match `DATETIME(6)` above.
            FieldType::Time => "TIME(6)".into(),
            // These are all Postgres-only types with no MySQL
            // equivalent, so the column is TEXT and the bind and
            // decode paths reject them.
            FieldType::Array(_) => "TEXT".into(),
            // Ditto for PG range columns (#343).
            FieldType::Range(_) => "TEXT".into(),
            // Ditto for PG hstore columns (#342).
            FieldType::HStore => "TEXT".into(),
            // Ditto for pgvector `vector` columns (#824).
            FieldType::Vector(_) => "TEXT".into(),
            // Ditto for PostGIS `geometry` columns (#443).
            FieldType::Geometry(_) => "TEXT".into(),
        }
    }

    // Case-insensitive only, as PG citext. The server picks: MySQL 8 runs
    // `/*!80000`, MariaDB 10.10+ only `/*M!101000`; older ones keep the
    // charset default `general_ci`, which also ignores accents and spaces.
    fn ci_text_type(&self, max_length: Option<u32>) -> String {
        const CI: &str = "CHARACTER SET utf8mb4 /*!80000 COLLATE utf8mb4_0900_as_ci */ \
                          /*M!101000 COLLATE utf8mb4_uca1400_nopad_as_ci */";
        match max_length {
            Some(n) => format!("VARCHAR({n}) {CI}"),
            None => format!("LONGTEXT {CI}"),
        }
    }

    // Not `_bin`: MySQL flags those columns BINARY and sqlx refuses `String`.
    fn cs_text_type(&self, max_length: u32) -> String {
        format!(
            "VARCHAR({max_length}) CHARACTER SET utf8mb4 /*!80000 COLLATE utf8mb4_0900_as_cs */ \
             /*M!101000 COLLATE utf8mb4_uca1400_as_cs */"
        )
    }

    // None where `cs_text_type` gets no collation (MariaDB before 10.10),
    // or every run would restate the column.
    fn ci_columns_sql(&self, table: &str) -> Option<CompiledStatement> {
        Some(CompiledStatement {
            sql: "SELECT CAST(COLUMN_NAME AS CHAR) FROM information_schema.COLUMNS \
                  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? \
                  AND RIGHT(COLLATION_NAME, 3) = '_ci' \
                  AND EXISTS (SELECT 1 FROM information_schema.COLLATIONS WHERE COLLATION_NAME \
                  IN ('utf8mb4_0900_as_cs', 'utf8mb4_uca1400_as_cs'))"
                .to_owned(),
            params: vec![crate::core::SqlValue::String(table.to_owned())],
        })
    }

    // Columns inherit it, and a `_ci` one makes `=` and `unique` ignore
    // case and accents, unlike PG and SQLite (#1742).
    fn default_collation_sql(&self) -> Option<&'static str> {
        Some(
            "SELECT CAST(DEFAULT_COLLATION_NAME AS CHAR) FROM information_schema.SCHEMATA \
             WHERE SCHEMA_NAME = DATABASE()",
        )
    }

    // CONVERT_TZ returns NULL for a named zone when the server's time
    // zone tables are not loaded.
    fn named_time_zone_sql(&self) -> Option<&'static str> {
        Some("SELECT CAST(CONVERT_TZ('2000-01-01 00:00:00', '+00:00', 'UTC') AS CHAR)")
    }

    /// Translate Postgres-native `DEFAULT` expressions to MySQL
    /// spelling.
    ///
    /// - `now()` becomes `CURRENT_TIMESTAMP(6)`. MySQL wants the
    ///   default's precision to match the column, and a `DateTime`
    ///   column is `DATETIME(6)`; without the `(6)` it rejects the
    ///   default outright.
    /// - `gen_random_uuid()` becomes the 8.0.13+ expression `(UUID())`.
    /// - `'<lit>'::<type>` becomes `'<lit>'`.
    /// - A JSON, TEXT or BLOB column takes no literal default, only
    ///   MySQL 8.0.13+'s `DEFAULT (<expr>)` form, so those get
    ///   wrapped in parens. `max_length` is what tells an unbounded
    ///   `String`, which is LONGTEXT, from a `VARCHAR(n)`, which keeps
    ///   its literal default.
    /// - Everything else passes through.
    fn translate_default_expr(&self, expr: &str, ty: &str, max_length: Option<u32>) -> String {
        let mut out = expr.trim().to_owned();
        match out.as_str() {
            "now()" | "NOW()" | "current_timestamp" | "CURRENT_TIMESTAMP" => {
                out = "CURRENT_TIMESTAMP(6)".to_owned();
            }
            e if super::dialect::is_uuid_expr(e) => return "(UUID())".to_owned(),
            _ => {
                if let Some(idx) = out.rfind("::") {
                    let suffix = &out[idx + 2..];
                    if !suffix.is_empty()
                        && suffix
                            .chars()
                            .all(|c| c.is_ascii_alphanumeric() || c == '_')
                    {
                        out.truncate(idx);
                    }
                }
            }
        }
        let renders_as_lob = ty.eq_ignore_ascii_case("json")
            || ty.eq_ignore_ascii_case("binary")
            || (ty.eq_ignore_ascii_case("string") && max_length.is_none());
        if renders_as_lob {
            // Already an expression; do not wrap it twice.
            if !(out.starts_with('(') && out.ends_with(')')) {
                out = format!("({out})");
            }
        }
        out
    }

    /// MySQL takes `USING BTREE` and `USING HASH`, the latter only
    /// on the MEMORY engine. Any other method is Postgres-only, so
    /// drop the clause and let MySQL build a btree.
    fn index_method_clause(&self, method: &str) -> String {
        match method {
            "hash" => " USING HASH".to_owned(),
            // Anything else, including "btree", needs no clause:
            // MySQL's default is already a btree.
            _ => String::new(),
        }
    }

    /// MySQL has no `CREATE INDEX IF NOT EXISTS`. The migration
    /// ledger already stops a re-run, so nothing is lost.
    fn supports_create_index_if_not_exists(&self) -> bool {
        false
    }

    /// MySQL has no `CREATE INDEX … WHERE <expr>`. The migration
    /// writer drops the WHERE clause and warns, so the index is
    /// still created, just without the filter. If you need a real
    /// partial unique index, check it in the application or add a
    /// CHECK constraint over a generated column.
    fn supports_partial_index(&self) -> bool {
        false
    }

    /// InnoDB parses `SET DEFAULT` and refuses the parent delete (1451).
    fn supports_on_delete_set_default(&self) -> bool {
        false
    }

    /// MySQL spells this `DROP CHECK`, MariaDB `DROP CONSTRAINT`, and
    /// neither takes `IF EXISTS` there.
    ///
    /// **So this is not idempotent.** Dropping a constraint that is
    /// already gone is an error, where the Postgres form does
    /// nothing.
    fn drop_check_constraint_sql(&self, table: &str, name: &str) -> Option<String> {
        let mut sql = format!("ALTER TABLE {} DROP ", self.quote_ident(table));
        ByServer {
            mysql: "CHECK",
            mariadb: "CONSTRAINT",
        }
        .write(&mut sql);
        sql.push(' ');
        sql.push_str(&self.quote_ident(name));
        Some(sql)
    }

    /// MySQL spells this `DROP FOREIGN KEY` and takes no
    /// `IF EXISTS`, so it is not idempotent either.
    fn drop_foreign_key_sql(&self, table: &str, name: &str) -> Option<String> {
        Some(format!(
            "ALTER TABLE {} DROP FOREIGN KEY {}",
            self.quote_ident(table),
            self.quote_ident(name)
        ))
    }

    /// No deferred FKs: the hint drops checks for one statement only, so
    /// no session state leaks back into the pool (a `SET` would).
    /// `foreign_key_checks` is per session or per statement, never per transaction.
    fn defer_foreign_keys_sql(&self) -> Option<&'static str> {
        None
    }

    fn clear_tables_sql(&self, tables: &[&str]) -> Vec<String> {
        tables
            .iter()
            .map(|t| {
                format!(
                    "DELETE /*+ SET_VAR(foreign_key_checks=0) */ FROM {}",
                    self.quote_ident(t)
                )
            })
            .collect()
    }

    /// `CAST AS CHAR`: information_schema columns decode as binary otherwise.
    fn foreign_key_names_sql(&self) -> Option<&'static str> {
        Some(
            "SELECT CAST(k.CONSTRAINT_NAME AS CHAR) FROM information_schema.KEY_COLUMN_USAGE k \
             WHERE k.TABLE_SCHEMA = DATABASE() AND k.TABLE_NAME = ? AND k.COLUMN_NAME = ? \
             AND k.REFERENCED_TABLE_NAME IS NOT NULL AND NOT EXISTS (\
             SELECT 1 FROM information_schema.KEY_COLUMN_USAGE o \
             WHERE o.TABLE_SCHEMA = k.TABLE_SCHEMA AND o.TABLE_NAME = k.TABLE_NAME \
             AND o.CONSTRAINT_NAME = k.CONSTRAINT_NAME AND o.ORDINAL_POSITION > 1)",
        )
    }

    fn referencing_foreign_keys_sql(&self) -> Option<&'static str> {
        Some(
            "SELECT CAST(k.TABLE_NAME AS CHAR), CAST(k.CONSTRAINT_NAME AS CHAR) \
             FROM information_schema.KEY_COLUMN_USAGE k \
             WHERE k.TABLE_SCHEMA = DATABASE() AND k.REFERENCED_TABLE_SCHEMA = DATABASE() \
             AND k.REFERENCED_TABLE_NAME = ? AND k.REFERENCED_COLUMN_NAME = ? AND NOT EXISTS (\
             SELECT 1 FROM information_schema.KEY_COLUMN_USAGE o \
             WHERE o.TABLE_SCHEMA = k.TABLE_SCHEMA AND o.TABLE_NAME = k.TABLE_NAME \
             AND o.CONSTRAINT_NAME = k.CONSTRAINT_NAME AND o.ORDINAL_POSITION > 1)",
        )
    }

    fn composite_foreign_keys_sql(&self) -> Option<&'static str> {
        Some(
            "SELECT CAST(k.TABLE_NAME AS CHAR), CAST(k.CONSTRAINT_NAME AS CHAR), \
             CAST(k.COLUMN_NAME AS CHAR), CAST(k.REFERENCED_TABLE_NAME AS CHAR), \
             CAST(k.REFERENCED_COLUMN_NAME AS CHAR), CAST(r.DELETE_RULE AS CHAR), \
             CAST(r.UPDATE_RULE AS CHAR) \
             FROM information_schema.KEY_COLUMN_USAGE k \
             JOIN information_schema.REFERENTIAL_CONSTRAINTS r \
             ON r.CONSTRAINT_SCHEMA = k.TABLE_SCHEMA AND r.TABLE_NAME = k.TABLE_NAME \
             AND r.CONSTRAINT_NAME = k.CONSTRAINT_NAME \
             WHERE k.TABLE_SCHEMA = DATABASE() AND k.REFERENCED_TABLE_SCHEMA = DATABASE() \
             AND EXISTS (SELECT 1 FROM information_schema.KEY_COLUMN_USAGE m \
             WHERE m.TABLE_SCHEMA = k.TABLE_SCHEMA AND m.TABLE_NAME = k.TABLE_NAME \
             AND m.CONSTRAINT_NAME = k.CONSTRAINT_NAME \
             AND ((m.TABLE_NAME = ? AND m.COLUMN_NAME = ?) \
             OR (m.REFERENCED_TABLE_NAME = ? AND m.REFERENCED_COLUMN_NAME = ?))) \
             AND EXISTS (SELECT 1 FROM information_schema.KEY_COLUMN_USAGE o \
             WHERE o.TABLE_SCHEMA = k.TABLE_SCHEMA AND o.TABLE_NAME = k.TABLE_NAME \
             AND o.CONSTRAINT_NAME = k.CONSTRAINT_NAME AND o.ORDINAL_POSITION > 1) \
             ORDER BY k.TABLE_NAME, k.CONSTRAINT_NAME, k.ORDINAL_POSITION",
        )
    }

    fn sole_leading_column_sql(&self) -> Option<&'static str> {
        Some(
            "SELECT CAST(s.COLUMN_NAME AS CHAR) FROM information_schema.STATISTICS s \
             WHERE s.TABLE_SCHEMA = DATABASE() AND s.TABLE_NAME = ? AND s.INDEX_NAME = ? \
             AND s.SEQ_IN_INDEX = 1 AND NOT EXISTS (\
             SELECT 1 FROM information_schema.STATISTICS o \
             WHERE o.TABLE_SCHEMA = s.TABLE_SCHEMA AND o.TABLE_NAME = s.TABLE_NAME \
             AND o.COLUMN_NAME = s.COLUMN_NAME AND o.SEQ_IN_INDEX = 1 \
             AND o.INDEX_NAME <> s.INDEX_NAME \
             AND o.SUB_PART IS NULL AND o.INDEX_TYPE = 'BTREE')",
        )
    }

    /// Each FK column must sit at its position in the index, unprefixed.
    fn composite_fks_needing_index_sql(&self) -> Option<&'static str> {
        Some(
            "SELECT CAST(k.CONSTRAINT_NAME AS CHAR) FROM information_schema.KEY_COLUMN_USAGE k \
             WHERE k.TABLE_SCHEMA = DATABASE() AND k.TABLE_NAME = ? \
             AND k.REFERENCED_TABLE_NAME IS NOT NULL AND k.ORDINAL_POSITION = 2 \
             AND NOT EXISTS (SELECT 1 FROM information_schema.KEY_COLUMN_USAGE f \
             WHERE f.TABLE_SCHEMA = k.TABLE_SCHEMA AND f.TABLE_NAME = k.TABLE_NAME \
             AND f.CONSTRAINT_NAME = k.CONSTRAINT_NAME AND NOT EXISTS (\
             SELECT 1 FROM information_schema.STATISTICS s \
             WHERE s.TABLE_SCHEMA = f.TABLE_SCHEMA AND s.TABLE_NAME = f.TABLE_NAME \
             AND s.INDEX_NAME = ? AND s.SEQ_IN_INDEX = f.ORDINAL_POSITION \
             AND s.COLUMN_NAME = f.COLUMN_NAME AND s.SUB_PART IS NULL)) \
             AND NOT EXISTS (SELECT 1 FROM information_schema.STATISTICS j \
             WHERE j.TABLE_SCHEMA = k.TABLE_SCHEMA AND j.TABLE_NAME = k.TABLE_NAME \
             AND j.INDEX_NAME <> ? AND j.SEQ_IN_INDEX = 1 \
             AND NOT EXISTS (SELECT 1 FROM information_schema.KEY_COLUMN_USAGE f \
             WHERE f.TABLE_SCHEMA = k.TABLE_SCHEMA AND f.TABLE_NAME = k.TABLE_NAME \
             AND f.CONSTRAINT_NAME = k.CONSTRAINT_NAME AND NOT EXISTS (\
             SELECT 1 FROM information_schema.STATISTICS s \
             WHERE s.TABLE_SCHEMA = j.TABLE_SCHEMA AND s.TABLE_NAME = j.TABLE_NAME \
             AND s.INDEX_NAME = j.INDEX_NAME AND s.SEQ_IN_INDEX = f.ORDINAL_POSITION \
             AND s.COLUMN_NAME = f.COLUMN_NAME AND s.SUB_PART IS NULL \
             AND s.INDEX_TYPE = 'BTREE')))",
        )
    }

    fn modifies_whole_column(&self) -> bool {
        true
    }

    fn check_blocks_column_rename(&self) -> bool {
        true
    }

    // sqlx decodes any MySQL integer as `i64`, so the junction need not widen.
    fn fk_needs_same_int_width(&self) -> bool {
        true
    }

    /// `PRIMARY` and multi-column indexes are left out.
    fn unique_index_names_sql(&self) -> Option<&'static str> {
        Some(
            "SELECT CAST(s.INDEX_NAME AS CHAR) FROM information_schema.STATISTICS s \
             WHERE s.TABLE_SCHEMA = DATABASE() AND s.TABLE_NAME = ? AND s.COLUMN_NAME = ? \
             AND s.NON_UNIQUE = 0 AND s.INDEX_NAME <> 'PRIMARY' AND NOT EXISTS (\
             SELECT 1 FROM information_schema.STATISTICS o \
             WHERE o.TABLE_SCHEMA = s.TABLE_SCHEMA AND o.TABLE_NAME = s.TABLE_NAME \
             AND o.INDEX_NAME = s.INDEX_NAME AND o.SEQ_IN_INDEX > 1)",
        )
    }

    fn drop_unique_index_sql(&self, table: &str, name: &str) -> Option<String> {
        Some(format!(
            "ALTER TABLE {} DROP INDEX {}",
            self.quote_ident(table),
            self.quote_ident(name)
        ))
    }

    /// MySQL has no `ON CONFLICT`, so this writes
    /// `ON DUPLICATE KEY UPDATE <col> = <col>`: a no-op assignment
    /// that satisfies its need for at least one. Any of the conflict
    /// columns will do, so it uses the first.
    fn insert_on_conflict_skip(&self, conflict_cols: &[&str]) -> String {
        if conflict_cols.is_empty() {
            return String::new();
        }
        let pivot = conflict_cols[0];
        format!("ON DUPLICATE KEY UPDATE {pivot} = {pivot}")
    }

    /// MySQL's `BOOLEAN` is an alias for `TINYINT(1)`, so emit `1`
    /// and `0` to match how the value is stored.
    fn bool_literal(&self, b: bool) -> &'static str {
        if b {
            "1"
        } else {
            "0"
        }
    }

    // MySQL handles every operator in the IR, through the `write_*`
    // methods below, so the `true` default is right.

    fn write_ilike(&self, sql: &mut String, qualified_col: &str, placeholder: &str, negated: bool) {
        // MySQL has no `ILIKE`. A `_ci` collation would already make
        // `LIKE` case-insensitive, but lowercasing both sides makes
        // it so whatever the column's collation.
        sql.push_str("LOWER(");
        sql.push_str(qualified_col);
        sql.push_str(if negated {
            ") NOT LIKE LOWER("
        } else {
            ") LIKE LOWER("
        });
        sql.push_str(placeholder);
        sql.push(')');
    }

    /// MySQL's `REGEXP` is case-sensitive and has no insensitive
    /// form; the column's collation decides. As with ILIKE, the
    /// insensitive variants lowercase both sides instead.
    /// The case flag rides in the pattern: REGEXP alone follows the column
    /// collation, and lowering the pattern turns `\D` into `\d`.
    /// ICU (MySQL) and PCRE (MariaDB) both read `(?i)` / `(?-i)`.
    fn write_regex(
        &self,
        sql: &mut String,
        qualified_col: &str,
        placeholder: &str,
        case_sensitive: bool,
        negated: bool,
    ) {
        sql.push_str(qualified_col);
        sql.push_str(if negated { " NOT REGEXP " } else { " REGEXP " });
        sql.push_str(if case_sensitive {
            "CONCAT('(?-i)', "
        } else {
            "CONCAT('(?i)', "
        });
        sql.push_str(placeholder);
        sql.push(')');
    }

    /// LIKE follows the column collation, case-insensitive by default.
    fn write_like_typed(
        &self,
        sql: &mut String,
        qualified_col: &str,
        ty: Option<FieldType>,
        placeholder: &str,
        negated: bool,
    ) {
        sql.push_str(&self.like_operand(qualified_col, ty));
        sql.push_str(if negated { " NOT LIKE " } else { " LIKE " });
        sql.push_str(placeholder);
        sql.push_str(" COLLATE utf8mb4_bin");
    }

    /// MySQL has no trigram operators; its `FULLTEXT` similarity is
    /// a different shape. Reject the query here rather than let the
    /// driver fail on it later.
    fn write_trigram_similar(
        &self,
        _sql: &mut String,
        _qualified_col: &str,
        _placeholder: &str,
        word: bool,
    ) -> Result<(), super::SqlError> {
        Err(super::SqlError::OpNotSupportedInDialect {
            op: if word {
                "trigram_word_similar (%>) — pg_trgm is Postgres-only"
            } else {
                "trigram_similar (%) — pg_trgm is Postgres-only"
            },
            dialect: "mysql",
        })
    }

    /// MySQL's full-text search is `MATCH(col) AGAINST(?)`, which
    /// means something different from the Postgres shape, so this is
    /// not translated. Write a raw predicate instead.
    fn write_search(
        &self,
        _sql: &mut String,
        _qualified_col: &str,
        _placeholder: &str,
    ) -> Result<(), super::SqlError> {
        Err(super::SqlError::OpNotSupportedInDialect {
            op: "search (__search) — full-text search shape is Postgres-only; \
                 use MySQL `MATCH … AGAINST` via a raw predicate",
            dialect: "mysql",
        })
    }

    /// MySQL has no array type, so reject every array operator.
    fn write_array_op(
        &self,
        _sql: &mut String,
        _qualified_col: &str,
        _placeholder: &str,
        _op: &'static str,
    ) -> Result<(), super::SqlError> {
        Err(super::SqlError::OpNotSupportedInDialect {
            op: "array operators (@>, <@, &&) — PG ArrayField is Postgres-only; \
                 use JSON columns + JSON-shape operators on MySQL",
            dialect: "mysql",
        })
    }

    /// MySQL has no range type, so reject every range operator.
    fn write_range_op(
        &self,
        _sql: &mut String,
        _qualified_col: &str,
        _placeholder: &str,
        _op: &'static str,
    ) -> Result<(), super::SqlError> {
        Err(super::SqlError::OpNotSupportedInDialect {
            op: "range operators (@>, <@, &&, <<, >>, -|-) — PG RangeField is Postgres-only; \
                 store lo/hi as separate columns on MySQL",
            dialect: "mysql",
        })
    }

    fn write_null_safe_eq(
        &self,
        sql: &mut String,
        qualified_col: &str,
        placeholder: &str,
        distinct: bool,
    ) {
        // MySQL's `<=>` is null-safe equality, and
        // `IS DISTINCT FROM` is its negation, so wrap it in `NOT`.
        if distinct {
            sql.push_str("NOT (");
        }
        sql.push_str(qualified_col);
        sql.push_str(" <=> ");
        sql.push_str(placeholder);
        if distinct {
            sql.push(')');
        }
    }

    /// Bound as text, a JSON value never equals a JSON column. MariaDB has
    /// no JSON cast and skips `/*!80000`; its JSON is text anyway.
    fn write_json_param(&self, sql: &mut String, placeholder: &str) {
        sql.push_str("/*!80000 CAST(*/");
        sql.push_str(placeholder);
        sql.push_str("/*!80000 AS JSON)*/");
    }

    /// MariaDB's JSON is text, so `=` would compare key order and `1` vs `1.0`.
    fn write_json_eq(&self, sql: &mut String, lhs: &str, placeholder: &str, ne: bool) -> bool {
        // A comment in `lhs` would end the per-server one.
        if lhs.contains("*/") {
            return false;
        }
        let (op, want) = if ne { ("<>", 0) } else { ("=", 1) };
        // Bare in a WHERE, MariaDB 11 takes a NULL `JSON_EQUALS` as true.
        ByServer {
            mysql: &format!("{lhs} {op} CAST({placeholder} AS JSON)"),
            mariadb: &format!("JSON_EQUALS({lhs}, {placeholder}) = {want}"),
        }
        .write(sql);
        true
    }

    fn write_json_contains(&self, sql: &mut String, qualified_col: &str, placeholder: &str) {
        // `JSON_CONTAINS(target, candidate)` is true when every
        // value in the candidate is in the target, the same as
        // Postgres' `target @> candidate`.
        sql.push_str("JSON_CONTAINS(");
        sql.push_str(qualified_col);
        sql.push_str(", ");
        sql.push_str(placeholder);
        sql.push(')');
    }

    fn write_json_contained_by(&self, sql: &mut String, qualified_col: &str, placeholder: &str) {
        // The same call with the arguments the other way round.
        sql.push_str("JSON_CONTAINS(");
        sql.push_str(placeholder);
        sql.push_str(", ");
        sql.push_str(qualified_col);
        sql.push(')');
    }

    fn write_json_has_key(&self, sql: &mut String, qualified_col: &str, placeholder: &str) {
        // `JSON_CONTAINS_PATH(col, 'one', '$."key"')` is MySQL's
        // top-level key check.
        write_my_json_has_keys(sql, qualified_col, &[placeholder], "one");
    }

    fn write_json_has_any_keys(
        &self,
        sql: &mut String,
        qualified_col: &str,
        placeholders: &[String],
    ) {
        write_my_json_has_keys(sql, qualified_col, placeholders, "one");
    }

    fn write_json_has_all_keys(
        &self,
        sql: &mut String,
        qualified_col: &str,
        placeholders: &[String],
    ) {
        write_my_json_has_keys(sql, qualified_col, placeholders, "all");
    }

    /// MySQL's `ON DUPLICATE KEY UPDATE` takes no target column
    /// list; it fires on any unique violation, so a `DoUpdate` is
    /// refused unless its target is the only unique key a row can hit:
    ///
    /// ```sql
    /// INSERT INTO `t` (a, b) VALUES (?, ?)
    /// ON DUPLICATE KEY UPDATE `a` = VALUES(`a`), `b` = VALUES(`b`)
    /// ```
    ///
    /// `DoNothing` becomes a self-assignment, which skips the
    /// duplicate. `INSERT IGNORE` would do that too, but it also hides
    /// every other error. The assignment also sets
    /// [`SKIPPED_INSERT_ID`]: sqlx sets `CLIENT_FOUND_ROWS`, so a skip
    /// reports 1 affected row, the same as an insert.
    ///
    /// On an auto-increment PK, `DoUpdate`'s first assignment also calls
    /// `LAST_INSERT_ID(pk)`, so it names the updated row, not a stale one.
    fn write_conflict_clause(
        &self,
        sql: &mut String,
        model: &ModelSchema,
        caller_set: &[&str],
        conflict: &ConflictClause,
    ) -> Result<(), SqlError> {
        match conflict {
            ConflictClause::DoNothing => {
                sql.push_str(" ON DUPLICATE KEY UPDATE ");
                write_my_skip_marker(sql, model, &SKIPPED_INSERT_ID.to_string())?;
            }
            ConflictClause::DoUpdate {
                target,
                update_columns,
            } => {
                if update_columns.is_empty() {
                    return Err(SqlError::EmptyUpdateSet);
                }
                // The clause fires on every unique key, so another key
                // that can clash would update the wrong row.
                if !my_target_is_only_unique_key(model, caller_set, target) {
                    return Err(SqlError::ConflictNotSupportedInDialect {
                        shape: "DO UPDATE on a target that is not the only unique key",
                        dialect: "mysql",
                    });
                }
                sql.push_str(" ON DUPLICATE KEY UPDATE ");
                // The id rides inside the first assignment: assigning the
                // auto-increment PK fails with 1869 in a batch (#2200).
                let mut id = model.primary_key().filter(|f| f.is_serial()).map(|pk| {
                    let mut id = String::new();
                    write_my_ident(&mut id, pk.column);
                    id
                });
                for (i, col) in update_columns.iter().enumerate() {
                    if i > 0 {
                        sql.push_str(", ");
                    }
                    let mut value = String::from("VALUES(");
                    write_my_ident(&mut value, col);
                    value.push(')');
                    write_my_ident(sql, col);
                    sql.push_str(" = ");
                    match id.take() {
                        Some(id) => write_my_set_insert_id(sql, &id, &value),
                        None => sql.push_str(&value),
                    }
                }
            }
        }
        Ok(())
    }

    // ---- advisory locks ----

    // GET_LOCK names are server-wide: suffix the database so tenants don't share one (#1991).
    fn acquire_session_lock_sql(&self) -> Option<String> {
        Some(format!(
            "SELECT GET_LOCK({}, 0)",
            my_db_lock_name(self.placeholder(1))
        ))
    }

    fn release_session_lock_sql(&self) -> Option<String> {
        Some(format!(
            "SELECT RELEASE_LOCK({})",
            my_db_lock_name(self.placeholder(1))
        ))
    }

    // MySQL has no transaction-scoped advisory lock.

    // ---- compilation ----

    fn compile_select(&self, query: &SelectQuery) -> Result<CompiledStatement, SqlError> {
        let mut b = Sql::new(self);
        write_select(&mut b, query)?;
        b.finish()
    }

    fn compile_count(&self, query: &CountQuery) -> Result<CompiledStatement, SqlError> {
        let mut b = Sql::new(self);
        write_count(&mut b, query)?;
        b.finish()
    }

    fn compile_aggregate(&self, query: &AggregateQuery) -> Result<CompiledStatement, SqlError> {
        let mut b = Sql::new(self);
        write_aggregate(&mut b, query)?;
        b.finish()
    }

    fn compile_insert(&self, query: &InsertQuery) -> Result<CompiledStatement, SqlError> {
        let mut b = Sql::with_capacity(self, query.values.len());
        write_insert(&mut b, query)?;
        b.finish()
    }

    fn compile_bulk_insert(&self, query: &BulkInsertQuery) -> Result<CompiledStatement, SqlError> {
        let mut b = Sql::with_capacity(self, query.columns.len() * query.rows.len());
        write_bulk_insert(&mut b, query)?;
        b.finish()
    }

    fn compile_update(&self, query: &UpdateQuery) -> Result<CompiledStatement, SqlError> {
        let mut b = Sql::new(self);
        write_update(&mut b, query)?;
        b.finish()
    }

    fn compile_delete(&self, query: &DeleteQuery) -> Result<CompiledStatement, SqlError> {
        let mut b = Sql::new(self);
        write_delete(&mut b, query)?;
        b.finish()
    }

    fn compile_bulk_update(&self, query: &BulkUpdateQuery) -> Result<CompiledStatement, SqlError> {
        // Needs MySQL 8.0.19+, for the `VALUES ROW(…)` table
        // constructor. On anything older, update row by row.
        let mut b = Sql::new(self);
        write_mysql_bulk_update(&mut b, query)?;
        b.finish()
    }
}

/// Bound prefix + SHA1 of the current database: 57 chars, under MySQL's 64-char lock-name cap.
fn my_db_lock_name(prefix: impl std::fmt::Display) -> String {
    format!("CONCAT({prefix}, SHA1(COALESCE(DATABASE(), '')))")
}

/// Write a backtick-quoted identifier in place, for the conflict
/// clause, which writes straight into a `String` rather than through
/// the [`Sql`] builder.
fn write_my_ident(sql: &mut String, name: &str) {
    sql.push('`');
    for ch in name.chars() {
        if ch == '`' {
            sql.push_str("``");
        } else {
            sql.push(ch);
        }
    }
    sql.push('`');
}

/// True when every unique key a new row can clash on is `target`. A key
/// with an `Auto` column the caller did not set holds a generated value,
/// so it never clashes; a caller-set one can.
fn my_target_is_only_unique_key(model: &ModelSchema, caller_set: &[&str], target: &[&str]) -> bool {
    let fits = |cols: &[&str]| {
        let generated = cols.iter().any(|c| {
            model
                .field_by_column(c)
                .is_some_and(|f| f.auto && !caller_set.contains(c))
        });
        generated || (cols.len() == target.len() && cols.iter().all(|c| target.contains(c)))
    };
    let pk: Vec<&str> = model
        .fields
        .iter()
        .filter(|f| f.primary_key)
        .map(|f| f.column)
        .collect();
    (pk.is_empty() || fits(&pk))
        && model
            .fields
            .iter()
            .filter(|f| f.unique && !f.primary_key)
            .all(|f| fits(&[f.column]))
        && model
            .indexes
            .iter()
            .filter(|i| i.unique)
            .all(|i| fits(i.columns))
}

/// `<col> = IF(LAST_INSERT_ID(<id>), <col>, <col>)`: a no-op write that
/// sets the insert id and leaves `ON UPDATE CURRENT_TIMESTAMP` alone.
/// `<col>` avoids the auto-increment column, whose assignment fails with
/// 1869 once two rows of one batch collide (#2200); a table whose other
/// columns are all generated falls back to it and can still hit 1869.
fn write_my_skip_marker(sql: &mut String, model: &ModelSchema, id: &str) -> Result<(), SqlError> {
    let plain = |f: &&FieldSchema| !f.is_serial() && f.generated_as.is_none();
    let pivot = model
        .primary_key()
        .filter(plain)
        .or_else(|| model.fields.iter().find(plain))
        .or_else(|| model.primary_key())
        .ok_or(SqlError::MissingPrimaryKey)?
        .column;
    let mut col = String::new();
    write_my_ident(&mut col, pivot);
    sql.push_str(&col);
    sql.push_str(" = ");
    write_my_set_insert_id(sql, id, &col);
    Ok(())
}

/// `IF(LAST_INSERT_ID(<id>), <value>, <value>)`: `<value>`, with the
/// session insert id set to `<id>` on the way.
fn write_my_set_insert_id(sql: &mut String, id: &str, value: &str) {
    sql.push_str("IF(LAST_INSERT_ID(");
    sql.push_str(id);
    sql.push_str("), ");
    sql.push_str(value);
    sql.push_str(", ");
    sql.push_str(value);
    sql.push(')');
}

/// Shared body of [`MySql::write_json_has_any_keys`] /
/// [`MySql::write_json_has_all_keys`]. `mode` is `"one"` for "any key
/// matches" or `"all"` for "every key matches".
fn write_my_json_has_keys<P: AsRef<str>>(
    sql: &mut String,
    qualified_col: &str,
    placeholders: &[P],
    mode: &'static str,
) {
    sql.push_str("JSON_CONTAINS_PATH(");
    sql.push_str(qualified_col);
    sql.push_str(", '");
    sql.push_str(mode);
    sql.push('\'');
    // `JSON_QUOTE` makes the bound key one literal member, as PG's `?`
    // reads it; bare, `a.b` or `*` is a path and `2fa` a syntax error.
    for p in placeholders {
        sql.push_str(", CONCAT('$.', JSON_QUOTE(");
        sql.push_str(p.as_ref());
        sql.push_str("))");
    }
    sql.push(')');
}

/// MySQL's bulk UPDATE: `UPDATE t INNER JOIN (VALUES ROW(…), …) AS
/// d(pk, c1, …) ON t.pk = d.pk SET t.c1 = d.c1`. MariaDB has no `ROW(…)`
/// and gets `(SELECT ? AS pk, … UNION ALL SELECT …) AS d` instead. It lives
/// here rather than in `writers` because the syntax is MySQL's own.
fn write_mysql_bulk_update(
    b: &mut crate::sql::writers::Sql<'_>,
    query: &crate::core::BulkUpdateQuery,
) -> Result<(), SqlError> {
    if query.rows.is_empty() {
        return Err(SqlError::EmptyBulkInsert);
    }
    if query.update_columns.is_empty() {
        return Err(SqlError::EmptyUpdateSet);
    }
    let pk_field = query
        .model
        .primary_key()
        .ok_or(SqlError::MissingPrimaryKey)?;
    let columns: Vec<String> = std::iter::once(pk_field.column)
        .chain(query.update_columns.iter().copied())
        .map(|c| b.d.quote_ident(c))
        .collect();

    b.sql.push_str("UPDATE ");
    b.write_ident(query.model.table);
    b.sql.push_str(" INNER JOIN (");
    // Both spellings bind the rows in the same order.
    let (mut my, mut maria) = (String::from("VALUES "), String::new());
    for (r, row) in query.rows.iter().enumerate() {
        my.push_str(if r == 0 { "ROW(" } else { ", ROW(" });
        maria.push_str(if r == 0 {
            "SELECT "
        } else {
            " UNION ALL SELECT "
        });
        for (i, val) in row.iter().enumerate() {
            b.params.push(val.clone());
            let p = b.d.placeholder(b.params.len());
            let sep = if i > 0 { ", " } else { "" };
            my.push_str(sep);
            my.push_str(&p);
            maria.push_str(sep);
            maria.push_str(&p);
            if let (0, Some(c)) = (r, columns.get(i)) {
                maria.push_str(" AS ");
                maria.push_str(c);
            }
        }
        my.push(')');
    }
    my.push_str(") AS __data(");
    my.push_str(&columns.join(", "));
    my.push(')');
    maria.push_str(") AS __data");
    ByServer {
        mysql: &my,
        mariadb: &maria,
    }
    .write(&mut b.sql);
    b.sql.push_str(" ON ");
    b.write_ident(query.model.table);
    b.sql.push('.');
    b.write_ident(pk_field.column);
    b.sql.push_str(" = __data.");
    b.write_ident(pk_field.column);
    b.sql.push_str(" SET ");
    let mut first = true;
    for col in &query.update_columns {
        if !first {
            b.sql.push_str(", ");
        }
        first = false;
        b.write_ident(query.model.table);
        b.sql.push('.');
        b.write_ident(col);
        b.sql.push_str(" = __data.");
        b.write_ident(col);
    }
    Ok(())
}

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

    #[test]
    fn name_is_mysql() {
        assert_eq!(MySql.name(), "mysql");
    }

    #[test]
    fn quote_literal_escapes_backslash_and_quote() {
        assert_eq!(MySql.quote_literal(r"a\'b"), r"'a\\''b'");
        assert_eq!(crate::sql::Postgres.quote_literal(r"a\'b"), r"'a\''b'");
    }

    #[test]
    fn quote_ident_uses_backticks() {
        assert_eq!(MySql.quote_ident("col"), "`col`");
        assert_eq!(MySql.quote_ident("schema.table"), "`schema.table`");
    }

    #[test]
    fn quote_ident_escapes_embedded_backticks() {
        assert_eq!(MySql.quote_ident("a`b"), "`a``b`");
    }

    #[test]
    fn placeholder_is_question_mark() {
        assert_eq!(MySql.placeholder(1), "?");
        assert_eq!(MySql.placeholder(7), "?");
    }

    #[test]
    fn serial_type_uses_auto_increment() {
        assert_eq!(MySql.serial_type(FieldType::I32), "INT AUTO_INCREMENT");
        assert_eq!(MySql.serial_type(FieldType::I64), "BIGINT AUTO_INCREMENT");
    }

    #[test]
    fn unbounded_string_is_longtext() {
        // #1708: TEXT caps at 64 KiB; PG and SQLite text has no cap.
        assert_eq!(MySql.column_type(FieldType::String, None), "LONGTEXT");
        assert_eq!(
            MySql.column_type(FieldType::String, Some(64)),
            "VARCHAR(64)"
        );
        assert_eq!(
            MySql.ci_text_type(None),
            "LONGTEXT CHARACTER SET utf8mb4 /*!80000 COLLATE utf8mb4_0900_as_ci */ \
             /*M!101000 COLLATE utf8mb4_uca1400_nopad_as_ci */"
        );
    }

    #[test]
    fn bool_literal_uses_one_zero() {
        assert_eq!(MySql.bool_literal(true), "1");
        assert_eq!(MySql.bool_literal(false), "0");
    }

    #[test]
    fn null_cast_returns_none() {
        // MySQL doesn't need NULL casts — sqlx binds the right type.
        assert!(MySql.null_cast(FieldType::I32).is_none());
        assert!(MySql.null_cast(FieldType::String).is_none());
    }

    #[test]
    fn does_not_support_returning() {
        assert!(!MySql.supports_returning());
    }

    #[test]
    fn does_not_support_concurrent_index() {
        assert!(!MySql.supports_concurrent_index());
    }

    #[test]
    fn default_on_lob_columns_uses_expression_form() {
        // MySQL rejects a literal `DEFAULT` on JSON, TEXT, BLOB and
        // GEOMETRY columns, but accepts the `DEFAULT (<expr>)` form,
        // so those types must be wrapped. An unbounded `String` is
        // one of them, since it renders as LONGTEXT.
        assert_eq!(
            MySql.translate_default_expr("'{}'", "string", None),
            "('{}')"
        );
        assert_eq!(MySql.translate_default_expr("'{}'", "json", None), "('{}')");
        assert_eq!(MySql.translate_default_expr("''", "binary", None), "('')");
        // A bounded String is a VARCHAR(n), which takes a literal
        // default, so it must not be wrapped.
        assert_eq!(
            MySql.translate_default_expr("''", "string", Some(500)),
            "''"
        );
        // Non-LOB scalars are never wrapped.
        assert_eq!(MySql.translate_default_expr("0", "i64", None), "0");
        // now() still becomes the (6)-precision form.
        assert_eq!(
            MySql.translate_default_expr("now()", "datetime", None),
            "CURRENT_TIMESTAMP(6)"
        );
    }

    #[test]
    fn supports_op_accepts_every_operator_after_batch4() {
        // ILIKE, IS DISTINCT FROM and the JSONB operators all
        // translate, so `supports_op` is true for every one.
        use crate::core::Op;
        for op in [
            Op::Eq,
            Op::Ne,
            Op::Lt,
            Op::Lte,
            Op::Gt,
            Op::Gte,
            Op::In,
            Op::NotIn,
            Op::Like,
            Op::NotLike,
            Op::ILike,
            Op::NotILike,
            Op::Between,
            Op::IsNull,
            Op::IsDistinctFrom,
            Op::IsNotDistinctFrom,
            Op::JsonContains,
            Op::JsonContainedBy,
            Op::JsonHasKey,
            Op::JsonHasAnyKey,
            Op::JsonHasAllKeys,
        ] {
            assert!(MySql.supports_op(op), "expected {op:?} to be supported");
        }
    }

    #[test]
    fn session_lock_uses_get_lock() {
        let acq = MySql.acquire_session_lock_sql().unwrap();
        assert!(acq.contains("GET_LOCK"));
        assert!(acq.contains("?"));
        assert!(acq.contains("DATABASE()"), "{acq}");
        let rel = MySql.release_session_lock_sql().unwrap();
        assert!(rel.contains("RELEASE_LOCK"));
        assert!(rel.contains("DATABASE()"), "{rel}");
    }

    #[test]
    fn xact_lock_is_none() {
        assert!(MySql.acquire_xact_lock_sql().is_none());
    }

    #[test]
    fn conflict_do_nothing_assigns_the_pk_and_marks_the_skip() {
        let model = with_pk(
            empty_model_with("t", &[("code", FieldType::String), ("n", FieldType::I64)]),
            "code",
            false,
        );
        let mut sql = String::new();
        MySql
            .write_conflict_clause(&mut sql, model, &[], &ConflictClause::DoNothing)
            .unwrap();
        assert_eq!(
            sql,
            " ON DUPLICATE KEY UPDATE `code` = IF(LAST_INSERT_ID(18446744073709551615), `code`, `code`)"
        );
    }

    #[test]
    fn conflict_do_update_on_auto_pk_reports_the_updated_id() {
        let model = with_pk(
            empty_model_with("t", &[("id", FieldType::I64), ("a", FieldType::String)]),
            "id",
            true,
        );
        let mut sql = String::new();
        MySql
            .write_conflict_clause(
                &mut sql,
                model,
                &[],
                &ConflictClause::DoUpdate {
                    target: vec![],
                    update_columns: vec!["a"],
                },
            )
            .unwrap();
        assert_eq!(
            sql,
            " ON DUPLICATE KEY UPDATE `a` = IF(LAST_INSERT_ID(`id`), VALUES(`a`), VALUES(`a`))"
        );
    }

    /// #2200: assigning the auto-increment PK fails with 1869 in a batch.
    #[test]
    fn conflict_do_nothing_on_auto_pk_assigns_another_column() {
        let model = with_pk(
            empty_model_with("t", &[("id", FieldType::I64), ("code", FieldType::String)]),
            "id",
            true,
        );
        let mut sql = String::new();
        MySql
            .write_conflict_clause(&mut sql, model, &[], &ConflictClause::DoNothing)
            .unwrap();
        assert_eq!(
            sql,
            " ON DUPLICATE KEY UPDATE `code` = IF(LAST_INSERT_ID(18446744073709551615), `code`, `code`)"
        );
    }

    #[test]
    fn conflict_do_update_with_empty_target_translates() {
        let model = empty_model_with("t", &[("a", FieldType::String), ("b", FieldType::String)]);
        let mut sql = String::new();
        MySql
            .write_conflict_clause(
                &mut sql,
                model,
                &[],
                &ConflictClause::DoUpdate {
                    target: vec![],
                    update_columns: vec!["a", "b"],
                },
            )
            .unwrap();
        assert_eq!(
            sql,
            " ON DUPLICATE KEY UPDATE `a` = VALUES(`a`), `b` = VALUES(`b`)"
        );
    }

    #[test]
    fn conflict_do_update_with_target_silently_ignores_target() {
        // No other unique key can clash, so the target needs no clause.
        let model = empty_model_with("t", &[("id", FieldType::I64), ("a", FieldType::String)]);
        let mut sql = String::new();
        MySql
            .write_conflict_clause(
                &mut sql,
                model,
                &[],
                &ConflictClause::DoUpdate {
                    target: vec!["id"],
                    update_columns: vec!["a"],
                },
            )
            .expect("no other unique key, so the target is accepted");
        assert_eq!(sql, " ON DUPLICATE KEY UPDATE `a` = VALUES(`a`)");
    }

    /// A bound `Auto` PK can clash like any key.
    #[test]
    fn conflict_do_update_refuses_a_bound_auto_pk() {
        use crate::core::{InsertQuery, SqlValue};
        let base = with_pk(
            empty_model_with(
                "acct",
                &[("id", FieldType::I64), ("email", FieldType::String)],
            ),
            "id",
            true,
        );
        let fields: Vec<crate::core::FieldSchema> = base
            .fields
            .iter()
            .map(|f| crate::core::FieldSchema {
                unique: f.column == "email",
                ..*f
            })
            .collect();
        let model: &'static crate::core::ModelSchema =
            Box::leak(Box::new(crate::core::ModelSchema {
                fields: Box::leak(fields.into_boxed_slice()),
                ..*base
            }));
        let upsert = |columns: Vec<&'static str>, values| {
            InsertQuery::new(model, columns, values).on_conflict(ConflictClause::DoUpdate {
                target: vec!["email"],
                update_columns: vec!["email"],
            })
        };
        let email = || SqlValue::String("a@x".into());
        MySql
            .compile_insert(&upsert(vec!["email"], vec![email()]))
            .expect("an omitted Auto PK is generated, so it never clashes");
        let bound = MySql.compile_insert(&upsert(
            vec!["id", "email"],
            vec![SqlValue::I64(5), email()],
        ));
        assert!(
            matches!(bound, Err(SqlError::ConflictNotSupportedInDialect { .. })),
            "{bound:?}"
        );
    }

    /// A bound uuid PK clashes unless the writer marks it generated.
    #[test]
    fn conflict_do_update_trusts_only_a_generated_uuid_pk() {
        use crate::core::{InsertQuery, SqlValue};
        let base = with_pk(
            empty_model_with(
                "item",
                &[("id", FieldType::Uuid), ("name", FieldType::String)],
            ),
            "id",
            true,
        );
        let fields: Vec<crate::core::FieldSchema> = base
            .fields
            .iter()
            .map(|f| crate::core::FieldSchema {
                unique: f.column == "name",
                ..*f
            })
            .collect();
        let model: &'static crate::core::ModelSchema =
            Box::leak(Box::new(crate::core::ModelSchema {
                fields: Box::leak(fields.into_boxed_slice()),
                ..*base
            }));
        let q = InsertQuery::new(
            model,
            vec!["id", "name"],
            vec![
                SqlValue::Uuid(uuid::Uuid::nil()),
                SqlValue::String("a".into()),
            ],
        )
        .on_conflict(ConflictClause::DoUpdate {
            target: vec!["name"],
            update_columns: vec!["name"],
        });
        let set = MySql.compile_insert(&q);
        assert!(
            matches!(set, Err(SqlError::ConflictNotSupportedInDialect { .. })),
            "{set:?}"
        );
        MySql
            .compile_insert(&q.generated(vec!["id"]))
            .expect("a generated uuid PK never clashes");
    }

    #[test]
    fn ilike_translates_to_lower_like_lower() {
        use crate::core::{Filter, Op, SelectQuery, SqlValue, WhereExpr};
        let model = empty_model_with("users", &[("name", FieldType::String)]);
        let q = SelectQuery {
            model,
            joins: vec![],
            subquery_joins: vec![],
            where_clause: WhereExpr::Predicate(Filter {
                column: "name",
                op: Op::ILike,
                value: SqlValue::String("%Alice%".into()),
            }),
            search: None,
            order_by: vec![],
            limit: None,
            offset: None,
            lock_mode: None,
            compound: vec![],
            projection: None,
            distinct: None,
            compound_order_by: vec![],
            compound_limit: None,
            compound_offset: None,
            select_related: vec![],
        };
        let stmt = MySql.compile_select(&q).unwrap();
        assert_eq!(
            stmt.sql,
            "SELECT `name` FROM `users` WHERE LOWER(`name`) LIKE LOWER(?)"
        );
        assert_eq!(stmt.params.len(), 1);
    }

    #[test]
    fn not_ilike_translates_to_not_like() {
        use crate::core::{Filter, Op, SelectQuery, SqlValue, WhereExpr};
        let model = empty_model_with("users", &[("name", FieldType::String)]);
        let q = SelectQuery {
            model,
            joins: vec![],
            subquery_joins: vec![],
            where_clause: WhereExpr::Predicate(Filter {
                column: "name",
                op: Op::NotILike,
                value: SqlValue::String("%bot%".into()),
            }),
            search: None,
            order_by: vec![],
            limit: None,
            offset: None,
            lock_mode: None,
            compound: vec![],
            projection: None,
            distinct: None,
            compound_order_by: vec![],
            compound_limit: None,
            compound_offset: None,
            select_related: vec![],
        };
        let stmt = MySql.compile_select(&q).unwrap();
        assert!(stmt.sql.contains("LOWER(`name`) NOT LIKE LOWER(?)"));
    }

    #[test]
    fn is_distinct_from_translates_to_not_null_safe_eq() {
        use crate::core::{Filter, Op, SelectQuery, SqlValue, WhereExpr};
        let model = empty_model_with("users", &[("email", FieldType::String)]);
        let q = SelectQuery {
            model,
            joins: vec![],
            subquery_joins: vec![],
            where_clause: WhereExpr::Predicate(Filter {
                column: "email",
                op: Op::IsDistinctFrom,
                value: SqlValue::String("a@b".into()),
            }),
            search: None,
            order_by: vec![],
            limit: None,
            offset: None,
            lock_mode: None,
            compound: vec![],
            projection: None,
            distinct: None,
            compound_order_by: vec![],
            compound_limit: None,
            compound_offset: None,
            select_related: vec![],
        };
        let stmt = MySql.compile_select(&q).unwrap();
        assert!(stmt.sql.contains("NOT (`email` <=> ?)"));
    }

    #[test]
    fn is_not_distinct_from_translates_to_null_safe_eq() {
        use crate::core::{Filter, Op, SelectQuery, SqlValue, WhereExpr};
        let model = empty_model_with("users", &[("email", FieldType::String)]);
        let q = SelectQuery {
            model,
            joins: vec![],
            subquery_joins: vec![],
            where_clause: WhereExpr::Predicate(Filter {
                column: "email",
                op: Op::IsNotDistinctFrom,
                value: SqlValue::Null,
            }),
            search: None,
            order_by: vec![],
            limit: None,
            offset: None,
            lock_mode: None,
            compound: vec![],
            projection: None,
            distinct: None,
            compound_order_by: vec![],
            compound_limit: None,
            compound_offset: None,
            select_related: vec![],
        };
        let stmt = MySql.compile_select(&q).unwrap();
        // No outer NOT; bare null-safe equality.
        assert!(stmt.sql.contains("`email` <=> ?"));
        assert!(!stmt.sql.contains("NOT"));
    }

    #[test]
    fn json_contains_translates_to_json_contains_function() {
        use crate::core::{Filter, Op, SelectQuery, SqlValue, WhereExpr};
        let model = empty_model_with("posts", &[("meta", FieldType::Json)]);
        let q = SelectQuery {
            model,
            joins: vec![],
            subquery_joins: vec![],
            where_clause: WhereExpr::Predicate(Filter {
                column: "meta",
                op: Op::JsonContains,
                value: SqlValue::Json(serde_json::json!({"k": "v"})),
            }),
            search: None,
            order_by: vec![],
            limit: None,
            offset: None,
            lock_mode: None,
            compound: vec![],
            projection: None,
            distinct: None,
            compound_order_by: vec![],
            compound_limit: None,
            compound_offset: None,
            select_related: vec![],
        };
        let stmt = MySql.compile_select(&q).unwrap();
        assert!(stmt.sql.contains("JSON_CONTAINS(`meta`, ?)"));
        assert!(!stmt.sql.contains("@>"));
    }

    #[test]
    fn json_contained_by_swaps_argument_order() {
        use crate::core::{Filter, Op, SelectQuery, SqlValue, WhereExpr};
        let model = empty_model_with("posts", &[("meta", FieldType::Json)]);
        let q = SelectQuery {
            model,
            joins: vec![],
            subquery_joins: vec![],
            where_clause: WhereExpr::Predicate(Filter {
                column: "meta",
                op: Op::JsonContainedBy,
                value: SqlValue::Json(serde_json::json!({"k": "v"})),
            }),
            search: None,
            order_by: vec![],
            limit: None,
            offset: None,
            lock_mode: None,
            compound: vec![],
            projection: None,
            distinct: None,
            compound_order_by: vec![],
            compound_limit: None,
            compound_offset: None,
            select_related: vec![],
        };
        let stmt = MySql.compile_select(&q).unwrap();
        // Argument order is swapped vs JSON_CONTAINS — value first.
        assert!(stmt.sql.contains("JSON_CONTAINS(?, `meta`)"));
    }

    #[test]
    fn json_has_key_translates_to_contains_path_with_concat() {
        use crate::core::{Filter, Op, SelectQuery, SqlValue, WhereExpr};
        let model = empty_model_with("posts", &[("meta", FieldType::Json)]);
        let q = SelectQuery {
            model,
            joins: vec![],
            subquery_joins: vec![],
            where_clause: WhereExpr::Predicate(Filter {
                column: "meta",
                op: Op::JsonHasKey,
                value: SqlValue::String("title".into()),
            }),
            search: None,
            order_by: vec![],
            limit: None,
            offset: None,
            lock_mode: None,
            compound: vec![],
            projection: None,
            distinct: None,
            compound_order_by: vec![],
            compound_limit: None,
            compound_offset: None,
            select_related: vec![],
        };
        let stmt = MySql.compile_select(&q).unwrap();
        assert!(stmt
            .sql
            .contains("JSON_CONTAINS_PATH(`meta`, 'one', CONCAT('$.', JSON_QUOTE(?)))"));
    }

    #[test]
    fn json_has_any_keys_translates_to_contains_path_one() {
        use crate::core::{Filter, Op, SelectQuery, SqlValue, WhereExpr};
        let model = empty_model_with("posts", &[("meta", FieldType::Json)]);
        let q = SelectQuery {
            model,
            joins: vec![],
            subquery_joins: vec![],
            where_clause: WhereExpr::Predicate(Filter {
                column: "meta",
                op: Op::JsonHasAnyKey,
                value: SqlValue::List(vec![
                    SqlValue::String("title".into()),
                    SqlValue::String("body".into()),
                ]),
            }),
            search: None,
            order_by: vec![],
            limit: None,
            offset: None,
            lock_mode: None,
            compound: vec![],
            projection: None,
            distinct: None,
            compound_order_by: vec![],
            compound_limit: None,
            compound_offset: None,
            select_related: vec![],
        };
        let stmt = MySql.compile_select(&q).unwrap();
        assert!(stmt
            .sql
            .contains("JSON_CONTAINS_PATH(`meta`, 'one', CONCAT('$.', JSON_QUOTE(?)), CONCAT('$.', JSON_QUOTE(?)))"));
        assert_eq!(stmt.params.len(), 2);
    }

    #[test]
    fn json_has_all_keys_uses_all_mode() {
        use crate::core::{Filter, Op, SelectQuery, SqlValue, WhereExpr};
        let model = empty_model_with("posts", &[("meta", FieldType::Json)]);
        let q = SelectQuery {
            model,
            joins: vec![],
            subquery_joins: vec![],
            where_clause: WhereExpr::Predicate(Filter {
                column: "meta",
                op: Op::JsonHasAllKeys,
                value: SqlValue::List(vec![
                    SqlValue::String("a".into()),
                    SqlValue::String("b".into()),
                ]),
            }),
            search: None,
            order_by: vec![],
            limit: None,
            offset: None,
            lock_mode: None,
            compound: vec![],
            projection: None,
            distinct: None,
            compound_order_by: vec![],
            compound_limit: None,
            compound_offset: None,
            select_related: vec![],
        };
        let stmt = MySql.compile_select(&q).unwrap();
        assert!(stmt
            .sql
            .contains("JSON_CONTAINS_PATH(`meta`, 'all', CONCAT('$.', JSON_QUOTE(?)), CONCAT('$.', JSON_QUOTE(?)))"));
    }

    #[test]
    fn bulk_update_translates_to_inner_join_values_row() {
        use crate::core::{BulkUpdateQuery, SqlValue};
        let model = empty_model_with(
            "users",
            &[("id", FieldType::I64), ("name", FieldType::String)],
        );
        // Mark the id field as the PK so primary_key() resolves.
        let pk_model = with_pk(model, "id", false);
        let q = BulkUpdateQuery {
            model: pk_model,
            update_columns: vec!["name"],
            rows: vec![
                vec![SqlValue::I64(1), SqlValue::String("Alice".into())],
                vec![SqlValue::I64(2), SqlValue::String("Bob".into())],
            ],
        };
        let stmt = MySql.compile_bulk_update(&q).unwrap();
        // Spot-check the key shape: VALUES ROW(?, ?) plus the JOIN
        // on the PK plus the SET on the qualified target column.
        assert!(stmt
            .sql
            .starts_with("UPDATE `users` INNER JOIN (/*!80000 VALUES "));
        assert!(stmt.sql.contains("ROW(?, ?), ROW(?, ?)"));
        assert!(stmt
            .sql
            .contains("/*M! SELECT ? AS `id`, ? AS `name` UNION ALL SELECT ?, ?) AS __data */"));
        assert!(stmt.sql.contains(") AS __data(`id`, `name`)"));
        assert!(stmt.sql.contains("ON `users`.`id` = __data.`id`"));
        assert!(stmt.sql.contains("SET `users`.`name` = __data.`name`"));
        assert_eq!(stmt.params.len(), 4);
    }

    /// Copy `model` with `pk_col` marked as the primary key, `auto`
    /// making it auto-increment.
    fn with_pk(
        model: &'static crate::core::ModelSchema,
        pk_col: &'static str,
        auto: bool,
    ) -> &'static crate::core::ModelSchema {
        let new_fields: Vec<crate::core::FieldSchema> = model
            .fields
            .iter()
            .map(|f| {
                let mut f = *f;
                if f.column == pk_col {
                    f.primary_key = true;
                    f.auto = auto;
                }
                f
            })
            .collect();
        let leaked: &'static [crate::core::FieldSchema] = Box::leak(new_fields.into_boxed_slice());
        Box::leak(Box::new(crate::core::ModelSchema {
            fields: leaked,
            ..*model
        }))
    }

    // Hand-built IR, to confirm the writers and this dialect
    // produce backticks and `?` placeholders, with no `RETURNING`
    // and no NULL casts.

    #[test]
    fn select_emits_backticks_and_question_marks() {
        use crate::core::{Filter, Op, SelectQuery, SqlValue, WhereExpr};
        let model = empty_model_with(
            "users",
            &[("id", FieldType::I64), ("name", FieldType::String)],
        );
        let q = SelectQuery {
            model,
            joins: vec![],
            subquery_joins: vec![],
            where_clause: WhereExpr::Predicate(Filter {
                column: "name",
                op: Op::Eq,
                value: SqlValue::String("alice".into()),
            }),
            search: None,
            order_by: vec![],
            limit: None,
            offset: None,
            lock_mode: None,
            compound: vec![],
            projection: None,
            distinct: None,
            compound_order_by: vec![],
            compound_limit: None,
            compound_offset: None,
            select_related: vec![],
        };
        let stmt = MySql.compile_select(&q).unwrap();
        assert_eq!(
            stmt.sql,
            "SELECT `id`, `name` FROM `users` WHERE `name` = ?"
        );
        assert_eq!(stmt.params.len(), 1);
    }

    #[test]
    fn insert_with_returning_errors() {
        // MySQL has no RETURNING, so the writer errors rather than
        // emit SQL the parser would reject.
        use crate::core::{InsertQuery, SqlValue};
        let model = empty_model_with(
            "users",
            &[("id", FieldType::I64), ("name", FieldType::String)],
        );
        let q = InsertQuery {
            model,
            columns: vec!["name"],
            values: vec![SqlValue::String("alice".into())],
            returning: vec!["id"],
            on_conflict: None,
            generated: vec![],
        };
        let err = MySql.compile_insert(&q).unwrap_err();
        assert!(matches!(
            err,
            SqlError::OperatorNotSupportedInDialect {
                op: "RETURNING",
                dialect: "mysql"
            }
        ));
    }

    fn empty_model_with(
        table: &'static str,
        fields: &[(&'static str, FieldType)],
    ) -> &'static crate::core::ModelSchema {
        // Build a minimal ModelSchema for tests. Fields are leaked
        // for `'static` lifetime — fine in test code.
        let field_vec: Vec<crate::core::FieldSchema> = fields
            .iter()
            .map(|(col, ty)| crate::core::FieldSchema {
                name: col,
                column: col,
                ty: *ty,
                nullable: false,
                primary_key: false,
                relation: None,
                max_length: None,
                min: None,
                max: None,
                default: None,
                auto: false,
                auto_now: false,
                unique: false,
                generated_as: None,
                help_text: None,
                choices: None,
                db_comment: None,
                verbose_name: None,
                editable: true,
                blank: false,
                case_insensitive: false,
                fk_on_delete: None,
                validators: &[],
                cast: None,
            })
            .collect();
        let leaked: &'static [crate::core::FieldSchema] = Box::leak(field_vec.into_boxed_slice());
        Box::leak(Box::new(crate::core::ModelSchema {
            name: table,
            table,
            fields: leaked,
            display: None,
            app_label: None,
            admin: None,
            soft_delete_column: None,
            permissions: false,
            audit_track: None,
            m2m: &[],
            indexes: &[],
            check_constraints: &[],
            exclusion_constraints: &[],
            default_permissions: &[],
            composite_relations: &[],
            generic_relations: &[],
            // Every introspected schema is tenant-scoped.
            scope: crate::core::ModelScope::Tenant,
            default_order: &[],
            is_view: false,
            verbose_name: None,
            verbose_name_plural: None,
            managed: true,
            db_table_comment: None,
            default_related_name: None,
            base_manager_name: None,
            required_db_vendor: None,
            required_db_features: &[],
            order_with_respect_to: None,
            proxy: false,
            get_latest_by: None,
            extra_permissions: &[],
            global_scopes: &[],
        }))
    }
}