bsql-driver-postgres 0.26.4

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

use crate::DriverError;

// --- Protocol constants ---

/// Protocol version 3.0: major=3, minor=0.
const PROTOCOL_VERSION: i32 = 196608; // 3 << 16

/// SSLRequest magic code.
#[cfg(feature = "tls")]
const SSL_REQUEST_CODE: i32 = 80877103;

/// CancelRequest magic code.
const CANCEL_REQUEST_CODE: i32 = 80877102;

// Frontend message type bytes
const MSG_PASSWORD: u8 = b'p';
const MSG_QUERY: u8 = b'Q';
const MSG_PARSE: u8 = b'P';
const MSG_BIND: u8 = b'B';
const MSG_EXECUTE: u8 = b'E';
const MSG_DESCRIBE: u8 = b'D';
const MSG_CLOSE: u8 = b'C';
const MSG_SYNC: u8 = b'S';
const MSG_TERMINATE: u8 = b'X';

// Backend message type bytes (documented inline in BackendMessage match arms):
// 'R' = Auth, 'S' = ParameterStatus, 'K' = BackendKeyData, 'Z' = ReadyForQuery,
// '1' = ParseComplete, '2' = BindComplete, '3' = CloseComplete, 'T' = RowDescription,
// 'D' = DataRow, 'C' = CommandComplete, 'E' = ErrorResponse, 'N' = NoticeResponse,
// 'A' = NotificationResponse, 'I' = EmptyQueryResponse, 'n' = NoData,
// 't' = ParameterDescription, 's' = PortalSuspended

// --- Backend message types ---

/// A parsed backend message. Borrows from the read buffer for zero-allocation parsing.
///
/// `RowDescription`, `DataRow`, `ErrorResponse`, and `NoticeResponse` carry raw byte
/// slices — their contents are parsed lazily only when accessed.
#[derive(Debug)]
#[allow(dead_code)] // Variant fields are part of the complete protocol representation
pub enum BackendMessage<'a> {
    AuthOk,
    AuthCleartext,
    AuthMd5 {
        salt: [u8; 4],
    },
    AuthSasl {
        mechanisms: &'a [u8],
    },
    AuthSaslContinue {
        data: &'a [u8],
    },
    AuthSaslFinal {
        data: &'a [u8],
    },
    ParameterStatus {
        name: &'a str,
        value: &'a str,
    },
    BackendKeyData {
        pid: i32,
        secret: i32,
    },
    ReadyForQuery {
        status: u8,
    },
    ParseComplete,
    BindComplete,
    CloseComplete,
    NoData,
    ParameterDescription {
        data: &'a [u8],
    },
    RowDescription {
        data: &'a [u8],
    },
    DataRow {
        data: &'a [u8],
    },
    CommandComplete {
        tag: &'a str,
    },
    ErrorResponse {
        data: &'a [u8],
    },
    NoticeResponse {
        data: &'a [u8],
    },
    NotificationResponse {
        pid: i32,
        channel: &'a str,
        payload: &'a str,
    },
    EmptyQuery,
    PortalSuspended,
    CopyInResponse {
        format: u8,
        column_formats: smallvec::SmallVec<[u16; 16]>,
    },
    CopyOutResponse {
        format: u8,
        column_formats: smallvec::SmallVec<[u16; 16]>,
    },
    CopyData {
        data: &'a [u8],
    },
    CopyDone,
}

// --- Frontend message writers ---
//
// Each function appends a complete protocol message to `buf`. The caller flushes
// the entire buffer in one TCP write for pipelining.

/// Write a typed message: type byte + 4-byte length (includes self) + payload.
#[inline]
pub fn write_message(buf: &mut Vec<u8>, msg_type: u8, payload: &[u8]) {
    buf.push(msg_type);
    let len = (payload.len() as i32) + 4; // length includes itself
    buf.extend_from_slice(&len.to_be_bytes());
    buf.extend_from_slice(payload);
}

/// Startup message — no type byte: `[length: i32] [version: i32] [params...] [0x00]`
///
/// `extra_params` are appended as key-value pairs (e.g., `statement_timeout`).
/// Sending them in the startup message eliminates a separate `SET` round-trip.
#[inline]
pub fn write_startup(buf: &mut Vec<u8>, user: &str, database: &str, extra_params: &[(&str, &str)]) {
    let start = buf.len();
    buf.extend_from_slice(&[0u8; 4]); // placeholder for length
    buf.extend_from_slice(&PROTOCOL_VERSION.to_be_bytes());

    buf.extend_from_slice(b"user\0");
    buf.extend_from_slice(user.as_bytes());
    buf.push(0);

    buf.extend_from_slice(b"database\0");
    buf.extend_from_slice(database.as_bytes());
    buf.push(0);

    // Extra startup parameters (e.g., statement_timeout)
    for &(key, value) in extra_params {
        buf.extend_from_slice(key.as_bytes());
        buf.push(0);
        buf.extend_from_slice(value.as_bytes());
        buf.push(0);
    }

    buf.push(0); // terminating zero

    let len = (buf.len() - start) as i32;
    buf[start..start + 4].copy_from_slice(&len.to_be_bytes());
}

/// SSLRequest — 8 bytes, no type byte: `[length=8: i32] [code=80877103: i32]`
#[cfg(feature = "tls")]
pub fn write_ssl_request(buf: &mut Vec<u8>) {
    buf.extend_from_slice(&8i32.to_be_bytes());
    buf.extend_from_slice(&SSL_REQUEST_CODE.to_be_bytes());
}

/// CancelRequest — 16 bytes, no type byte:
/// `[length=16: i32] [code=80877102: i32] [pid: i32] [secret: i32]`
///
/// Sent on a NEW TCP connection to cancel a running query.
/// The connection is closed immediately after sending.
#[inline]
pub fn write_cancel_request(buf: &mut Vec<u8>, pid: i32, secret: i32) {
    buf.extend_from_slice(&16i32.to_be_bytes());
    buf.extend_from_slice(&CANCEL_REQUEST_CODE.to_be_bytes());
    buf.extend_from_slice(&pid.to_be_bytes());
    buf.extend_from_slice(&secret.to_be_bytes());
}

/// Parse message — prepare a named statement.
///
/// Format: `'P' [len] [name\0] [sql\0] [num_param_types: i16] [oid: i32]...`
#[inline]
pub fn write_parse(buf: &mut Vec<u8>, name: &[u8], sql: &str, param_oids: &[u32]) {
    let payload_len = name.len()
        + 1 // NUL
        + sql.len()
        + 1 // NUL
        + 2 // i16 param count
        + param_oids.len() * 4;

    buf.push(MSG_PARSE);
    let len = (payload_len as i32) + 4;
    buf.extend_from_slice(&len.to_be_bytes());

    buf.extend_from_slice(name);
    buf.push(0);
    buf.extend_from_slice(sql.as_bytes());
    buf.push(0);
    buf.extend_from_slice(&(param_oids.len() as i16).to_be_bytes());
    for &oid in param_oids {
        buf.extend_from_slice(&(oid as i32).to_be_bytes());
    }
}

/// Bind message — bind parameters to a prepared statement, requesting binary format
/// for both parameters and results. Encodes parameters inline into the write buffer,
/// eliminating intermediate `Vec<Vec<u8>>` allocation.
///
/// Supports NULL parameters. When a param's `encode_binary` produces 0 bytes
/// AND `is_null()` returns true, a length of -1 is written (PG binary NULL).
/// By default, `is_null()` returns false, so a 0-byte encode is sent as length 0.
///
/// Validates `params.len() <= i16::MAX` before cast.
///
/// Format: `'B' [len] [portal\0] [stmt\0] [num_fmt_codes: i16] [fmt_code: i16]...
///          [num_params: i16] ([param_len: i32] [param_data]...)
///          [num_result_fmt_codes: i16] [result_fmt_code: i16]...`
#[inline]
pub fn write_bind_params(
    buf: &mut Vec<u8>,
    portal: &[u8],
    statement: &[u8],
    params: &[&(dyn crate::codec::Encode + Sync)],
) {
    buf.push(MSG_BIND);
    let len_pos = buf.len();
    buf.extend_from_slice(&[0u8; 4]); // placeholder

    // Portal name
    buf.extend_from_slice(portal);
    buf.push(0);

    // Statement name
    buf.extend_from_slice(statement);
    buf.push(0);

    // Parameter format codes: all binary (format code 1)
    if params.is_empty() {
        buf.extend_from_slice(&0i16.to_be_bytes()); // 0 format codes
    } else {
        buf.extend_from_slice(&1i16.to_be_bytes()); // 1 format code (applies to all)
        buf.extend_from_slice(&1i16.to_be_bytes()); // binary
    }

    // Truncate to i16::MAX — the PG wire protocol uses i16 for param count.
    let param_count = params.len().min(i16::MAX as usize) as i16;

    // Parameter values — encoded inline, no intermediate Vec<Vec<u8>>
    buf.extend_from_slice(&param_count.to_be_bytes());
    for param in params.iter().take(param_count as usize) {
        if param.is_null() {
            // PG binary protocol: NULL = length -1, no data bytes
            buf.extend_from_slice(&(-1i32).to_be_bytes());
        } else {
            let len_pos_param = buf.len();
            buf.extend_from_slice(&[0u8; 4]); // placeholder for param length
            param.encode_binary(buf);
            let data_len = (buf.len() - len_pos_param - 4) as i32;
            buf[len_pos_param..len_pos_param + 4].copy_from_slice(&data_len.to_be_bytes());
        }
    }

    // Result format codes: all binary
    buf.extend_from_slice(&1i16.to_be_bytes()); // 1 format code
    buf.extend_from_slice(&1i16.to_be_bytes()); // binary

    // Patch length
    let len = (buf.len() - len_pos) as i32;
    buf[len_pos..len_pos + 4].copy_from_slice(&len.to_be_bytes());
}

