athena_rs 3.26.3

Hyper performant polyglot Database driver
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
use actix_web::{
    HttpRequest, Responder, post,
    web::{Data, Json},
};
use serde_json::json;
use sqlx::{PgPool, Row};
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::ffi::OsString;
use tokio::process::Command;

use super::types::{
    DdlObjectFamily, ExportedDdlObject, ManagementDdlExportFiltersApplied,
    ManagementDdlExportRequest, ManagementDdlExportResponse,
};
use super::{
    MANAGEMENT_READ_RIGHT, authorize_management_request, registered_client_for,
    required_client_name,
};
use crate::AppState;
use crate::api::client_context::pool_for_client;
use crate::api::response::{api_success, bad_request, internal_error};
use crate::drivers::postgresql::sqlx_driver::RegisteredClient;
use crate::parser::resolve_compatible_postgres_uri;
use crate::utils::pg_tools::ensure_pg_tools;
use crate::utils::request_logging::{log_operation_event, log_request};

#[derive(Debug, Clone)]
struct DdlExportRequestNormalized {
    client_name: String,
    schemas: Option<Vec<String>>,
    schema_filter: Option<HashSet<String>>,
    object_families: Vec<DdlObjectFamily>,
    requested_families: HashSet<DdlObjectFamily>,
    include_system: bool,
    include_roles: bool,
    include_databases: bool,
    format: String,
}

#[derive(Debug, Clone)]
struct ClassifiedStatement {
    family: DdlObjectFamily,
    schema_name: Option<String>,
    object_name: String,
    identity: Option<String>,
    ddl: String,
    original_index: usize,
    sort_weight: usize,
    sort_schema_name: Option<String>,
    sort_object_name: String,
}

#[derive(Debug, Clone)]
struct SchemaPreamble {
    schema_name: String,
    ddl: String,
}

#[derive(Debug, Clone)]
struct ExportedDdlGroup {
    family: DdlObjectFamily,
    schema_name: Option<String>,
    object_name: String,
    identity: Option<String>,
    statements: Vec<String>,
    original_index: usize,
    sort_weight: usize,
    sort_schema_name: Option<String>,
    sort_object_name: String,
}

#[derive(Debug, Clone)]
struct DdlExportResult {
    sql: String,
    objects: Vec<ExportedDdlObject>,
    warnings: Vec<String>,
}

#[post("/management/ddl/export")]
pub async fn management_export_ddl(
    req: HttpRequest,
    body: Json<ManagementDdlExportRequest>,
    app_state: Data<AppState>,
) -> impl Responder {
    let started = std::time::Instant::now();
    let caller_client = match required_client_name(&req) {
        Ok(value) => value,
        Err(resp) => return resp,
    };
    let normalized = match normalize_request(&body.0, &caller_client) {
        Ok(value) => value,
        Err(resp) => return resp,
    };
    if let Err(resp) = registered_client_for(app_state.get_ref(), &normalized.client_name) {
        return resp;
    }
    let auth = match authorize_management_request(
        &req,
        app_state.get_ref(),
        &normalized.client_name,
        vec![MANAGEMENT_READ_RIGHT.to_string()],
    )
    .await
    {
        Ok(auth) => auth,
        Err(resp) => return resp,
    };
    let logged_request = log_request(
        req.clone(),
        Some(app_state.get_ref()),
        Some(auth.request_id.clone()),
        Some(&auth.log_context),
    );

    let client = match registered_client_for(app_state.get_ref(), &normalized.client_name) {
        Ok(value) => value,
        Err(resp) => return resp,
    };
    let pool = match pool_for_client(app_state.get_ref(), &normalized.client_name).await {
        Ok(value) => value,
        Err(resp) => return resp,
    };

    let export = match run_ddl_export(&normalized, &client, &pool).await {
        Ok(value) => value,
        Err(err) => {
            log_operation_event(
                Some(app_state.get_ref()),
                &logged_request,
                "management_export_ddl",
                None,
                started.elapsed().as_millis(),
                actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
                Some(json!({ "error": err })),
            );
            return internal_error("Failed to export DDL", err);
        }
    };

    let filters_applied = ManagementDdlExportFiltersApplied {
        client_name: normalized.client_name.clone(),
        schemas: normalized.schemas.clone(),
        object_families: normalized.object_families.clone(),
        include_system: normalized.include_system,
        include_roles: normalized.include_roles,
        include_databases: normalized.include_databases,
        format: normalized.format.clone(),
    };
    let response = ManagementDdlExportResponse {
        sql: export.sql,
        objects: export.objects,
        warnings: export.warnings,
        filters_applied,
    };

    log_operation_event(
        Some(app_state.get_ref()),
        &logged_request,
        "management_export_ddl",
        None,
        started.elapsed().as_millis(),
        actix_web::http::StatusCode::OK,
        Some(json!({
            "object_count": response.objects.len(),
            "warning_count": response.warnings.len(),
        })),
    );

    api_success("Exported DDL", json!(response))
}

fn normalize_request(
    body: &ManagementDdlExportRequest,
    caller_client: &str,
) -> Result<DdlExportRequestNormalized, actix_web::HttpResponse> {
    let body_client = body.client_name.trim();
    if body_client.is_empty() {
        return Err(bad_request(
            "Invalid DDL export request",
            "client_name is required",
        ));
    }
    if body_client != caller_client {
        return Err(bad_request(
            "Invalid DDL export request",
            format!(
                "Body client_name '{}' must match the bound client header '{}'.",
                body_client, caller_client
            ),
        ));
    }

    let format = body.format.trim().to_ascii_lowercase();
    if format != "sql" {
        return Err(bad_request(
            "Invalid DDL export request",
            format!(
                "Unsupported export format '{}'. Only 'sql' is supported.",
                body.format
            ),
        ));
    }

    let requested_families: HashSet<DdlObjectFamily> =
        body.object_families.iter().copied().collect();
    if requested_families.is_empty() {
        return Err(bad_request(
            "Invalid DDL export request",
            "At least one object family must be selected.",
        ));
    }

    let mut schema_values: Vec<String> = body
        .schemas
        .clone()
        .unwrap_or_default()
        .into_iter()
        .map(|value| normalize_schema_name(&value))
        .filter(|value| !value.is_empty())
        .collect();
    schema_values.sort();
    schema_values.dedup();
    let schemas = if schema_values.is_empty() {
        None
    } else {
        Some(schema_values)
    };
    let schema_filter = schemas
        .as_ref()
        .map(|items| items.iter().cloned().collect::<HashSet<String>>());

    Ok(DdlExportRequestNormalized {
        client_name: body_client.to_string(),
        schemas,
        schema_filter,
        object_families: body.object_families.clone(),
        requested_families,
        include_system: body.include_system,
        include_roles: body.include_roles,
        include_databases: body.include_databases,
        format,
    })
}

