djogi 0.1.0-alpha.17

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

use crate::context::DjogiContext;
use crate::pg::connection::PgConnection;
use crate::pg::decode::{FromPgRow, try_get_scalar};
use crate::pg::pool::{ClientFuture, DjogiPool};
use crate::query::stream::{DEFAULT_FETCH_SIZE, RawCursorStream, build_raw_stream};
use crate::{DbError, DjogiError};
use postgres_types::{FromSql, ToSql};
use tokio_postgres::Row;

fn reject_transaction_backed_sql(ctx: &mut DjogiContext, sql: &str) -> Result<(), DjogiError> {
    if let Some(err) = ctx.transaction_poison_error() {
        return Err(err);
    }
    if ctx.conn().is_some()
        && let Some(refusal) = classify_transaction_backed_refusal(sql)
    {
        return Err(refusal.into_error());
    }
    Ok(())
}

fn reject_transaction_backed_sql_batch(
    ctx: &mut DjogiContext,
    sql: &str,
) -> Result<(), DjogiError> {
    if let Some(err) = ctx.transaction_poison_error() {
        return Err(err);
    }
    if ctx.conn().is_some()
        && let Some(refusal) = classify_raw_ddl_transaction_backed_refusal(sql)
    {
        return Err(refusal.into_error());
    }
    Ok(())
}

pub(crate) async fn guarded_batch_execute(
    ctx: &mut DjogiContext,
    sql: &str,
) -> Result<(), DjogiError> {
    reject_transaction_backed_sql_batch(ctx, sql)?;
    ctx.batch_execute(sql).await
}

fn classify_transaction_session_statement(sql: &str) -> Option<&'static str> {
    let (keyword, next_idx) = parse_keyword(sql, 0)?;

    if keyword.eq_ignore_ascii_case("SET") {
        let second = parse_keyword(sql, next_idx).map(|(word, _)| word);
        return match second {
            Some(word)
                if word.eq_ignore_ascii_case("LOCAL")
                    || word.eq_ignore_ascii_case("CONSTRAINTS")
                    || word.eq_ignore_ascii_case("TRANSACTION") =>
            {
                None
            }
            _ => Some("SET"),
        };
    }

    [
        "RESET",
        "DISCARD",
        "LISTEN",
        "UNLISTEN",
        "PREPARE",
        "DEALLOCATE",
    ]
    .into_iter()
    .find(|statement| keyword.eq_ignore_ascii_case(statement))
}

#[allow(dead_code)] // Retained by existing regression tests; superseded in production by classify_raw_ddl_transaction_backed_refusal.
fn classify_raw_ddl_transaction_session_statement(sql: &str) -> Option<&'static str> {
    let bytes = sql.as_bytes();
    let mut statement_start = 0usize;
    let mut idx = 0usize;
    let mut block_comment_depth = 0usize;
    let mut in_line_comment = false;
    let mut in_single_quote = false;
    let mut in_double_quote = false;
    let mut dollar_quote: Option<String> = None;

    while idx < bytes.len() {
        if let Some(delimiter) = dollar_quote.as_deref() {
            if bytes[idx..].starts_with(delimiter.as_bytes()) {
                idx += delimiter.len();
                dollar_quote = None;
            } else {
                idx += 1;
            }
            continue;
        }

        if in_line_comment {
            if bytes[idx] == b'\n' {
                in_line_comment = false;
            }
            idx += 1;
            continue;
        }

        if block_comment_depth > 0 {
            if bytes.get(idx) == Some(&b'/') && bytes.get(idx + 1) == Some(&b'*') {
                block_comment_depth += 1;
                idx += 2;
            } else if bytes.get(idx) == Some(&b'*') && bytes.get(idx + 1) == Some(&b'/') {
                block_comment_depth -= 1;
                idx += 2;
            } else {
                idx += 1;
            }
            continue;
        }

        if in_single_quote {
            if bytes[idx] == b'\'' {
                if bytes.get(idx + 1) == Some(&b'\'') {
                    idx += 2;
                } else {
                    in_single_quote = false;
                    idx += 1;
                }
            } else {
                idx += 1;
            }
            continue;
        }

        if in_double_quote {
            if bytes[idx] == b'"' {
                if bytes.get(idx + 1) == Some(&b'"') {
                    idx += 2;
                } else {
                    in_double_quote = false;
                    idx += 1;
                }
            } else {
                idx += 1;
            }
            continue;
        }

        match bytes[idx] {
            b';' => {
                if let Some(statement) =
                    classify_transaction_session_statement(&sql[statement_start..idx])
                {
                    return Some(statement);
                }
                statement_start = idx + 1;
                idx += 1;
            }
            b'\'' => {
                in_single_quote = true;
                idx += 1;
            }
            b'"' => {
                in_double_quote = true;
                idx += 1;
            }
            b'-' if bytes.get(idx + 1) == Some(&b'-') => {
                in_line_comment = true;
                idx += 2;
            }
            b'/' if bytes.get(idx + 1) == Some(&b'*') => {
                block_comment_depth = 1;
                idx += 2;
            }
            b'$' => {
                if let Some(end_idx) = parse_dollar_quote_delimiter_end(sql, idx) {
                    dollar_quote = Some(sql[idx..end_idx].to_owned());
                    idx = end_idx;
                } else {
                    idx += 1;
                }
            }
            _ => {
                idx += 1;
            }
        }
    }

    classify_transaction_session_statement(&sql[statement_start..])
}

// ---------------------------------------------------------------------------
// Transaction-control statement classifier (#306).
// ---------------------------------------------------------------------------

fn classify_transaction_control_statement(sql: &str) -> Option<&'static str> {
    let (first, after_first) = parse_keyword(sql, 0)?;

    if first.eq_ignore_ascii_case("START") {
        return match parse_keyword(sql, after_first) {
            Some((second, _)) if second.eq_ignore_ascii_case("TRANSACTION") => {
                Some("START TRANSACTION")
            }
            _ => None,
        };
    }

    if first.eq_ignore_ascii_case("ROLLBACK") {
        let second = parse_keyword(sql, after_first);
        return match second {
            Some((w, _))
                if w.eq_ignore_ascii_case("WORK") || w.eq_ignore_ascii_case("TRANSACTION") =>
            {
                // ROLLBACK WORK / ROLLBACK TRANSACTION (plain rollback)
                // and ROLLBACK WORK TO sp / ROLLBACK TRANSACTION TO sp
                Some("ROLLBACK")
            }
            Some((w, _)) if w.eq_ignore_ascii_case("TO") => Some("ROLLBACK"),
            _ => Some("ROLLBACK"),
        };
    }

    if first.eq_ignore_ascii_case("RELEASE") {
        // Bare RELEASE or RELEASE SAVEPOINT — both are transaction control.
        return Some("RELEASE");
    }

    if first.eq_ignore_ascii_case("BEGIN") {
        return match parse_keyword(sql, after_first) {
            Some((second, _)) if second.eq_ignore_ascii_case("ATOMIC") => {
                // BEGIN ATOMIC is a SQL-standard compound-statement delimiter
                // (used in `LANGUAGE SQL` function bodies), not transaction
                // control. Postgres treats it differently from bare BEGIN,
                // which starts a transaction block.
                None
            }
            // Bare BEGIN, BEGIN WORK, BEGIN TRANSACTION are all transaction control.
            _ => Some("BEGIN"),
        };
    }

    ["COMMIT", "END", "ABORT", "SAVEPOINT"]
        .into_iter()
        .find(|s| first.eq_ignore_ascii_case(s))
}

// ---------------------------------------------------------------------------
// Unified refusal enum (#306).
// ---------------------------------------------------------------------------