/// Execute message — execute a bound portal.
///
/// `max_rows = 0` means unlimited.
#[inline]
pub fn write_execute(buf: &mut Vec<u8>, portal: &[u8], max_rows: i32) {
    let payload_len = portal.len() + 1 + 4;
    buf.push(MSG_EXECUTE);
    let len = (payload_len as i32) + 4;
    buf.extend_from_slice(&len.to_be_bytes());
    buf.extend_from_slice(portal);
    buf.push(0);
    buf.extend_from_slice(&max_rows.to_be_bytes());
}

/// Pre-built Execute(portal="", max_rows=0) + Sync message pair.
///
/// This is the most common suffix for non-streaming pipelines. Using a constant
/// avoids two function calls and their per-field length calculations on every query.
///
/// Layout:
///   Execute: 'E' [len=9: i32 BE] [portal="" NUL] [max_rows=0: i32 BE]
///   Sync:    'S' [len=4: i32 BE]
pub const EXECUTE_SYNC: &[u8] = &[
    b'E', 0, 0, 0, 9, 0, 0, 0, 0, 0, // Execute(portal="", max_rows=0)
    b'S', 0, 0, 0, 4, // Sync
];

/// Pre-built Execute(portal="", max_rows=0) message WITHOUT Sync.
///
/// Used by `execute_pipeline` to send N×(Bind+Execute) messages followed by
/// one Sync at the end — true PG pipeline mode for batch operations.
///
/// Layout:
///   Execute: 'E' [len=9: i32 BE] [portal="" NUL] [max_rows=0: i32 BE]
pub const EXECUTE_ONLY: &[u8] = &[
    b'E', 0, 0, 0, 9, 0, 0, 0, 0, 0, // Execute(portal="", max_rows=0)
];

/// Pre-built Sync message — standalone, for terminating a pipeline.
///
/// Layout:
///   Sync: 'S' [len=4: i32 BE]
pub const SYNC_ONLY: &[u8] = &[
    b'S', 0, 0, 0, 4, // Sync
];

/// Sync message — marks the end of a message pipeline.
///
/// Causes PG to close the implicit transaction (if outside BEGIN) and destroy
/// all portals (including the unnamed portal). Always sends ReadyForQuery.
#[inline]
pub fn write_sync(buf: &mut Vec<u8>) {
    write_message(buf, MSG_SYNC, &[]);
}

/// Flush message — forces PG to send any buffered output.
///
/// Unlike Sync, Flush does NOT close portals or end transactions. This is
/// essential for streaming: between Execute calls, use Flush to get the
/// PortalSuspended response without destroying the portal.
#[inline]
pub fn write_flush(buf: &mut Vec<u8>) {
    write_message(buf, b'H', &[]);
}

/// Describe message — request description of a statement ('S') or portal ('P').
#[inline]
pub fn write_describe(buf: &mut Vec<u8>, kind: u8, name: &[u8]) {
    let payload_len = 1 + name.len() + 1;
    buf.push(MSG_DESCRIBE);
    let len = (payload_len as i32) + 4;
    buf.extend_from_slice(&len.to_be_bytes());
    buf.push(kind);
    buf.extend_from_slice(name);
    buf.push(0);
}

/// Close message — close a statement ('S') or portal ('P').
#[inline]
pub fn write_close(buf: &mut Vec<u8>, kind: u8, name: &[u8]) {
    let payload_len = 1 + name.len() + 1;
    buf.push(MSG_CLOSE);
    let len = (payload_len as i32) + 4;
    buf.extend_from_slice(&len.to_be_bytes());
    buf.push(kind);
    buf.extend_from_slice(name);
    buf.push(0);
}

/// Terminate message — close the connection.
#[inline]
pub fn write_terminate(buf: &mut Vec<u8>) {
    write_message(buf, MSG_TERMINATE, &[]);
}

/// Simple query message — for non-prepared SQL (BEGIN, COMMIT, SET, etc.).
#[inline]
pub fn write_simple_query(buf: &mut Vec<u8>, sql: &str) {
    let payload_len = sql.len() + 1;
    buf.push(MSG_QUERY);
    let len = (payload_len as i32) + 4;
    buf.extend_from_slice(&len.to_be_bytes());
    buf.extend_from_slice(sql.as_bytes());
    buf.push(0);
}

/// Password message (MD5 or cleartext).
#[inline]
pub fn write_password(buf: &mut Vec<u8>, password: &[u8]) {
    write_message(buf, MSG_PASSWORD, password);
}

/// SASLInitialResponse message.
///
/// Format: `'p' [len] [mechanism\0] [data_len: i32] [data]`
#[inline]
pub fn write_sasl_initial(buf: &mut Vec<u8>, mechanism: &str, data: &[u8]) {
    buf.push(MSG_PASSWORD);
    let payload_len = mechanism.len() + 1 + 4 + data.len();
    let len = (payload_len as i32) + 4;
    buf.extend_from_slice(&len.to_be_bytes());
    buf.extend_from_slice(mechanism.as_bytes());
    buf.push(0);
    buf.extend_from_slice(&(data.len() as i32).to_be_bytes());
    buf.extend_from_slice(data);
}

/// SASLResponse message.
#[inline]
pub fn write_sasl_response(buf: &mut Vec<u8>, data: &[u8]) {
    write_message(buf, MSG_PASSWORD, data);
}

// --- Backend message reading ---

// --- Backend message parsing ---

/// Parse a backend message from its type byte and payload.
///
/// The payload slice must remain valid for the lifetime of the returned message.
#[inline]
pub fn parse_backend_message(
    msg_type: u8,
    payload: &[u8],
) -> Result<BackendMessage<'_>, DriverError> {
    match msg_type {
        b'R' => parse_auth(payload),
        b'S' => parse_parameter_status(payload),
        b'K' => parse_backend_key_data(payload),
        b'Z' => parse_ready_for_query(payload),
        b'1' => Ok(BackendMessage::ParseComplete),
        b'2' => Ok(BackendMessage::BindComplete),
        b'3' => Ok(BackendMessage::CloseComplete),
        b'n' => Ok(BackendMessage::NoData),
        b't' => Ok(BackendMessage::ParameterDescription { data: payload }),
        b'T' => Ok(BackendMessage::RowDescription { data: payload }),
        b'D' => Ok(BackendMessage::DataRow { data: payload }),
        b'C' => parse_command_complete(payload),
        b'E' => Ok(BackendMessage::ErrorResponse { data: payload }),
        b'N' => Ok(BackendMessage::NoticeResponse { data: payload }),
        b'A' => parse_notification(payload),
        b'I' => Ok(BackendMessage::EmptyQuery),
        b's' => Ok(BackendMessage::PortalSuspended),

        b'G' => parse_copy_in_response(payload),
        b'H' => parse_copy_out_response(payload),
        b'W' => Err(DriverError::Protocol(
            "COPY BOTH protocol not supported: server sent CopyBothResponse ('W')".into(),
        )),
        b'd' => Ok(BackendMessage::CopyData { data: payload }),
        b'c' => Ok(BackendMessage::CopyDone),
        _ => Err(DriverError::Protocol(format!(
            "unknown backend message type: '{}' (0x{:02x})",
            msg_type as char, msg_type
        ))),
    }
}

// --- COPY frontend message writers ---

/// CopyData message — sends a chunk of COPY data to the server.
///
/// Format: `'d' [length: i32] [data]`
///
/// Note: `copy_in` writes CopyData frames directly to `write_buf` for
/// batched I/O. This function exists for API completeness and tests.
#[inline]
#[cfg(test)]
pub fn write_copy_data(buf: &mut Vec<u8>, data: &[u8]) {
    buf.push(b'd');
    let len = (4 + data.len()) as i32;
    buf.extend_from_slice(&len.to_be_bytes());
    buf.extend_from_slice(data);
}

/// CopyDone message — signals end of COPY data stream.
///
/// Format: `'c' [length=4: i32]`
#[inline]
pub fn write_copy_done(buf: &mut Vec<u8>) {
    buf.push(b'c');
    buf.extend_from_slice(&4i32.to_be_bytes());
}

// --- Parse helpers ---

/// Parse a CopyInResponse message.
///
/// Format: `[format: u8] [num_columns: i16] [column_format: i16]...`
#[inline]
fn parse_copy_in_response(payload: &[u8]) -> Result<BackendMessage<'_>, DriverError> {
    if payload.len() < 3 {
        return Err(DriverError::Protocol("CopyInResponse too short".into()));
    }
    let format = payload[0];
    let raw_cols = i16::from_be_bytes([payload[1], payload[2]]);
    if raw_cols < 0 {
        return Err(DriverError::Protocol(
            "CopyInResponse: negative column count".into(),
        ));
    }
    let num_cols = raw_cols as usize;
    let needed = num_cols.checked_mul(2).and_then(|n| n.checked_add(3));
    match needed {
        Some(n) if payload.len() >= n => {}
        _ => {
            return Err(DriverError::Protocol(
                "CopyInResponse truncated: not enough column format codes".into(),
            ));
        }
    }
    let mut column_formats = smallvec::SmallVec::with_capacity(num_cols);
    for i in 0..num_cols {
        let offset = 3 + i * 2;
        column_formats.push(u16::from_be_bytes([payload[offset], payload[offset + 1]]));
    }
    Ok(BackendMessage::CopyInResponse {
        format,
        column_formats,
    })
}