async fn run_ddl_export(
    request: &DdlExportRequestNormalized,
    client: &RegisteredClient,
    pool: &PgPool,
) -> Result<DdlExportResult, String> {
    let mut warnings: Vec<String> = Vec::new();
    let mut schema_preambles: Vec<SchemaPreamble> = Vec::new();
    let mut groups: Vec<ExportedDdlGroup> = Vec::new();

    if needs_schema_dump(request) {
        let dump_sql = run_pg_dump_schema_only(request, client).await?;
        let (dump_preambles, dump_groups, dump_warnings) = classify_dump_sql(&dump_sql, request);
        schema_preambles = dump_preambles;
        groups = dump_groups;
        warnings.extend(dump_warnings);
    }

    if request.requested_families.contains(&DdlObjectFamily::Roles) {
        if request.include_roles {
            let role_groups = export_roles(pool, request).await?;
            if role_groups.is_empty() {
                warnings
                    .push("No database roles matched the requested export filters.".to_string());
            } else {
                groups.extend(role_groups);
                warnings.push(
                    "Role export omits passwords, memberships, and object grants in this v1 DDL export."
                        .to_string(),
                );
            }
        } else {
            warnings.push(
                "Roles were requested but omitted because include_roles is disabled.".to_string(),
            );
        }
    }

    if request
        .requested_families
        .contains(&DdlObjectFamily::Databases)
    {
        if request.include_databases {
            let database_groups = export_current_database(pool).await?;
            if database_groups.is_empty() {
                warnings.push(
                    "No database definition was available for the connected client.".to_string(),
                );
            } else {
                groups.extend(database_groups);
                warnings.push(
                    "Database export emits the connected database definition only and omits ownership, tablespace, and database-level grants."
                        .to_string(),
                );
            }
        } else {
            warnings.push(
                "Databases were requested but omitted because include_databases is disabled."
                    .to_string(),
            );
        }
    }

    groups.sort_by(compare_exported_group_order);

    let generated_at = chrono::Utc::now().to_rfc3339();
    let objects: Vec<ExportedDdlObject> = groups
        .iter()
        .enumerate()
        .map(|(index, group)| ExportedDdlObject {
            family: group.family,
            schema_name: group.schema_name.clone(),
            object_name: group.object_name.clone(),
            identity: group.identity.clone(),
            ddl: group.statements.join("\n\n"),
            order_key: format!(
                "{:02}:{:04}:{}:{}:{}",
                group.sort_weight,
                index,
                group.schema_name.clone().unwrap_or_default(),
                group.object_name,
                group.identity.clone().unwrap_or_default()
            ),
        })
        .collect();
    let sql = build_output_sql(&generated_at, request, &schema_preambles, &objects);

    Ok(DdlExportResult {
        sql,
        objects,
        warnings,
    })
}

fn needs_schema_dump(request: &DdlExportRequestNormalized) -> bool {
    request.requested_families.iter().any(|family| {
        matches!(
            family,
            DdlObjectFamily::Tables
                | DdlObjectFamily::Views
                | DdlObjectFamily::MaterializedViews
                | DdlObjectFamily::Functions
                | DdlObjectFamily::Types
                | DdlObjectFamily::Extensions
                | DdlObjectFamily::Sequences
        )
    })
}

async fn run_pg_dump_schema_only(
    request: &DdlExportRequestNormalized,
    client: &RegisteredClient,
) -> Result<String, String> {
    let pg_tools = ensure_pg_tools()
        .await
        .map_err(|err| format!("pg_dump resolution failed: {err}"))?;
    let connection_uri = resolve_registered_client_connection_uri(client).ok_or_else(|| {
        format!(
            "No usable PostgreSQL connection URI is configured for client '{}'.",
            request.client_name
        )
    })?;
    let (sanitized_uri, pg_password) = extract_pg_password(&connection_uri);

    let mut cmd = Command::new(pg_tools.pg_dump);
    if let Some(password) = pg_password {
        cmd.env("PGPASSWORD", password);
    }
    cmd.args(pg_dump_export_cli_args(&sanitized_uri, request));
    cmd.stdout(std::process::Stdio::piped());
    cmd.stderr(std::process::Stdio::piped());

    let output = cmd
        .output()
        .await
        .map_err(|err| format!("Failed to run pg_dump: {err}"))?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
        let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
        let code = output.status.code().unwrap_or(-1);
        let detail = if !stderr.is_empty() {
            stderr
        } else if !stdout.is_empty() {
            stdout
        } else {
            "No output from pg_dump.".to_string()
        };
        return Err(format!("pg_dump exited with code {code}: {detail}"));
    }

    String::from_utf8(output.stdout)
        .map_err(|err| format!("pg_dump returned non-UTF8 schema output: {err}"))
}

fn pg_dump_export_cli_args(
    pg_uri_safe: &str,
    request: &DdlExportRequestNormalized,
) -> Vec<OsString> {
    let mut args = vec![
        OsString::from("--dbname"),
        OsString::from(pg_uri_safe),
        OsString::from("--schema-only"),
        OsString::from("--no-owner"),
        OsString::from("--no-privileges"),
        OsString::from("--no-comments"),
    ];
    if let Some(schemas) = &request.schemas {
        for schema in schemas {
            args.push(OsString::from("--schema"));
            args.push(OsString::from(schema));
        }
    }
    args
}