#[derive(Debug, PartialEq)]
pub(crate) enum TransactionBackedRawSqlRefusal {
    SessionStatement(&'static str),
    TransactionControl(&'static str),
}

impl TransactionBackedRawSqlRefusal {
    pub(crate) fn into_error(self) -> DjogiError {
        match self {
            Self::SessionStatement(s) => {
                DjogiError::SessionStatementDisallowedInTransaction { statement: s }
            }
            Self::TransactionControl(s) => {
                DjogiError::RawTransactionControlDisallowedInTransaction { statement: s }
            }
        }
    }
}

fn classify_transaction_backed_refusal(sql: &str) -> Option<TransactionBackedRawSqlRefusal> {
    if let Some(s) = classify_transaction_control_statement(sql) {
        return Some(TransactionBackedRawSqlRefusal::TransactionControl(s));
    }
    if let Some(s) = classify_transaction_session_statement(sql) {
        return Some(TransactionBackedRawSqlRefusal::SessionStatement(s));
    }
    None
}

fn classify_raw_ddl_transaction_backed_refusal(
    sql: &str,
) -> Option<TransactionBackedRawSqlRefusal> {
    let bytes = sql.as_bytes();
    let mut statement_start = 0usize;
    let mut idx = 0usize;
    let mut block_comment_depth = 0usize;
    let mut in_line_comment = false;
    let mut in_single_quote = false;
    let mut in_double_quote = false;
    let mut dollar_quote: Option<String> = None;
    // Depth of open `BEGIN ATOMIC ... END` compound-statement blocks. Inside
    // them, semicolons do not split statements and the closing END is not
    // transaction control.
    let mut begin_atomic_depth = 0usize;
    // Depth of open `CASE ... END` expressions inside the current atomic block,
    // so a CASE's END does not prematurely close the BEGIN ATOMIC block.
    let mut case_depth = 0usize;

    while idx < bytes.len() {
        if let Some(delimiter) = dollar_quote.as_deref() {
            if bytes[idx..].starts_with(delimiter.as_bytes()) {
                idx += delimiter.len();
                dollar_quote = None;
            } else {
                idx += 1;
            }
            continue;
        }

        if in_line_comment {
            if bytes[idx] == b'\n' {
                in_line_comment = false;
            }
            idx += 1;
            continue;
        }

        if block_comment_depth > 0 {
            if bytes.get(idx) == Some(&b'/') && bytes.get(idx + 1) == Some(&b'*') {
                block_comment_depth += 1;
                idx += 2;
            } else if bytes.get(idx) == Some(&b'*') && bytes.get(idx + 1) == Some(&b'/') {
                block_comment_depth -= 1;
                idx += 2;
            } else {
                idx += 1;
            }
            continue;
        }

        if in_single_quote {
            if bytes[idx] == b'\'' {
                if bytes.get(idx + 1) == Some(&b'\'') {
                    idx += 2;
                } else {
                    in_single_quote = false;
                    idx += 1;
                }
            } else {
                idx += 1;
            }
            continue;
        }

        if in_double_quote {
            if bytes[idx] == b'"' {
                if bytes.get(idx + 1) == Some(&b'"') {
                    idx += 2;
                } else {
                    in_double_quote = false;
                    idx += 1;
                }
            } else {
                idx += 1;
            }
            continue;
        }

        // Track BEGIN ATOMIC ... END compound-statement boundaries. Quote and
        // comment state is handled above, so this runs only on real, unquoted
        // SQL text.
        if begin_atomic_depth > 0 {
            // CASE ... END is the only bare-END construct that appears unquoted
            // inside a SQL-standard atomic body (e.g. SELECT CASE WHEN x > 0
            // THEN 1 ELSE 0 END). Count CASE opens so their END does not
            // prematurely close the atomic block. A qualified END such as
            // END IF or END LOOP belongs to PL/pgSQL and only appears inside
            // dollar-quoted function bodies, which existing dollar-quote
            // tracking already skips.
            if let Some(after_case) = match_keyword_at(bytes, idx, "CASE") {
                case_depth += 1;
                idx = after_case;
                continue;
            }

            // A bare END closes the innermost open CASE first; only when no
            // CASE is open does it close the BEGIN ATOMIC block itself.
            if let Some(after_end) = match_keyword_at(bytes, idx, "END") {
                if case_depth > 0 {
                    case_depth -= 1;
                } else {
                    begin_atomic_depth -= 1;
                    // Reset CASE depth as the block fully closes so no residual
                    // imbalance leaks across sequential atomic blocks in one
                    // batch.
                    if begin_atomic_depth == 0 {
                        case_depth = 0;
                    }
                }
                idx = after_end;
                continue;
            }

            // A nested BEGIN ATOMIC raises the depth.
            if let Some(after_begin) = match_keyword_at(bytes, idx, "BEGIN")
                && let Some(after_atomic) = skip_whitespace_and_match(bytes, after_begin, "ATOMIC")
            {
                begin_atomic_depth += 1;
                idx = after_atomic;
                continue;
            }

            // Semicolons inside the block belong to the compound statement
            // never split on them.
            if bytes[idx] == b';' {
                idx += 1;
                continue;
            }

            // Anything else falls through to the match block below so quote and
            // comment state is entered correctly: a string literal or comment
            // opened inside the block (a quoted 'END', a block comment, or a
            // line comment containing END) is skipped without touching depth.
        }

        // Outside any atomic block, an unquoted BEGIN ATOMIC opens one.
        if begin_atomic_depth == 0
            && let Some(after_begin) = match_keyword_at(bytes, idx, "BEGIN")
            && let Some(after_atomic) = skip_whitespace_and_match(bytes, after_begin, "ATOMIC")
        {
            begin_atomic_depth = 1;
            idx = after_atomic;
            continue;
        }

        match bytes[idx] {
            b';' => {
                if let Some(refusal) =
                    classify_transaction_backed_refusal(&sql[statement_start..idx])
                {
                    return Some(refusal);
                }
                statement_start = idx + 1;
                idx += 1;
            }
            b'\'' => {
                in_single_quote = true;
                idx += 1;
            }
            b'"' => {
                in_double_quote = true;
                idx += 1;
            }
            b'-' if bytes.get(idx + 1) == Some(&b'-') => {
                in_line_comment = true;
                idx += 2;
            }
            b'/' if bytes.get(idx + 1) == Some(&b'*') => {
                block_comment_depth = 1;
                idx += 2;
            }
            b'$' => {
                if let Some(end_idx) = parse_dollar_quote_delimiter_end(sql, idx) {
                    dollar_quote = Some(sql[idx..end_idx].to_owned());
                    idx = end_idx;
                } else {
                    idx += 1;
                }
            }
            _ => {
                idx += 1;
            }
        }
    }

    classify_transaction_backed_refusal(&sql[statement_start..])
}

/// Returns `true` when `b` can appear *inside* an unquoted PostgreSQL
/// identifier (i.e. as a non-leading identifier byte), and therefore must be
/// treated as part of an identifier for keyword-boundary purposes.
/// Per the PostgreSQL lexical rules, identifier continuation characters are
/// ASCII letters, ASCII digits, `_`, and `$`. PostgreSQL additionally permits
/// non-ASCII letters (any byte `> 127` in a UTF-8 source — either the lead
/// byte or a continuation byte of a multibyte identifier character). Treating
/// every non-ASCII byte as an identifier byte is the conservative choice for a
/// byte-level scanner: it guarantees a keyword is never matched at a position
/// that is actually inside a multibyte identifier character.
/// This is used for the word-boundary checks around keyword matches, so that
/// `BEGIN` in `x$begin`, `begin$x`, or a non-ASCII-adjacent identifier is not
/// mistaken for the standalone `BEGIN` keyword. See .
fn is_identifier_byte(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_' || b == b'$' || b > 127
}

/// Checks whether `target` keyword matches in `bytes` at exactly `idx`.
/// Returns the index just past the keyword on a match, or `None`.
/// This is O(1): it performs no whitespace or trivia skipping, so it is safe to
/// call at every byte in the scanner loop without an O(N^2) blow-up on
/// whitespace-heavy input. Leading and trailing word-boundary checks (via
/// [`is_identifier_byte`]) prevent matching inside an identifier — `END` inside
/// `dividend`, `BEGIN` inside `xBEGIN`, `END` inside `ENDPOINT`, and (because
/// PostgreSQL allows `$` and non-ASCII letters in identifiers) `BEGIN` inside
/// `x$begin` / `begin$x` or alongside a multibyte identifier character are all
/// rejected. See .
fn match_keyword_at(bytes: &[u8], idx: usize, target: &str) -> Option<usize> {
    // Leading boundary: the previous byte must not be an identifier byte.
    if idx > 0 && is_identifier_byte(bytes[idx - 1]) {
        return None;
    }
    let target_len = target.len();
    if idx + target_len > bytes.len() {
        return None;
    }
    if bytes[idx..idx + target_len].eq_ignore_ascii_case(target.as_bytes()) {
        // Trailing boundary: the next byte must not be an identifier byte.
        let end = idx + target_len;
        if end < bytes.len() && is_identifier_byte(bytes[end]) {
            return None;
        }
        Some(end)
    } else {
        None
    }
}

/// Skips SQL trivia (ASCII whitespace, `--` line comments, and nestable
/// `/* ... */` block comments) starting at `idx`, then checks whether `target`
/// keyword matches at the first significant byte.
/// Returns the index just past the keyword on a match, or `None`.
/// This is O(N) in the run of trivia it skips, so it is only safe to call
/// once per keyword match — never per-byte in the main loop. It backs the
/// second-keyword peek after a `BEGIN` match (checking for `ATOMIC`), where the
/// trivia run is bounded by the comment/whitespace between two keywords. The
/// trivia rules mirror [`skip_sql_trivia`]; `BEGIN /* x */ ATOMIC` and
/// `BEGIN -- x\nATOMIC` therefore open a compound-statement block as the spec
/// requires.
fn skip_whitespace_and_match(bytes: &[u8], idx: usize, target: &str) -> Option<usize> {
    let mut i = idx;
    loop {
        while i < bytes.len() && bytes[i].is_ascii_whitespace() {
            i += 1;
        }

        // Line comment: `--` through end of line.
        if bytes.get(i) == Some(&b'-') && bytes.get(i + 1) == Some(&b'-') {
            i += 2;
            while i < bytes.len() && bytes[i] != b'\n' {
                i += 1;
            }
            continue;
        }

        // Block comment: `/* ... */`, nestable to match `skip_sql_trivia`.
        if bytes.get(i) == Some(&b'/') && bytes.get(i + 1) == Some(&b'*') {
            i += 2;
            let mut depth = 1usize;
            while i < bytes.len() && depth > 0 {
                if bytes.get(i) == Some(&b'/') && bytes.get(i + 1) == Some(&b'*') {
                    depth += 1;
                    i += 2;
                } else if bytes.get(i) == Some(&b'*') && bytes.get(i + 1) == Some(&b'/') {
                    depth -= 1;
                    i += 2;
                } else {
                    i += 1;
                }
            }
            continue;
        }

        break;
    }
    match_keyword_at(bytes, i, target)
}

fn parse_keyword(sql: &str, start_idx: usize) -> Option<(&str, usize)> {
    let bytes = sql.as_bytes();
    let mut idx = skip_sql_trivia(sql, start_idx);
    if idx >= bytes.len() || !bytes[idx].is_ascii_alphabetic() {
        return None;
    }
    let start = idx;
    idx += 1;
    while idx < bytes.len() && (bytes[idx].is_ascii_alphanumeric() || bytes[idx] == b'_') {
        idx += 1;
    }
    Some((&sql[start..idx], idx))
}

fn skip_sql_trivia(sql: &str, start_idx: usize) -> usize {
    let bytes = sql.as_bytes();
    let mut idx = start_idx;

    loop {
        while idx < bytes.len() && bytes[idx].is_ascii_whitespace() {
            idx += 1;
        }

        if bytes.get(idx) == Some(&b'-') && bytes.get(idx + 1) == Some(&b'-') {
            idx += 2;
            while idx < bytes.len() && bytes[idx] != b'\n' {
                idx += 1;
            }
            continue;
        }

        if bytes.get(idx) == Some(&b'/') && bytes.get(idx + 1) == Some(&b'*') {
            idx += 2;
            let mut depth = 1usize;
            while idx < bytes.len() && depth > 0 {
                if bytes.get(idx) == Some(&b'/') && bytes.get(idx + 1) == Some(&b'*') {
                    depth += 1;
                    idx += 2;
                } else if bytes.get(idx) == Some(&b'*') && bytes.get(idx + 1) == Some(&b'/') {
                    depth -= 1;
                    idx += 2;
                } else {
                    idx += 1;
                }
            }
            continue;
        }

        return idx;
    }
}

fn parse_dollar_quote_delimiter_end(sql: &str, start_idx: usize) -> Option<usize> {
    let bytes = sql.as_bytes();
    if bytes.get(start_idx) != Some(&b'$') {
        return None;
    }

    let mut idx = start_idx + 1;
    while idx < bytes.len() {
        match bytes[idx] {
            b'$' => return Some(idx + 1),
            b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_' => idx += 1,
            _ => return None,
        }
    }

    None
}

mod sealed {
    pub trait Sealed {}

    impl Sealed for crate::context::DjogiContext {}
    impl Sealed for crate::pg::pool::DjogiPool {}
}

/// Sealed extension trait exposing djogi's raw SQL context escape hatches.
/// Base trait: no `Send` bound. The generated [`RawAccessExt`] variant adds
/// `Send` bounds to the futures returned by async methods. Reaching any
/// method here is djogi's `unsafe`-equivalent — see the
/// [module docs](self) and the
/// [Raw SQL escape hatches spec](https://github.com/tarunvir/djogi/blob/main/docs/spec/raw-sql-escape-hatches.md).
#[doc(hidden)]
#[trait_variant::make(RawAccessExt: Send)]
pub trait RawAccessExtBase: sealed::Sealed {
    /// Run a raw `SELECT` and decode every row into `T` via
    /// [`FromPgRow`](crate::pg::decode::FromPgRow).
    /// Raw escape hatch — djogi's `unsafe`-equivalent. See the
    /// [module docs](self) for the bypass-attribute convention.
    /// Reaching this is djogi's `unsafe`-equivalent — every call must walk
    /// through `#[djogi::deliberately_bypass_convention_with_raw_sql]` (under
    /// `tests/` the attribute requires a paired `// JUSTIFICATION (djogi#<n>):`
    /// comment, validated by `cargo xtask check-justifications`). See the
    /// [Raw SQL escape hatches spec](https://github.com/tarunvir/djogi/blob/main/docs/spec/raw-sql-escape-hatches.md).
    /// Prefer the typed surface — `Model::objects().filter(...).fetch_all(ctx)`
    /// for any predicate the queryset can express. Reach for `raw_query`
    /// only for shapes the typed layer cannot describe today (recursive CTEs,
    /// set-returning functions, bespoke joins).
    /// `T: FromPgRow` decodes positionally against the wire row, so the
    /// `SELECT` projection list must match the model's column order. The
    /// canonical order is `id, created_at, updated_at, ...user_fields` for
    /// `#[model]`-derived structs; ad-hoc rowtypes implement
    /// [`FromPgRow`](crate::pg::decode::FromPgRow) with whatever shape they
    /// need.
    /// ```ignore
    /// use djogi::prelude::*;
    ///
    /// #[djogi::deliberately_bypass_convention_with_raw_sql]
    /// // JUSTIFICATION (djogi#234): typed surface lacks recursive CTE.
    /// async fn ancestor_threads(ctx: &mut DjogiContext, root_id: HeerId)
    /// -> djogi::Result<Vec<Comment>>
    /// {
    /// ctx.raw_query(
    /// "WITH RECURSIVE ancestors AS (
    /// SELECT * FROM comments WHERE id = $1
    /// UNION ALL
    /// SELECT c.* FROM comments c
    /// JOIN ancestors a ON c.id = a.parent_id
    /// )
    /// SELECT id, created_at, updated_at, parent_id, body
    /// FROM ancestors",
    /// &[&root_id],
    /// ).await
    /// }
    /// ```
    async fn raw_query<T: FromPgRow>(
        &mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<Vec<T>, DjogiError>;

    /// Run a raw `SELECT` and return undecoded `tokio_postgres::Row` values.
    /// Raw escape hatch — djogi's `unsafe`-equivalent. See the
    /// [module docs](self) for the bypass-attribute convention.
    /// Prefer the typed surface — `Model::objects().filter(...).fetch_all(ctx)`
    /// for any predicate the queryset can express. If the typed surface
    /// cannot describe the shape but the row decodes into a `FromPgRow`,
    /// prefer [`raw_query`](RawAccessExtBase::raw_query) over `raw_rows` so
    /// the per-row decode is positional and debug-asserted. Reach for
    /// `raw_rows` only when the caller really does need to inspect column
    /// metadata or call [`tokio_postgres::Row::try_get`] on heterogenous
    /// columns by name (e.g. dynamic introspection helpers).
    /// Reaching this is djogi's `unsafe`-equivalent — every call must walk
    /// through `#[djogi::deliberately_bypass_convention_with_raw_sql]`. See
    /// the [Raw SQL escape hatches spec](https://github.com/tarunvir/djogi/blob/main/docs/spec/raw-sql-escape-hatches.md).
    /// Prefer [`raw_query`](RawAccessExtBase::raw_query) when the row shape
    /// fits a `FromPgRow` impl — the per-row decode is positional and
    /// debug-asserted. Reach for `raw_rows` only when the caller really does
    /// need to inspect column metadata or call [`tokio_postgres::Row::try_get`]
    /// on heterogenous columns by name (e.g. dynamic introspection helpers).
    /// ```ignore
    /// #[djogi::deliberately_bypass_convention_with_raw_sql]
    /// // JUSTIFICATION (djogi#456): introspecting column metadata for the admin
    /// // schema diff renderer; FromPgRow does not expose column types.
    /// async fn dump_columns(ctx: &mut DjogiContext, table: &str)
    /// -> djogi::Result<Vec<tokio_postgres::Row>>
    /// {
    /// ctx.raw_rows(
    /// "SELECT column_name, data_type FROM information_schema.columns
    /// WHERE table_name = $1",
    /// &[&table],
    /// ).await
    /// }
    /// ```
    async fn raw_rows(
        &mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<Vec<Row>, DjogiError>;

    /// Run a raw `SELECT` expected to return exactly one row, decoded into `T`.
    /// Raw escape hatch — djogi's `unsafe`-equivalent. See the
    /// [module docs](self) for the bypass-attribute convention.
    /// Reaching this is djogi's `unsafe`-equivalent — every call must walk
    /// through `#[djogi::deliberately_bypass_convention_with_raw_sql]`. See
    /// the [Raw SQL escape hatches spec](https://github.com/tarunvir/djogi/blob/main/docs/spec/raw-sql-escape-hatches.md).
    /// Returns [`DjogiError::NotFound`] when the query produces zero rows;
    /// the framework does not enforce the upper bound, so the caller is
    /// responsible for using `LIMIT 1` (or otherwise guaranteeing
    /// uniqueness) when required. Prefer
    /// [`Model::get`](crate::model::Model::get) /
    /// `QuerySet::fetch_one` for typed-surface lookups.
    /// ```ignore
    /// #[djogi::deliberately_bypass_convention_with_raw_sql]
    /// // JUSTIFICATION (djogi#789): typed surface lacks JSON-aggregated reads.
    /// async fn fetch_one_summary(ctx: &mut DjogiContext, id: HeerId)
    /// -> djogi::Result<UserSummary>
    /// {
    /// ctx.raw_fetch_one(
    /// "SELECT id, jsonb_build_object('posts', count(p.id)) AS summary
    /// FROM users u LEFT JOIN posts p ON p.author_id = u.id
    /// WHERE u.id = $1 GROUP BY u.id LIMIT 1",
    /// &[&id],
    /// ).await
    /// }
    /// ```
    async fn raw_fetch_one<T: FromPgRow>(
        &mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<T, DjogiError>;

    /// Run a raw `SELECT` and return the first column of the first row as a
    /// scalar value of type `T`.
    /// Raw escape hatch — djogi's `unsafe`-equivalent. See the
    /// [module docs](self) for the bypass-attribute convention.
    /// Reaching this is djogi's `unsafe`-equivalent — every call must walk
    /// through `#[djogi::deliberately_bypass_convention_with_raw_sql]`. See
    /// the [Raw SQL escape hatches spec](https://github.com/tarunvir/djogi/blob/main/docs/spec/raw-sql-escape-hatches.md).
    /// Returns [`DjogiError::NotFound`] when the query produces zero rows.
    /// Use for `SELECT COUNT(*)`, `SELECT MAX(...)`, and similar single-value
    /// reductions. Prefer the queryset's `.count(ctx)` / `.exists(ctx)` /
    /// aggregate-projection terminals when the typed surface covers the
    /// shape.
    /// ```ignore
    /// #[djogi::deliberately_bypass_convention_with_raw_sql]
    /// // JUSTIFICATION (djogi#101): summary aggregate the visage layer doesn't
    /// // yet project for nested JSONB facets.
    /// async fn open_invoices_total_cents(ctx: &mut DjogiContext)
    /// -> djogi::Result<i64>
    /// {
    /// ctx.raw_scalar(
    /// "SELECT COALESCE(SUM(total_cents), 0)
    /// FROM invoices WHERE status = 'open'",
    /// &[],
    /// ).await
    /// }
    /// ```
    async fn raw_scalar<T>(
        &mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<T, DjogiError>
    where
        T: for<'row> FromSql<'row> + Send + 'static;

    /// Run a raw `INSERT`, `UPDATE`, `DELETE`, or other no-row-returning
    /// statement and return the affected-row count.
    /// Raw escape hatch — djogi's `unsafe`-equivalent. See the
    /// [module docs](self) for the bypass-attribute convention.
    /// Reaching this is djogi's `unsafe`-equivalent — every call must walk
    /// through `#[djogi::deliberately_bypass_convention_with_raw_sql]`. See
    /// the [Raw SQL escape hatches spec](https://github.com/tarunvir/djogi/blob/main/docs/spec/raw-sql-escape-hatches.md).
    /// Prefer `Model::create` / `Model::save` / `Model::delete` for single-row
    /// CRUD and `QuerySet::update` / `QuerySet::delete` for bulk writes. Reach
    /// for `raw_execute` only for shapes the typed layer cannot express today
    /// (e.g. preserving `updated_at` across a bulk update — the queryset
    /// always stamps it).
    /// ```ignore
    /// #[djogi::deliberately_bypass_convention_with_raw_sql]
    /// // JUSTIFICATION (djogi#202): bulk update must preserve updated_at; the
    /// // queryset bulk-update path always stamps `updated_at = now()`.
    /// async fn restamp_recent(ctx: &mut DjogiContext, days: i32)
    /// -> djogi::Result<u64>
    /// {
    /// ctx.raw_execute(
    /// "UPDATE posts SET view_count = view_count + 1
    /// WHERE created_at > now() - $1::interval",
    /// &[&format!("{days} days")],
    /// ).await
    /// }
    /// ```
    async fn raw_execute(
        &mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<u64, DjogiError>;

    /// Run a raw DDL batch (one or more semicolon-separated statements,
    /// no parameters).
    /// Raw escape hatch — djogi's `unsafe`-equivalent. See the
    /// [module docs](self) for the bypass-attribute convention.
    /// Reaching this is djogi's `unsafe`-equivalent — every call must walk
    /// through `#[djogi::deliberately_bypass_convention_with_raw_sql]`. See
    /// the [Raw SQL escape hatches spec](https://github.com/tarunvir/djogi/blob/main/docs/spec/raw-sql-escape-hatches.md).
    /// `raw_ddl` is `batch_execute(sql)` under a friendlier name — it
    /// carries the same blast radius as [`raw_execute`](RawAccessExtBase::raw_execute)
    /// and intentionally does not project through the migration substrate.
    /// When the context is already transaction-backed, Djogi preflights each
    /// top-level statement in the batch and rejects session-scoped statement
    /// heads (`SET`, `RESET`, `DISCARD`, `LISTEN`, `UNLISTEN`, `PREPARE`,
    /// `DEALLOCATE`) with [`DjogiError::SessionStatementDisallowedInTransaction`]
    /// and transaction-control statements (`BEGIN` — but not `BEGIN ATOMIC`,
    /// a compound-statement delimiter — `COMMIT`, `ROLLBACK`, `END`, `ABORT`,
    /// `SAVEPOINT`, `RELEASE`) with
    /// [`DjogiError::RawTransactionControlDisallowedInTransaction`] before SQL
    /// reaches Postgres. The scanner respects comments, string literals,
    /// dollar-quoted bodies, and `BEGIN ATOMIC ... END` compound-statement
    /// boundaries; it is not a naive `split(';')`.
    /// Tests that need to set up tables MUST use
    /// `#[djogi::djogi_test(sync_models = [...])]` instead — `sync_models`
    /// projects through the descriptor / `pk_default_sql` pipeline so
    /// projection bugs surface from the test surface (tracking issue
    /// ).
    /// Reach for `raw_ddl` only for setup that cannot live in a model
    /// descriptor (`CREATE EXTENSION`, custom types declared outside djogi's
    /// schema-snapshot model, role / permission grants).
    /// ```ignore
    /// #[djogi::deliberately_bypass_convention_with_raw_sql]
    /// // JUSTIFICATION (djogi#303): PostGIS extension install runs once per
    /// // database; the future #[djogi_test(extensions = ["postgis"])] surface
    /// // This surface is the preferred path once implemented.
    /// async fn install_postgis(ctx: &mut DjogiContext) -> djogi::Result<()> {
    /// ctx.raw_ddl("CREATE EXTENSION IF NOT EXISTS postgis").await
    /// }
    /// ```
    async fn raw_ddl(&mut self, sql: &str) -> Result<(), DjogiError>;

    /// Open a server-side cursor and yield rows lazily as a
    /// [`RawCursorStream`].
    /// Raw escape hatch — djogi's `unsafe`-equivalent. See the
    /// [module docs](self) for the bypass-attribute convention.
    /// Reaching this is djogi's `unsafe`-equivalent — every call must walk
    /// through `#[djogi::deliberately_bypass_convention_with_raw_sql]`. See
    /// the [Raw SQL escape hatches spec](https://github.com/tarunvir/djogi/blob/main/docs/spec/raw-sql-escape-hatches.md).
    /// Postgres cursors are transaction-local — the surrounding context MUST
    /// be transaction-backed. Calling `raw_stream` on a pool-backed context
    /// returns [`DjogiError::StreamOutsideTransaction`] at construction time
    /// (not at the first `poll_next`), so the misuse surfaces immediately.
    /// Wrap the consumer in `atomic(&mut ctx, |tx| Box::pin(async move {
    /// ... }))` so the `tx` argument is transaction-backed.
    /// Uses the framework default fetch size (chunk-size for the
    /// `FETCH FORWARD` calls under the cursor). For control over the chunk
    /// shape, use [`raw_stream_with_fetch_size`](RawAccessExtBase::raw_stream_with_fetch_size).
    /// Prefer `QuerySet::stream(ctx)` for typed-surface streaming.
    /// ```ignore
    /// use djogi::prelude::*;
    /// use futures::StreamExt;
    ///
    /// #[djogi::deliberately_bypass_convention_with_raw_sql]
    /// // JUSTIFICATION (djogi#404): export job needs server-side cursor; the
    /// // typed QuerySet::stream is preferred when the shape fits.
    /// async fn export_orders(ctx: &mut DjogiContext) -> djogi::Result<()> {
    /// atomic(ctx, |tx| Box::pin(async move {
    /// let mut stream = tx.raw_stream(
    /// "SELECT id, total_cents FROM orders ORDER BY id",
    /// &[],
    /// ).await?;
    /// while let Some(row) = stream.next().await {
    /// let _row = row?; // process row
    /// }
    /// Ok(())
    /// })).await
    /// }
    /// ```
    async fn raw_stream<'ctx>(
        &'ctx mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<RawCursorStream<'ctx>, DjogiError>;

    /// Same as [`raw_stream`](RawAccessExtBase::raw_stream) but caller-tunable
    /// `FETCH FORWARD` chunk size.
    /// Raw escape hatch — djogi's `unsafe`-equivalent. See the
    /// [module docs](self) for the bypass-attribute convention.
    /// Prefer the typed surface — `Model::objects()` / `QuerySet::stream`
    /// inside an `atomic(...)` scope — for any shape the typed layer can
    /// describe. Reach for `raw_stream_with_fetch_size` only when the typed
    /// stream cannot describe the projection AND the default chunk size used
    /// by `raw_stream` is the wrong shape for the consumer (typically very
    /// large exports or very latency-sensitive previews).
    /// Reaching this is djogi's `unsafe`-equivalent — every call must walk
    /// through `#[djogi::deliberately_bypass_convention_with_raw_sql]`. See
    /// the [Raw SQL escape hatches spec](https://github.com/tarunvir/djogi/blob/main/docs/spec/raw-sql-escape-hatches.md).
    /// `fetch_size` of `0` returns [`DjogiError::Validation`] — the cursor
    /// driver cannot make progress on an empty fetch chunk. Larger values
    /// reduce round-trips at the cost of per-chunk memory; smaller values
    /// reduce latency to the first row at the cost of more network round
    /// trips. The framework default (used by `raw_stream`) is a balanced
    /// middle ground.
    /// Same transaction-context rules as [`raw_stream`](RawAccessExtBase::raw_stream):
    /// pool-backed contexts return [`DjogiError::StreamOutsideTransaction`]
    /// at construction time.
    /// ```ignore
    /// #[djogi::deliberately_bypass_convention_with_raw_sql]
    /// // JUSTIFICATION (djogi#505): export job tunes chunk size to match the
    /// // downstream consumer's batch boundary.
    /// async fn export_orders_chunked(ctx: &mut DjogiContext) -> djogi::Result<()> {
    /// atomic(ctx, |tx| Box::pin(async move {
    /// let mut stream = tx.raw_stream_with_fetch_size(
    /// "SELECT id, total_cents FROM orders ORDER BY id",
    /// &[],
    /// 100, // fetch 100 rows per round-trip
    /// ).await?;
    /// use futures::StreamExt;
    /// while let Some(row) = stream.next().await {
    /// let _row = row?;
    /// }
    /// Ok(())
    /// })).await
    /// }
    /// ```
    async fn raw_stream_with_fetch_size<'ctx>(
        &'ctx mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
        fetch_size: u32,
    ) -> Result<RawCursorStream<'ctx>, DjogiError>;
}

impl RawAccessExt for DjogiContext {
    async fn raw_query<T: FromPgRow>(
        &mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<Vec<T>, DjogiError> {
        reject_transaction_backed_sql(self, sql)?;
        // Route through `query_all_with` so the per-row `FromPgRow::from_pg_row`
        // decode runs inside the `PoolConnGuard`'s lifetime. A decode failure
        // here would otherwise leave the pool with a possibly poisoned
        // connection — the underlying SQL succeeded (guard armed for clean
        // return) while the framework-side decode failed afterwards.
        self.query_all_with(sql, params, |rows| {
            rows.iter().map(T::from_pg_row).collect()
        })
        .await
    }

    async fn raw_rows(
        &mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<Vec<Row>, DjogiError> {
        reject_transaction_backed_sql(self, sql)?;
        // No post-query decode — the existing `query_all` guard already
        // covers the only Err/cancel exit shape.
        self.__query_all_for_macros(sql, params).await
    }

    async fn raw_fetch_one<T: FromPgRow>(
        &mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<T, DjogiError> {
        reject_transaction_backed_sql(self, sql)?;
        // Decode runs inside the guard's lifetime via `query_opt_with`. The
        // `not_found` branch is also reported as `Err`, so the guard's
        // `committed` flag stays `false` and a no-row response still
        // recycles the connection cleanly (no session state mutated — the
        // recycle path is appropriate). Server-side failure paths are
        // funnelled through the inner `query_opt` `Err`, and decode
        // failures funnel through the `T::from_pg_row(...)` return.
        self.query_opt_with(sql, params, |row_opt| {
            let row = row_opt.ok_or_else(|| DjogiError::not_found("<raw>"))?;
            T::from_pg_row(&row)
        })
        .await
    }

    async fn raw_scalar<T>(
        &mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<T, DjogiError>
    where
        T: for<'row> FromSql<'row> + Send + 'static,
    {
        reject_transaction_backed_sql(self, sql)?;
        // `try_get_scalar` is the decode step that can fail on a row that
        // the underlying SQL produced successfully — e.g.
        // `SELECT set_config('application_name', '...', false)` returns
        // text and mutates the session GUC, so calling
        // `raw_scalar::<i32>` decode-fails AFTER the session was poisoned.
        // Routing through `query_opt_with` keeps that decode inside the
        // guard's lifetime so the connection detaches on the Err path.
        self.query_opt_with(sql, params, |row_opt| {
            let row = row_opt.ok_or_else(|| DjogiError::not_found("<raw>"))?;
            try_get_scalar(&row, 0)
        })
        .await
    }

    async fn raw_execute(
        &mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<u64, DjogiError> {
        reject_transaction_backed_sql(self, sql)?;
        self.__execute_for_macros(sql, params).await
    }

    async fn raw_ddl(&mut self, sql: &str) -> Result<(), DjogiError> {
        reject_transaction_backed_sql_batch(self, sql)?;
        self.batch_execute(sql).await
    }

    async fn raw_stream<'ctx>(
        &'ctx mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
    ) -> Result<RawCursorStream<'ctx>, DjogiError> {
        build_raw_stream(self, sql, params, DEFAULT_FETCH_SIZE).await
    }

    async fn raw_stream_with_fetch_size<'ctx>(
        &'ctx mut self,
        sql: &str,
        params: &[&(dyn ToSql + Sync)],
        fetch_size: u32,
    ) -> Result<RawCursorStream<'ctx>, DjogiError> {
        if fetch_size == 0 {
            return Err(DjogiError::Validation(
                "raw_stream fetch_size must be at least 1".to_owned(),
            ));
        }
        build_raw_stream(self, sql, params, fetch_size).await
    }
}

/// Sealed extension trait exposing pool/client escape hatches.
/// Base trait: no `Send` bound. The generated [`RawPoolAccessExt`] variant
/// adds `Send` bounds to the future returned by `raw_with_client`. Reaching
/// any method here is djogi's `unsafe`-equivalent — see the
/// [module docs](self) and the
/// [Raw SQL escape hatches spec](https://github.com/tarunvir/djogi/blob/main/docs/spec/raw-sql-escape-hatches.md).
#[doc(hidden)]
#[trait_variant::make(RawPoolAccessExt: Send)]
pub trait RawPoolAccessExtBase: sealed::Sealed {
    /// Borrow the underlying [`DjogiPool`] when the context is pool-backed.
    /// Raw escape hatch — djogi's `unsafe`-equivalent. See the
    /// [module docs](self) for the bypass-attribute convention.
    /// Reaching this is djogi's `unsafe`-equivalent — every call must walk
    /// through `#[djogi::deliberately_bypass_convention_with_raw_sql]`. See
    /// the [Raw SQL escape hatches spec](https://github.com/tarunvir/djogi/blob/main/docs/spec/raw-sql-escape-hatches.md).
    /// Returns `None` when the context is transaction-backed — pool reads
    /// during a transaction would route around the surrounding scope. Use
    /// for pool-state introspection (capacity, idle counts) when wiring
    /// adopter-side metrics; otherwise prefer the typed surface
    /// (`DjogiContext::from_pool` for fresh handles, `share_pool` to clone
    /// the inner `Arc`).
    /// ```ignore
    /// #[djogi::deliberately_bypass_convention_with_raw_sql]
    /// // JUSTIFICATION (djogi#606): pool-state introspection for adopter
    /// // metrics; the typed surface does not yet expose pool-stats reads.
    /// fn pool_status(ctx: &DjogiContext) -> Option<usize> {
    /// ctx.raw_pool().map(|p| p.status().available)
    /// }
    /// ```
    fn raw_pool(&self) -> Option<&DjogiPool>;

    /// Borrow the underlying [`PgConnection`] when the context is
    /// transaction-backed.
    /// Raw escape hatch — djogi's `unsafe`-equivalent. See the
    /// [module docs](self) for the bypass-attribute convention.
    /// Reaching this is djogi's `unsafe`-equivalent — every call must walk
    /// through `#[djogi::deliberately_bypass_convention_with_raw_sql]`. See
    /// the [Raw SQL escape hatches spec](https://github.com/tarunvir/djogi/blob/main/docs/spec/raw-sql-escape-hatches.md).
    /// Returns `None` when the context is pool-backed — there is no
    /// long-lived connection to borrow — and also when a transaction-backed
    /// context has been poisoned by a nested `atomic()` cancellation. Use for
    /// connection-state inspection (savepoint depth, in-progress transaction
    /// state) when an adopter-side helper needs to branch on the inner state.
    /// Prefer
    /// [`DjogiContext::savepoint_depth`](crate::DjogiContext::savepoint_depth)
    /// and the typed transaction substrate for ordinary use.
    /// ```ignore
    /// #[djogi::deliberately_bypass_convention_with_raw_sql]
    /// // JUSTIFICATION (djogi#707): transaction-state inspection for a custom
    /// // tracing layer.
    /// fn debug_conn(ctx: &mut DjogiContext) -> bool {
    /// ctx.raw_conn().is_some()
    /// }
    /// ```
    fn raw_conn(&mut self) -> Option<&mut PgConnection>;

    /// Run a closure with a checked-out raw [`tokio_postgres::Client`] from
    /// the underlying pool.
    /// Raw escape hatch — djogi's `unsafe`-equivalent. See the
    /// [module docs](self) for the bypass-attribute convention.
    /// Prefer the typed surface — `Model::objects()` / `QuerySet`,
    /// `Model::create` / `save` / `delete`, and `djogi::transaction::atomic`
    /// for routine reads, writes, and transactions. `raw_with_client` is
    /// the framework's only path to the underlying `tokio_postgres::Client`
    /// and the only way to reach binary-protocol helpers like
    /// `client.copy_in(...)`, `client.simple_query(...)`, `CREATE EXTENSION`
    /// (which requires `simple_query` outside a transaction), and the
    /// prepared-statement cache directly — typed-surface equivalents do not
    /// exist for those binary-protocol primitives today. The closure receives
    /// `&mut Client` for the duration of the borrow; the returned connection
    /// is **dirty by default** — adopters that issue `SET` / `LISTEN` / role
    /// changes inside the closure are responsible for resetting the
    /// connection (or the surrounding pool's `Manager` impl must declare a
    /// `reset` step).
    /// Reaching this is djogi's `unsafe`-equivalent — every call must walk
    /// through `#[djogi::deliberately_bypass_convention_with_raw_sql]`. See
    /// the [Raw SQL escape hatches spec](https://github.com/tarunvir/djogi/blob/main/docs/spec/raw-sql-escape-hatches.md).
    /// `raw_with_client` is the framework's only path to the underlying
    /// `tokio_postgres::Client` and the canonical public route for
    /// driver-level operations that the typed surface cannot express:
    /// `client.copy_in(...)`, `client.copy_out(...)`, server-side cursor
    /// protocol work, `client.simple_query(...)`, `CREATE EXTENSION`, and
    /// third-party helpers that require a `&tokio_postgres::Client`. The
    /// closure receives `&mut Client` for the duration of the borrow; keep
    /// the full COPY/cursor exchange inside that closure so the pool guard
    /// can return the connection on `Ok` or detach it on `Err`, panic, or
    /// cancellation. Typed-surface COPY and streaming wrappers do not exist
    /// today; this explicit bypass is the supported public route for those
    /// driver-level operations.
    /// Returns [`DjogiError::Db`] wrapping the underlying transport / pool
    /// error when the context has no pool to draw from (pure transaction-
    /// scoped contexts cannot satisfy `raw_with_client`).
    /// See the [connection-pool guide](https://github.com/tarunvir/djogi/blob/main/docs/guide/pool.md#raw-client-escape-hatch--raw_with_client)
    /// for the canonical treatment of when to reach for this surface.
    /// ```ignore
    /// use djogi::prelude::*;
    ///
    /// #[djogi::deliberately_bypass_convention_with_raw_sql]
    /// // JUSTIFICATION (djogi#808): COPY IN ingest needs binary protocol; the
    /// // typed surface has no streaming-bulk-insert primitive yet.
    /// async fn copy_in_orders(pool: &DjogiPool) -> djogi::Result<()> {
    /// pool.raw_with_client(|client| Box::pin(async move {
    /// let _sink = client.copy_in("COPY orders FROM STDIN").await?;
    /// // write rows to the sink ...
    /// Ok(())
    /// })).await
    /// }
    /// ```
    async fn raw_with_client<F, R>(&self, f: F) -> Result<R, DjogiError>
    where
        F: for<'client> FnOnce(&'client mut tokio_postgres::Client) -> ClientFuture<'client, R>
            + Send,
        R: Send + 'static;
}

impl RawPoolAccessExt for DjogiContext {
    fn raw_pool(&self) -> Option<&DjogiPool> {
        self.pool()
    }

    fn raw_conn(&mut self) -> Option<&mut PgConnection> {
        if self.is_transaction_poisoned() {
            None
        } else {
            self.conn()
        }
    }

    fn raw_with_client<F, R>(
        &self,
        f: F,
    ) -> impl std::future::Future<Output = Result<R, DjogiError>> + Send
    where
        F: for<'client> FnOnce(&'client mut tokio_postgres::Client) -> ClientFuture<'client, R>
            + Send,
        R: Send + 'static,
    {
        let pool = self.pool().cloned();
        async move {
            match pool {
                Some(pool) => pool.with_client(f).await,
                None => Err(DjogiError::Db(DbError::other(
                    "raw_with_client requires a pool-backed DjogiContext",
                ))),
            }
        }
    }
}

impl RawPoolAccessExt for DjogiPool {
    fn raw_pool(&self) -> Option<&DjogiPool> {
        Some(self)
    }

    fn raw_conn(&mut self) -> Option<&mut PgConnection> {
        None
    }

    fn raw_with_client<F, R>(
        &self,
        f: F,
    ) -> impl std::future::Future<Output = Result<R, DjogiError>> + Send
    where
        F: for<'client> FnOnce(&'client mut tokio_postgres::Client) -> ClientFuture<'client, R>
            + Send,
        R: Send + 'static,
    {
        let pool = self.clone();
        async move { pool.with_client(f).await }
    }
}

#[cfg(test)]
#[allow(dead_code)]
async fn _raw_stream_trait_canary<'ctx>(
    ctx: &'ctx mut DjogiContext,
) -> Result<RawCursorStream<'ctx>, DjogiError> {
    let params: &[&(dyn ToSql + Sync)] = &[];
    <DjogiContext as RawAccessExt>::raw_stream(ctx, "SELECT 1", params).await
}

#[cfg(test)]
#[allow(dead_code)]
async fn _raw_stream_with_fetch_size_trait_canary<'ctx>(
    ctx: &'ctx mut DjogiContext,
) -> Result<RawCursorStream<'ctx>, DjogiError> {
    let params: &[&(dyn ToSql + Sync)] = &[];
    <DjogiContext as RawAccessExt>::raw_stream_with_fetch_size(ctx, "SELECT 1", params, 1).await
}

