sql-splitter 1.18.1

High-performance CLI tool for splitting large SQL dump files into individual table files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
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
//! Convert command for translating SQL dumps between dialects.
//!
//! Supports conversion between MySQL, PostgreSQL, and SQLite dialects with:
//! - Identifier quoting conversion (backticks ↔ double quotes)
//! - String escape normalization (\' ↔ '')
//! - Data type mapping (AUTO_INCREMENT ↔ SERIAL ↔ INTEGER PRIMARY KEY)
//! - COPY FROM stdin → INSERT conversion
//! - Session header conversion
//! - Warning system for unsupported features

pub mod copy_to_insert;
mod types;
mod warnings;

#[allow(unused_imports)]
pub use copy_to_insert::{
    copy_to_inserts, parse_copy_data, parse_copy_header, CopyHeader, CopyValue,
};

use crate::parser::{Parser, SqlDialect, StatementType};
use crate::splitter::{open_input, open_input_with_progress};
use indicatif::{ProgressBar, ProgressStyle};
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufWriter, Read, Write};
use std::path::PathBuf;

pub use types::TypeMapper;
// Only `render::ddl` (lib-only; see the `copy_to_insert` re-export above for
// the same bin/lib split) calls this, so the `bin` target's parallel module
// tree sees it as unused.
#[allow(unused_imports)]
pub(crate) use types::map_column_type;
pub use warnings::{print_warnings_summary, ConvertWarning, WarningCollector};

/// Configuration for the convert command
#[derive(Debug)]
pub struct ConvertConfig {
    /// Input SQL file
    pub input: PathBuf,
    /// Output SQL file (None for stdout)
    pub output: Option<PathBuf>,
    /// Source dialect (auto-detected if None)
    pub from_dialect: Option<SqlDialect>,
    /// Target dialect
    pub to_dialect: SqlDialect,
    /// Dry run mode
    pub dry_run: bool,
    /// Show progress
    pub progress: bool,
    /// Strict mode (fail on any unsupported feature)
    pub strict: bool,
}

impl Default for ConvertConfig {
    fn default() -> Self {
        Self {
            input: PathBuf::new(),
            output: None,
            from_dialect: None,
            to_dialect: SqlDialect::Postgres,
            dry_run: false,
            progress: false,
            strict: false,
        }
    }
}

/// Statistics from convert operation
#[derive(Debug, Default, serde::Serialize)]
pub struct ConvertStats {
    /// Total statements processed
    pub statements_processed: u64,
    /// Statements converted
    pub statements_converted: u64,
    /// Statements passed through unchanged
    pub statements_unchanged: u64,
    /// Statements skipped (unsupported)
    pub statements_skipped: u64,
    /// Warnings generated
    pub warnings: Vec<ConvertWarning>,
}

/// Main converter that dispatches to specific dialect converters
pub struct Converter {
    from: SqlDialect,
    to: SqlDialect,
    warnings: WarningCollector,
    strict: bool,
    /// Pending COPY header for data block processing
    pending_copy_header: Option<CopyHeader>,
    /// Converted `CREATE TABLE` text, keyed by [`table_key`]. Only retained when
    /// the target needs to re-declare a column later (MySQL's `MODIFY COLUMN`
    /// takes a full definition), so this holds DDL for the schema and never row
    /// data.
    created_tables: HashMap<String, String>,
    /// Statements that only become valid once the rest of the dump has been
    /// applied; drained by [`Converter::take_deferred_statements`].
    deferred: Vec<String>,
}

/// Normalise a table name for lookup: unquoted, unqualified, lowercased, so
/// `public."Posts"` and `` `posts` `` resolve to the same entry.
fn table_key(name: &str) -> String {
    name.rsplit('.')
        .next()
        .unwrap_or(name)
        .trim_matches(|c| c == '"' || c == '`' || c == '[' || c == ']')
        .to_lowercase()
}

/// The type `column` was declared with in a converted `CREATE TABLE`, e.g.
/// `"integer"` for `  id integer NOT NULL,`.
///
/// Anchored on the column-list delimiter (`(` or `,`) rather than start-of-line,
/// so it reads a definition whether the DDL is formatted one column per line or
/// collapsed onto one. Captures a single word plus an optional length: callers
/// re-declare integer columns, and a greedier pattern would swallow the
/// trailing `NOT NULL`. A key clause such as `PRIMARY KEY (id)` cannot match,
/// since a type has to follow the name.
fn declared_column_type(create_table: &str, column: &str) -> Option<String> {
    use regex::Regex;

    let pattern = format!(
        r#"(?i)[(,]\s*[`"\[]?{}[`"\]]?\s+(?P<type>[A-Za-z]+(?:\s*\(\s*\d+\s*\))?)"#,
        regex::escape(column)
    );
    let captures = Regex::new(&pattern).ok()?.captures(create_table)?;
    Some(captures.name("type")?.as_str().trim().to_string())
}

impl Converter {
    pub fn new(from: SqlDialect, to: SqlDialect) -> Self {
        Self {
            from,
            to,
            warnings: WarningCollector::new(),
            strict: false,
            pending_copy_header: None,
            created_tables: HashMap::new(),
            deferred: Vec::new(),
        }
    }

    /// Statements held back until the whole dump has been applied, in the order
    /// they were queued. Callers must append these to the output after the last
    /// converted statement; draining leaves the converter reusable.
    pub fn take_deferred_statements(&mut self) -> Vec<String> {
        std::mem::take(&mut self.deferred)
    }

    pub fn with_strict(mut self, strict: bool) -> Self {
        self.strict = strict;
        self
    }

    /// Check if we have a pending COPY header (waiting for data block)
    pub fn has_pending_copy(&self) -> bool {
        self.pending_copy_header.is_some()
    }

    /// Process a COPY data block using the pending header
    pub fn process_copy_data(&mut self, data: &[u8]) -> Result<Vec<Vec<u8>>, ConvertWarning> {
        if let Some(header) = self.pending_copy_header.take() {
            if self.from == SqlDialect::Postgres && self.to != SqlDialect::Postgres {
                // Convert COPY data to INSERT statements
                let inserts = copy_to_inserts(&header, data, self.to);
                return Ok(inserts);
            }
        }
        // Pass through if same dialect or no pending header
        Ok(vec![data.to_vec()])
    }

    /// Convert a single statement
    pub fn convert_statement(&mut self, stmt: &[u8]) -> Result<Vec<u8>, ConvertWarning> {
        let (stmt_type, table_name) =
            Parser::<&[u8]>::parse_statement_with_dialect(stmt, self.from);

        let table = if table_name.is_empty() {
            None
        } else {
            Some(table_name.as_str())
        };

        match stmt_type {
            StatementType::CreateTable => self.convert_create_table(stmt, table),
            StatementType::Insert => self.convert_insert(stmt, table),
            StatementType::CreateIndex => self.convert_create_index(stmt),
            StatementType::AlterTable => self.convert_alter_table(stmt),
            StatementType::DropTable => self.convert_drop_table(stmt),
            StatementType::Copy => self.convert_copy(stmt, table),
            StatementType::Unknown => self.convert_other(stmt),
        }
    }

    /// Convert CREATE TABLE statement
    fn convert_create_table(
        &mut self,
        stmt: &[u8],
        table_name: Option<&str>,
    ) -> Result<Vec<u8>, ConvertWarning> {
        let stmt_str = String::from_utf8_lossy(stmt);
        let mut result = stmt_str.to_string();

        // Detect unsupported features BEFORE conversion (so we see original types)
        self.detect_unsupported_features(&result, table_name)?;

        // Convert MSSQL-specific syntax BEFORE identifier conversion
        // (so we can strip [dbo]. schema prefix properly)
        if self.from == SqlDialect::Mssql && self.to != SqlDialect::Mssql {
            result = self.strip_mssql_schema_prefix(&result);
            result = self.convert_mssql_getdate(&result);
            result = self.strip_mssql_on_filegroup(&result);
            result = self.strip_mssql_clustered(&result);
            result = self.convert_mssql_unicode_strings(&result);
        }

        // Convert identifier quoting
        result = self.convert_identifiers(&result);

        // Convert data types
        result = self.convert_data_types(&result);

        // Convert AUTO_INCREMENT
        result = self.convert_auto_increment(&result, table_name);

        // Convert PostgreSQL-specific syntax
        if self.from == SqlDialect::Postgres && self.to != SqlDialect::Postgres {
            result = self.strip_postgres_casts(&result);
            result = self.convert_nextval(&result);
            result = self.convert_default_now(&result);
            result = self.strip_schema_prefix(&result);
        }

        // Convert string escapes
        result = self.convert_string_escapes(&result);

        // Strip MySQL conditional comments
        result = self.strip_conditional_comments(&result);

        // Convert ENGINE clause
        result = self.strip_engine_clause(&result);

        // Convert CHARSET/COLLATE
        result = self.strip_charset_clauses(&result);

        // Strip MySQL inline column/table COMMENT annotations
        result = self.strip_mysql_comment_clauses(&result);

        // Convert UNIQUE KEY ... USING BTREE table constraints
        result = self.convert_unique_key_constraint(&result);

        // MySQL is the only target that has to re-declare a column after the
        // fact (to restore AUTO_INCREMENT), and that needs the column's
        // converted type. Retained only for that target so no other conversion
        // pays for the bookkeeping.
        if self.to == SqlDialect::MySql {
            if let Some(name) = table_name {
                self.created_tables.insert(table_key(name), result.clone());
            }
        }

        Ok(result.into_bytes())
    }