fn classify_dump_sql(
    dump_sql: &str,
    request: &DdlExportRequestNormalized,
) -> (Vec<SchemaPreamble>, Vec<ExportedDdlGroup>, Vec<String>) {
    let mut preambles: Vec<SchemaPreamble> = Vec::new();
    let mut warnings: Vec<String> = Vec::new();
    let mut groups: HashMap<String, ExportedDdlGroup> = HashMap::new();
    let mut order: Vec<String> = Vec::new();

    for statement in split_pg_dump_sql_statements(dump_sql) {
        let trimmed = statement.sql.trim();
        if trimmed.is_empty() {
            continue;
        }
        let classified_sql = strip_leading_sql_comments(trimmed);
        if classified_sql.is_empty() {
            continue;
        }
        if let Some(schema_name) = classify_schema_preamble(classified_sql) {
            if schema_allowed(Some(&schema_name), request) {
                preambles.push(SchemaPreamble {
                    schema_name,
                    ddl: classified_sql.to_string(),
                });
            }
            continue;
        }
        let Some(classified) =
            classify_schema_statement(classified_sql, statement.index, &mut warnings)
        else {
            continue;
        };
        if !request.requested_families.contains(&classified.family) {
            continue;
        }
        if !schema_allowed(classified.schema_name.as_deref(), request) {
            continue;
        }
        let group_key = format!(
            "{:?}|{}|{}|{}|{}|{}",
            classified.family,
            classified.schema_name.clone().unwrap_or_default(),
            classified.object_name,
            classified.identity.clone().unwrap_or_default(),
            classified.sort_schema_name.clone().unwrap_or_default(),
            classified.sort_object_name
        );
        if let Some(group) = groups.get_mut(&group_key) {
            group.statements.push(classified.ddl);
        } else {
            order.push(group_key.clone());
            groups.insert(
                group_key,
                ExportedDdlGroup {
                    family: classified.family,
                    schema_name: classified.schema_name,
                    object_name: classified.object_name,
                    identity: classified.identity,
                    statements: vec![classified.ddl],
                    original_index: classified.original_index,
                    sort_weight: classified.sort_weight,
                    sort_schema_name: classified.sort_schema_name,
                    sort_object_name: classified.sort_object_name,
                },
            );
        }
    }

    let ordered_groups = order
        .into_iter()
        .filter_map(|key| groups.remove(&key))
        .collect::<Vec<_>>();
    preambles.sort_by(|left, right| left.schema_name.cmp(&right.schema_name));
    preambles.dedup_by(|left, right| left.schema_name == right.schema_name);
    (preambles, ordered_groups, warnings)
}

fn strip_leading_sql_comments(statement: &str) -> &str {
    let mut current = statement.trim_start();
    loop {
        if let Some(rest) = current.strip_prefix("--") {
            if let Some(newline_index) = rest.find('\n') {
                current = rest[newline_index + 1..].trim_start();
                continue;
            }
            return "";
        }
        if let Some(rest) = current.strip_prefix("/*") {
            if let Some(end_index) = rest.find("*/") {
                current = rest[end_index + 2..].trim_start();
                continue;
            }
            return "";
        }
        return current;
    }
}

#[derive(Debug, Clone)]
struct ParsedStatement {
    index: usize,
    sql: String,
}

fn split_pg_dump_sql_statements(input: &str) -> Vec<ParsedStatement> {
    let chars: Vec<char> = input.chars().collect();
    let mut statements: Vec<ParsedStatement> = Vec::new();

    let mut statement_start = 0usize;
    let mut in_single_quote = false;
    let mut in_double_quote = false;
    let mut in_line_comment = false;
    let mut in_block_comment = false;
    let mut dollar_tag: Option<String> = None;

    let mut idx = 0usize;
    while idx < chars.len() {
        let ch = chars[idx];
        let next = chars.get(idx + 1).copied();

        if in_line_comment {
            if ch == '\n' {
                in_line_comment = false;
            }
            idx += 1;
            continue;
        }

        if in_block_comment {
            if ch == '*' && next == Some('/') {
                in_block_comment = false;
                idx += 2;
            } else {
                idx += 1;
            }
            continue;
        }

        if let Some(tag) = &dollar_tag {
            if ch == '$' {
                let candidate = read_dollar_tag(&chars, idx);
                if candidate.as_deref() == Some(tag.as_str()) {
                    let tag_len = tag.len();
                    dollar_tag = None;
                    idx += tag_len;
                    continue;
                }
            }
            idx += 1;
            continue;
        }

        if !in_single_quote && !in_double_quote {
            if ch == '-' && next == Some('-') {
                in_line_comment = true;
                idx += 2;
                continue;
            }
            if ch == '/' && next == Some('*') {
                in_block_comment = true;
                idx += 2;
                continue;
            }
            if ch == '$'
                && let Some(tag) = read_dollar_tag(&chars, idx)
            {
                dollar_tag = Some(tag.clone());
                idx += tag.len();
                continue;
            }
        }

        if ch == '\'' && !in_double_quote {
            if in_single_quote && next == Some('\'') {
                idx += 2;
                continue;
            }
            in_single_quote = !in_single_quote;
            idx += 1;
            continue;
        }

        if ch == '"' && !in_single_quote {
            if in_double_quote && next == Some('"') {
                idx += 2;
                continue;
            }
            in_double_quote = !in_double_quote;
            idx += 1;
            continue;
        }

        if ch == ';' && !in_single_quote && !in_double_quote {
            let sql: String = chars[statement_start..=idx].iter().collect();
            if !sql.trim().is_empty() {
                statements.push(ParsedStatement {
                    index: statements.len(),
                    sql,
                });
            }
            statement_start = idx + 1;
        }
        idx += 1;
    }

    if statement_start < chars.len() {
        let sql: String = chars[statement_start..].iter().collect();
        if !sql.trim().is_empty() {
            statements.push(ParsedStatement {
                index: statements.len(),
                sql,
            });
        }
    }

    statements
}

fn read_dollar_tag(chars: &[char], start: usize) -> Option<String> {
    if chars.get(start).copied() != Some('$') {
        return None;
    }
    let mut idx = start + 1;
    while idx < chars.len() {
        let ch = chars[idx];
        if ch == '$' {
            let tag: String = chars[start..=idx].iter().collect();
            let valid = tag[1..tag.len() - 1]
                .chars()
                .all(|value| value == '_' || value.is_ascii_alphanumeric());
            return valid.then_some(tag);
        }
        if !(ch == '_' || ch.is_ascii_alphanumeric()) {
            return None;
        }
        idx += 1;
    }
    None
}

fn classify_schema_preamble(statement: &str) -> Option<String> {
    let lower = statement.trim_start().to_ascii_lowercase();
    if !lower.starts_with("create schema") {
        return None;
    }
    let rest = statement.trim_start()[lower.find("schema").unwrap_or(0) + "schema".len()..].trim();
    let rest = rest.strip_prefix("IF NOT EXISTS ").unwrap_or(rest);
    let (qualified, _) = read_qualified_identifier(rest)?;
    split_qualified_identifier_parts(&qualified)
        .last()
        .map(|value| unquote_identifier(value))
}