#[cfg(test)]
mod tests {
    use super::{
        TransactionBackedRawSqlRefusal, classify_raw_ddl_transaction_backed_refusal,
        classify_raw_ddl_transaction_session_statement, classify_transaction_backed_refusal,
        classify_transaction_control_statement, classify_transaction_session_statement,
    };
    use crate::DjogiError;

    #[test]
    fn classify_transaction_session_statement_rejects_plain_set_after_leading_comments() {
        let sql = "  /* prelude ; */ -- line comment\n  sEt search_path = public";
        assert_eq!(classify_transaction_session_statement(sql), Some("SET"));
    }

    #[test]
    fn classify_transaction_session_statement_allows_transaction_local_set_forms() {
        assert_eq!(
            classify_transaction_session_statement("SET LOCAL statement_timeout = '5s'"),
            None
        );
        assert_eq!(
            classify_transaction_session_statement("SET CONSTRAINTS ALL IMMEDIATE"),
            None
        );
        assert_eq!(
            classify_transaction_session_statement(
                "SET TRANSACTION ISOLATION LEVEL READ COMMITTED"
            ),
            None
        );
    }

    #[test]
    fn classify_transaction_session_statement_rejects_other_session_statement_heads() {
        for (sql, expected) in [
            ("RESET ALL", "RESET"),
            ("discard all", "DISCARD"),
            ("LISTEN djogi_updates", "LISTEN"),
            ("unlisten *", "UNLISTEN"),
            ("PREPARE x AS SELECT 1", "PREPARE"),
            ("deallocate all", "DEALLOCATE"),
        ] {
            assert_eq!(
                classify_transaction_session_statement(sql),
                Some(expected),
                "expected {expected} to be rejected for {sql:?}"
            );
        }
    }