    /// Convert INSERT statement
    fn convert_insert(
        &mut self,
        stmt: &[u8],
        _table_name: Option<&str>,
    ) -> Result<Vec<u8>, ConvertWarning> {
        let stmt_str = String::from_utf8_lossy(stmt);
        let mut result = stmt_str.to_string();

        // Convert MSSQL-specific syntax BEFORE identifier conversion
        if self.from == SqlDialect::Mssql && self.to != SqlDialect::Mssql {
            result = self.strip_mssql_schema_prefix(&result);
            result = self.convert_mssql_unicode_strings(&result);
        }

        // Convert identifier quoting
        result = self.convert_identifiers(&result);

        // Convert PostgreSQL-specific syntax
        if self.from == SqlDialect::Postgres && self.to != SqlDialect::Postgres {
            result = self.strip_postgres_casts(&result);
            result = self.strip_schema_prefix(&result);
        }

        // Convert string escapes (careful with data!)
        result = self.convert_string_escapes(&result);

        Ok(result.into_bytes())
    }

    /// Convert CREATE INDEX statement
    fn convert_create_index(&mut self, stmt: &[u8]) -> Result<Vec<u8>, ConvertWarning> {
        let stmt_str = String::from_utf8_lossy(stmt);
        let mut result = stmt_str.to_string();

        // Convert MSSQL-specific syntax BEFORE identifier conversion
        if self.from == SqlDialect::Mssql && self.to != SqlDialect::Mssql {
            result = self.strip_mssql_schema_prefix(&result);
            result = self.strip_mssql_clustered(&result);
        }

        // Convert identifier quoting
        result = self.convert_identifiers(&result);

        // Convert PostgreSQL-specific syntax
        if self.from == SqlDialect::Postgres && self.to != SqlDialect::Postgres {
            result = self.strip_postgres_casts(&result);
            result = self.strip_schema_prefix(&result);
        }

        // Detect FULLTEXT/SPATIAL
        if result.contains("FULLTEXT") || result.contains("fulltext") {
            self.warnings.add(ConvertWarning::UnsupportedFeature {
                feature: "FULLTEXT INDEX".to_string(),
                suggestion: Some("Use PostgreSQL GIN index or skip".to_string()),
            });
            if self.strict {
                return Err(ConvertWarning::UnsupportedFeature {
                    feature: "FULLTEXT INDEX".to_string(),
                    suggestion: None,
                });
            }
        }

        Ok(result.into_bytes())
    }

    /// Convert ALTER TABLE statement
    fn convert_alter_table(&mut self, stmt: &[u8]) -> Result<Vec<u8>, ConvertWarning> {
        let stmt_str = String::from_utf8_lossy(stmt);
        let mut result = stmt_str.to_string();

        // pg_dump expands `serial` into a plain integer column plus a separate
        // `ALTER COLUMN ... SET DEFAULT nextval(...)`. Removing just the
        // `DEFAULT nextval(...)` would leave a dangling `ALTER COLUMN id SET`,
        // so the statement never survives as-is. MySQL can restore the
        // auto-increment separately; every other target loses it.
        if self.from == SqlDialect::Postgres
            && self.to != SqlDialect::Postgres
            && self.is_sequence_default_alter(&result)
        {
            if let Some(restored) = self.defer_mysql_auto_increment(&result) {
                self.deferred.push(restored);
                return Ok(Vec::new());
            }
            let preview = self.strip_leading_sql_comments(&result);
            self.warnings.add(ConvertWarning::SkippedStatement {
                reason: "sequence-backed column default has no equivalent".to_string(),
                statement_preview: preview.trim().chars().take(60).collect(),
            });
            return Ok(Vec::new());
        }

        result = self.convert_identifiers(&result);
        result = self.convert_data_types(&result);

        // Convert PostgreSQL-specific syntax
        if self.from == SqlDialect::Postgres && self.to != SqlDialect::Postgres {
            result = self.strip_postgres_casts(&result);
            result = self.convert_nextval(&result);
            result = self.convert_default_now(&result);
            result = self.strip_schema_prefix(&result);
            result = self.strip_postgres_only(&result);
        }

        // SQLite's ALTER TABLE understands only RENAME / ADD COLUMN /
        // DROP COLUMN, so a constraint attached after the table exists has to be
        // re-expressed as an index or dropped. Runs last: the rewrite reads the
        // table name out of the statement, which the steps above normalise.
        if self.to == SqlDialect::Sqlite {
            if let Some(rewritten) = self.rewrite_add_constraint_for_sqlite(&result) {
                return Ok(rewritten.into_bytes());
            }
        }

        Ok(result.into_bytes())
    }