fn classify_schema_statement(
    statement: &str,
    original_index: usize,
    warnings: &mut Vec<String>,
) -> Option<ClassifiedStatement> {
    let compact = statement.trim_start();
    let lower = compact.to_ascii_lowercase();

    if lower.starts_with("create extension") {
        let rest = compact[lower.find("extension").unwrap_or(0) + "extension".len()..].trim();
        let rest = rest.strip_prefix("IF NOT EXISTS ").unwrap_or(rest);
        let (name, _) = read_qualified_identifier(rest)?;
        return Some(ClassifiedStatement {
            family: DdlObjectFamily::Extensions,
            schema_name: None,
            object_name: unquote_identifier(&name),
            identity: None,
            ddl: compact.to_string(),
            original_index,
            sort_weight: family_sort_weight(DdlObjectFamily::Extensions),
            sort_schema_name: None,
            sort_object_name: unquote_identifier(&name),
        });
    }

    if lower.starts_with("create type") || lower.starts_with("create domain") {
        let keyword = if lower.starts_with("create type") {
            "type"
        } else {
            "domain"
        };
        let rest = compact[lower.find(keyword).unwrap_or(0) + keyword.len()..].trim();
        let (qualified, _) = read_qualified_identifier(rest)?;
        let (schema_name, object_name) = split_qualified_identifier(&qualified);
        return Some(ClassifiedStatement {
            family: DdlObjectFamily::Types,
            sort_schema_name: schema_name.clone(),
            sort_object_name: object_name.clone(),
            schema_name,
            object_name,
            identity: None,
            ddl: compact.to_string(),
            original_index,
            sort_weight: family_sort_weight(DdlObjectFamily::Types),
        });
    }

    if lower.starts_with("alter type") || lower.starts_with("alter domain") {
        let keyword = if lower.starts_with("alter type") {
            "type"
        } else {
            "domain"
        };
        let rest = compact[lower.find(keyword).unwrap_or(0) + keyword.len()..].trim();
        let (qualified, _) = read_qualified_identifier(rest)?;
        let (schema_name, object_name) = split_qualified_identifier(&qualified);
        return Some(ClassifiedStatement {
            family: DdlObjectFamily::Types,
            sort_schema_name: schema_name.clone(),
            sort_object_name: object_name.clone(),
            schema_name,
            object_name,
            identity: None,
            ddl: compact.to_string(),
            original_index,
            sort_weight: family_sort_weight(DdlObjectFamily::Types),
        });
    }

    if lower.starts_with("create sequence") {
        let rest = compact[lower.find("sequence").unwrap_or(0) + "sequence".len()..].trim();
        let (qualified, _) = read_qualified_identifier(rest)?;
        let (schema_name, object_name) = split_qualified_identifier(&qualified);
        return Some(ClassifiedStatement {
            family: DdlObjectFamily::Sequences,
            sort_schema_name: schema_name.clone(),
            sort_object_name: object_name.clone(),
            schema_name,
            object_name,
            identity: None,
            ddl: compact.to_string(),
            original_index,
            sort_weight: family_sort_weight(DdlObjectFamily::Sequences),
        });
    }

    if lower.starts_with("alter sequence") {
        let rest = compact[lower.find("sequence").unwrap_or(0) + "sequence".len()..].trim();
        let (qualified, _) = read_qualified_identifier(rest)?;
        let (schema_name, object_name) = split_qualified_identifier(&qualified);
        let is_owned_by_statement = extract_owned_by_sort_target(compact).is_some();
        return Some(ClassifiedStatement {
            family: DdlObjectFamily::Sequences,
            sort_schema_name: schema_name.clone(),
            sort_object_name: if is_owned_by_statement {
                grouped_statement_sort_object_name(&object_name, "owned_by", original_index)
            } else {
                object_name.clone()
            },
            schema_name,
            object_name,
            identity: None,
            ddl: compact.to_string(),
            original_index,
            sort_weight: family_sort_weight(DdlObjectFamily::Sequences),
        });
    }

    if lower.starts_with("create table") {
        let rest = compact[lower.find("table").unwrap_or(0) + "table".len()..].trim();
        let rest = rest.strip_prefix("IF NOT EXISTS ").unwrap_or(rest);
        let (qualified, _) = read_qualified_identifier(rest)?;
        let (schema_name, object_name) = split_qualified_identifier(&qualified);
        return Some(ClassifiedStatement {
            family: DdlObjectFamily::Tables,
            sort_schema_name: schema_name.clone(),
            sort_object_name: object_name.clone(),
            schema_name,
            object_name,
            identity: None,
            ddl: compact.to_string(),
            original_index,
            sort_weight: family_sort_weight(DdlObjectFamily::Tables),
        });
    }

    if lower.starts_with("alter table") {
        let rest = compact[lower.find("table").unwrap_or(0) + "table".len()..].trim();
        let rest = rest.strip_prefix("ONLY ").unwrap_or(rest);
        let (qualified, _) = read_qualified_identifier(rest)?;
        let (schema_name, object_name) = split_qualified_identifier(&qualified);
        let is_reference_alter = contains_references_keyword(compact);
        return Some(ClassifiedStatement {
            family: DdlObjectFamily::Tables,
            sort_schema_name: schema_name.clone(),
            sort_object_name: if is_reference_alter {
                grouped_statement_sort_object_name(&object_name, "references", original_index)
            } else {
                object_name.clone()
            },
            schema_name,
            object_name,
            identity: None,
            ddl: compact.to_string(),
            original_index,
            sort_weight: family_sort_weight(DdlObjectFamily::Tables),
        });
    }

    if lower.starts_with("create index")
        || lower.starts_with("create unique index")
        || lower.starts_with("create index concurrently")
        || lower.starts_with("create unique index concurrently")
    {
        let Some(qualified) = extract_on_target_qualified_name(compact) else {
            warnings.push(format!(
                "Skipped CREATE INDEX statement with unrecognized target: {compact}"
            ));
            return None;
        };
        let (schema_name, object_name) = split_qualified_identifier(&qualified);
        return Some(ClassifiedStatement {
            family: DdlObjectFamily::Tables,
            sort_schema_name: schema_name.clone(),
            sort_object_name: object_name.clone(),
            schema_name,
            object_name,
            identity: None,
            ddl: compact.to_string(),
            original_index,
            sort_weight: family_sort_weight(DdlObjectFamily::Tables),
        });
    }

    if lower.starts_with("create materialized view") {
        let rest = compact[lower.find("view").unwrap_or(0) + "view".len()..].trim();
        let (qualified, _) = read_qualified_identifier(rest)?;
        let (schema_name, object_name) = split_qualified_identifier(&qualified);
        return Some(ClassifiedStatement {
            family: DdlObjectFamily::MaterializedViews,
            sort_schema_name: schema_name.clone(),
            sort_object_name: object_name.clone(),
            schema_name,
            object_name,
            identity: None,
            ddl: compact.to_string(),
            original_index,
            sort_weight: family_sort_weight(DdlObjectFamily::MaterializedViews),
        });
    }

    if lower.starts_with("alter materialized view") {
        let rest = compact[lower.find("view").unwrap_or(0) + "view".len()..].trim();
        let (qualified, _) = read_qualified_identifier(rest)?;
        let (schema_name, object_name) = split_qualified_identifier(&qualified);
        return Some(ClassifiedStatement {
            family: DdlObjectFamily::MaterializedViews,
            sort_schema_name: schema_name.clone(),
            sort_object_name: object_name.clone(),
            schema_name,
            object_name,
            identity: None,
            ddl: compact.to_string(),
            original_index,
            sort_weight: family_sort_weight(DdlObjectFamily::MaterializedViews),
        });
    }

    if lower.starts_with("create view") {
        let rest = compact[lower.find("view").unwrap_or(0) + "view".len()..].trim();
        let (qualified, _) = read_qualified_identifier(rest)?;
        let (schema_name, object_name) = split_qualified_identifier(&qualified);
        return Some(ClassifiedStatement {
            family: DdlObjectFamily::Views,
            sort_schema_name: schema_name.clone(),
            sort_object_name: object_name.clone(),
            schema_name,
            object_name,
            identity: None,
            ddl: compact.to_string(),
            original_index,
            sort_weight: family_sort_weight(DdlObjectFamily::Views),
        });
    }

    if lower.starts_with("create function") || lower.starts_with("create or replace function") {
        let function_rest =
            compact[lower.find("function").unwrap_or(0) + "function".len()..].trim();
        let (qualified, args, _) = parse_function_signature(function_rest)?;
        let (schema_name, object_name) = split_qualified_identifier(&qualified);
        return Some(ClassifiedStatement {
            family: DdlObjectFamily::Functions,
            sort_schema_name: schema_name.clone(),
            sort_object_name: object_name.clone(),
            schema_name,
            object_name,
            identity: Some(args),
            ddl: compact.to_string(),
            original_index,
            sort_weight: family_sort_weight(DdlObjectFamily::Functions),
        });
    }

    None
}