/// Parse a CopyOutResponse message.
///
/// Format: `[format: u8] [num_columns: i16] [column_format: i16]...`
#[inline]
fn parse_copy_out_response(payload: &[u8]) -> Result<BackendMessage<'_>, DriverError> {
    if payload.len() < 3 {
        return Err(DriverError::Protocol("CopyOutResponse too short".into()));
    }
    let format = payload[0];
    let raw_cols = i16::from_be_bytes([payload[1], payload[2]]);
    if raw_cols < 0 {
        return Err(DriverError::Protocol(
            "CopyOutResponse: negative column count".into(),
        ));
    }
    let num_cols = raw_cols as usize;
    let needed = num_cols.checked_mul(2).and_then(|n| n.checked_add(3));
    match needed {
        Some(n) if payload.len() >= n => {}
        _ => {
            return Err(DriverError::Protocol(
                "CopyOutResponse truncated: not enough column format codes".into(),
            ));
        }
    }
    let mut column_formats = smallvec::SmallVec::with_capacity(num_cols);
    for i in 0..num_cols {
        let offset = 3 + i * 2;
        column_formats.push(u16::from_be_bytes([payload[offset], payload[offset + 1]]));
    }
    Ok(BackendMessage::CopyOutResponse {
        format,
        column_formats,
    })
}

#[inline]
fn parse_auth(payload: &[u8]) -> Result<BackendMessage<'_>, DriverError> {
    if payload.len() < 4 {
        return Err(DriverError::Protocol("auth message too short".into()));
    }
    let auth_type = i32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]]);

    match auth_type {
        0 => Ok(BackendMessage::AuthOk),
        3 => Ok(BackendMessage::AuthCleartext),
        5 => {
            if payload.len() < 8 {
                return Err(DriverError::Protocol("MD5 auth message too short".into()));
            }
            let mut salt = [0u8; 4];
            salt.copy_from_slice(&payload[4..8]);
            Ok(BackendMessage::AuthMd5 { salt })
        }
        10 => {
            // SASL — mechanisms follow as NUL-terminated strings, double NUL at end
            Ok(BackendMessage::AuthSasl {
                mechanisms: &payload[4..],
            })
        }
        11 => Ok(BackendMessage::AuthSaslContinue {
            data: &payload[4..],
        }),
        12 => Ok(BackendMessage::AuthSaslFinal {
            data: &payload[4..],
        }),
        _ => Err(DriverError::Protocol(format!(
            "unsupported authentication method (type {auth_type}). bsql supports: cleartext (3), \
             MD5 (5), SCRAM-SHA-256 (10). Your server requires method {auth_type} which may be \
             GSSAPI, SSPI, or certificate auth."
        ))),
    }
}

#[inline]
fn parse_parameter_status(payload: &[u8]) -> Result<BackendMessage<'_>, DriverError> {
    let name = read_cstring(payload, 0)?;
    let name_end = name.len() + 1;
    let value = read_cstring(payload, name_end)?;
    Ok(BackendMessage::ParameterStatus { name, value })
}

#[inline]
fn parse_backend_key_data(payload: &[u8]) -> Result<BackendMessage<'_>, DriverError> {
    if payload.len() < 8 {
        return Err(DriverError::Protocol(
            "BackendKeyData message too short".into(),
        ));
    }
    let pid = i32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]]);
    let secret = i32::from_be_bytes([payload[4], payload[5], payload[6], payload[7]]);
    Ok(BackendMessage::BackendKeyData { pid, secret })
}

#[inline]
fn parse_ready_for_query(payload: &[u8]) -> Result<BackendMessage<'_>, DriverError> {
    if payload.is_empty() {
        return Err(DriverError::Protocol("ReadyForQuery message empty".into()));
    }
    Ok(BackendMessage::ReadyForQuery { status: payload[0] })
}

#[inline]
fn parse_command_complete(payload: &[u8]) -> Result<BackendMessage<'_>, DriverError> {
    let tag = read_cstring(payload, 0)?;
    Ok(BackendMessage::CommandComplete { tag })
}

#[inline]
fn parse_notification(payload: &[u8]) -> Result<BackendMessage<'_>, DriverError> {
    if payload.len() < 4 {
        return Err(DriverError::Protocol("notification too short".into()));
    }
    let pid = i32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]]);
    let channel = read_cstring(payload, 4)?;
    let channel_end = 4 + channel.len() + 1;
    let msg_payload = read_cstring(payload, channel_end)?;
    Ok(BackendMessage::NotificationResponse {
        pid,
        channel,
        payload: msg_payload,
    })
}

/// Read a NUL-terminated C string from `data` starting at `offset`.
#[inline]
fn read_cstring(data: &[u8], offset: usize) -> Result<&str, DriverError> {
    let remaining = data
        .get(offset..)
        .ok_or_else(|| DriverError::Protocol("c-string read out of bounds".into()))?;

    let nul_pos = remaining
        .iter()
        .position(|&b| b == 0)
        .ok_or_else(|| DriverError::Protocol("c-string not NUL-terminated".into()))?;

    std::str::from_utf8(&remaining[..nul_pos])
        .map_err(|e| DriverError::Protocol(format!("invalid UTF-8 in protocol string: {e}")))
}

// --- RowDescription parsing ---

/// Parse a RowDescription payload into column descriptors.
///
/// Returns `Vec<ColumnDesc>` directly — no intermediate `ColumnInfo` type.
///
/// Format: `[num_fields: i16] ([name\0] [table_oid: i32] [col_attr: i16]
///           [type_oid: i32] [type_size: i16] [type_mod: i32] [format: i16])...`
#[inline]
pub fn parse_row_description(data: &[u8]) -> Result<Vec<crate::types::ColumnDesc>, DriverError> {
    if data.len() < 2 {
        return Err(DriverError::Protocol("RowDescription too short".into()));
    }

    // A negative i16 from a malicious server would become usize::MAX -> OOM.
    let raw_fields = i16::from_be_bytes([data[0], data[1]]);
    if raw_fields < 0 {
        return Err(DriverError::Protocol(format!(
            "RowDescription: negative field count {raw_fields}"
        )));
    }
    let num_fields = raw_fields as usize;
    // Cap at 2000 columns — no sane query returns more.
    if num_fields > 2000 {
        return Err(DriverError::Protocol(format!(
            "RowDescription: field count {num_fields} exceeds maximum 2000"
        )));
    }
    let mut columns = Vec::with_capacity(num_fields);
    let mut pos = 2;

    for _ in 0..num_fields {
        let name = read_cstring(data, pos)?;
        pos += name.len() + 1;

        if pos + 18 > data.len() {
            return Err(DriverError::Protocol(
                "RowDescription field truncated".into(),
            ));
        }

        let table_oid =
            u32::from_be_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]]);
        pos += 4;

        let column_id = i16::from_be_bytes([data[pos], data[pos + 1]]);
        pos += 2;

        let type_oid = u32::from_be_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]]);
        pos += 4;

        let type_size = i16::from_be_bytes([data[pos], data[pos + 1]]);
        pos += 2;

        // type_mod (4) + format (2) = 6 bytes, skip
        pos += 6;

        columns.push(crate::types::ColumnDesc {
            name: name.into(),
            type_oid,
            type_size,
            table_oid,
            column_id,
        });
    }

    Ok(columns)
}

/// Parse a ParameterDescription payload into parameter type OIDs.
///
/// Format: `[num_params: i16] [oid: i32]...`
#[inline]
pub fn parse_parameter_description(data: &[u8]) -> Result<Vec<u32>, DriverError> {
    if data.len() < 2 {
        return Err(DriverError::Protocol(
            "ParameterDescription too short".into(),
        ));
    }
    let raw_count = i16::from_be_bytes([data[0], data[1]]);
    if raw_count < 0 {
        return Err(DriverError::Protocol(format!(
            "ParameterDescription: negative param count {raw_count}"
        )));
    }
    let count = raw_count as usize;
    if data.len() < 2 + count * 4 {
        return Err(DriverError::Protocol(
            "ParameterDescription truncated".into(),
        ));
    }
    let mut oids = Vec::with_capacity(count);
    let mut pos = 2;
    for _ in 0..count {
        let oid = u32::from_be_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]]);
        oids.push(oid);
        pos += 4;
    }
    Ok(oids)
}

/// Parse rows from the simple query protocol text result.
///
/// The simple query protocol returns rows as text strings (not binary).
/// Each `DataRow` in `data` contains columns as NUL-terminated C strings.
///
/// This is a lightweight helper for compile-time schema introspection queries
/// only. It processes the raw bytes of multiple DataRow messages that were
/// collected by the caller.
#[inline]
pub fn parse_simple_data_row(data: &[u8]) -> Result<Vec<Option<String>>, DriverError> {
    if data.len() < 2 {
        return Err(DriverError::Protocol("DataRow too short".into()));
    }
    let col_count = i16::from_be_bytes([data[0], data[1]]);
    if col_count < 0 {
        return Err(DriverError::Protocol(format!(
            "DataRow: negative column count {col_count}"
        )));
    }
    let mut row = Vec::with_capacity(col_count as usize);
    let mut pos = 2;
    for _ in 0..col_count as usize {
        if pos + 4 > data.len() {
            return Err(DriverError::Protocol("DataRow column truncated".into()));
        }
        let len = i32::from_be_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]]);
        pos += 4;
        if len == -1 {
            row.push(None);
        } else {
            let len = len as usize;
            if pos + len > data.len() {
                return Err(DriverError::Protocol("DataRow value truncated".into()));
            }
            let text = std::str::from_utf8(&data[pos..pos + len])
                .map_err(|e| DriverError::Protocol(format!("invalid UTF-8 in DataRow: {e}")))?;
            row.push(Some(text.to_owned()));
            pos += len;
        }
    }
    Ok(row)
}