    /// Re-express an `ALTER TABLE ... ADD CONSTRAINT` for SQLite, which has no
    /// such statement.
    ///
    /// `UNIQUE` and `PRIMARY KEY` become a unique index — SQLite enforces the
    /// same guarantee, though a re-expressed primary key is no longer a rowid
    /// alias and, unlike a real primary key, does not itself imply NOT NULL.
    /// A foreign key or check constraint cannot be attached to an existing
    /// SQLite table by any syntax, so it is dropped with a warning.
    ///
    /// Returns `None` for the ALTER forms SQLite already accepts (`ADD COLUMN`,
    /// `RENAME`, `DROP COLUMN`), which pass through untouched.
    fn rewrite_add_constraint_for_sqlite(&mut self, stmt: &str) -> Option<String> {
        use once_cell::sync::Lazy;
        use regex::Regex;

        static RE_ADD_CONSTRAINT: Lazy<Regex> = Lazy::new(|| {
            Regex::new(
                r"(?is)^\s*ALTER\s+TABLE\s+(?P<table>\S+)\s+ADD\s+CONSTRAINT\s+(?P<name>[^\s(]+)\s+(?P<kind>UNIQUE|PRIMARY\s+KEY|FOREIGN\s+KEY|CHECK)\b(?P<rest>.*)$",
            )
            .unwrap()
        });
        static RE_LEADING_COLUMNS: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?s)^\s*\(([^)]*)\)").unwrap());

        // pg_dump prefixes each statement with a comment banner; the constraint
        // itself is what has to be matched.
        let body = self.strip_leading_sql_comments(stmt);
        let caps = RE_ADD_CONSTRAINT.captures(&body)?;
        let kind = caps["kind"]
            .split_whitespace()
            .collect::<Vec<_>>()
            .join(" ");
        let kind = kind.to_uppercase();

        if kind == "UNIQUE" || kind == "PRIMARY KEY" {
            if let Some(columns) = RE_LEADING_COLUMNS.captures(&caps["rest"]) {
                if kind == "PRIMARY KEY" {
                    self.warnings.add(ConvertWarning::UnsupportedFeature {
                        feature: format!("PRIMARY KEY added to existing table {}", &caps["table"]),
                        suggestion: Some(
                            "re-expressed as a UNIQUE INDEX; SQLite only accepts a \
                             primary key inside CREATE TABLE"
                                .to_string(),
                        ),
                    });
                }
                return Some(format!(
                    "CREATE UNIQUE INDEX {} ON {} ({});",
                    &caps["name"],
                    &caps["table"],
                    columns[1].trim()
                ));
            }
        }

        self.warnings.add(ConvertWarning::SkippedStatement {
            reason: format!("SQLite cannot add a {kind} constraint to an existing table"),
            statement_preview: body.trim().chars().take(60).collect(),
        });
        Some(String::new())
    }

    /// Convert DROP TABLE statement
    fn convert_drop_table(&mut self, stmt: &[u8]) -> Result<Vec<u8>, ConvertWarning> {
        let stmt_str = String::from_utf8_lossy(stmt);
        let mut result = stmt_str.to_string();

        result = self.convert_identifiers(&result);

        // Strip PostgreSQL schema prefix
        if self.from == SqlDialect::Postgres && self.to != SqlDialect::Postgres {
            result = self.strip_schema_prefix(&result);
        }

        Ok(result.into_bytes())
    }

    /// Convert COPY statement (PostgreSQL-specific)
    ///
    /// This handles the COPY header. The data block is processed separately
    /// via process_copy_data() when called from the run() function.
    fn convert_copy(
        &mut self,
        stmt: &[u8],
        _table_name: Option<&str>,
    ) -> Result<Vec<u8>, ConvertWarning> {
        let stmt_str = String::from_utf8_lossy(stmt);

        // Check if this contains "FROM stdin" (COPY header) or is data
        let upper = stmt_str.to_uppercase();
        if upper.contains("FROM STDIN") {
            // This is a COPY header - parse it and store for later
            if let Some(header) = parse_copy_header(&stmt_str) {
                if self.from == SqlDialect::Postgres && self.to != SqlDialect::Postgres {
                    // Store the header, will convert data block in process_copy_data
                    self.pending_copy_header = Some(header);
                    // Return empty - the actual INSERT will be generated from data
                    return Ok(Vec::new());
                }
            }
        }

        // If same dialect or couldn't parse, pass through
        Ok(stmt.to_vec())
    }

    /// Convert other statements (comments, session settings, etc.)
    fn convert_other(&mut self, stmt: &[u8]) -> Result<Vec<u8>, ConvertWarning> {
        let stmt_str = String::from_utf8_lossy(stmt);
        let result = stmt_str.to_string();
        let trimmed = result.trim();

        // Skip MySQL session commands when converting to other dialects
        if self.from == SqlDialect::MySql
            && self.to != SqlDialect::MySql
            && self.is_mysql_session_command(&result)
        {
            return Ok(Vec::new()); // Skip
        }

        // Skip PostgreSQL session commands and unsupported features when converting to other dialects
        if self.from == SqlDialect::Postgres
            && self.to != SqlDialect::Postgres
            && self.is_postgres_session_command(&result)
        {
            return Ok(Vec::new()); // Skip
        }
        // psql meta-commands are client directives, not SQL. pg_dump 16.6+ wraps
        // its output in `\restrict` / `\unrestrict`; fed to the mysql client,
        // `\u` is read as its "use database" shorthand and the import dies with
        // ERROR 1049.
        if self.from == SqlDialect::Postgres
            && self.to != SqlDialect::Postgres
            && self.is_psql_meta_command(trimmed)
        {
            return Ok(Vec::new()); // Skip
        }
        if self.from == SqlDialect::Postgres
            && self.to != SqlDialect::Postgres
            && self.is_postgres_only_feature(trimmed)
        {
            self.warnings.add(ConvertWarning::SkippedStatement {
                reason: "PostgreSQL-only feature".to_string(),
                statement_preview: trimmed.chars().take(60).collect(),
            });
            return Ok(Vec::new()); // Skip
        }

        // Skip SQLite pragmas when converting to other dialects
        if self.from == SqlDialect::Sqlite
            && self.to != SqlDialect::Sqlite
            && self.is_sqlite_pragma(&result)
        {
            return Ok(Vec::new()); // Skip
        }

        // Skip MSSQL session commands when converting to other dialects
        if self.from == SqlDialect::Mssql
            && self.to != SqlDialect::Mssql
            && self.is_mssql_session_command(&result)
        {
            return Ok(Vec::new()); // Skip
        }

        // Strip conditional comments
        if result.contains("/*!") {
            let stripped = self.strip_conditional_comments(&result);
            return Ok(stripped.into_bytes());
        }

        Ok(stmt.to_vec())
    }

    /// Check if statement is a MySQL session command
    fn is_mysql_session_command(&self, stmt: &str) -> bool {
        let upper = stmt.to_uppercase();
        upper.contains("SET NAMES")
            || upper.contains("SET CHARACTER")
            || upper.contains("SET SQL_MODE")
            || upper.contains("SET TIME_ZONE")
            || upper.contains("SET FOREIGN_KEY_CHECKS")
            || upper.contains("LOCK TABLES")
            || upper.contains("UNLOCK TABLES")
    }

    /// Check if statement is a PostgreSQL session command or unsupported statement
    fn is_postgres_session_command(&self, stmt: &str) -> bool {
        let upper = stmt.to_uppercase();
        // Session/transaction settings
        upper.contains("SET CLIENT_ENCODING")
            || upper.contains("SET STANDARD_CONFORMING_STRINGS")
            || upper.contains("SET CHECK_FUNCTION_BODIES")
            || upper.contains("SET SEARCH_PATH")
            || upper.contains("SET DEFAULT_TABLESPACE")
            || upper.contains("SET LOCK_TIMEOUT")
            || upper.contains("SET IDLE_IN_TRANSACTION_SESSION_TIMEOUT")
            || upper.contains("SET ROW_SECURITY")
            || upper.contains("SET STATEMENT_TIMEOUT")
            || upper.contains("SET XMLOPTION")
            || upper.contains("SET CLIENT_MIN_MESSAGES")
            || upper.contains("SET DEFAULT_TABLE_ACCESS_METHOD")
            || upper.contains("SELECT PG_CATALOG")
            // Ownership/permission statements
            || upper.contains("OWNER TO")
            || upper.contains("GRANT ")
            || upper.contains("REVOKE ")
    }

    /// Check if statement is a PostgreSQL-only feature that should be skipped
    fn is_postgres_only_feature(&self, stmt: &str) -> bool {
        // Strip leading comments to find the actual statement
        let stripped = self.strip_leading_sql_comments(stmt);
        let upper = stripped.to_uppercase();

        // These PostgreSQL features have no MySQL/SQLite equivalent
        upper.starts_with("CREATE DOMAIN")
            || upper.starts_with("CREATE TYPE")
            || upper.starts_with("CREATE FUNCTION")
            || upper.starts_with("CREATE PROCEDURE")
            || upper.starts_with("CREATE AGGREGATE")
            || upper.starts_with("CREATE OPERATOR")
            || upper.starts_with("CREATE SEQUENCE")
            || upper.starts_with("CREATE EXTENSION")
            || upper.starts_with("CREATE SCHEMA")
            || upper.starts_with("CREATE TRIGGER")
            || upper.starts_with("ALTER DOMAIN")
            || upper.starts_with("ALTER TYPE")
            || upper.starts_with("ALTER FUNCTION")
            || upper.starts_with("ALTER SEQUENCE")
            || upper.starts_with("ALTER SCHEMA")
            || upper.starts_with("COMMENT ON")
    }

    /// Strip leading SQL comments (-- and /* */) from a string
    fn strip_leading_sql_comments(&self, stmt: &str) -> String {
        let mut result = stmt.trim();
        loop {
            // Strip -- comments
            if result.starts_with("--") {
                if let Some(pos) = result.find('\n') {
                    result = result[pos + 1..].trim();
                    continue;
                } else {
                    return String::new();
                }
            }
            // Strip /* */ comments
            if result.starts_with("/*") {
                if let Some(pos) = result.find("*/") {
                    result = result[pos + 2..].trim();
                    continue;
                } else {
                    return String::new();
                }
            }
            break;
        }
        result.to_string()
    }

    /// Check if statement is a SQLite pragma
    fn is_sqlite_pragma(&self, stmt: &str) -> bool {
        let upper = stmt.to_uppercase();
        upper.contains("PRAGMA")
    }

    /// Check if statement is an MSSQL session command
    fn is_mssql_session_command(&self, stmt: &str) -> bool {
        let upper = stmt.to_uppercase();
        upper.contains("SET ANSI_NULLS")
            || upper.contains("SET QUOTED_IDENTIFIER")
            || upper.contains("SET NOCOUNT")
            || upper.contains("SET XACT_ABORT")
            || upper.contains("SET ARITHABORT")
            || upper.contains("SET ANSI_WARNINGS")
            || upper.contains("SET ANSI_PADDING")
            || upper.contains("SET CONCAT_NULL_YIELDS_NULL")
            || upper.contains("SET NUMERIC_ROUNDABORT")
            || upper.contains("SET IDENTITY_INSERT")
    }

    /// Convert identifier quoting based on dialects
    fn convert_identifiers(&self, stmt: &str) -> String {
        match (self.from, self.to) {
            (SqlDialect::MySql, SqlDialect::Postgres | SqlDialect::Sqlite) => {
                // Backticks → double quotes
                self.backticks_to_double_quotes(stmt)
            }
            (SqlDialect::MySql, SqlDialect::Mssql) => {
                // Backticks → square brackets
                self.backticks_to_square_brackets(stmt)
            }
            (SqlDialect::Postgres | SqlDialect::Sqlite, SqlDialect::MySql) => {
                // Double quotes → backticks
                self.double_quotes_to_backticks(stmt)
            }
            (SqlDialect::Postgres | SqlDialect::Sqlite, SqlDialect::Mssql) => {
                // Double quotes → square brackets
                self.double_quotes_to_square_brackets(stmt)
            }
            (SqlDialect::Mssql, SqlDialect::MySql) => {
                // Square brackets → backticks
                self.square_brackets_to_backticks(stmt)
            }
            (SqlDialect::Mssql, SqlDialect::Postgres | SqlDialect::Sqlite) => {
                // Square brackets → double quotes
                self.square_brackets_to_double_quotes(stmt)
            }
            _ => stmt.to_string(),
        }
    }

    /// Convert backticks to double quotes
    pub fn backticks_to_double_quotes(&self, stmt: &str) -> String {
        let mut result = String::with_capacity(stmt.len());
        let mut in_string = false;
        let mut in_backtick = false;

        for c in stmt.chars() {
            if c == '\'' && !in_backtick {
                in_string = !in_string;
                result.push(c);
            } else if c == '`' && !in_string {
                in_backtick = !in_backtick;
                result.push('"');
            } else {
                result.push(c);
            }
        }
        result
    }

    /// Convert double quotes to backticks
    pub fn double_quotes_to_backticks(&self, stmt: &str) -> String {
        let mut result = String::with_capacity(stmt.len());
        let mut in_string = false;
        let mut in_dquote = false;
        let chars = stmt.chars();

        for c in chars {
            if c == '\'' && !in_dquote {
                in_string = !in_string;
                result.push(c);
            } else if c == '"' && !in_string {
                in_dquote = !in_dquote;
                result.push('`');
            } else {
                result.push(c);
            }
        }
        result
    }

    /// Convert backticks to square brackets (for MSSQL)
    pub fn backticks_to_square_brackets(&self, stmt: &str) -> String {
        let mut result = String::with_capacity(stmt.len());
        let mut in_string = false;
        let mut in_backtick = false;

        for c in stmt.chars() {
            if c == '\'' && !in_backtick {
                in_string = !in_string;
                result.push(c);
            } else if c == '`' && !in_string {
                if !in_backtick {
                    result.push('[');
                } else {
                    result.push(']');
                }
                in_backtick = !in_backtick;
            } else {
                result.push(c);
            }
        }
        result
    }

    /// Convert double quotes to square brackets (for MSSQL)
    pub fn double_quotes_to_square_brackets(&self, stmt: &str) -> String {
        let mut result = String::with_capacity(stmt.len());
        let mut in_string = false;
        let mut in_dquote = false;

        for c in stmt.chars() {
            if c == '\'' && !in_dquote {
                in_string = !in_string;
                result.push(c);
            } else if c == '"' && !in_string {
                if !in_dquote {
                    result.push('[');
                } else {
                    result.push(']');
                }
                in_dquote = !in_dquote;
            } else {
                result.push(c);
            }
        }
        result
    }

    /// Convert square brackets to backticks (from MSSQL to MySQL)
    pub fn square_brackets_to_backticks(&self, stmt: &str) -> String {
        let mut result = String::with_capacity(stmt.len());
        let mut in_string = false;

        for c in stmt.chars() {
            if c == '\'' {
                in_string = !in_string;
                result.push(c);
            } else if !in_string && (c == '[' || c == ']') {
                result.push('`');
            } else {
                result.push(c);
            }
        }
        result
    }

    /// Convert square brackets to double quotes (from MSSQL to PostgreSQL/SQLite)
    pub fn square_brackets_to_double_quotes(&self, stmt: &str) -> String {
        let mut result = String::with_capacity(stmt.len());
        let mut in_string = false;

        for c in stmt.chars() {
            if c == '\'' {
                in_string = !in_string;
                result.push(c);
            } else if !in_string && (c == '[' || c == ']') {
                result.push('"');
            } else {
                result.push(c);
            }
        }
        result
    }

    /// Convert data types between dialects
    fn convert_data_types(&self, stmt: &str) -> String {
        TypeMapper::convert(stmt, self.from, self.to)
    }

    /// Convert AUTO_INCREMENT/SERIAL syntax
    fn convert_auto_increment(&self, stmt: &str, _table_name: Option<&str>) -> String {
        // Strip the MySQL table-level `AUTO_INCREMENT=N` option first (distinct from
        // the column-level `AUTO_INCREMENT` keyword handled below). Otherwise the
        // column-level replacements below strip the word "AUTO_INCREMENT" and leave
        // a dangling "=N" behind, e.g. `) ENGINE=InnoDB AUTO_INCREMENT=2 ...` becomes
        // `) =2 ...`. See https://github.com/HelgeSverre/sql-splitter/issues/64
        let stmt = if self.from == SqlDialect::MySql && self.to != SqlDialect::MySql {
            self.strip_mysql_auto_increment_table_option(stmt)
        } else {
            stmt.to_string()
        };
        let stmt = stmt.as_str();

        match (self.from, self.to) {
            (SqlDialect::MySql, SqlDialect::Postgres) => {
                // INT AUTO_INCREMENT → SERIAL, BIGINT AUTO_INCREMENT → BIGSERIAL,
                // case-insensitive (MySQL dumps commonly use lowercase
                // `auto_increment`) and string-literal-aware (a DEFAULT value or
                // CHECK expression containing the literal text "AUTO_INCREMENT"
                // must not be touched).
                self.convert_mysql_auto_increment_keyword(stmt)
            }
            (SqlDialect::MySql, SqlDialect::Sqlite) => {
                use once_cell::sync::Lazy;
                use regex::Regex;
                // `<int type> AUTO_INCREMENT` → INTEGER, then any bare keyword
                // dropped. Case-insensitive (MySQL dumps commonly lowercase
                // `auto_increment`) and only outside string literals so a
                // DEFAULT / value containing the text is left alone. Matching
                // the whole `…INT AUTO_INCREMENT` unit avoids the "BIGINTEGER"
                // substring hazard.
                static RE_SIZED_AI: Lazy<Regex> = Lazy::new(|| {
                    Regex::new(r"(?i)\b(?:BIG|MEDIUM|SMALL|TINY)?INT\s+AUTO_INCREMENT").unwrap()
                });
                // Trailing boundary only: the integer type may already have been
                // mapped and glued to the keyword (e.g. `INTEGERAUTO_INCREMENT`)
                // by convert_data_types running first.
                static RE_BARE_AI: Lazy<Regex> =
                    Lazy::new(|| Regex::new(r"(?i)AUTO_INCREMENT\b").unwrap());
                map_outside_string_literals(stmt, |seg| {
                    let seg = RE_SIZED_AI.replace_all(seg, "INTEGER");
                    RE_BARE_AI.replace_all(&seg, "").into_owned()
                })
            }
            (SqlDialect::Postgres, SqlDialect::MySql) => {
                // SERIAL → INT AUTO_INCREMENT
                // BIGSERIAL → BIGINT AUTO_INCREMENT
                let result = stmt.replace("BIGSERIAL", "BIGINT AUTO_INCREMENT");
                let result = result.replace("bigserial", "BIGINT AUTO_INCREMENT");
                let result = result.replace("SMALLSERIAL", "SMALLINT AUTO_INCREMENT");
                let result = result.replace("smallserial", "SMALLINT AUTO_INCREMENT");
                let result = result.replace("SERIAL", "INT AUTO_INCREMENT");
                result.replace("serial", "INT AUTO_INCREMENT")
            }
            (SqlDialect::Postgres, SqlDialect::Sqlite) => {
                // SERIAL → INTEGER (SQLite auto-increments INTEGER PRIMARY KEY)
                let result = stmt.replace("BIGSERIAL", "INTEGER");
                let result = result.replace("bigserial", "INTEGER");
                let result = result.replace("SMALLSERIAL", "INTEGER");
                let result = result.replace("smallserial", "INTEGER");
                let result = result.replace("SERIAL", "INTEGER");
                result.replace("serial", "INTEGER")
            }
            (SqlDialect::Sqlite, SqlDialect::MySql) => {
                // SQLite uses INTEGER PRIMARY KEY for auto-increment
                // We can't easily detect this pattern, so just pass through
                stmt.to_string()
            }
            (SqlDialect::Sqlite, SqlDialect::Postgres) => {
                // SQLite uses INTEGER PRIMARY KEY for auto-increment
                // We can't easily detect this pattern, so just pass through
                stmt.to_string()
            }
            // MSSQL conversions
            (SqlDialect::MySql, SqlDialect::Mssql) => {
                use once_cell::sync::Lazy;
                use regex::Regex;
                // AUTO_INCREMENT → IDENTITY(1,1), keeping the integer type.
                // Case-insensitive and only outside string literals. BIGINT is
                // handled before the bare INT rule; `\bINT` won't match the
                // `INT` inside `BIGINT`.
                static RE_BIGINT_AI: Lazy<Regex> =
                    Lazy::new(|| Regex::new(r"(?i)\bBIGINT\s+AUTO_INCREMENT").unwrap());
                static RE_INT_AI: Lazy<Regex> =
                    Lazy::new(|| Regex::new(r"(?i)\bINT\s+AUTO_INCREMENT").unwrap());
                // Trailing boundary only (the type may already be glued to the
                // keyword by an earlier type mapping).
                static RE_BARE_AI: Lazy<Regex> =
                    Lazy::new(|| Regex::new(r"(?i)AUTO_INCREMENT\b").unwrap());
                map_outside_string_literals(stmt, |seg| {
                    let seg = RE_BIGINT_AI.replace_all(seg, "BIGINT IDENTITY(1,1)");
                    let seg = RE_INT_AI.replace_all(&seg, "INT IDENTITY(1,1)");
                    RE_BARE_AI.replace_all(&seg, "IDENTITY(1,1)").into_owned()
                })
            }
            (SqlDialect::Mssql, SqlDialect::MySql) => {
                // IDENTITY(1,1) → AUTO_INCREMENT
                self.convert_identity_to_auto_increment(stmt)
            }
            (SqlDialect::Postgres, SqlDialect::Mssql) => {
                // SERIAL → INT IDENTITY(1,1) (handled by type mapper)
                stmt.to_string()
            }
            (SqlDialect::Mssql, SqlDialect::Postgres) => {
                // IDENTITY(1,1) → SERIAL (need to add SERIAL instead)
                self.convert_identity_to_serial(stmt)
            }
            (SqlDialect::Sqlite, SqlDialect::Mssql) => {
                // SQLite → MSSQL: pass through
                stmt.to_string()
            }
            (SqlDialect::Mssql, SqlDialect::Sqlite) => {
                // IDENTITY → strip (SQLite uses INTEGER PRIMARY KEY)
                self.strip_identity(stmt)
            }
            _ => stmt.to_string(),
        }
    }

    /// Convert MSSQL IDENTITY to MySQL AUTO_INCREMENT
    fn convert_identity_to_auto_increment(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        static RE_IDENTITY: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\bIDENTITY\s*\(\s*\d+\s*,\s*\d+\s*\)").unwrap());

        RE_IDENTITY.replace_all(stmt, "AUTO_INCREMENT").to_string()
    }

    /// Convert MSSQL IDENTITY to PostgreSQL SERIAL
    fn convert_identity_to_serial(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        // Match INT IDENTITY(1,1) and replace with SERIAL
        static RE_BIGINT_IDENTITY: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\bBIGINT\s+IDENTITY\s*\(\s*\d+\s*,\s*\d+\s*\)").unwrap());
        static RE_INT_IDENTITY: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\bINT\s+IDENTITY\s*\(\s*\d+\s*,\s*\d+\s*\)").unwrap());
        static RE_SMALLINT_IDENTITY: Lazy<Regex> = Lazy::new(|| {
            Regex::new(r"(?i)\bSMALLINT\s+IDENTITY\s*\(\s*\d+\s*,\s*\d+\s*\)").unwrap()
        });

        let result = RE_BIGINT_IDENTITY
            .replace_all(stmt, "BIGSERIAL")
            .to_string();
        let result = RE_INT_IDENTITY.replace_all(&result, "SERIAL").to_string();
        RE_SMALLINT_IDENTITY
            .replace_all(&result, "SMALLSERIAL")
            .to_string()
    }

    /// Strip MSSQL IDENTITY clause for SQLite
    fn strip_identity(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        static RE_IDENTITY: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\s*IDENTITY\s*\(\s*\d+\s*,\s*\d+\s*\)").unwrap());

        RE_IDENTITY.replace_all(stmt, "").to_string()
    }

    /// Convert string escape sequences
    fn convert_string_escapes(&self, stmt: &str) -> String {
        match (self.from, self.to) {
            // MySQL uses backslash escapes; PostgreSQL (standard strings),
            // SQLite and MSSQL all treat backslash as a literal byte and escape
            // a quote by doubling it, so the same decode applies to all three.
            (SqlDialect::MySql, SqlDialect::Postgres | SqlDialect::Sqlite | SqlDialect::Mssql) => {
                self.mysql_escapes_to_standard(stmt)
            }
            _ => stmt.to_string(),
        }
    }

    /// Convert MySQL backslash escapes to standard SQL double-quote escapes
    fn mysql_escapes_to_standard(&self, stmt: &str) -> String {
        let mut result = String::with_capacity(stmt.len());
        let mut chars = stmt.chars().peekable();
        let mut in_string = false;

        while let Some(c) = chars.next() {
            if c == '\'' {
                in_string = !in_string;
                result.push(c);
            } else if c == '\\' && in_string {
                // Decode the MySQL escape to the byte it represents. The target
                // dialects (Postgres standard strings, SQLite, MSSQL) treat
                // backslash as a literal, so a passed-through `\\` or `\n` would
                // be read as two literal characters and corrupt the data.
                match chars.next() {
                    Some(next) => match next {
                        // Quote becomes a doubled '' (standard SQL escaping).
                        '\'' => result.push_str("''"),
                        '"' => result.push('"'),
                        // One literal backslash.
                        '\\' => result.push('\\'),
                        'n' => result.push('\n'),
                        'r' => result.push('\r'),
                        't' => result.push('\t'),
                        '0' => result.push('\0'),
                        'b' => result.push('\u{08}'),
                        'Z' => result.push('\u{1a}'),
                        // MySQL keeps the backslash for the LIKE metacharacters.
                        '%' => result.push_str("\\%"),
                        '_' => result.push_str("\\_"),
                        // Any other escaped char is the char itself.
                        other => result.push(other),
                    },
                    // Trailing backslash at end of input.
                    None => result.push('\\'),
                }
            } else {
                result.push(c);
            }
        }
        result
    }

    /// Strip MySQL conditional comments /*!40101 ... */
    fn strip_conditional_comments(&self, stmt: &str) -> String {
        let mut result = String::with_capacity(stmt.len());
        let mut chars = stmt.chars().peekable();

        while let Some(c) = chars.next() {
            if c == '/' && chars.peek() == Some(&'*') {
                chars.next(); // consume *
                if chars.peek() == Some(&'!') {
                    // Skip conditional comment
                    chars.next(); // consume !
                                  // Skip version number
                    while chars.peek().map(|c| c.is_ascii_digit()).unwrap_or(false) {
                        chars.next();
                    }
                    // Skip content until */
                    let mut depth = 1;
                    while depth > 0 {
                        match chars.next() {
                            Some('*') if chars.peek() == Some(&'/') => {
                                chars.next();
                                depth -= 1;
                            }
                            Some('/') if chars.peek() == Some(&'*') => {
                                chars.next();
                                depth += 1;
                            }
                            None => break,
                            _ => {}
                        }
                    }
                } else {
                    // Regular comment, keep it
                    result.push('/');
                    result.push('*');
                }
            } else {
                result.push(c);
            }
        }
        result
    }

    /// Strip ENGINE clause
    fn strip_engine_clause(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        if self.to == SqlDialect::MySql {
            return stmt.to_string();
        }

        // Remove ENGINE=InnoDB, ENGINE=MyISAM, etc.
        static RE_ENGINE: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\s*ENGINE\s*=\s*\w+").unwrap());
        RE_ENGINE.replace_all(stmt, "").to_string()
    }

    /// Strip CHARSET/COLLATE clauses
    fn strip_charset_clauses(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        if self.to == SqlDialect::MySql {
            return stmt.to_string();
        }

        static RE_CHARSET: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\s*(DEFAULT\s+)?CHARSET\s*=\s*\w+").unwrap());
        static RE_COLLATE: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\s*COLLATE\s*=?\s*\w+").unwrap());

        let result = RE_CHARSET.replace_all(stmt, "").to_string();
        RE_COLLATE.replace_all(&result, "").to_string()
    }

    /// Case-insensitively checks whether `keyword` starts at `chars[pos]`, with
    /// word boundaries on both sides (so e.g. "COMMENT" doesn't match inside
    /// "COMMENTARY" or a preceding identifier char like "XCOMMENT"). Returns the
    /// index just past the keyword on success.
    fn match_keyword_ci(chars: &[char], pos: usize, keyword: &str) -> Option<usize> {
        let kw_len = keyword.len();
        if pos > 0 && (chars[pos - 1].is_alphanumeric() || chars[pos - 1] == '_') {
            return None;
        }
        let end = pos + kw_len;
        if end > chars.len() {
            return None;
        }
        let candidate: String = chars[pos..end].iter().collect();
        if !candidate.eq_ignore_ascii_case(keyword) {
            return None;
        }
        if end < chars.len() && (chars[end].is_alphanumeric() || chars[end] == '_') {
            return None;
        }
        Some(end)
    }

    /// Removes trailing whitespace already pushed into `result`, so that stripping
    /// a clause also absorbs the whitespace that preceded it (matching the old
    /// `\s*<keyword>...` regex behavior).
    fn trim_trailing_whitespace(result: &mut String) {
        while result.ends_with(|c: char| c.is_whitespace()) {
            result.pop();
        }
    }

    /// Strip MySQL table-level `AUTO_INCREMENT=N` option. String-literal-aware so
    /// that a DEFAULT value or other string content containing the literal text
    /// "AUTO_INCREMENT=N" is left untouched.
    /// See https://github.com/HelgeSverre/sql-splitter/issues/64
    fn strip_mysql_auto_increment_table_option(&self, stmt: &str) -> String {
        let chars: Vec<char> = stmt.chars().collect();
        let n = chars.len();
        let mut result = String::with_capacity(stmt.len());
        let mut in_string = false;
        let mut i = 0;

        while i < n {
            let c = chars[i];
            if c == '\'' {
                in_string = !in_string;
                result.push(c);
                i += 1;
                continue;
            }
            if !in_string {
                if let Some(after_kw) = Self::match_keyword_ci(&chars, i, "AUTO_INCREMENT") {
                    let mut j = after_kw;
                    while j < n && chars[j].is_whitespace() {
                        j += 1;
                    }
                    if j < n && chars[j] == '=' {
                        j += 1;
                        while j < n && chars[j].is_whitespace() {
                            j += 1;
                        }
                        let digits_start = j;
                        while j < n && chars[j].is_ascii_digit() {
                            j += 1;
                        }
                        if j > digits_start {
                            Self::trim_trailing_whitespace(&mut result);
                            i = j;
                            continue;
                        }
                    }
                }
            }
            result.push(c);
            i += 1;
        }
        result
    }

    /// Strip MySQL column/table `COMMENT 'text'` (or `COMMENT='text'`) annotations.
    /// Neither is valid inside a PostgreSQL column definition or as a CREATE TABLE
    /// table option, so they are removed entirely rather than translated.
    /// String-literal-aware: only matches a real `COMMENT` clause, never the word
    /// "comment" sitting inside an unrelated DEFAULT value or CHECK expression.
    fn strip_mysql_comment_clauses(&self, stmt: &str) -> String {
        if self.to == SqlDialect::MySql {
            return stmt.to_string();
        }

        let chars: Vec<char> = stmt.chars().collect();
        let n = chars.len();
        let mut result = String::with_capacity(stmt.len());
        let mut in_string = false;
        let mut i = 0;

        while i < n {
            let c = chars[i];
            if c == '\'' {
                in_string = !in_string;
                result.push(c);
                i += 1;
                continue;
            }
            if !in_string {
                if let Some(after_kw) = Self::match_keyword_ci(&chars, i, "COMMENT") {
                    let mut j = after_kw;
                    while j < n && chars[j].is_whitespace() {
                        j += 1;
                    }
                    if j < n && chars[j] == '=' {
                        j += 1;
                        while j < n && chars[j].is_whitespace() {
                            j += 1;
                        }
                    }
                    if j < n && chars[j] == '\'' {
                        // Consume the quoted comment text itself, honoring ''
                        // as an escaped quote, then drop the whole clause.
                        j += 1;
                        loop {
                            if j >= n {
                                break;
                            }
                            if chars[j] == '\'' {
                                if j + 1 < n && chars[j + 1] == '\'' {
                                    j += 2;
                                    continue;
                                }
                                j += 1;
                                break;
                            }
                            j += 1;
                        }
                        Self::trim_trailing_whitespace(&mut result);
                        i = j;
                        continue;
                    }
                }
            }
            result.push(c);
            i += 1;
        }
        result
    }

    /// Convert MySQL's column-level `AUTO_INCREMENT` keyword to PostgreSQL's
    /// `SERIAL`/`BIGSERIAL`, case-insensitively and string-literal-aware (a
    /// DEFAULT value or CHECK expression containing the literal text
    /// "AUTO_INCREMENT" must not be touched — see finding in issue #64 follow-up
    /// review).
    fn convert_mysql_auto_increment_keyword(&self, stmt: &str) -> String {
        let chars: Vec<char> = stmt.chars().collect();
        let n = chars.len();
        let mut result = String::with_capacity(stmt.len());
        let mut in_string = false;
        let mut i = 0;

        while i < n {
            let c = chars[i];
            if c == '\'' {
                in_string = !in_string;
                result.push(c);
                i += 1;
                continue;
            }
            if !in_string {
                if let Some(after_type) = Self::match_keyword_ci(&chars, i, "BIGINT") {
                    let mut j = after_type;
                    while j < n && chars[j].is_whitespace() {
                        j += 1;
                    }
                    if let Some(end) = Self::match_keyword_ci(&chars, j, "AUTO_INCREMENT") {
                        result.push_str("BIGSERIAL");
                        i = end;
                        continue;
                    }
                }
                if let Some(after_type) = Self::match_keyword_ci(&chars, i, "INT") {
                    let mut j = after_type;
                    while j < n && chars[j].is_whitespace() {
                        j += 1;
                    }
                    if let Some(end) = Self::match_keyword_ci(&chars, j, "AUTO_INCREMENT") {
                        result.push_str("SERIAL");
                        i = end;
                        continue;
                    }
                }
                if let Some(end) = Self::match_keyword_ci(&chars, i, "AUTO_INCREMENT") {
                    // Clean up any remaining standalone occurrence (e.g. when
                    // other modifiers like NOT NULL sit between the type and
                    // the keyword).
                    i = end;
                    continue;
                }
            }
            result.push(c);
            i += 1;
        }
        result
    }

    /// Convert MySQL `UNIQUE KEY [name] (cols) [USING BTREE|HASH]` table constraint
    /// to standard `UNIQUE (cols)`, which is the closest PostgreSQL equivalent.
    /// Also strips MySQL prefix-length index annotations (e.g. `email(191)`),
    /// which have no PostgreSQL equivalent and aren't valid there.
    fn convert_unique_key_constraint(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::{Captures, Regex};

        if self.to == SqlDialect::MySql {
            return stmt.to_string();
        }

        // The column-list group allows one level of nested `(digits)` prefix-length
        // annotations (e.g. `("email"(191),"other")`), which are common in
        // real-world MySQL dumps using utf8mb4 prefix indexes.
        static RE: Lazy<Regex> = Lazy::new(|| {
            Regex::new(
                r#"(?i)UNIQUE\s+KEY\s+(?:"[^"]*"\s+)?(\([^()]*(?:\(\d+\)[^()]*)*\))(?:\s*USING\s+(?:BTREE|HASH))?"#,
            )
            .unwrap()
        });
        static RE_PREFIX_LEN: Lazy<Regex> = Lazy::new(|| Regex::new(r"\(\d+\)").unwrap());

        RE.replace_all(stmt, |caps: &Captures| {
            let cols = RE_PREFIX_LEN.replace_all(&caps[1], "");
            format!("UNIQUE {}", cols)
        })
        .to_string()
    }

    /// Strip PostgreSQL type casts (::type and ::regclass)
    fn strip_postgres_casts(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        // Match ::regclass, ::text, ::integer, etc. (including complex types like character varying)
        static RE_CAST: Lazy<Regex> = Lazy::new(|| {
            Regex::new(r"::[a-zA-Z_][a-zA-Z0-9_]*(?:\s+[a-zA-Z_][a-zA-Z0-9_]*)*").unwrap()
        });

        // Only outside string literals: a value like '2001:db8::1' or
        // 'a::b text' must keep its ':: …' rather than have it stripped.
        map_outside_string_literals(stmt, |seg| RE_CAST.replace_all(seg, "").into_owned())
    }

    /// Convert nextval('sequence') to NULL or remove (AUTO_INCREMENT handles it)
    fn convert_nextval(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        // Match nextval('sequence_name'::regclass) or nextval('sequence_name')
        // Remove the DEFAULT nextval(...) entirely - AUTO_INCREMENT is already applied
        static RE_NEXTVAL: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\s*DEFAULT\s+nextval\s*\([^)]+\)").unwrap());

        RE_NEXTVAL.replace_all(stmt, "").to_string()
    }

    /// Rebuild a PostgreSQL sequence-backed default as a MySQL AUTO_INCREMENT
    /// column, returned as a statement to emit once the dump is fully applied.
    ///
    /// MySQL rejects an AUTO_INCREMENT column that is not a key, and pg_dump
    /// emits the primary key after the table data, so this cannot be applied in
    /// place — it has to run last. `MODIFY COLUMN` also takes a full column
    /// definition, so the type is read back from the `CREATE TABLE` this
    /// converter already emitted.
    ///
    /// Returns `None` when the target is not MySQL, when the table was never
    /// seen, or when the column's type does not read as an integer — in every
    /// one of those cases the caller drops the statement with a warning rather
    /// than emit a half-formed `MODIFY`.
    fn defer_mysql_auto_increment(&self, stmt: &str) -> Option<String> {
        use once_cell::sync::Lazy;
        use regex::Regex;

        static RE_SEQUENCE_TARGET: Lazy<Regex> = Lazy::new(|| {
            Regex::new(
                r"(?is)\bALTER\s+TABLE\s+(?:ONLY\s+)?(?P<table>\S+)\s+ALTER\s+COLUMN\s+(?P<column>[^\s]+)\s+SET\s+DEFAULT\s+nextval",
            )
            .unwrap()
        });

        if self.to != SqlDialect::MySql {
            return None;
        }

        let caps = RE_SEQUENCE_TARGET.captures(stmt)?;
        let table = table_key(&caps["table"]);
        let column = caps["column"].trim_matches(|c| c == '"' || c == '`');
        let create_table = self.created_tables.get(&table)?;
        let column_type = declared_column_type(create_table, column)?;

        // A sequence always backs an integer column. Anything else means the
        // type was misread, and a wrong MODIFY would rewrite the column.
        if !matches!(
            column_type.to_uppercase().as_str(),
            "INT" | "INTEGER" | "BIGINT" | "SMALLINT" | "MEDIUMINT" | "TINYINT"
        ) {
            return None;
        }

        Some(format!(
            "ALTER TABLE `{table}` MODIFY COLUMN `{column}` {column_type} NOT NULL AUTO_INCREMENT;"
        ))
    }

    /// Strip PostgreSQL's `ONLY` inheritance qualifier from `ALTER TABLE ONLY t`.
    /// pg_dump attaches every constraint and column default that way, and no
    /// other engine accepts the keyword.
    fn strip_postgres_only(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        static RE_ALTER_ONLY: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\b(ALTER\s+TABLE)\s+ONLY\s+").unwrap());

        RE_ALTER_ONLY.replace_all(stmt, "$1 ").to_string()
    }

    /// Whether an `ALTER TABLE` exists only to attach a sequence-backed default
    /// (`ALTER COLUMN x SET DEFAULT nextval(...)`) — pg_dump's expansion of a
    /// `serial` column.
    fn is_sequence_default_alter(&self, stmt: &str) -> bool {
        use once_cell::sync::Lazy;
        use regex::Regex;

        static RE_SEQUENCE_DEFAULT: Lazy<Regex> = Lazy::new(|| {
            Regex::new(r"(?is)\bALTER\s+COLUMN\b.*?\bSET\s+DEFAULT\s+nextval\s*\(").unwrap()
        });

        RE_SEQUENCE_DEFAULT.is_match(stmt)
    }

    /// Whether the statement is a psql client meta-command rather than SQL.
    /// `\.` is excluded: it terminates a COPY data block and belongs to the
    /// COPY path.
    fn is_psql_meta_command(&self, stmt: &str) -> bool {
        let stripped = self.strip_leading_sql_comments(stmt);
        let trimmed = stripped.trim_start();
        trimmed.starts_with('\\') && !trimmed.starts_with("\\.")
    }

    /// Convert DEFAULT now() to DEFAULT CURRENT_TIMESTAMP
    fn convert_default_now(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        static RE_NOW: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\bDEFAULT\s+now\s*\(\s*\)").unwrap());

        RE_NOW
            .replace_all(stmt, "DEFAULT CURRENT_TIMESTAMP")
            .to_string()
    }

    /// Strip schema prefix from table names (e.g., public.users -> users)
    fn strip_schema_prefix(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        // Match schema.table patterns (with optional quotes)
        // Handle: public.table, "public"."table", public."table"
        static RE_SCHEMA: Lazy<Regex> =
            Lazy::new(|| Regex::new(r#"(?i)\b(public|pg_catalog|pg_temp)\s*\.\s*"#).unwrap());

        // Only outside string literals so a value like 'see public.docs' keeps
        // its text.
        map_outside_string_literals(stmt, |seg| RE_SCHEMA.replace_all(seg, "").into_owned())
    }

    /// Convert MSSQL GETDATE() to CURRENT_TIMESTAMP
    fn convert_mssql_getdate(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        static RE_GETDATE: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\bGETDATE\s*\(\s*\)").unwrap());
        static RE_SYSDATETIME: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\bSYSDATETIME\s*\(\s*\)").unwrap());
        static RE_GETUTCDATE: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\bGETUTCDATE\s*\(\s*\)").unwrap());

        let result = RE_GETDATE
            .replace_all(stmt, "CURRENT_TIMESTAMP")
            .to_string();
        let result = RE_SYSDATETIME
            .replace_all(&result, "CURRENT_TIMESTAMP")
            .to_string();
        RE_GETUTCDATE
            .replace_all(&result, "CURRENT_TIMESTAMP")
            .to_string()
    }

    /// Strip MSSQL ON [filegroup] clause
    fn strip_mssql_on_filegroup(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        // Match ON [PRIMARY] or ON [filegroup_name]
        static RE_ON_FILEGROUP: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\s+ON\s*\[\s*\w+\s*\]").unwrap());

        RE_ON_FILEGROUP.replace_all(stmt, "").to_string()
    }

    /// Strip MSSQL CLUSTERED/NONCLUSTERED keywords
    fn strip_mssql_clustered(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        static RE_CLUSTERED: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bCLUSTERED\s+").unwrap());
        static RE_NONCLUSTERED: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\bNONCLUSTERED\s+").unwrap());

        let result = RE_CLUSTERED.replace_all(stmt, "").to_string();
        RE_NONCLUSTERED.replace_all(&result, "").to_string()
    }

    /// Convert MSSQL `N'unicode'` string literals to regular `'unicode'`
    /// strings by dropping the `N` prefix. Only a genuine prefix — an `N`
    /// immediately before a string-opening quote and not itself inside a string
    /// — is converted, so an `N'` sequence appearing inside string data (e.g.
    /// `'season N''4'`) is left untouched.
    fn convert_mssql_unicode_strings(&self, stmt: &str) -> String {
        let bytes = stmt.as_bytes();
        let mut out = String::with_capacity(stmt.len());
        let mut i = 0;
        // Everything in `stmt[..copied]` has already been emitted to `out`.
        let mut copied = 0;
        while i < bytes.len() {
            if bytes[i] == b'\'' {
                // A string literal: skip it whole so its contents are preserved.
                i = skip_sql_string_literal(bytes, i);
            } else if matches!(bytes[i], b'N' | b'n')
                && bytes.get(i + 1) == Some(&b'\'')
                && (i == 0 || !(bytes[i - 1].is_ascii_alphanumeric() || bytes[i - 1] == b'_'))
            {
                // A genuine `N'` string prefix: emit up to it, drop the `N`, and
                // resume at the opening quote (copied to the literal-skip path).
                out.push_str(&stmt[copied..i]);
                copied = i + 1;
                i += 1;
            } else {
                i += 1;
            }
        }
        out.push_str(&stmt[copied..]);
        out
    }

    /// Strip MSSQL schema prefix (dbo., etc.) from table names
    fn strip_mssql_schema_prefix(&self, stmt: &str) -> String {
        use once_cell::sync::Lazy;
        use regex::Regex;

        // Match [dbo].[table] or dbo.table and keep just [table] or table
        // We replace schema.table with just table, handling both bracketed and unbracketed forms
        static RE_MSSQL_SCHEMA: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"(?i)\[?dbo\]?\s*\.\s*").unwrap());

        // Only outside string literals so a value like 'user dbo.smith' keeps
        // its text.
        map_outside_string_literals(stmt, |seg| {
            RE_MSSQL_SCHEMA.replace_all(seg, "").into_owned()
        })
    }

    /// Detect unsupported features and add warnings
    fn detect_unsupported_features(
        &mut self,
        stmt: &str,
        table_name: Option<&str>,
    ) -> Result<(), ConvertWarning> {
        let upper = stmt.to_uppercase();

        // MySQL-specific features
        if self.from == SqlDialect::MySql {
            // ENUM types
            if upper.contains("ENUM(") {
                let warning = ConvertWarning::UnsupportedFeature {
                    feature: format!(
                        "ENUM type{}",
                        table_name
                            .map(|t| format!(" in table {}", t))
                            .unwrap_or_default()
                    ),
                    suggestion: Some(
                        "Converted to VARCHAR - consider adding CHECK constraint".to_string(),
                    ),
                };
                self.warnings.add(warning.clone());
                if self.strict {
                    return Err(warning);
                }
            }

            // SET types (MySQL)
            if upper.contains("SET(") {
                let warning = ConvertWarning::UnsupportedFeature {
                    feature: format!(
                        "SET type{}",
                        table_name
                            .map(|t| format!(" in table {}", t))
                            .unwrap_or_default()
                    ),
                    suggestion: Some(
                        "Converted to VARCHAR - SET semantics not preserved".to_string(),
                    ),
                };
                self.warnings.add(warning.clone());
                if self.strict {
                    return Err(warning);
                }
            }

            // UNSIGNED
            if upper.contains("UNSIGNED") {
                self.warnings.add(ConvertWarning::UnsupportedFeature {
                    feature: "UNSIGNED modifier".to_string(),
                    suggestion: Some(
                        "Removed - consider adding CHECK constraint for non-negative values"
                            .to_string(),
                    ),
                });
            }
        }

        // PostgreSQL-specific features
        if self.from == SqlDialect::Postgres {
            // Array types
            if upper.contains("[]") || upper.contains("ARRAY[") {
                let warning = ConvertWarning::UnsupportedFeature {
                    feature: format!(
                        "Array type{}",
                        table_name
                            .map(|t| format!(" in table {}", t))
                            .unwrap_or_default()
                    ),
                    suggestion: Some(
                        "Array types not supported in target dialect - consider using JSON"
                            .to_string(),
                    ),
                };
                self.warnings.add(warning.clone());
                if self.strict {
                    return Err(warning);
                }
            }

            // INHERITS
            if upper.contains("INHERITS") {
                let warning = ConvertWarning::UnsupportedFeature {
                    feature: "Table inheritance (INHERITS)".to_string(),
                    suggestion: Some(
                        "PostgreSQL table inheritance not supported in target dialect".to_string(),
                    ),
                };
                self.warnings.add(warning.clone());
                if self.strict {
                    return Err(warning);
                }
            }

            // PARTITION BY
            if upper.contains("PARTITION BY") && self.to == SqlDialect::Sqlite {
                let warning = ConvertWarning::UnsupportedFeature {
                    feature: "Table partitioning".to_string(),
                    suggestion: Some("Partitioning not supported in SQLite".to_string()),
                };
                self.warnings.add(warning.clone());
                if self.strict {
                    return Err(warning);
                }
            }
        }

        Ok(())
    }

    /// Get collected warnings
    pub fn warnings(&self) -> &[ConvertWarning] {
        self.warnings.warnings()
    }
}