    #[test]
    fn classify_raw_ddl_transaction_session_statement_ignores_semicolons_inside_bodies() {
        let sql = r#"
            DO $body$
            BEGIN
                PERFORM '; still inside the body';
                PERFORM $$nested ; dollar quote$$;
            END
            $body$;
            /* scanner must only inspect the real next statement */
            LISTEN djogi_updates;
        "#;

        assert_eq!(
            classify_raw_ddl_transaction_session_statement(sql),
            Some("LISTEN")
        );
    }

    #[test]
    fn classify_raw_ddl_transaction_session_statement_handles_utf8_inside_dollar_quote() {
        let sql = r#"
            DO $body$
            BEGIN
                -- Unicode comment inside the body: bootstrap — extensions
                PERFORM 1;
            END
            $body$;
            CREATE TEMP TABLE djogi_282_classifier_utf8_ok (value integer);
        "#;

        assert_eq!(classify_raw_ddl_transaction_session_statement(sql), None);
    }

    #[test]
    fn classify_raw_ddl_transaction_session_statement_allows_trivia_only_and_safe_batches() {
        assert_eq!(
            classify_raw_ddl_transaction_session_statement(
                " /* nothing here */ \n -- still nothing\n"
            ),
            None
        );

        let sql = r#"
            DO $body$
            BEGIN
                PERFORM '; safe body';
            END
            $body$;
            CREATE TEMP TABLE djogi_282_classifier_ok (value integer);
        "#;
        assert_eq!(classify_raw_ddl_transaction_session_statement(sql), None);
    }