// --- ErrorResponse parsing ---

/// Parsed fields from an ErrorResponse or NoticeResponse.
#[derive(Debug)]
pub struct ErrorFields {
    pub code: [u8; 5],
    pub message: String,
    pub detail: Option<String>,
    pub hint: Option<String>,
    /// Character position in the original query where the error occurred.
    /// Field type `b'P'` in the PG wire protocol. 1-indexed.
    pub position: Option<u32>,
}

impl fmt::Display for ErrorFields {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "[{}] {}",
            std::str::from_utf8(&self.code).unwrap_or("?????"),
            self.message
        )?;
        if let Some(pos) = self.position {
            write!(f, " (at position {pos})")?;
        }
        if let Some(ref detail) = self.detail {
            write!(f, " DETAIL: {detail}")?;
        }
        if let Some(ref hint) = self.hint {
            write!(f, " HINT: {hint}")?;
        }
        Ok(())
    }
}

/// Parse an ErrorResponse / NoticeResponse payload into fields.
///
/// Format: `([field_type: u8] [value\0])... [0x00]`
///
/// Marked `#[cold]` + `#[inline(never)]` because error responses are rare
/// on the hot path. Keeping this out of the caller's instruction stream
/// improves i-cache utilization for the common DataRow processing loop.
#[cold]
#[inline(never)]
pub fn parse_error_response(data: &[u8]) -> ErrorFields {
    let mut code: [u8; 5] = *b"     ";
    let mut message = String::new();
    let mut detail = None;
    let mut hint = None;
    let mut position = None;

    let mut pos = 0;
    while pos < data.len() {
        let field_type = data[pos];
        pos += 1;

        if field_type == 0 {
            break;
        }

        let value = match read_cstring(data, pos) {
            Ok(s) => {
                pos += s.len() + 1;
                s
            }
            Err(_) => break,
        };

        match field_type {
            b'S' => {} // severity — not stored
            b'C' => {
                let bytes = value.as_bytes();
                let len = bytes.len().min(5);
                code[..len].copy_from_slice(&bytes[..len]);
            }
            b'M' => message = value.to_owned(),
            b'D' => detail = Some(value.to_owned()),
            b'H' => hint = Some(value.to_owned()),
            b'P' => position = value.parse::<u32>().ok(),
            _ => {} // skip other fields (internal query, where, schema, etc.)
        }
    }

    // Truncated or malformed error responses should produce a meaningful error.
    if message.is_empty() {
        if code == *b"     " {
            message = "(malformed error response: no message or code)".to_owned();
        } else {
            message = format!(
                "(malformed error response: code={}, no message)",
                std::str::from_utf8(&code).unwrap_or("?????")
            );
        }
    }

    ErrorFields {
        code,
        message,
        detail,
        hint,
        position,
    }
}

/// Extract affected row count from a CommandComplete tag.
///
/// Tags like "INSERT 0 5", "UPDATE 3", "DELETE 12", "SELECT 100" — the last
/// number is the affected/returned row count.
#[inline]
pub fn parse_command_tag(tag: &str) -> u64 {
    tag.rsplit(' ')
        .next()
        .and_then(|s| s.parse::<u64>().ok())
        .unwrap_or(0)
}

/// Parse the affected-row count from a CommandComplete tag stored as raw bytes.
///
/// The tag format is `"INSERT 0 N\0"`, `"UPDATE N\0"`, `"DELETE N\0"`, etc.
/// We scan backwards from the NUL terminator (or end of slice) to find the
/// last space, then parse the digits. This avoids UTF-8 validation overhead
/// since the tag is always ASCII.
#[inline]
pub fn parse_command_tag_bytes(payload: &[u8]) -> u64 {
    // Strip trailing NUL if present.
    let data = match payload.last() {
        Some(&0) => &payload[..payload.len() - 1],
        _ => payload,
    };
    // Find the last space.
    let space_pos = match data.iter().rposition(|&b| b == b' ') {
        Some(p) => p,
        None => return 0,
    };
    // Parse ASCII digits after the space.
    let mut n: u64 = 0;
    for &b in &data[space_pos + 1..] {
        if b.is_ascii_digit() {
            n = n * 10 + (b - b'0') as u64;
        } else {
            return 0;
        }
    }
    n
}