/// Run the convert command
pub fn run(config: ConvertConfig) -> anyhow::Result<ConvertStats> {
    let mut stats = ConvertStats::default();

    // Detect or use specified source dialect
    let from_dialect = if let Some(d) = config.from_dialect {
        d
    } else {
        let result = crate::parser::detect_dialect_from_file(&config.input)?;
        if config.progress {
            eprintln!(
                "Auto-detected source dialect: {} (confidence: {:?})",
                result.dialect, result.confidence
            );
        }
        result.dialect
    };

    // Check for same dialect
    if from_dialect == config.to_dialect {
        anyhow::bail!(
            "Source and target dialects are the same ({}). No conversion needed.",
            from_dialect
        );
    }

    // Get file size for progress tracking
    let file_size = std::fs::metadata(&config.input)?.len();

    let progress_bar = if config.progress {
        let pb = ProgressBar::new(file_size);
        pb.set_style(
            ProgressStyle::with_template(
                "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({percent}%) {msg}",
            )
            .unwrap()
            .progress_chars("█▓▒░  ")
            .tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"),
        );
        pb.enable_steady_tick(std::time::Duration::from_millis(100));
        pb.set_message("Converting...");
        Some(pb)
    } else {
        None
    };

    // Create converter
    let mut converter = Converter::new(from_dialect, config.to_dialect).with_strict(config.strict);

    // Open input, transparently handling any supported compression format
    // (including zip archives), with optional progress tracking.
    let reader: Box<dyn Read> = if let Some(ref pb) = progress_bar {
        let pb_clone = pb.clone();
        open_input_with_progress(
            &config.input,
            Box::new(move |bytes| pb_clone.set_position(bytes)),
        )?
    } else {
        open_input(&config.input)?
    };
    let mut parser = Parser::with_dialect(reader, 64 * 1024, from_dialect);

    // Open output
    let mut writer: Box<dyn Write> = if config.dry_run {
        Box::new(std::io::sink())
    } else {
        match &config.output {
            Some(path) => {
                if let Some(parent) = path.parent() {
                    std::fs::create_dir_all(parent)?;
                }
                Box::new(BufWriter::with_capacity(256 * 1024, File::create(path)?))
            }
            None => Box::new(BufWriter::new(std::io::stdout())),
        }
    };

    // Write header
    if !config.dry_run {
        write_header(&mut writer, &config, from_dialect)?;
    }

    // Process statements
    while let Some(stmt) = parser.read_statement()? {
        stats.statements_processed += 1;

        // Check if this is a COPY data block (follows a COPY header)
        if converter.has_pending_copy() {
            // This is a data block, convert it to INSERT statements
            match converter.process_copy_data(&stmt) {
                Ok(inserts) => {
                    for insert in inserts {
                        if !insert.is_empty() {
                            stats.statements_converted += 1;
                            if !config.dry_run {
                                writer.write_all(&insert)?;
                                writer.write_all(b"\n")?;
                            }
                        }
                    }
                }
                Err(warning) => {
                    stats.warnings.push(warning);
                    stats.statements_skipped += 1;
                }
            }
            continue;
        }

        match converter.convert_statement(&stmt) {
            Ok(converted) => {
                if converted.is_empty() {
                    stats.statements_skipped += 1;
                } else if converted == stmt {
                    stats.statements_unchanged += 1;
                    if !config.dry_run {
                        writer.write_all(&converted)?;
                        writer.write_all(b"\n")?;
                    }
                } else {
                    stats.statements_converted += 1;
                    if !config.dry_run {
                        writer.write_all(&converted)?;
                        writer.write_all(b"\n")?;
                    }
                }
            }
            Err(warning) => {
                stats.warnings.push(warning);
                stats.statements_skipped += 1;
            }
        }
    }

    // Statements that only become valid once everything else has been applied
    // (a MySQL AUTO_INCREMENT column has to be a key first, and pg_dump adds the
    // primary key after the data).
    for statement in converter.take_deferred_statements() {
        stats.statements_converted += 1;
        if !config.dry_run {
            writer.write_all(statement.as_bytes())?;
            writer.write_all(b"\n")?;
        }
    }

    // Collect warnings
    stats.warnings.extend(converter.warnings().iter().cloned());

    if let Some(pb) = progress_bar {
        pb.finish_with_message("done");
    }

    Ok(stats)
}