    #[test]
    fn classify_raw_ddl_transaction_session_statement_rejects_session_set_in_batch() {
        let sql = r#"
            CREATE TEMP TABLE djogi_282_classifier_set_rejected (value integer);
            SET statement_timeout = '1ms';
        "#;

        assert_eq!(
            classify_raw_ddl_transaction_session_statement(sql),
            Some("SET")
        );
    }

    // -----------------------------------------------------------------------
    // Transaction control statement classification (#306).
    // -----------------------------------------------------------------------

    #[test]
    fn classify_transaction_control_statement_detects_all_nine_forms() {
        assert_eq!(
            classify_transaction_control_statement("BEGIN"),
            Some("BEGIN")
        );
        assert_eq!(
            classify_transaction_control_statement("START TRANSACTION"),
            Some("START TRANSACTION")
        );
        assert_eq!(
            classify_transaction_control_statement("COMMIT"),
            Some("COMMIT")
        );
        assert_eq!(
            classify_transaction_control_statement("ROLLBACK"),
            Some("ROLLBACK")
        );
        assert_eq!(classify_transaction_control_statement("END"), Some("END"));
        assert_eq!(
            classify_transaction_control_statement("ABORT"),
            Some("ABORT")
        );
        assert_eq!(
            classify_transaction_control_statement("SAVEPOINT my_sp"),
            Some("SAVEPOINT")
        );
        assert_eq!(
            classify_transaction_control_statement("RELEASE SAVEPOINT my_sp"),
            Some("RELEASE")
        );
        assert_eq!(
            classify_transaction_control_statement("RELEASE"),
            Some("RELEASE")
        );
        assert_eq!(
            classify_transaction_control_statement("ROLLBACK TO my_sp"),
            Some("ROLLBACK")
        );
        assert_eq!(
            classify_transaction_control_statement("ROLLBACK WORK TO my_sp"),
            Some("ROLLBACK")
        );
        assert_eq!(
            classify_transaction_control_statement("ROLLBACK TRANSACTION TO my_sp"),
            Some("ROLLBACK")
        );
        assert_eq!(
            classify_transaction_control_statement("ROLLBACK WORK"),
            Some("ROLLBACK")
        );
        assert_eq!(
            classify_transaction_control_statement("ROLLBACK TRANSACTION"),
            Some("ROLLBACK")
        );
        assert_eq!(
            classify_transaction_control_statement("COMMIT WORK"),
            Some("COMMIT")
        );
        assert_eq!(
            classify_transaction_control_statement("COMMIT TRANSACTION"),
            Some("COMMIT")
        );
        assert_eq!(
            classify_transaction_control_statement("END WORK"),
            Some("END")
        );
        assert_eq!(
            classify_transaction_control_statement("END TRANSACTION"),
            Some("END")
        );
    }