/// Quote a PostgreSQL identifier with double quotes.
///
/// Embedded double quotes are escaped by doubling them (`"` -> `""`).
/// This prevents SQL injection in table/column names used by COPY.
#[inline]
pub fn quote_ident(ident: &str) -> String {
    let mut out = String::with_capacity(ident.len() + 2);
    out.push('"');
    for ch in ident.chars() {
        if ch == '"' {
            out.push('"');
        }
        out.push(ch);
    }
    out.push('"');
    out
}

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

    #[test]
    fn startup_message_format() {
        let mut buf = Vec::new();
        write_startup(&mut buf, "testuser", "testdb", &[]);

        // First 4 bytes = length
        let len = i32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]]);
        assert_eq!(len as usize, buf.len());

        // Next 4 bytes = protocol version 3.0
        let ver = i32::from_be_bytes([buf[4], buf[5], buf[6], buf[7]]);
        assert_eq!(ver, PROTOCOL_VERSION);

        // Must contain user\0testuser\0database\0testdb\0\0
        let payload = &buf[8..];
        assert!(payload.starts_with(b"user\0testuser\0database\0testdb\0"));
        assert_eq!(*buf.last().unwrap(), 0); // trailing NUL
    }

    #[test]
    fn startup_message_with_extra_params() {
        let mut buf = Vec::new();
        write_startup(
            &mut buf,
            "testuser",
            "testdb",
            &[("statement_timeout", "30s")],
        );

        let len = i32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]]);
        assert_eq!(len as usize, buf.len());

        let payload = &buf[8..];
        // Must contain the extra parameter
        let payload_str = String::from_utf8_lossy(payload);
        assert!(payload_str.contains("statement_timeout"));
        assert!(payload_str.contains("30s"));
        assert_eq!(*buf.last().unwrap(), 0); // trailing NUL
    }

    #[cfg(feature = "tls")]
    #[test]
    fn ssl_request_format() {
        let mut buf = Vec::new();
        write_ssl_request(&mut buf);
        assert_eq!(buf.len(), 8);
        let len = i32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]]);
        assert_eq!(len, 8);
        let code = i32::from_be_bytes([buf[4], buf[5], buf[6], buf[7]]);
        assert_eq!(code, SSL_REQUEST_CODE);
    }

    #[test]
    fn parse_message_framing() {
        let mut buf = Vec::new();
        write_message(&mut buf, b'X', &[]);
        assert_eq!(buf, &[b'X', 0, 0, 0, 4]);
    }

    #[test]
    fn sync_message_format() {
        let mut buf = Vec::new();
        write_sync(&mut buf);
        assert_eq!(buf, &[b'S', 0, 0, 0, 4]);
    }

    #[test]
    fn terminate_message_format() {
        let mut buf = Vec::new();
        write_terminate(&mut buf);
        assert_eq!(buf, &[b'X', 0, 0, 0, 4]);
    }

    #[test]
    fn parse_complete_parses() {
        let msg = parse_backend_message(b'1', &[]).unwrap();
        assert!(matches!(msg, BackendMessage::ParseComplete));
    }

    #[test]
    fn bind_complete_parses() {
        let msg = parse_backend_message(b'2', &[]).unwrap();
        assert!(matches!(msg, BackendMessage::BindComplete));
    }

    #[test]
    fn auth_ok_parses() {
        let payload = 0i32.to_be_bytes();
        let msg = parse_backend_message(b'R', &payload).unwrap();
        assert!(matches!(msg, BackendMessage::AuthOk));
    }

    #[test]
    fn auth_md5_parses() {
        let mut payload = 5i32.to_be_bytes().to_vec();
        payload.extend_from_slice(&[0xDE, 0xAD, 0xBE, 0xEF]);
        let msg = parse_backend_message(b'R', &payload).unwrap();
        match msg {
            BackendMessage::AuthMd5 { salt } => {
                assert_eq!(salt, [0xDE, 0xAD, 0xBE, 0xEF]);
            }
            _ => panic!("expected AuthMd5"),
        }
    }

    #[test]
    fn ready_for_query_parses() {
        let msg = parse_backend_message(b'Z', b"I").unwrap();
        match msg {
            BackendMessage::ReadyForQuery { status } => assert_eq!(status, b'I'),
            _ => panic!("expected ReadyForQuery"),
        }
    }

    #[test]
    fn command_complete_parses() {
        let payload = b"SELECT 42\0".to_vec();
        let msg = parse_backend_message(b'C', &payload).unwrap();
        match msg {
            BackendMessage::CommandComplete { tag } => assert_eq!(tag, "SELECT 42"),
            _ => panic!("expected CommandComplete"),
        }
    }

    #[test]
    fn parameter_status_parses() {
        let payload = b"server_version\x0015.2\0".to_vec();
        let msg = parse_backend_message(b'S', &payload).unwrap();
        match msg {
            BackendMessage::ParameterStatus { name, value } => {
                assert_eq!(name, "server_version");
                assert_eq!(value, "15.2");
            }
            _ => panic!("expected ParameterStatus"),
        }
    }

    #[test]
    fn command_tag_parsing() {
        assert_eq!(parse_command_tag("SELECT 100"), 100);
        assert_eq!(parse_command_tag("INSERT 0 5"), 5);
        assert_eq!(parse_command_tag("UPDATE 3"), 3);
        assert_eq!(parse_command_tag("DELETE 12"), 12);
        assert_eq!(parse_command_tag("BEGIN"), 0);
        assert_eq!(parse_command_tag("COMMIT"), 0);
    }

    #[test]
    fn command_tag_bytes_parsing() {
        // With NUL terminator (as received from the wire).
        assert_eq!(parse_command_tag_bytes(b"SELECT 100\0"), 100);
        assert_eq!(parse_command_tag_bytes(b"INSERT 0 5\0"), 5);
        assert_eq!(parse_command_tag_bytes(b"UPDATE 3\0"), 3);
        assert_eq!(parse_command_tag_bytes(b"DELETE 12\0"), 12);
        assert_eq!(parse_command_tag_bytes(b"BEGIN\0"), 0);
        assert_eq!(parse_command_tag_bytes(b"COMMIT\0"), 0);
        assert_eq!(parse_command_tag_bytes(b"CREATE TABLE\0"), 0);
        // Without NUL terminator.
        assert_eq!(parse_command_tag_bytes(b"INSERT 0 1"), 1);
        assert_eq!(parse_command_tag_bytes(b"DELETE 999"), 999);
        // Empty / edge cases.
        assert_eq!(parse_command_tag_bytes(b""), 0);
        assert_eq!(parse_command_tag_bytes(b"\0"), 0);
    }

    #[test]
    fn unknown_backend_message_errors() {
        let result = parse_backend_message(0xFF, &[]);
        assert!(result.is_err());
    }

    #[test]
    fn parse_message_writes_correct_format() {
        let mut buf = Vec::new();
        write_parse(&mut buf, b"s_test", "SELECT 1", &[23]);

        assert_eq!(buf[0], b'P');
        // After type byte, next 4 bytes are length
        let len = i32::from_be_bytes([buf[1], buf[2], buf[3], buf[4]]);
        assert_eq!(len as usize + 1, buf.len()); // +1 for type byte
    }

    #[test]
    fn bind_message_binary_format() {
        let mut buf = Vec::new();
        let val = 42i32;
        let params: Vec<&(dyn crate::codec::Encode + Sync)> = vec![&val];
        write_bind_params(&mut buf, b"", b"s_test", &params);

        assert_eq!(buf[0], b'B');
        // Verify it contains binary format codes
        // The message should request binary results
    }

    #[test]
    fn bind_no_params() {
        let mut buf = Vec::new();
        let params: Vec<&(dyn crate::codec::Encode + Sync)> = vec![];
        write_bind_params(&mut buf, b"", b"s_test", &params);
        assert_eq!(buf[0], b'B');
    }

    #[test]
    fn execute_message_format() {
        let mut buf = Vec::new();
        write_execute(&mut buf, b"", 0);
        assert_eq!(buf[0], b'E');
    }

    #[test]
    fn execute_sync_constant_matches_functions() {
        let mut buf = Vec::new();
        write_execute(&mut buf, b"", 0);
        write_sync(&mut buf);
        assert_eq!(buf.as_slice(), EXECUTE_SYNC);
    }

    #[test]
    fn execute_only_matches_execute_without_sync() {
        let mut buf = Vec::new();
        write_execute(&mut buf, b"", 0);
        assert_eq!(buf.as_slice(), EXECUTE_ONLY);
    }

    #[test]
    fn sync_only_matches_sync() {
        let mut buf = Vec::new();
        write_sync(&mut buf);
        assert_eq!(buf.as_slice(), SYNC_ONLY);
    }

    #[test]
    fn execute_sync_equals_execute_only_plus_sync_only() {
        let mut combined = Vec::new();
        combined.extend_from_slice(EXECUTE_ONLY);
        combined.extend_from_slice(SYNC_ONLY);
        assert_eq!(combined.as_slice(), EXECUTE_SYNC);
    }

    #[test]
    fn describe_message_format() {
        let mut buf = Vec::new();
        write_describe(&mut buf, b'S', b"s_test");
        assert_eq!(buf[0], b'D');
        assert_eq!(buf[5], b'S');
    }

    #[test]
    fn close_message_format() {
        let mut buf = Vec::new();
        write_close(&mut buf, b'S', b"s_test");
        assert_eq!(buf[0], b'C');
        assert_eq!(buf[5], b'S');
    }

    #[test]
    fn simple_query_format() {
        let mut buf = Vec::new();
        write_simple_query(&mut buf, "BEGIN");
        assert_eq!(buf[0], b'Q');
        // Should end with NUL
        assert_eq!(*buf.last().unwrap(), 0);
    }

    #[test]
    fn error_response_parsing() {
        let mut data = Vec::new();
        data.push(b'S');
        data.extend_from_slice(b"ERROR\0");
        data.push(b'C');
        data.extend_from_slice(b"42P01\0");
        data.push(b'M');
        data.extend_from_slice(b"relation does not exist\0");
        data.push(b'D');
        data.extend_from_slice(b"some detail\0");
        data.push(b'H');
        data.extend_from_slice(b"some hint\0");
        data.push(0);

        let fields = parse_error_response(&data);
        assert_eq!(&fields.code, b"42P01");
        assert_eq!(fields.message, "relation does not exist");
        assert_eq!(fields.detail.as_deref(), Some("some detail"));
        assert_eq!(fields.hint.as_deref(), Some("some hint"));
    }

    #[test]
    fn row_description_parsing() {
        // Build a minimal RowDescription: 1 field named "id", type int4 (OID 23),
        // size 4, format binary (1)
        let mut data = Vec::new();
        data.extend_from_slice(&1i16.to_be_bytes()); // 1 field

        data.extend_from_slice(b"id\0"); // name
        data.extend_from_slice(&0i32.to_be_bytes()); // table OID
        data.extend_from_slice(&0i16.to_be_bytes()); // column attr
        data.extend_from_slice(&23u32.to_be_bytes()); // type OID (int4)
        data.extend_from_slice(&4i16.to_be_bytes()); // type size
        data.extend_from_slice(&(-1i32).to_be_bytes()); // type mod
        data.extend_from_slice(&1i16.to_be_bytes()); // format (binary)

        let cols = parse_row_description(&data).unwrap();
        assert_eq!(cols.len(), 1);
        assert_eq!(&*cols[0].name, "id");
        assert_eq!(cols[0].type_oid, 23);
        assert_eq!(cols[0].type_size, 4);
    }

    #[test]
    fn portal_suspended_parses() {
        let msg = parse_backend_message(b's', &[]).unwrap();
        assert!(matches!(msg, BackendMessage::PortalSuspended));
    }

    #[test]
    fn execute_with_max_rows() {
        let mut buf = Vec::new();
        write_execute(&mut buf, b"", 64);
        assert_eq!(buf[0], b'E');
        // Portal name "" (1 byte NUL) + max_rows (4 bytes) = 5 bytes payload
        // Message: type(1) + length(4) + portal_NUL(1) + max_rows(4) = 10 bytes
        assert_eq!(buf.len(), 10);
        // Last 4 bytes should be max_rows=64 in big-endian
        let max_rows = i32::from_be_bytes([buf[6], buf[7], buf[8], buf[9]]);
        assert_eq!(max_rows, 64);
    }

    #[test]
    fn row_description_negative_field_count() {
        let mut data = Vec::new();
        data.extend_from_slice(&(-1i16).to_be_bytes()); // negative field count
        let result = parse_row_description(&data);
        assert!(result.is_err(), "negative field count should error");
    }

    #[test]
    fn row_description_excessive_field_count() {
        let mut data = Vec::new();
        data.extend_from_slice(&2001i16.to_be_bytes()); // > 2000 cap
        let result = parse_row_description(&data);
        assert!(result.is_err(), "field count > 2000 should error");
    }

    #[test]
    fn error_response_empty_produces_synthetic_message() {
        let data = vec![0u8]; // just terminator
        let fields = parse_error_response(&data);
        assert!(
            !fields.message.is_empty(),
            "empty error response should produce synthetic message"
        );
        assert!(fields.message.contains("malformed"));
    }

    #[test]
    fn error_response_code_only_no_message() {
        let mut data = Vec::new();
        data.push(b'C');
        data.extend_from_slice(b"42P01\0");
        data.push(0);
        let fields = parse_error_response(&data);
        assert!(
            !fields.message.is_empty(),
            "missing message should produce synthetic"
        );
        assert!(fields.message.contains("42P01"));
    }

    #[test]
    fn copy_in_response_parsed() {
        // Valid CopyInResponse: text format, 2 columns both text
        let payload = [0u8, 0, 2, 0, 0, 0, 0]; // format=0(text), num_cols=2, col_fmt=0, col_fmt=0
        let result = parse_backend_message(b'G', &payload);
        assert!(result.is_ok());
        match result.unwrap() {
            BackendMessage::CopyInResponse {
                format,
                column_formats,
            } => {
                assert_eq!(format, 0);
                assert_eq!(column_formats.as_slice(), &[0u16, 0]);
            }
            other => panic!("expected CopyInResponse, got: {other:?}"),
        }
    }

    #[test]
    fn copy_in_response_too_short() {
        let result = parse_backend_message(b'G', &[]);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("too short"));
    }

    #[test]
    fn copy_out_response_parsed() {
        let payload = [0u8, 0, 1, 0, 0]; // format=0, 1 column, text format
        let result = parse_backend_message(b'H', &payload);
        assert!(result.is_ok());
        match result.unwrap() {
            BackendMessage::CopyOutResponse {
                format,
                column_formats,
            } => {
                assert_eq!(format, 0);
                assert_eq!(column_formats.as_slice(), &[0u16]);
            }
            other => panic!("expected CopyOutResponse, got: {other:?}"),
        }
    }

    #[test]
    fn copy_out_response_too_short() {
        let result = parse_backend_message(b'H', &[]);
        assert!(result.is_err());
    }

    #[test]
    fn copy_both_response_rejected() {
        let result = parse_backend_message(b'W', &[]);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("COPY BOTH protocol not supported"));
    }

    #[test]
    fn copy_data_parsed() {
        let result = parse_backend_message(b'd', b"hello\tworld\n");
        assert!(result.is_ok());
        match result.unwrap() {
            BackendMessage::CopyData { data } => {
                assert_eq!(data, b"hello\tworld\n");
            }
            other => panic!("expected CopyData, got: {other:?}"),
        }
    }

    #[test]
    fn copy_data_empty() {
        let result = parse_backend_message(b'd', &[]);
        assert!(result.is_ok());
        match result.unwrap() {
            BackendMessage::CopyData { data } => assert!(data.is_empty()),
            other => panic!("expected CopyData, got: {other:?}"),
        }
    }

    #[test]
    fn copy_done_parsed() {
        let result = parse_backend_message(b'c', &[]);
        assert!(result.is_ok());
        assert!(matches!(result.unwrap(), BackendMessage::CopyDone));
    }

    // --- Audit gap tests ---

    // #28: Auth type 3 (cleartext) parse
    #[test]
    fn auth_cleartext_parses() {
        let payload = 3i32.to_be_bytes();
        let msg = parse_backend_message(b'R', &payload).unwrap();
        assert!(matches!(msg, BackendMessage::AuthCleartext));
    }

    // #29: Auth unsupported type (e.g. type=7) error
    #[test]
    fn auth_unsupported_type_error() {
        let payload = 7i32.to_be_bytes();
        let result = parse_backend_message(b'R', &payload);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("unsupported authentication method (type 7)"),
            "unexpected error: {err}"
        );
        assert!(
            err.contains("bsql supports: cleartext (3), MD5 (5), SCRAM-SHA-256 (10)"),
            "missing supported methods list: {err}"
        );
        assert!(
            err.contains("Your server requires method 7"),
            "missing server method hint: {err}"
        );
    }

    // Auth unsupported type=2 (Kerberos) shows helpful message
    #[test]
    fn auth_unsupported_type_2_kerberos() {
        let payload = 2i32.to_be_bytes();
        let result = parse_backend_message(b'R', &payload);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("unsupported authentication method (type 2)"),
            "unexpected error: {err}"
        );
        assert!(
            err.contains("GSSAPI, SSPI, or certificate auth"),
            "missing fallback hint: {err}"
        );
    }

    // #30: Auth message too short
    #[test]
    fn auth_message_too_short() {
        let result = parse_backend_message(b'R', &[0, 0]);
        assert!(result.is_err());
    }

    // #31: BackendKeyData parse
    #[test]
    fn backend_key_data_parses() {
        let mut payload = Vec::new();
        payload.extend_from_slice(&1234i32.to_be_bytes());
        payload.extend_from_slice(&5678i32.to_be_bytes());
        let msg = parse_backend_message(b'K', &payload).unwrap();
        match msg {
            BackendMessage::BackendKeyData { pid, secret } => {
                assert_eq!(pid, 1234);
                assert_eq!(secret, 5678);
            }
            _ => panic!("expected BackendKeyData"),
        }
    }

    // #32: BackendKeyData too short
    #[test]
    fn backend_key_data_too_short() {
        let result = parse_backend_message(b'K', &[0, 0, 0]);
        assert!(result.is_err());
    }

    // #33: ReadyForQuery empty payload error
    #[test]
    fn ready_for_query_empty_error() {
        let result = parse_backend_message(b'Z', &[]);
        assert!(result.is_err());
    }

    // #34: RowDescription with 0 fields
    #[test]
    fn row_description_zero_fields() {
        let data = 0i16.to_be_bytes();
        let cols = parse_row_description(&data).unwrap();
        assert!(cols.is_empty());
    }

    // #35: RowDescription truncated
    #[test]
    fn row_description_truncated_error() {
        // Says 1 field but has no data for the field
        let mut data = Vec::new();
        data.extend_from_slice(&1i16.to_be_bytes());
        let result = parse_row_description(&data);
        assert!(result.is_err(), "truncated row description should error");
    }

    // #36: RowDescription negative field count (already tested, confirming)
    #[test]
    fn row_description_negative_field_count_standalone() {
        let data = (-5i16).to_be_bytes();
        let result = parse_row_description(&data);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("negative"), "should mention negative: {err}");
    }

    // #37: RowDescription excessive field count
    #[test]
    fn row_description_excessive_field_count_standalone() {
        let data = 2001i16.to_be_bytes();
        let result = parse_row_description(&data);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("2000"), "should mention max 2000: {err}");
    }

    // #38: Notification parse
    #[test]
    fn notification_parses() {
        let mut payload = Vec::new();
        payload.extend_from_slice(&42i32.to_be_bytes()); // pid
        payload.extend_from_slice(b"my_channel\0"); // channel
        payload.extend_from_slice(b"hello\0"); // payload
        let msg = parse_backend_message(b'A', &payload).unwrap();
        match msg {
            BackendMessage::NotificationResponse {
                pid,
                channel,
                payload,
            } => {
                assert_eq!(pid, 42);
                assert_eq!(channel, "my_channel");
                assert_eq!(payload, "hello");
            }
            _ => panic!("expected NotificationResponse"),
        }
    }

    // #39: Notification too short
    #[test]
    fn notification_too_short_error() {
        let result = parse_backend_message(b'A', &[0, 0]);
        assert!(result.is_err());
    }

    // #40: EmptyQuery response parse
    #[test]
    fn empty_query_response_parses() {
        let msg = parse_backend_message(b'I', &[]).unwrap();
        assert!(matches!(msg, BackendMessage::EmptyQuery));
    }

    // #41: NoData response parse
    #[test]
    fn no_data_response_parses() {
        let msg = parse_backend_message(b'n', &[]).unwrap();
        assert!(matches!(msg, BackendMessage::NoData));
    }

    // #42: CopyInResponse proper error message
    #[test]
    fn copy_in_response_error_message() {
        let result = parse_backend_message(b'G', &[]);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("CopyInResponse"),
            "should name CopyInResponse: {err}"
        );
    }

    // #43: CopyOutResponse proper error message
    #[test]
    fn copy_out_response_error_message() {
        let result = parse_backend_message(b'H', &[]);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("CopyOutResponse"),
            "should name CopyOutResponse: {err}"
        );
    }

    // #44: Command tag "CREATE TABLE" -> 0 affected rows
    #[test]
    fn command_tag_create_table_zero_rows() {
        assert_eq!(parse_command_tag("CREATE TABLE"), 0);
    }

    // #46: NULL parameter in write_bind_params
    #[test]
    fn bind_null_param() {
        let mut buf = Vec::new();
        let val: Option<i32> = None;
        let params: Vec<&(dyn crate::codec::Encode + Sync)> = vec![&val];
        write_bind_params(&mut buf, b"", b"s_test", &params);
        assert_eq!(buf[0], b'B');
        // The bind message should contain -1 length for the NULL param
        // We verify the message is well-formed and starts with 'B'
    }

    // Test for error response Display formatting
    #[test]
    fn error_fields_display_with_detail_and_hint() {
        let fields = ErrorFields {
            code: *b"23505",
            message: "duplicate key".to_owned(),
            detail: Some("key already exists".to_owned()),
            hint: Some("use ON CONFLICT".to_owned()),
            position: None,
        };
        let display = fields.to_string();
        assert!(display.contains("[23505]"));
        assert!(display.contains("duplicate key"));
        assert!(display.contains("DETAIL: key already exists"));
        assert!(display.contains("HINT: use ON CONFLICT"));
    }

    // Test for error response Display without detail/hint
    #[test]
    fn error_fields_display_without_extras() {
        let fields = ErrorFields {
            code: *b"42P01",
            message: "relation does not exist".to_owned(),
            detail: None,
            hint: None,
            position: None,
        };
        let display = fields.to_string();
        assert_eq!(display, "[42P01] relation does not exist");
    }

    // Flush message format
    #[test]
    fn flush_message_format() {
        let mut buf = Vec::new();
        write_flush(&mut buf);
        assert_eq!(buf, &[b'H', 0, 0, 0, 4]);
    }

    // Password message format
    #[test]
    fn password_message_format() {
        let mut buf = Vec::new();
        write_password(&mut buf, b"secret\0");
        assert_eq!(buf[0], b'p');
    }

    // SASL initial response format
    #[test]
    fn sasl_initial_response_format() {
        let mut buf = Vec::new();
        write_sasl_initial(&mut buf, "SCRAM-SHA-256", b"n,,n=user,r=nonce");
        assert_eq!(buf[0], b'p');
    }

    // SASL response format
    #[test]
    fn sasl_response_format() {
        let mut buf = Vec::new();
        write_sasl_response(&mut buf, b"client-final-message");
        assert_eq!(buf[0], b'p');
    }

    // AuthSasl parse
    #[test]
    fn auth_sasl_parses() {
        let mut payload = 10i32.to_be_bytes().to_vec();
        payload.extend_from_slice(b"SCRAM-SHA-256\0\0");
        let msg = parse_backend_message(b'R', &payload).unwrap();
        match msg {
            BackendMessage::AuthSasl { mechanisms } => {
                assert!(!mechanisms.is_empty());
            }
            _ => panic!("expected AuthSasl"),
        }
    }

    // AuthSaslContinue parse
    #[test]
    fn auth_sasl_continue_parses() {
        let mut payload = 11i32.to_be_bytes().to_vec();
        payload.extend_from_slice(b"server-first-data");
        let msg = parse_backend_message(b'R', &payload).unwrap();
        assert!(matches!(msg, BackendMessage::AuthSaslContinue { .. }));
    }

    // AuthSaslFinal parse
    #[test]
    fn auth_sasl_final_parses() {
        let mut payload = 12i32.to_be_bytes().to_vec();
        payload.extend_from_slice(b"v=signature");
        let msg = parse_backend_message(b'R', &payload).unwrap();
        assert!(matches!(msg, BackendMessage::AuthSaslFinal { .. }));
    }

    // --- Task 1: CancelRequest ---

    #[test]
    fn cancel_request_format() {
        let mut buf = Vec::new();
        write_cancel_request(&mut buf, 1234, 5678);
        assert_eq!(buf.len(), 16);
        let len = i32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]]);
        assert_eq!(len, 16);
        let code = i32::from_be_bytes([buf[4], buf[5], buf[6], buf[7]]);
        assert_eq!(code, CANCEL_REQUEST_CODE);
        let pid = i32::from_be_bytes([buf[8], buf[9], buf[10], buf[11]]);
        assert_eq!(pid, 1234);
        let secret = i32::from_be_bytes([buf[12], buf[13], buf[14], buf[15]]);
        assert_eq!(secret, 5678);
    }

    // --- Task 4: Position field ---

    #[test]
    fn error_response_parses_position() {
        let mut data = Vec::new();
        data.push(b'S');
        data.extend_from_slice(b"ERROR\0");
        data.push(b'C');
        data.extend_from_slice(b"42601\0");
        data.push(b'M');
        data.extend_from_slice(b"syntax error at or near \"SELEC\"\0");
        data.push(b'P');
        data.extend_from_slice(b"8\0");
        data.push(0);

        let fields = parse_error_response(&data);
        assert_eq!(fields.position, Some(8));
    }

    #[test]
    fn error_response_no_position() {
        let mut data = Vec::new();
        data.push(b'S');
        data.extend_from_slice(b"ERROR\0");
        data.push(b'C');
        data.extend_from_slice(b"42P01\0");
        data.push(b'M');
        data.extend_from_slice(b"table does not exist\0");
        data.push(0);

        let fields = parse_error_response(&data);
        assert_eq!(fields.position, None);
    }

    #[test]
    fn error_response_invalid_position_ignored() {
        let mut data = Vec::new();
        data.push(b'S');
        data.extend_from_slice(b"ERROR\0");
        data.push(b'C');
        data.extend_from_slice(b"42601\0");
        data.push(b'M');
        data.extend_from_slice(b"syntax error\0");
        data.push(b'P');
        data.extend_from_slice(b"notanumber\0");
        data.push(0);

        let fields = parse_error_response(&data);
        assert_eq!(fields.position, None);
    }

    #[test]
    fn error_fields_display_with_position() {
        let fields = ErrorFields {
            code: *b"42601",
            message: "syntax error".to_owned(),
            detail: None,
            hint: None,
            position: Some(8),
        };
        let display = fields.to_string();
        assert!(display.contains("(at position 8)"));
    }

    // --- Audit: parse_row_description rejects huge field count ---

    #[test]
    fn audit_row_description_huge_field_count() {
        let data = 2001i16.to_be_bytes();
        let result = parse_row_description(&data);
        assert!(result.is_err());
        let msg = format!("{}", result.unwrap_err());
        assert!(msg.contains("exceeds maximum"));
    }

    // --- Audit: parse_backend_message handles COPY protocol ---

    #[test]
    fn backend_message_copy_in_truncated() {
        // Truncated payload should return error
        let result = parse_backend_message(b'G', &[0]);
        assert!(result.is_err());
        let msg = format!("{}", result.unwrap_err());
        assert!(msg.contains("too short"));
    }

    #[test]
    fn backend_message_copy_out_truncated() {
        let result = parse_backend_message(b'H', &[0, 0]);
        assert!(result.is_err());
    }

    // --- Audit: parse_command_tag handles empty and weird tags ---

    #[test]
    fn parse_command_tag_empty() {
        assert_eq!(parse_command_tag(""), 0);
    }

    #[test]
    fn parse_command_tag_no_number() {
        assert_eq!(parse_command_tag("BEGIN"), 0);
    }

    #[test]
    fn parse_command_tag_insert() {
        assert_eq!(parse_command_tag("INSERT 0 5"), 5);
    }

    #[test]
    fn parse_command_tag_bytes_empty() {
        assert_eq!(parse_command_tag_bytes(&[]), 0);
    }

    #[test]
    fn parse_command_tag_bytes_nul_terminated() {
        assert_eq!(parse_command_tag_bytes(b"UPDATE 3\0"), 3);
    }

    // --- Audit: parse_auth rejects short payloads ---

    #[test]
    fn parse_auth_too_short() {
        let result = parse_backend_message(b'R', &[0, 0]);
        assert!(result.is_err());
    }

    // --- Audit: parse_simple_data_row rejects negative column count ---

    #[test]
    fn simple_data_row_negative_col_count() {
        let data = (-1i16).to_be_bytes();
        let result = parse_simple_data_row(&data);
        assert!(result.is_err());
    }

    // --- Audit: read_cstring rejects offset beyond data ---

    #[test]
    fn read_cstring_offset_beyond_data() {
        let result = read_cstring(b"hello\0", 100);
        assert!(result.is_err());
    }

    #[test]
    fn read_cstring_no_nul_terminator() {
        let result = read_cstring(b"hello", 0);
        assert!(result.is_err());
    }

    // --- Audit: parse_parameter_description rejects negative count ---

    #[test]
    fn parameter_description_negative_count() {
        let data = (-1i16).to_be_bytes();
        let result = parse_parameter_description(&data);
        assert!(result.is_err());
    }

    // --- Audit: unknown backend message type ---

    #[test]
    fn unknown_backend_message_type() {
        let result = parse_backend_message(0xFF, &[]);
        assert!(result.is_err());
        let msg = format!("{}", result.unwrap_err());
        assert!(msg.contains("unknown backend message type"));
    }

    // --- Gap: error response with only severity field ---

    #[test]
    fn error_response_only_severity() {
        let mut data = Vec::new();
        data.push(b'S');
        data.extend_from_slice(b"FATAL\0");
        data.push(0); // terminator

        let fields = parse_error_response(&data);
        // No code or message fields, so message should be synthetic
        assert!(!fields.message.is_empty());
        assert!(fields.message.contains("malformed"));
        assert_eq!(&fields.code, b"     ");
        assert!(fields.detail.is_none());
        assert!(fields.hint.is_none());
        assert!(fields.position.is_none());
    }

    // --- Gap: error response completely empty data (zero bytes) ---

    #[test]
    fn error_response_empty_data_zero_bytes() {
        let data: Vec<u8> = Vec::new();
        let fields = parse_error_response(&data);
        // Should not panic, should produce synthetic message
        assert!(!fields.message.is_empty());
        assert!(fields.message.contains("malformed"));
    }

    // --- Gap: individual parse_command_tag tests ---

    #[test]
    fn parse_command_tag_update_standalone() {
        assert_eq!(parse_command_tag("UPDATE 10"), 10);
    }

    #[test]
    fn parse_command_tag_delete_standalone() {
        assert_eq!(parse_command_tag("DELETE 3"), 3);
    }

    #[test]
    fn parse_command_tag_select_standalone() {
        assert_eq!(parse_command_tag("SELECT 100"), 100);
    }

    // --- Gap: parse_command_tag_bytes individual variants ---

    #[test]
    fn parse_command_tag_bytes_insert_standalone() {
        assert_eq!(parse_command_tag_bytes(b"INSERT 0 5\0"), 5);
    }

    #[test]
    fn parse_command_tag_bytes_update_standalone() {
        assert_eq!(parse_command_tag_bytes(b"UPDATE 10\0"), 10);
    }

    #[test]
    fn parse_command_tag_bytes_delete_standalone() {
        assert_eq!(parse_command_tag_bytes(b"DELETE 3\0"), 3);
    }

    #[test]
    fn parse_command_tag_bytes_select_standalone() {
        assert_eq!(parse_command_tag_bytes(b"SELECT 100\0"), 100);
    }

    // --- Gap: ParameterDescription valid parse ---

    #[test]
    fn parameter_description_valid_two_params() {
        let mut data = Vec::new();
        data.extend_from_slice(&2i16.to_be_bytes()); // 2 params
        data.extend_from_slice(&23u32.to_be_bytes()); // int4
        data.extend_from_slice(&25u32.to_be_bytes()); // text
        let oids = parse_parameter_description(&data).unwrap();
        assert_eq!(oids, vec![23, 25]);
    }

    #[test]
    fn parameter_description_zero_params() {
        let data = 0i16.to_be_bytes();
        let oids = parse_parameter_description(&data).unwrap();
        assert!(oids.is_empty());
    }

    #[test]
    fn parameter_description_truncated() {
        let mut data = Vec::new();
        data.extend_from_slice(&2i16.to_be_bytes()); // says 2 params
        data.extend_from_slice(&23u32.to_be_bytes()); // only 1 param worth of data
        let result = parse_parameter_description(&data);
        assert!(result.is_err());
    }

    // --- Gap: simple_data_row edge cases ---

    #[test]
    fn simple_data_row_null_value() {
        let mut data = Vec::new();
        data.extend_from_slice(&1i16.to_be_bytes()); // 1 column
        data.extend_from_slice(&(-1i32).to_be_bytes()); // NULL
        let row = parse_simple_data_row(&data).unwrap();
        assert_eq!(row, vec![None]);
    }

    #[test]
    fn simple_data_row_one_text_value() {
        let mut data = Vec::new();
        data.extend_from_slice(&1i16.to_be_bytes()); // 1 column
        data.extend_from_slice(&5i32.to_be_bytes()); // 5 bytes
        data.extend_from_slice(b"hello");
        let row = parse_simple_data_row(&data).unwrap();
        assert_eq!(row, vec![Some("hello".to_owned())]);
    }

    #[test]
    fn simple_data_row_truncated_value() {
        let mut data = Vec::new();
        data.extend_from_slice(&1i16.to_be_bytes()); // 1 column
        data.extend_from_slice(&100i32.to_be_bytes()); // says 100 bytes
        data.extend_from_slice(b"short"); // only 5 bytes
        let result = parse_simple_data_row(&data);
        assert!(result.is_err());
    }

    // --- Gap: multiple fields in RowDescription ---

    #[test]
    fn row_description_two_fields() {
        let mut data = Vec::new();
        data.extend_from_slice(&2i16.to_be_bytes()); // 2 fields

        // Field 1: "id" int4
        data.extend_from_slice(b"id\0");
        data.extend_from_slice(&0u32.to_be_bytes()); // table OID
        data.extend_from_slice(&0i16.to_be_bytes()); // column attr
        data.extend_from_slice(&23u32.to_be_bytes()); // type OID (int4)
        data.extend_from_slice(&4i16.to_be_bytes()); // type size
        data.extend_from_slice(&(-1i32).to_be_bytes()); // type mod
        data.extend_from_slice(&1i16.to_be_bytes()); // format

        // Field 2: "name" text
        data.extend_from_slice(b"name\0");
        data.extend_from_slice(&0u32.to_be_bytes());
        data.extend_from_slice(&0i16.to_be_bytes());
        data.extend_from_slice(&25u32.to_be_bytes()); // text
        data.extend_from_slice(&(-1i16).to_be_bytes()); // variable
        data.extend_from_slice(&(-1i32).to_be_bytes());
        data.extend_from_slice(&0i16.to_be_bytes()); // text format

        let cols = parse_row_description(&data).unwrap();
        assert_eq!(cols.len(), 2);
        assert_eq!(&*cols[0].name, "id");
        assert_eq!(cols[0].type_oid, 23);
        assert_eq!(&*cols[1].name, "name");
        assert_eq!(cols[1].type_oid, 25);
    }

    // --- COPY frontend message writer tests ---

    #[test]
    fn write_copy_data_message() {
        let mut buf = Vec::new();
        write_copy_data(&mut buf, b"hello\tworld\n");
        assert_eq!(buf[0], b'd');
        let len = i32::from_be_bytes([buf[1], buf[2], buf[3], buf[4]]);
        assert_eq!(len, 4 + 12); // 4 + "hello\tworld\n".len()
        assert_eq!(&buf[5..], b"hello\tworld\n");
    }

    #[test]
    fn write_copy_data_empty() {
        let mut buf = Vec::new();
        write_copy_data(&mut buf, &[]);
        assert_eq!(buf[0], b'd');
        let len = i32::from_be_bytes([buf[1], buf[2], buf[3], buf[4]]);
        assert_eq!(len, 4);
        assert_eq!(buf.len(), 5);
    }

    #[test]
    fn write_copy_done_message() {
        let mut buf = Vec::new();
        write_copy_done(&mut buf);
        assert_eq!(buf, &[b'c', 0, 0, 0, 4]);
    }

    // --- quote_ident tests ---

    #[test]
    fn quote_ident_simple() {
        assert_eq!(quote_ident("users"), r#""users""#);
    }

    #[test]
    fn quote_ident_with_embedded_quotes() {
        assert_eq!(quote_ident(r#"my"table"#), r#""my""table""#);
    }

    #[test]
    fn quote_ident_empty() {
        assert_eq!(quote_ident(""), r#""""#);
    }

    #[test]
    fn quote_ident_with_spaces() {
        assert_eq!(quote_ident("my table"), r#""my table""#);
    }

    #[test]
    fn copy_in_response_truncated_columns() {
        // Says 3 columns but only provides data for 1
        let payload = [0u8, 0, 3, 0, 0];
        let result = parse_backend_message(b'G', &payload);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("truncated"));
    }

    #[test]
    fn copy_out_response_truncated_columns() {
        let payload = [0u8, 0, 3, 0, 0];
        let result = parse_backend_message(b'H', &payload);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("truncated"));
    }

    // --- Gap: write_startup with multiple extra params ---

    #[test]
    fn startup_message_with_multiple_extra_params() {
        let mut buf = Vec::new();
        write_startup(
            &mut buf,
            "testuser",
            "testdb",
            &[
                ("statement_timeout", "30s"),
                ("application_name", "bsql_test"),
            ],
        );

        let len = i32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]]);
        assert_eq!(len as usize, buf.len());

        let payload = &buf[8..];
        let payload_str = String::from_utf8_lossy(payload);
        assert!(
            payload_str.contains("statement_timeout"),
            "should contain statement_timeout"
        );
        assert!(payload_str.contains("30s"), "should contain timeout value");
        assert!(
            payload_str.contains("application_name"),
            "should contain application_name"
        );
        assert!(
            payload_str.contains("bsql_test"),
            "should contain app name value"
        );
        assert_eq!(*buf.last().unwrap(), 0); // trailing NUL
    }

    // --- Gap: parse_simple_data_row with zero columns ---

    #[test]
    fn simple_data_row_zero_columns() {
        let data = 0i16.to_be_bytes();
        let row = parse_simple_data_row(&data).unwrap();
        assert!(row.is_empty());
    }

    // --- Gap: parse_simple_data_row with multiple columns ---

    #[test]
    fn simple_data_row_multiple_columns() {
        let mut data = Vec::new();
        data.extend_from_slice(&3i16.to_be_bytes()); // 3 columns
                                                     // col 0: "foo"
        data.extend_from_slice(&3i32.to_be_bytes());
        data.extend_from_slice(b"foo");
        // col 1: NULL
        data.extend_from_slice(&(-1i32).to_be_bytes());
        // col 2: "bar"
        data.extend_from_slice(&3i32.to_be_bytes());
        data.extend_from_slice(b"bar");

        let row = parse_simple_data_row(&data).unwrap();
        assert_eq!(row.len(), 3);
        assert_eq!(row[0], Some("foo".to_owned()));
        assert_eq!(row[1], None);
        assert_eq!(row[2], Some("bar".to_owned()));
    }

    // --- Gap: parse_simple_data_row truncated column header ---

    #[test]
    fn simple_data_row_truncated_column_header() {
        let mut data = Vec::new();
        data.extend_from_slice(&2i16.to_be_bytes()); // 2 columns
        data.extend_from_slice(&3i32.to_be_bytes());
        data.extend_from_slice(b"foo");
        // Second column: missing length bytes
        data.push(0); // only 1 byte, need 4
        let result = parse_simple_data_row(&data);
        assert!(result.is_err());
    }

    // --- Gap: parse_simple_data_row too short ---

    #[test]
    fn simple_data_row_too_short() {
        let result = parse_simple_data_row(&[0]);
        assert!(result.is_err());
    }

    // --- Gap: CopyInResponse with negative column count ---

    #[test]
    fn copy_in_response_negative_col_count() {
        let mut payload = Vec::new();
        payload.push(0); // format
        payload.extend_from_slice(&(-1i16).to_be_bytes()); // negative column count
        let result = parse_backend_message(b'G', &payload);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("negative"));
    }

    // --- Gap: CopyOutResponse with negative column count ---

    #[test]
    fn copy_out_response_negative_col_count() {
        let mut payload = Vec::new();
        payload.push(0); // format
        payload.extend_from_slice(&(-1i16).to_be_bytes()); // negative column count
        let result = parse_backend_message(b'H', &payload);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("negative"));
    }

    mod proptest_fuzz {
        use super::*;
        use proptest::prelude::*;

        proptest! {
            #[test]
            fn parse_backend_message_never_panics(msg_type: u8, payload in proptest::collection::vec(any::<u8>(), 0..1024)) {
                // Must never panic — only return Ok or Err
                let _ = parse_backend_message(msg_type, &payload);
            }

            #[test]
            fn parse_error_response_never_panics(data in proptest::collection::vec(any::<u8>(), 0..1024)) {
                let _ = parse_error_response(&data);
            }

            #[test]
            fn parse_command_tag_never_panics(tag in ".*") {
                let _ = parse_command_tag(&tag);
            }

            #[test]
            fn parse_command_tag_bytes_never_panics(data in proptest::collection::vec(any::<u8>(), 0..256)) {
                let _ = parse_command_tag_bytes(&data);
            }

            #[test]
            fn parse_simple_data_row_never_panics(data in proptest::collection::vec(any::<u8>(), 0..4096)) {
                let _ = parse_simple_data_row(&data);
            }
        }
    }
}