async fn export_roles(
    pool: &PgPool,
    request: &DdlExportRequestNormalized,
) -> Result<Vec<ExportedDdlGroup>, String> {
    let rows = sqlx::query(
        r#"
        SELECT
            rolname,
            rolsuper,
            rolinherit,
            rolcreaterole,
            rolcreatedb,
            rolcanlogin,
            rolreplication,
            rolbypassrls,
            rolconnlimit
        FROM pg_roles
        WHERE ($1::bool OR rolname NOT LIKE 'pg\_%' ESCAPE '\')
        ORDER BY rolname ASC
        "#,
    )
    .bind(request.include_system)
    .fetch_all(pool)
    .await
    .map_err(|err| format!("Failed to query database roles: {err}"))?;

    Ok(rows
        .into_iter()
        .enumerate()
        .map(|(index, row)| {
            let role_name = row.try_get::<String, _>("rolname").unwrap_or_default();
            let ddl = format!(
                "CREATE ROLE {} WITH {} {} {} {} {} {} {} CONNECTION LIMIT {};",
                quote_identifier(&role_name),
                if row.try_get::<bool, _>("rolsuper").unwrap_or(false) {
                    "SUPERUSER"
                } else {
                    "NOSUPERUSER"
                },
                if row.try_get::<bool, _>("rolinherit").unwrap_or(false) {
                    "INHERIT"
                } else {
                    "NOINHERIT"
                },
                if row.try_get::<bool, _>("rolcreaterole").unwrap_or(false) {
                    "CREATEROLE"
                } else {
                    "NOCREATEROLE"
                },
                if row.try_get::<bool, _>("rolcreatedb").unwrap_or(false) {
                    "CREATEDB"
                } else {
                    "NOCREATEDB"
                },
                if row.try_get::<bool, _>("rolcanlogin").unwrap_or(false) {
                    "LOGIN"
                } else {
                    "NOLOGIN"
                },
                if row.try_get::<bool, _>("rolreplication").unwrap_or(false) {
                    "REPLICATION"
                } else {
                    "NOREPLICATION"
                },
                if row.try_get::<bool, _>("rolbypassrls").unwrap_or(false) {
                    "BYPASSRLS"
                } else {
                    "NOBYPASSRLS"
                },
                row.try_get::<i32, _>("rolconnlimit").unwrap_or(-1),
            );
            ExportedDdlGroup {
                family: DdlObjectFamily::Roles,
                schema_name: None,
                object_name: role_name.clone(),
                identity: None,
                statements: vec![ddl],
                original_index: index,
                sort_weight: family_sort_weight(DdlObjectFamily::Roles),
                sort_schema_name: None,
                sort_object_name: role_name,
            }
        })
        .collect())
}

async fn export_current_database(pool: &PgPool) -> Result<Vec<ExportedDdlGroup>, String> {
    let rows = sqlx::query(
        r#"
        SELECT
            datname,
            pg_encoding_to_char(encoding) AS encoding,
            datcollate,
            datctype
        FROM pg_database
        WHERE datname = current_database()
        "#,
    )
    .fetch_all(pool)
    .await
    .map_err(|err| format!("Failed to query current database metadata: {err}"))?;

    Ok(rows
        .into_iter()
        .enumerate()
        .map(|(index, row)| {
            let datname = row.try_get::<String, _>("datname").unwrap_or_default();
            let encoding = row
                .try_get::<String, _>("encoding")
                .unwrap_or_else(|_| "UTF8".to_string());
            let datcollate = row.try_get::<String, _>("datcollate").unwrap_or_default();
            let datctype = row.try_get::<String, _>("datctype").unwrap_or_default();
            let ddl = format!(
                "CREATE DATABASE {} WITH ENCODING = '{}' LC_COLLATE = '{}' LC_CTYPE = '{}';",
                quote_identifier(&datname),
                escape_sql_literal(&encoding),
                escape_sql_literal(&datcollate),
                escape_sql_literal(&datctype),
            );
            ExportedDdlGroup {
                family: DdlObjectFamily::Databases,
                schema_name: None,
                object_name: datname.clone(),
                identity: None,
                statements: vec![ddl],
                original_index: index,
                sort_weight: family_sort_weight(DdlObjectFamily::Databases),
                sort_schema_name: None,
                sort_object_name: datname,
            }
        })
        .collect())
}

fn build_output_sql(
    generated_at: &str,
    request: &DdlExportRequestNormalized,
    preambles: &[SchemaPreamble],
    objects: &[ExportedDdlObject],
) -> String {
    let selected_schemas = request
        .schemas
        .clone()
        .unwrap_or_else(|| vec!["all".to_string()])
        .join(", ");
    let selected_families = request
        .object_families
        .iter()
        .map(|family| family_label(*family))
        .collect::<Vec<_>>()
        .join(", ");

    let mut out = vec![
        "-- Athena generated DDL".to_string(),
        format!("-- Client: {}", request.client_name),
        format!("-- Generated at: {generated_at}"),
        format!("-- Schemas: {selected_schemas}"),
        format!("-- Families: {selected_families}"),
        format!(
            "-- Roles included: {} | Databases included: {}",
            if request.include_roles { "yes" } else { "no" },
            if request.include_databases {
                "yes"
            } else {
                "no"
            }
        ),
        String::new(),
    ];

    if !preambles.is_empty() {
        out.push("-- Schema preamble".to_string());
        out.push(String::new());
        for preamble in preambles {
            out.push(preamble.ddl.trim().to_string());
            out.push(String::new());
        }
    }

    for object in objects {
        let label = exported_object_label(object);
        out.push(format!("-- {label}"));
        out.push(object.ddl.trim().to_string());
        out.push(String::new());
    }

    out.join("\n").trim_end().to_string() + "\n"
}

fn family_sort_weight(family: DdlObjectFamily) -> usize {
    match family {
        DdlObjectFamily::Extensions => 0,
        DdlObjectFamily::Types => 1,
        DdlObjectFamily::Sequences => 2,
        DdlObjectFamily::Tables => 3,
        DdlObjectFamily::Views => 4,
        DdlObjectFamily::MaterializedViews => 5,
        DdlObjectFamily::Functions => 6,
        DdlObjectFamily::Roles => 7,
        DdlObjectFamily::Databases => 8,
    }
}

fn family_label(family: DdlObjectFamily) -> &'static str {
    match family {
        DdlObjectFamily::Tables => "tables",
        DdlObjectFamily::Views => "views",
        DdlObjectFamily::MaterializedViews => "materialized_views",
        DdlObjectFamily::Functions => "functions",
        DdlObjectFamily::Types => "types",
        DdlObjectFamily::Roles => "roles",
        DdlObjectFamily::Databases => "databases",
        DdlObjectFamily::Extensions => "extensions",
        DdlObjectFamily::Sequences => "sequences",
    }
}