/// Write output header
fn write_header(
    writer: &mut dyn Write,
    config: &ConvertConfig,
    from: SqlDialect,
) -> std::io::Result<()> {
    writeln!(writer, "-- Converted by sql-splitter")?;
    writeln!(writer, "-- From: {} → To: {}", from, config.to_dialect)?;
    writeln!(writer, "-- Source: {}", config.input.display())?;
    writeln!(writer)?;

    // Write dialect-specific header
    match config.to_dialect {
        SqlDialect::Postgres => {
            writeln!(writer, "SET client_encoding = 'UTF8';")?;
            writeln!(writer, "SET standard_conforming_strings = on;")?;
        }
        SqlDialect::Sqlite => {
            writeln!(writer, "PRAGMA foreign_keys = OFF;")?;
        }
        SqlDialect::MySql => {
            writeln!(writer, "SET NAMES utf8mb4;")?;
            writeln!(writer, "SET FOREIGN_KEY_CHECKS = 0;")?;
        }
        SqlDialect::Mssql => {
            writeln!(writer, "SET ANSI_NULLS ON;")?;
            writeln!(writer, "SET QUOTED_IDENTIFIER ON;")?;
            writeln!(writer, "SET NOCOUNT ON;")?;
        }
    }
    writeln!(writer)?;

    Ok(())
}