    #[test]
    fn classify_transaction_control_statement_is_case_insensitive() {
        for sql in ["commit", "CoMmIt", "COMMIT"] {
            assert_eq!(
                classify_transaction_control_statement(sql),
                Some("COMMIT"),
                "expected COMMIT for {sql:?}"
            );
        }
        for sql in ["begin", "BeGiN", "BEGIN"] {
            assert_eq!(
                classify_transaction_control_statement(sql),
                Some("BEGIN"),
                "expected BEGIN for {sql:?}"
            );
        }
        for sql in [
            "start transaction",
            "START TRANSACTION",
            "Start Transaction",
        ] {
            assert_eq!(
                classify_transaction_control_statement(sql),
                Some("START TRANSACTION"),
                "expected START TRANSACTION for {sql:?}"
            );
        }
    }

    #[test]
    fn classify_transaction_control_statement_handles_leading_trivia() {
        assert_eq!(
            classify_transaction_control_statement("  COMMIT"),
            Some("COMMIT")
        );
        assert_eq!(
            classify_transaction_control_statement("  \n  \t  rollback"),
            Some("ROLLBACK")
        );
        assert_eq!(
            classify_transaction_control_statement("-- line comment\nCOMMIT"),
            Some("COMMIT")
        );
        assert_eq!(
            classify_transaction_control_statement("/* block */ BEGIN"),
            Some("BEGIN")
        );
    }