fn exported_object_label(object: &ExportedDdlObject) -> String {
    let family = match object.family {
        DdlObjectFamily::Tables => "Table",
        DdlObjectFamily::Views => "View",
        DdlObjectFamily::MaterializedViews => "Materialized View",
        DdlObjectFamily::Functions => "Function",
        DdlObjectFamily::Types => "Type",
        DdlObjectFamily::Roles => "Role",
        DdlObjectFamily::Databases => "Database",
        DdlObjectFamily::Extensions => "Extension",
        DdlObjectFamily::Sequences => "Sequence",
    };
    let qualified = if let Some(schema_name) = &object.schema_name {
        format!("{schema_name}.{}", object.object_name)
    } else {
        object.object_name.clone()
    };
    if let Some(identity) = &object.identity {
        format!("{family}: {qualified}({identity})")
    } else {
        format!("{family}: {qualified}")
    }
}

fn resolve_registered_client_connection_uri(client: &RegisteredClient) -> Option<String> {
    if let Some(uri) = client
        .pg_uri
        .as_ref()
        .filter(|value| !value.trim().is_empty())
    {
        return Some(resolve_compatible_postgres_uri(uri));
    }
    if let Some(env_var) = client
        .pg_uri_env_var
        .as_ref()
        .filter(|value| !value.trim().is_empty())
    {
        return Some(resolve_compatible_postgres_uri(&format!("${{{env_var}}}")));
    }
    client
        .config_uri_template
        .as_ref()
        .filter(|value| !value.trim().is_empty())
        .map(|value| resolve_compatible_postgres_uri(value))
}

fn extract_pg_password(pg_uri: &str) -> (String, Option<String>) {
    let prefix = if pg_uri.starts_with("postgresql://") {
        "postgresql://"
    } else if pg_uri.starts_with("postgres://") {
        "postgres://"
    } else {
        return (pg_uri.to_string(), None);
    };
    let after_scheme = &pg_uri[prefix.len()..];
    if let Some(at_pos) = after_scheme.rfind('@') {
        let userinfo = &after_scheme[..at_pos];
        let after_at = &after_scheme[at_pos..];
        if let Some(colon_pos) = userinfo.find(':') {
            let user = &userinfo[..colon_pos];
            let password = decode_percent_component(&userinfo[colon_pos + 1..])
                .unwrap_or_else(|| userinfo[colon_pos + 1..].to_string());
            return (format!("{prefix}{user}{after_at}"), Some(password));
        }
    }
    (pg_uri.to_string(), None)
}

fn decode_percent_component(value: &str) -> Option<String> {
    let bytes = value.as_bytes();
    let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
    let mut idx = 0usize;
    while idx < bytes.len() {
        if bytes[idx] == b'%' {
            let hi = *bytes.get(idx + 1)?;
            let lo = *bytes.get(idx + 2)?;
            out.push((decode_hex_nibble(hi)? << 4) | decode_hex_nibble(lo)?);
            idx += 3;
            continue;
        }
        out.push(bytes[idx]);
        idx += 1;
    }
    String::from_utf8(out).ok()
}

fn decode_hex_nibble(value: u8) -> Option<u8> {
    match value {
        b'0'..=b'9' => Some(value - b'0'),
        b'a'..=b'f' => Some(value - b'a' + 10),
        b'A'..=b'F' => Some(value - b'A' + 10),
        _ => None,
    }
}

fn compare_exported_group_order(left: &ExportedDdlGroup, right: &ExportedDdlGroup) -> Ordering {
    match (
        is_schema_dump_family(left.family),
        is_schema_dump_family(right.family),
    ) {
        (true, true) => left.original_index.cmp(&right.original_index),
        (true, false) => Ordering::Less,
        (false, true) => Ordering::Greater,
        (false, false) => left
            .sort_weight
            .cmp(&right.sort_weight)
            .then_with(|| left.sort_schema_name.cmp(&right.sort_schema_name))
            .then_with(|| left.sort_object_name.cmp(&right.sort_object_name))
            .then_with(|| left.identity.cmp(&right.identity))
            .then_with(|| left.original_index.cmp(&right.original_index)),
    }
}

fn is_schema_dump_family(family: DdlObjectFamily) -> bool {
    !matches!(family, DdlObjectFamily::Roles | DdlObjectFamily::Databases)
}