/// Advance past a single-quoted SQL string literal starting at `start` (the
/// opening quote), returning the index just past the closing quote. A doubled
/// `''` is an escaped quote that keeps the string open. An unterminated literal
/// consumes to the end. `bytes[start]` must be `'`.
fn skip_sql_string_literal(bytes: &[u8], start: usize) -> usize {
    let mut i = start + 1;
    while i < bytes.len() {
        if bytes[i] == b'\'' {
            if bytes.get(i + 1) == Some(&b'\'') {
                i += 2; // escaped quote, stay inside the string
            } else {
                return i + 1; // closing quote
            }
        } else {
            i += 1;
        }
    }
    i
}

/// Apply `f` to every span of `stmt` that lies outside a single-quoted string
/// literal, copying the literals through verbatim. Used to keep statement-level
/// rewrites (cast/schema-prefix stripping) from mangling string *data*.
fn map_outside_string_literals(stmt: &str, f: impl Fn(&str) -> String) -> String {
    let bytes = stmt.as_bytes();
    let mut out = String::with_capacity(stmt.len());
    let mut i = 0;
    let mut seg_start = 0;
    while i < bytes.len() {
        if bytes[i] == b'\'' {
            // Flush the preceding non-string span through `f`.
            out.push_str(&f(&stmt[seg_start..i]));
            let lit_end = skip_sql_string_literal(bytes, i);
            out.push_str(&stmt[i..lit_end]); // literal, verbatim
            i = lit_end;
            seg_start = i;
        } else {
            i += 1;
        }
    }
    out.push_str(&f(&stmt[seg_start..]));
    out
}