    #[test]
    fn classify_transaction_control_statement_returns_none_for_non_transaction_sql() {
        for sql in [
            "SELECT 1",
            "INSERT INTO users (name) VALUES ('test')",
            "UPDATE posts SET title = 'x'",
            "DELETE FROM comments WHERE id = 1",
            "CREATE TABLE foo (id bigint)",
            "SET LOCAL statement_timeout = '5s'",
            "SET CONSTRAINTS ALL IMMEDIATE",
            "SET TRANSACTION ISOLATION LEVEL READ COMMITTED",
        ] {
            assert_eq!(
                classify_transaction_control_statement(sql),
                None,
                "expected None for non-transaction SQL: {sql:?}"
            );
        }
    }

    #[test]
    fn classify_transaction_backed_refusal_prioritizes_transaction_control_over_session() {
        // COMMIT is transaction control, not session — should be TransactionControl
        let refusal = classify_transaction_backed_refusal("COMMIT").expect("expected refusal");
        match refusal {
            TransactionBackedRawSqlRefusal::TransactionControl(s) => {
                assert_eq!(s, "COMMIT");
            }
            _ => panic!("expected TransactionControl(COMMIT), got {:?}", refusal),
        }
    }

    #[test]
    fn classify_transaction_backed_refusal_wraps_session_statements() {
        let refusal = classify_transaction_backed_refusal("RESET ALL").expect("expected refusal");
        match refusal {
            TransactionBackedRawSqlRefusal::SessionStatement(s) => {
                assert_eq!(s, "RESET");
            }
            _ => panic!("expected SessionStatement(RESET), got {:?}", refusal),
        }
    }

    #[test]
    fn classify_transaction_backed_refusal_into_error_produces_correct_variant() {
        let refusal = classify_transaction_backed_refusal("COMMIT").expect("expected refusal");
        let err = refusal.into_error();
        // Verify the error variant by matching on it
        match err {
            DjogiError::RawTransactionControlDisallowedInTransaction { statement } => {
                assert_eq!(statement, "COMMIT");
            }
            _ => panic!(
                "expected RawTransactionControlDisallowedInTransaction, got {:?}",
                err
            ),
        }

        let refusal = classify_transaction_backed_refusal("LISTEN foo").expect("expected refusal");
        let err = refusal.into_error();
        match err {
            DjogiError::SessionStatementDisallowedInTransaction { statement } => {
                assert_eq!(statement, "LISTEN");
            }
            _ => panic!(
                "expected SessionStatementDisallowedInTransaction, got {:?}",
                err
            ),
        }
    }

    // -----------------------------------------------------------------------
    // Batch scanner tests for unified refusal (#306).
    // -----------------------------------------------------------------------