fn schema_allowed(schema_name: Option<&str>, request: &DdlExportRequestNormalized) -> bool {
    if !request.include_system
        && let Some(schema_name) = schema_name
        && is_system_schema(schema_name)
    {
        return false;
    }
    if let Some(filter) = &request.schema_filter
        && let Some(schema_name) = schema_name
    {
        return filter.contains(&normalize_schema_name(schema_name));
    }
    true
}

fn is_system_schema(schema_name: &str) -> bool {
    matches!(
        normalize_schema_name(schema_name).as_str(),
        "pg_catalog" | "information_schema" | "pg_toast"
    )
}

fn normalize_schema_name(value: &str) -> String {
    unquote_identifier(value).trim().to_ascii_lowercase()
}

fn unquote_identifier(value: &str) -> String {
    let trimmed = value.trim();
    if trimmed.starts_with('"') && trimmed.ends_with('"') && trimmed.len() >= 2 {
        trimmed[1..trimmed.len() - 1].replace("\"\"", "\"")
    } else {
        trimmed.to_string()
    }
}

fn split_qualified_identifier(value: &str) -> (Option<String>, String) {
    let parts = split_qualified_identifier_parts(value);
    match parts.as_slice() {
        [] => (None, value.trim().to_string()),
        [only] => (Some("public".to_string()), unquote_identifier(only)),
        [schema, object] => (Some(unquote_identifier(schema)), unquote_identifier(object)),
        many => (
            Some(unquote_identifier(&many[many.len() - 2])),
            unquote_identifier(&many[many.len() - 1]),
        ),
    }
}

fn extract_owned_by_sort_target(statement: &str) -> Option<(Option<String>, String)> {
    let lower = statement.to_ascii_lowercase();
    let owned_by_index = lower.find("owned by")?;
    let owned_by = statement[owned_by_index + "owned by".len()..].trim();
    if owned_by.eq_ignore_ascii_case("none") {
        return None;
    }
    let (qualified, _) = read_qualified_identifier(owned_by)?;
    let parts = split_qualified_identifier_parts(&qualified);
    match parts.as_slice() {
        [] => None,
        [table] => Some((Some("public".to_string()), unquote_identifier(table))),
        [table, _column] => Some((Some("public".to_string()), unquote_identifier(table))),
        [schema, table, ..] => Some((Some(unquote_identifier(schema)), unquote_identifier(table))),
    }
}

fn grouped_statement_sort_object_name(
    object_name: &str,
    group_kind: &str,
    original_index: usize,
) -> String {
    format!("{object_name}__{group_kind}__{original_index:06}")
}

fn contains_references_keyword(statement: &str) -> bool {
    statement
        .split_whitespace()
        .any(|token| token.eq_ignore_ascii_case("references"))
}

fn split_qualified_identifier_parts(value: &str) -> Vec<String> {
    let mut parts: Vec<String> = Vec::new();
    let mut current = String::new();
    let mut in_quotes = false;
    let chars: Vec<char> = value.trim().chars().collect();
    let mut idx = 0usize;
    while idx < chars.len() {
        let ch = chars[idx];
        if ch == '"' {
            if in_quotes && chars.get(idx + 1).copied() == Some('"') {
                current.push('"');
                idx += 2;
                continue;
            }
            in_quotes = !in_quotes;
            current.push(ch);
            idx += 1;
            continue;
        }
        if ch == '.' && !in_quotes {
            if !current.trim().is_empty() {
                parts.push(current.trim().to_string());
            }
            current.clear();
            idx += 1;
            continue;
        }
        current.push(ch);
        idx += 1;
    }
    if !current.trim().is_empty() {
        parts.push(current.trim().to_string());
    }
    parts
}

fn read_qualified_identifier(input: &str) -> Option<(String, usize)> {
    let chars: Vec<char> = input.chars().collect();
    let mut idx = 0usize;
    while idx < chars.len() && chars[idx].is_whitespace() {
        idx += 1;
    }
    let start = idx;
    let mut in_quotes = false;
    while idx < chars.len() {
        let ch = chars[idx];
        if ch == '"' {
            if in_quotes && chars.get(idx + 1).copied() == Some('"') {
                idx += 2;
                continue;
            }
            in_quotes = !in_quotes;
            idx += 1;
            continue;
        }
        if !in_quotes && (ch.is_whitespace() || ch == '(') {
            break;
        }
        idx += 1;
    }
    let value: String = chars[start..idx].iter().collect();
    (!value.trim().is_empty()).then_some((value.trim().to_string(), idx))
}

fn parse_function_signature(input: &str) -> Option<(String, String, usize)> {
    let (qualified, mut idx) = read_qualified_identifier(input)?;
    let chars: Vec<char> = input.chars().collect();
    while idx < chars.len() && chars[idx].is_whitespace() {
        idx += 1;
    }
    if chars.get(idx).copied() != Some('(') {
        return None;
    }
    let args_start = idx + 1;
    let mut depth = 1isize;
    idx += 1;
    let mut in_single = false;
    let mut in_double = false;
    let mut dollar_tag: Option<String> = None;
    while idx < chars.len() {
        let ch = chars[idx];
        if let Some(tag) = &dollar_tag {
            if ch == '$' {
                let candidate = read_dollar_tag(&chars, idx);
                if candidate.as_deref() == Some(tag.as_str()) {
                    let tag_len = tag.len();
                    dollar_tag = None;
                    idx += tag_len;
                    continue;
                }
            }
            idx += 1;
            continue;
        }
        if !in_single
            && !in_double
            && ch == '$'
            && let Some(tag) = read_dollar_tag(&chars, idx)
        {
            dollar_tag = Some(tag.clone());
            idx += tag.len();
            continue;
        }
        if ch == '\'' && !in_double {
            if in_single && chars.get(idx + 1).copied() == Some('\'') {
                idx += 2;
                continue;
            }
            in_single = !in_single;
            idx += 1;
            continue;
        }
        if ch == '"' && !in_single {
            if in_double && chars.get(idx + 1).copied() == Some('"') {
                idx += 2;
                continue;
            }
            in_double = !in_double;
            idx += 1;
            continue;
        }
        if !in_single && !in_double {
            if ch == '(' {
                depth += 1;
            } else if ch == ')' {
                depth -= 1;
                if depth == 0 {
                    let args: String = chars[args_start..idx].iter().collect();
                    return Some((qualified, args.trim().to_string(), idx + 1));
                }
            }
        }
        idx += 1;
    }
    None
}

fn extract_on_target_qualified_name(statement: &str) -> Option<String> {
    let lower = statement.to_ascii_lowercase();
    let on_index = lower.find(" on ")?;
    let rest = &statement[on_index + 4..];
    let (qualified, _) = read_qualified_identifier(rest)?;
    Some(qualified)
}

fn quote_identifier(value: &str) -> String {
    format!("\"{}\"", value.replace('"', "\"\""))
}

fn escape_sql_literal(value: &str) -> String {
    value.replace('\'', "''")
}

#[cfg(test)]
mod tests {
    use super::{
        DdlExportRequestNormalized, DdlObjectFamily, classify_dump_sql,
        compare_exported_group_order, exported_object_label, extract_pg_password,
        family_sort_weight, split_pg_dump_sql_statements,
    };
    use crate::api::management::types::ExportedDdlObject;
    use std::collections::HashSet;

    fn request(families: &[DdlObjectFamily]) -> DdlExportRequestNormalized {
        DdlExportRequestNormalized {
            client_name: "athena_logging".to_string(),
            schemas: Some(vec!["public".to_string()]),
            schema_filter: Some(HashSet::from(["public".to_string()])),
            object_families: families.to_vec(),
            requested_families: families.iter().copied().collect(),
            include_system: false,
            include_roles: false,
            include_databases: false,
            format: "sql".to_string(),
        }
    }

    #[test]
    fn splitter_handles_dollar_quoted_function_bodies() {
        let sql = r#"
CREATE FUNCTION public.echo(value text) RETURNS text
LANGUAGE plpgsql
AS $function$
BEGIN
  RETURN value || ';';
END;
$function$;

CREATE VIEW public.sample AS SELECT 1 AS id;
"#;
        let statements = split_pg_dump_sql_statements(sql);
        assert_eq!(statements.len(), 2);
        assert!(statements[0].sql.contains("RETURN value || ';'"));
    }

    #[test]
    fn classify_dump_groups_requested_families() {
        let sql = r#"
CREATE SCHEMA analytics;
CREATE TYPE public.status_enum AS ENUM ('pending', 'done');
CREATE TABLE public.users (id uuid);
ALTER TABLE ONLY public.users ADD CONSTRAINT users_pkey PRIMARY KEY (id);
CREATE INDEX users_id_idx ON public.users USING btree (id);
CREATE VIEW public.active_users AS SELECT id FROM public.users;
CREATE MATERIALIZED VIEW public.rollup AS SELECT id FROM public.users;
CREATE FUNCTION public.echo(value text) RETURNS text LANGUAGE sql AS $$ SELECT value; $$;
"#;
        let (_, groups, warnings) = classify_dump_sql(
            sql,
            &request(&[
                DdlObjectFamily::Tables,
                DdlObjectFamily::Views,
                DdlObjectFamily::MaterializedViews,
                DdlObjectFamily::Functions,
                DdlObjectFamily::Types,
            ]),
        );
        assert!(warnings.is_empty());
        assert_eq!(groups.len(), 5);
        let users = groups
            .iter()
            .find(|group| group.object_name == "users")
            .expect("users table group");
        assert_eq!(users.family, DdlObjectFamily::Tables);
        assert_eq!(users.statements.len(), 3);
    }

    #[test]
    fn classify_dump_skips_pg_dump_toc_comments() {
        let sql = r#"
-- Name: users; Type: TABLE; Schema: public; Owner: postgres
CREATE TABLE public.users (id uuid);
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
ALTER TABLE ONLY public.users ADD CONSTRAINT users_pkey PRIMARY KEY (id);
"#;
        let (_, groups, warnings) = classify_dump_sql(sql, &request(&[DdlObjectFamily::Tables]));
        assert!(warnings.is_empty());
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].object_name, "users");
        assert_eq!(groups[0].statements.len(), 2);
    }

    #[test]
    fn classify_dump_keeps_owned_by_after_table_create() {
        let sql = r#"
CREATE SEQUENCE public.users_id_seq;
CREATE TABLE public.users (id integer NOT NULL);
ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;
"#;
        let (_, mut groups, warnings) = classify_dump_sql(
            sql,
            &request(&[DdlObjectFamily::Tables, DdlObjectFamily::Sequences]),
        );
        assert!(warnings.is_empty());
        groups.sort_by(compare_exported_group_order);
        let statements = groups
            .iter()
            .flat_map(|group| group.statements.iter().map(String::as_str))
            .collect::<Vec<_>>();
        assert_eq!(statements.len(), 3);
        assert!(statements[0].starts_with("CREATE SEQUENCE public.users_id_seq"));
        assert!(statements[1].starts_with("CREATE TABLE public.users"));
        assert!(statements[2].starts_with("ALTER SEQUENCE public.users_id_seq OWNED BY"));
    }

    #[test]
    fn classify_dump_keeps_foreign_keys_after_referenced_table_create() {
        let sql = r#"
CREATE TABLE public.child (parent_id uuid NOT NULL);
CREATE TABLE public.parent (id uuid NOT NULL);
ALTER TABLE ONLY public.child ADD CONSTRAINT child_parent_fkey FOREIGN KEY (parent_id) REFERENCES public.parent(id);
"#;
        let (_, mut groups, warnings) =
            classify_dump_sql(sql, &request(&[DdlObjectFamily::Tables]));
        assert!(warnings.is_empty());
        groups.sort_by(compare_exported_group_order);
        let statements = groups
            .iter()
            .flat_map(|group| group.statements.iter().map(String::as_str))
            .collect::<Vec<_>>();
        assert_eq!(statements.len(), 3);
        assert!(statements[0].starts_with("CREATE TABLE public.child"));
        assert!(statements[1].starts_with("CREATE TABLE public.parent"));
        assert!(statements[2].starts_with("ALTER TABLE ONLY public.child ADD CONSTRAINT"));
    }

    #[test]
    fn extract_pg_password_decodes_percent_encoding() {
        let (sanitized, password) =
            extract_pg_password("postgres://user:p%40ss%2Fword@localhost/example");
        assert_eq!(sanitized, "postgres://user@localhost/example");
        assert_eq!(password.as_deref(), Some("p@ss/word"));
    }

    #[test]
    fn object_label_is_family_aware() {
        let label = exported_object_label(&ExportedDdlObject {
            family: DdlObjectFamily::Functions,
            schema_name: Some("public".to_string()),
            object_name: "echo".to_string(),
            identity: Some("value text".to_string()),
            ddl: "CREATE FUNCTION public.echo(value text) ...".to_string(),
            order_key: format!("{:02}:0000", family_sort_weight(DdlObjectFamily::Functions)),
        });
        assert_eq!(label, "Function: public.echo(value text)");
    }
}