    #[test]
    fn classify_raw_ddl_batch_ignores_transaction_keywords_in_dollar_quoted_body() {
        let sql = r#"
            DO $body$
            BEGIN
                PERFORM 'COMMIT should be ignored here';
                PERFORM $$nested ROLLBACK$$;
            END
            $body$;
            SELECT 1;
        "#;
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_detects_transaction_control_after_safe_ddl() {
        let sql = r#"
            CREATE TEMP TABLE foo (value integer);
            COMMIT;
        "#;
        match classify_raw_ddl_transaction_backed_refusal(sql) {
            Some(TransactionBackedRawSqlRefusal::TransactionControl(s)) => {
                assert_eq!(s, "COMMIT");
            }
            _ => panic!("expected TransactionControl(COMMIT)"),
        }
    }

    #[test]
    fn classify_raw_ddl_batch_detects_session_statement_after_safe_ddl() {
        let sql = r#"
            CREATE TEMP TABLE foo (value integer);
            RESET ALL;
        "#;
        match classify_raw_ddl_transaction_backed_refusal(sql) {
            Some(TransactionBackedRawSqlRefusal::SessionStatement(s)) => {
                assert_eq!(s, "RESET");
            }
            _ => panic!("expected SessionStatement(RESET)"),
        }
    }

    #[test]
    fn classify_raw_ddl_batch_allows_trivia_only_and_safe_batches() {
        assert_eq!(
            classify_raw_ddl_transaction_backed_refusal(
                " /* nothing here */ \n -- still nothing\n"
            ),
            None
        );

        let sql = r#"
            DO $body$
            BEGIN
                PERFORM '; safe body';
            END
            $body$;
            CREATE TEMP TABLE djogi_306_classifier_ok (value integer);
        "#;
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_transaction_control_statement_start_without_transaction_is_none() {
        // "START" alone is not transaction control — requires "TRANSACTION" as second word
        assert_eq!(classify_transaction_control_statement("START"), None);
        assert_eq!(classify_transaction_control_statement("START ALL"), None);
    }

    #[test]
    fn classify_transaction_backed_refusal_returns_none_for_safe_sql() {
        assert_eq!(classify_transaction_backed_refusal("SELECT 1"), None);
        assert_eq!(
            classify_transaction_backed_refusal("INSERT INTO t VALUES (1)"),
            None
        );
    }

    // -----------------------------------------------------------------------
    // BEGIN ATOMIC ... END compound-statement scanning.
    // The raw_ddl batch scanner must treat `BEGIN ATOMIC ... END` as a single
    // compound statement: internal semicolons do not split, and the closing
    // END is not transaction control. CASE ... END nesting inside the block,
    // string / comment / quote contexts, and word boundaries must all be
    // handled without falsely opening or closing a block.
    // -----------------------------------------------------------------------

    #[test]
    fn classify_raw_ddl_batch_begin_atomic_block_allowed() {
        // BEGIN ATOMIC ... END is valid SQL-standard compound statement syntax.
        // The scanner must not classify the closing END as transaction control,
        // nor the head BEGIN as transaction control (BEGIN ATOMIC != bare BEGIN).
        let sql = "BEGIN ATOMIC SELECT 1; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_create_function_then_atomic_block_allowed() {
        // Realistic migration shape: CREATE FUNCTION followed by atomic compound.
        let sql = r#"
            CREATE FUNCTION f() RETURNS integer AS $$ SELECT 1; END $$ LANGUAGE sql;
            BEGIN ATOMIC SELECT 2; END;
            SELECT 3;
        "#;
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_case_inside_atomic_allowed() {
        // CASE ... END inside an atomic block must not prematurely close it.
        let sql = "BEGIN ATOMIC SELECT CASE WHEN x > 0 THEN 'pos' ELSE 'neg' END; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_nested_case_inside_atomic_allowed() {
        let sql = "BEGIN ATOMIC SELECT CASE WHEN x > 0 THEN CASE WHEN y > 0 THEN 1 ELSE 0 END ELSE -1 END; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_multiple_cases_inside_atomic_allowed() {
        let sql = "BEGIN ATOMIC SELECT CASE WHEN a THEN 1 END; SELECT CASE WHEN b THEN 2 END; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_case_in_atomic_then_commit_detected() {
        // Regression: double-counting CASE depth would leave the atomic block
        // open, swallowing the trailing COMMIT. CASE advances idx (counted
        // once), so the inner END closes the CASE, the block END closes the
        // atomic block, and the COMMIT is detected.
        let sql = "BEGIN ATOMIC SELECT CASE WHEN a THEN 1 END; END; COMMIT";
        assert!(classify_raw_ddl_transaction_backed_refusal(sql).is_some());
    }

    #[test]
    fn classify_raw_ddl_batch_atomic_then_commit_detected() {
        // Transaction control after a (closed) atomic block is still detected.
        let sql = "BEGIN ATOMIC SELECT 1; END; COMMIT";
        assert!(classify_raw_ddl_transaction_backed_refusal(sql).is_some());
    }

    #[test]
    fn classify_raw_ddl_batch_endpoint_inside_atomic_block() {
        // ENDPOINT must not match the END keyword (trailing word boundary).
        let sql = "BEGIN ATOMIC SELECT endpoint FROM t; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_nested_begin_atomic_allowed() {
        // Nested BEGIN ATOMIC blocks exercise depth increment / decrement.
        let sql = "BEGIN ATOMIC SELECT 1; BEGIN ATOMIC SELECT 2; END; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_begin_atomic_in_line_comment_ignored() {
        // BEGIN ATOMIC inside a line comment at depth 0 must not open a block;
        // the top-level END after the comment is genuine transaction control.
        let sql = "-- BEGIN ATOMIC\nEND;";
        assert!(classify_raw_ddl_transaction_backed_refusal(sql).is_some());
    }

    #[test]
    fn classify_raw_ddl_batch_begin_atomic_in_block_comment_ignored() {
        let sql = "/* BEGIN ATOMIC */ END;";
        assert!(classify_raw_ddl_transaction_backed_refusal(sql).is_some());
    }

    #[test]
    fn classify_raw_ddl_batch_begin_atomic_in_single_quote_ignored() {
        let sql = "SELECT 'BEGIN ATOMIC'; END;";
        assert!(classify_raw_ddl_transaction_backed_refusal(sql).is_some());
    }

    #[test]
    fn classify_raw_ddl_batch_begin_atomic_in_double_quote_ignored() {
        let sql = "\"BEGIN ATOMIC\"; END;";
        assert!(classify_raw_ddl_transaction_backed_refusal(sql).is_some());
    }

    #[test]
    fn classify_raw_ddl_batch_begin_atomic_in_dollar_quote_ignored() {
        let sql = "SELECT $$BEGIN ATOMIC$$; END;";
        assert!(classify_raw_ddl_transaction_backed_refusal(sql).is_some());
    }

    #[test]
    fn classify_raw_ddl_batch_create_function_dollar_quoted_atomic_allowed() {
        // Dollar-quoted CREATE FUNCTION with a BEGIN ATOMIC body (regression
        // guard: existing dollar-quote tracking already makes the body opaque).
        let sql = r#"
            CREATE FUNCTION f() RETURNS integer
                LANGUAGE SQL
                AS $$ BEGIN ATOMIC SELECT 1; END $$;
            SELECT 1;
        "#;
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_bare_begin_still_rejected() {
        // Bare BEGIN (not BEGIN ATOMIC) is still transaction control.
        let sql = "BEGIN; SELECT 1;";
        assert!(classify_raw_ddl_transaction_backed_refusal(sql).is_some());
    }

    #[test]
    fn classify_raw_ddl_batch_begin_without_atomic_still_rejected() {
        // BEGIN WORK is transaction control; END is also transaction control.
        let sql = "BEGIN WORK; SELECT 1; END;";
        assert!(classify_raw_ddl_transaction_backed_refusal(sql).is_some());
    }

    #[test]
    fn classify_raw_ddl_batch_begin_atomic_case_insensitive() {
        let sql = "begin ATOMIC SELECT 1; end";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_begin_atomic_keyword_boundary() {
        // BEGINNATIC is not BEGIN ATOMIC; the trailing END is transaction control.
        let sql = "BEGINNATIC SELECT 1; END;";
        assert!(classify_raw_ddl_transaction_backed_refusal(sql).is_some());
    }

    // --- CLASS B: string / comment / quote contexts inside atomic blocks ---

    #[test]
    fn classify_raw_ddl_batch_string_inside_atomic_block() {
        // 'END' in a string literal inside the block must not decrement depth.
        let sql = "BEGIN ATOMIC SELECT 'END'; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_block_comment_inside_atomic_block() {
        let sql = "BEGIN ATOMIC /* END */ SELECT 1; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_line_comment_inside_atomic_block() {
        // The comment's END appears before an internal semicolon. A premature
        // depth decrement would split there and misclassify — so this shape is
        // discriminating, not masked.
        let sql = "BEGIN ATOMIC SELECT 1 -- END\n; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_double_quote_inside_atomic_block() {
        let sql = r#"BEGIN ATOMIC SELECT "END" FROM t; END"#;
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_dollar_quote_inside_atomic_block() {
        let sql = "BEGIN ATOMIC SELECT $$END$$ FROM t; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_word_boundary_inside_atomic_block() {
        // `dividend` ends in `end` but is a single identifier — leading word
        // boundary prevents a false END match.
        let sql = "BEGIN ATOMIC UPDATE t SET x = dividend; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_leading_word_boundary() {
        // xBEGIN is not BEGIN; the block never opens and the top-level END is
        // genuine transaction control.
        let sql = "xBEGIN ATOMIC SELECT 1; END;";
        assert!(classify_raw_ddl_transaction_backed_refusal(sql).is_some());
    }

    #[test]
    fn classify_raw_ddl_batch_create_function_atomic_body_allowed() {
        // Canonical unquoted shape: a dollar-quoted CREATE FUNCTION body
        // followed by a bare BEGIN ATOMIC compound statement.
        let sql = "CREATE FUNCTION f() RETURNS integer LANGUAGE SQL AS $$ SELECT 1; END $$; BEGIN ATOMIC SELECT 2; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    // --- Classifier two-keyword peek (, Step 2c) ---

    #[test]
    fn classify_transaction_control_statement_begin_atomic_is_none() {
        assert_eq!(
            classify_transaction_control_statement("BEGIN ATOMIC SELECT 1"),
            None
        );
        assert_eq!(
            classify_transaction_control_statement("begin atomic SELECT 1"),
            None
        );
    }

    #[test]
    fn classify_transaction_control_statement_bare_begin_still_detected() {
        assert_eq!(
            classify_transaction_control_statement("BEGIN WORK"),
            Some("BEGIN")
        );
        assert_eq!(
            classify_transaction_control_statement("BEGIN TRANSACTION"),
            Some("BEGIN")
        );
        assert_eq!(
            classify_transaction_control_statement("BEGIN;"),
            Some("BEGIN")
        );
        assert_eq!(
            classify_transaction_control_statement("BEGIN"),
            Some("BEGIN")
        );
    }

    // --- : $ and non-ASCII identifier bytes must not trigger keyword matches ---

    #[test]
    fn classify_raw_ddl_batch_dollar_suffix_begin_not_atomic_opener() {
        // `x$begin atomic` must NOT open an atomic block. If it did,
        // the trailing COMMIT would be swallowed as internal and reach Postgres
        // inside atomic(). The COMMIT must be detected as transaction control.
        let sql = "CREATE TEMP TABLE t (x integer); SELECT x$begin atomic FROM t; COMMIT;";
        assert!(
            classify_raw_ddl_transaction_backed_refusal(sql).is_some(),
            "trailing COMMIT after a $-suffixed pseudo-BEGIN must still be refused"
        );
    }

    #[test]
    fn classify_raw_ddl_batch_dollar_prefix_end_does_not_close_atomic_block() {
        // END-site mirror (Task A.4): `x$end` inside a real atomic block must NOT
        // close it. If it closed prematurely, the trailing COMMIT after the real
        // END would be misclassified or the depth would be corrupted. Here the
        // genuine COMMIT after the true block END must be detected.
        let sql = "BEGIN ATOMIC SELECT x$end FROM t; END; COMMIT;";
        assert!(
            classify_raw_ddl_transaction_backed_refusal(sql).is_some(),
            "x$end inside the block must not prematurely close it; trailing COMMIT must be refused"
        );
    }

    #[test]
    fn classify_raw_ddl_batch_begin_dollar_suffix_is_plain_identifier() {
        // Trailing-boundary miss: `begin$x` is a single identifier, not BEGIN.
        // As a standalone statement head it is not transaction control.
        let sql = "SELECT begin$x FROM t;";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_non_ascii_suffix_begin_not_atomic_opener() {
        // Non-ASCII identifier byte before `begin atomic`: a multibyte identifier
        // char (here 'é') makes `begin` part of an identifier, so no block opens
        // and the trailing COMMIT must be refused.
        let sql = "SELECT café_begin atomic FROM t; COMMIT;";
        assert!(
            classify_raw_ddl_transaction_backed_refusal(sql).is_some(),
            "non-ASCII-adjacent pseudo-BEGIN must not open a block; trailing COMMIT must be refused"
        );
    }

    // --- : comments between BEGIN and ATOMIC must still open a block ---

    #[test]
    fn classify_raw_ddl_batch_begin_block_comment_atomic_opens_block() {
        // BEGIN /* c */ ATOMIC must open a compound-statement block (spec: scanner
        // respects comments). The internal COMMIT-looking text and the closing END
        // are then internal, so the batch is allowed.
        let sql = "BEGIN /* c */ ATOMIC SELECT 1; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_begin_line_comment_atomic_opens_block() {
        let sql = "BEGIN -- open the body\n ATOMIC SELECT 1; END";
        assert_eq!(classify_raw_ddl_transaction_backed_refusal(sql), None);
    }

    #[test]
    fn classify_raw_ddl_batch_begin_comment_atomic_then_commit_detected() {
        // Once the comment-separated BEGIN ATOMIC opens and closes, a trailing
        // top-level COMMIT is still detected.
        let sql = "BEGIN /* c */ ATOMIC SELECT 1; END; COMMIT;";
        assert!(classify_raw_ddl_transaction_backed_refusal(sql).is_some());
    }
}