neotales-dbapi 0.0.0-alpha.0

Driver-neutral SQL execution with asynchronous and synchronous row access
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
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
//! Driver-neutral async SQL execution and detached, bounded row access.
//!
//! This crate deliberately provides no query builder, ORM, runtime, or driver. A provider owns
//! connection acquisition, type conversion, transaction behavior, and cleanup. All operations use
//! boxed `Send` futures so the traits are object safe. This contract cannot directly wrap browser
//! APIs whose futures are `!Send` (for example, D1 Workers bindings). The [`local`] module mirrors
//! these traits without a `Send` future bound for that case; it is a local-thread API, not a
//! thread-transfer grant.
//!
//! The [`sync`] module provides runtime-neutral synchronous counterparts that reuse the same rows,
//! limits, options, and errors. The optional `blocking` feature adds `blocking::BlockingPool`,
//! which drives an asynchronous pool on dedicated worker threads for callers that cannot await.
//! The `blocking` feature is not supported on wasm32. Async or local traits may be used with
//! providers that support them, but D1's local DBAPI operations currently fail closed.
//!
//! # Example
//!
//! ```no_run
//! use neotales_dbapi::{DbType, Parameter, Pool, QueryOptions, RowLimit};
//!
//! # async fn run(pool: &dyn Pool) -> Result<(), neotales_dbapi::DbError> {
//! let mut session = pool.acquire_async().await?;
//! let params = [Parameter::new(DbType::I64, neotales_dbapi::Value::I64(7))?];
//! // SQLite uses `?`; PostgreSQL uses `$1`. The provider does not rewrite SQL.
//! let mut reader = session.fetch_async("SELECT name FROM users WHERE id = ?",
//!     QueryOptions::new().bind_all(&params).limit(RowLimit::new(10, 4096)?)).await?;
//! while let Some(row) = reader.next_row_async().await? {
//!     if let Some(name) = row.read::<Option<String>>("name")? {
//!         println!("{name}");
//!     }
//! }
//! reader.finish_async().await?;
//! # Ok(())
//! # }
//! ```
//!
//! A reader owns one statement's result sets until it is finished or dropped. `next_row_async` yields
//! rows from the current set. `has_next_set_async` advances, discarding any unread rows in the current
//! set, and reports whether another set exists. `finish_async` drains and discards the full batch so a
//! later statement error is not hidden. A provider with unread results still attached must discard
//! the connection when a reader is dropped; a provider that materializes the bounded batch before
//! returning may already have made the connection reusable.
//!
//! The [`paging`] module adds page and row views over one existing reader. It dispatches no SQL and
//! does not change the reader's parameters, telemetry, or bounded `RowLimit`; the page view exposes
//! result-set boundaries, including empty sets. Its [`paging::multi`] submodule adds explicit
//! multi-request paging where a caller-implemented source owns each request, continuation, and
//! keyset/offset logic. [`local::paging`] mirrors both for `!Send` local futures, and
//! [`sync::paging`] provides native blocking iterators over the synchronous reader.
//!
//! Native positional placeholder syntax is preserved (SQL is never rewritten):
//!
//! ```
//! use neotales_dbapi::{DbType, Parameter, Value};
//! let sqlite_sql = "SELECT name FROM users WHERE id = ?";
//! let sqlite_parameters = [Parameter::new(DbType::I64, Value::I64(7))?];
//! let postgres_sql = "SELECT name FROM users WHERE id = $1 AND active = $2";
//! let postgres_parameters = [
//!     Parameter::new(DbType::I64, Value::I64(7))?,
//!     Parameter::new(DbType::Bool, Value::Bool(true))?,
//! ];
//! # let _ = (sqlite_sql, sqlite_parameters, postgres_sql, postgres_parameters);
//! # Ok::<(), neotales_dbapi::DbError>(())
//! ```

#[cfg(all(feature = "blocking", target_arch = "wasm32"))]
compile_error!(
    "neotales-dbapi's blocking feature is unsupported on wasm32; disable it and consult your provider's supported APIs"
);

#[cfg(all(feature = "blocking", not(target_arch = "wasm32")))]
pub mod blocking;
pub mod local;
pub mod paging;
pub mod sync;

pub use neotales_resilience::{Cancellation, Delay};
use neotales_resilience::{
    RetryOutcome, RetryPolicy as ResilienceRetryPolicy, execute_idempotent_retry,
};
use std::{
    collections::VecDeque,
    error::Error,
    fmt,
    future::{Future, poll_fn},
    panic::{AssertUnwindSafe, catch_unwind},
    pin::Pin,
    sync::{
        Arc, Mutex, Weak,
        atomic::{AtomicBool, Ordering},
    },
    task::{Context, Poll},
    time::{Duration, Instant},
};

/// A `Send` boxed future returning a sanitized database result.
pub type DbFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T, DbError>> + Send + 'a>>;

/// SQL logical type used to define typed null parameters and expected row values.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DbType {
    /// Boolean.
    Bool,
    /// Signed 64-bit integer.
    I64,
    /// 64-bit floating-point number.
    F64,
    /// UTF-8 text.
    Text,
    /// Opaque bytes.
    Bytes,
}

/// A supported, owned parameter or cell value.
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
    /// Null with an explicit SQL type.
    Null(DbType),
    /// Boolean.
    Bool(bool),
    /// Signed 64-bit integer.
    I64(i64),
    /// Finite 64-bit float.
    F64(f64),
    /// UTF-8 text.
    Text(String),
    /// Opaque bytes.
    Bytes(Vec<u8>),
}
impl Value {
    /// Rejects non-finite floating-point values; providers must preserve all other variants exactly.
    ///
    /// # Errors
    /// Returns `InvalidArgument` for NaN or infinity.
    pub fn validate(&self) -> Result<(), DbError> {
        if matches!(self, Self::F64(value) if !value.is_finite()) {
            Err(DbError::invalid(InvalidArgumentReason::NonFiniteFloat))
        } else {
            Ok(())
        }
    }
}

/// A positional parameter whose value must match its declared type exactly.
#[derive(Clone, Debug, PartialEq)]
pub struct Parameter {
    db_type: DbType,
    value: Value,
}
impl Parameter {
    /// Creates and validates one positional parameter without coercion.
    ///
    /// # Errors
    /// Returns `InvalidArgument` for a type mismatch or non-finite float.
    pub fn new(db_type: DbType, value: Value) -> Result<Self, DbError> {
        let matches = matches!((&value, db_type), (Value::Null(t), expected) if *t == expected)
            || matches!(
                (&value, db_type),
                (Value::Bool(_), DbType::Bool)
                    | (Value::I64(_), DbType::I64)
                    | (Value::F64(_), DbType::F64)
                    | (Value::Text(_), DbType::Text)
                    | (Value::Bytes(_), DbType::Bytes)
            );
        if !matches {
            return Err(DbError::invalid(
                InvalidArgumentReason::ParameterTypeMismatch,
            ));
        }
        value.validate()?;
        Ok(Self { db_type, value })
    }
    /// Returns the declared SQL type.
    #[must_use]
    pub const fn db_type(&self) -> DbType {
        self.db_type
    }
    /// Returns the validated owned value.
    #[must_use]
    pub fn value(&self) -> &Value {
        &self.value
    }
}

/// Reported provider capabilities; true means the operation is supported as described.
#[allow(clippy::struct_excessive_bools)] // Independent capability flags must be directly reportable.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Capabilities {
    /// Interactive transactions on one acquired connection.
    pub transactions: bool,
    /// Requested portable isolation levels can be applied.
    pub isolation_level: bool,
    /// Read-only transactions can be requested.
    pub read_only_transactions: bool,
    /// Trusted script execution is supported.
    pub trusted_scripts: bool,
}

/// Portable transaction isolation level.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IsolationLevel {
    /// Serializable isolation.
    Serializable,
    /// Repeatable-read isolation.
    RepeatableRead,
    /// Read-committed isolation.
    ReadCommitted,
    /// Read-uncommitted isolation.
    ReadUncommitted,
}
/// Portable transaction options; omitted values retain provider-defined defaults.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct TxOptions {
    /// Requested isolation; unsupported requests fail before beginning.
    pub isolation: Option<IsolationLevel>,
    /// Requested read-only/access mode; unsupported requests fail before beginning.
    pub read_only: Option<bool>,
}
/// Statement execution options.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ExecuteOptions {
    /// Provider-defined timeout in milliseconds; `None` means no requested timeout.
    pub timeout_ms: Option<u64>,
}

/// Per-query inherit/off/on setting for telemetry or SQL capture.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum QuerySetting {
    /// Use the configured pool default.
    #[default]
    Inherit,
    /// Disable this query's setting, overriding an enabled pool default.
    Off,
    /// Enable this query's setting, overriding a disabled pool default.
    On,
}

/// Whether a standalone pool operation may use the configured retry policy.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ReplaySafety {
    /// Do not retry. SQL shape is never used to infer safety.
    #[default]
    Never,
    /// The caller asserts that repeating this standalone operation is safe.
    ExplicitlyReplaySafe,
}

/// Pool retry configuration backed by `neotales-resilience`.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum RetryPolicy {
    /// Execute once. This is the default.
    #[default]
    Disabled,
    /// Retry explicitly replay-safe standalone statements, up to the configured attempt cap.
    Enabled {
        /// Existing resilience retry policy, including bounded attempts and delay shape.
        policy: ResilienceRetryPolicy,
        /// Whether sanitized `Timeout` errors are retry candidates.
        retry_timeouts: bool,
    },
}
impl RetryPolicy {
    /// Builds an enabled retry policy using the resilience crate's bounded retry configuration.
    ///
    /// `retry_timeouts` is off unless explicitly requested. Busy and Connection failures are
    /// candidates by default, but retries still require [`ReplaySafety::ExplicitlyReplaySafe`].
    ///
    /// # Errors
    /// Returns [`InvalidRetryPolicy`] when `max_attempts` is zero.
    pub fn new(
        max_attempts: u32,
        delay: Delay,
        retry_timeouts: bool,
    ) -> Result<Self, InvalidRetryPolicy> {
        let policy =
            ResilienceRetryPolicy::new(max_attempts, delay).map_err(|_| InvalidRetryPolicy)?;
        Ok(Self::Enabled {
            policy,
            retry_timeouts,
        })
    }
}

/// Retry configuration was invalid.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct InvalidRetryPolicy;
impl fmt::Display for InvalidRetryPolicy {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("retry policy requires at least one attempt")
    }
}
impl Error for InvalidRetryPolicy {}

/// Per-operation retry configuration override.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum RetrySetting {
    /// Use the containing pool's default.
    #[default]
    Inherit,
    /// Disable retries for this operation.
    Off,
    /// Replace the containing pool's retry policy for this operation.
    Use(RetryPolicy),
}

/// Caller-provided timer, backoff sleeper, cancellation flag, and random source for retries.
///
/// No runtime or task is created by this crate. The timer must measure the complete execution
/// budget (acquisition, attempts, and backoff), and must wake the executor when it expires.
/// Pool defaults share this value across calls; use [`QueryOptions::retry_runtime`] for a
/// cancellation token scoped to an individual operation.
pub struct RetryRuntime {
    cancellation: Arc<Cancellation>,
    timer: Arc<dyn Fn() -> RetryWait + Send + Sync>,
    sleep: Arc<dyn Fn(Duration) -> RetryWait + Send + Sync>,
    random: Arc<dyn Fn() -> u64 + Send + Sync>,
    // Only operation-local copies carry this. Weak ownership prevents retained provider options
    // from extending the operation's lifetime or imposing its gate on later independent calls.
    dispatch: Option<Weak<RetryDispatch>>,
}
impl Clone for RetryRuntime {
    fn clone(&self) -> Self {
        Self {
            cancellation: Arc::clone(&self.cancellation),
            timer: Arc::clone(&self.timer),
            sleep: Arc::clone(&self.sleep),
            random: Arc::clone(&self.random),
            dispatch: self.dispatch.clone(),
        }
    }
}

/// A boxed sendable caller-owned timer or sleeper future.
pub type RetryWait = Pin<Box<dyn Future<Output = ()> + Send>>;

impl RetryRuntime {
    /// Creates retry runtime callbacks. For an enabled, explicitly replay-safe operation the timer
    /// factory is called exactly once, before acquisition, even if the operation succeeds immediately.
    /// The sleeper is called for each backoff. Use a pending timer when no total deadline is desired.
    pub fn new(
        cancellation: Arc<Cancellation>,
        timer: impl Fn() -> RetryWait + Send + Sync + 'static,
        sleep: impl Fn(Duration) -> RetryWait + Send + Sync + 'static,
        random: impl Fn() -> u64 + Send + Sync + 'static,
    ) -> Self {
        Self {
            cancellation,
            timer: Arc::new(timer),
            sleep: Arc::new(sleep),
            random: Arc::new(random),
            dispatch: None,
        }
    }
}

impl fmt::Debug for RetryRuntime {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("RetryRuntime")
            .field("cancelled", &self.cancellation.is_cancelled())
            .finish_non_exhaustive()
    }
}

/// Sanitized operation outcome used by the optional observer.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TelemetryOutcome {
    /// The operation succeeded. Row contents and affected-row counts are not included.
    Success,
    /// The operation failed with a stable database error category.
    Error(ErrorKind),
}

/// Fixed-cardinality operation name; SQL is never used as a metric label.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Operation {
    /// One execute statement.
    Execute,
    /// One bounded fetch statement.
    Fetch,
}

/// Safe telemetry event: no parameters, rows, results, driver text, or connection information.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DbEvent {
    /// Fixed operation name suitable for a bounded metric label.
    pub operation: Operation,
    /// Elapsed wall time for the statement operation.
    pub duration: Duration,
    /// Sanitized result category.
    pub outcome: TelemetryOutcome,
    /// Optional SQL text when capture was explicitly enabled and the hook retained it.
    pub sql: Option<String>,
}

/// Optional SQL-only hook. Returning `None` suppresses SQL text; returning a string replaces it.
///
/// Panics are contained. A panic payload is intentionally forgotten to prevent a panicking
/// destructor from unwinding after the database operation; each hook panic may leak its payload.
pub type SqlCaptureHook = Arc<dyn Fn(&str) -> Option<String> + Send + Sync>;
/// Optional per-pool observer.
///
/// Panics are contained and do not affect database results. Because a panic payload's destructor
/// can itself panic, payloads from callback panics are intentionally forgotten; each such panic
/// may leak its payload.
pub type DbObserver = Arc<dyn Fn(&DbEvent) + Send + Sync>;

/// Pool-level defaults for telemetry, SQL capture, and retry support.
#[derive(Clone)]
pub struct PoolPolicy {
    /// Whether telemetry is enabled by default. No observer means no event is emitted.
    pub telemetry_enabled: bool,
    /// Whether exact SQL capture is enabled by default. SQL literals/comments may contain secrets.
    pub sql_capture_enabled: bool,
    /// SQL-only pool hook, called only when SQL capture is enabled.
    pub sql_hook: Option<SqlCaptureHook>,
    /// Optional observer. Its panic is isolated from the database operation.
    pub observer: Option<DbObserver>,
    /// Retry default for standalone pool `execute_async`/`fetch_async` calls; default is Disabled.
    pub retry: RetryPolicy,
    /// Caller-owned scheduling and cancellation dependencies required when retries are enabled.
    pub retry_runtime: Option<RetryRuntime>,
    /// Default statement timeout for operations that inherit. `None` inherits the wrapped
    /// provider's default; use per-query `no_timeout()` to request disabling it.
    pub default_timeout: Option<Duration>,
    /// Default bounded fetch limit for operations that inherit.
    pub default_limit: RowLimit,
}
impl Default for PoolPolicy {
    fn default() -> Self {
        let defaults = PoolDefaults::default();
        Self {
            telemetry_enabled: defaults.telemetry_enabled,
            sql_capture_enabled: defaults.sql_capture_enabled,
            sql_hook: None,
            observer: None,
            retry: defaults.retry,
            retry_runtime: None,
            default_timeout: defaults.timeout,
            default_limit: defaults.limit,
        }
    }
}
fn policy_defaults(policy: &PoolPolicy) -> PoolDefaults {
    PoolDefaults {
        timeout: policy.default_timeout,
        limit: policy.default_limit,
        telemetry_enabled: policy.telemetry_enabled,
        sql_capture_enabled: policy.sql_capture_enabled,
        retry: policy.retry,
    }
}
impl fmt::Debug for PoolPolicy {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PoolPolicy")
            .field("telemetry_enabled", &self.telemetry_enabled)
            .field("sql_capture_enabled", &self.sql_capture_enabled)
            .field("has_sql_hook", &self.sql_hook.is_some())
            .field("has_observer", &self.observer.is_some())
            .field("retry", &self.retry)
            .field("has_retry_runtime", &self.retry_runtime.is_some())
            .field("default_timeout", &self.default_timeout)
            .field("default_limit", &self.default_limit)
            .finish()
    }
}

/// Owned parameters and policy overrides for one pool, session, transaction, or script operation.
#[derive(Clone, Default)]
pub struct QueryOptions {
    /// Ordered operation-local parameters. Never inherited from pool defaults.
    pub parameters: Vec<Parameter>,
    /// Tri-state timeout override.
    pub timeout: TimeoutSetting,
    /// Tri-state bounded fetch-limit override.
    pub limit: LimitSetting,
    /// Resolved provider timeout, not a separate caller override.
    execute: ExecuteOptions,
    /// Whether telemetry is inherited, disabled, or enabled for this operation.
    pub telemetry: QuerySetting,
    /// Whether SQL capture is inherited, disabled, or enabled for this operation.
    pub sql_capture: QuerySetting,
    /// Per-query SQL hook, replacing the pool hook when present.
    pub sql_hook: Option<SqlCaptureHook>,
    /// Retry policy override for a standalone `PolicyPool` `execute_async`/`fetch_async` operation.
    pub retry: RetrySetting,
    /// Explicit caller assertion required before any retry can occur.
    pub replay_safety: ReplaySafety,
    /// Per-operation retry runtime, replacing the pool default when present.
    pub retry_runtime: Option<RetryRuntime>,
    // An enclosing policy handles telemetry and retry; callers cannot bypass validation.
    policy_applied: bool,
}
impl fmt::Debug for QueryOptions {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("QueryOptions")
            .field("parameter_count", &self.parameters.len())
            .field("timeout", &self.timeout)
            .field("limit", &self.limit)
            .field("execute", &self.execute)
            .field("telemetry", &self.telemetry)
            .field("sql_capture", &self.sql_capture)
            .field("has_sql_hook", &self.sql_hook.is_some())
            .field("retry", &self.retry)
            .field("replay_safety", &self.replay_safety)
            .field("has_retry_runtime", &self.retry_runtime.is_some())
            .finish_non_exhaustive()
    }
}
impl QueryOptions {
    /// Provider-facing execution settings after resolution.
    #[must_use]
    pub fn provider_execute_options(&self) -> ExecuteOptions {
        self.execute
    }
    /// Creates options that inherit every global default, carry no parameters, and remain
    /// non-replay-safe.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Appends one already validated parameter in positional order.
    #[must_use]
    pub fn bind(mut self, parameter: Parameter) -> Self {
        self.parameters.push(parameter);
        self
    }

    /// Appends parameters in slice order.
    #[must_use]
    pub fn bind_all(mut self, parameters: &[Parameter]) -> Self {
        self.parameters.extend_from_slice(parameters);
        self
    }

    /// Appends finite float, text, or byte parameters with exact declared types.
    ///
    /// # Errors
    /// Returns `InvalidArgument` for a non-finite float.
    pub fn bind_f64(mut self, value: f64) -> Result<Self, DbError> {
        self.parameters
            .push(Parameter::new(DbType::F64, Value::F64(value))?);
        Ok(self)
    }

    /// Appends a typed SQL null.
    #[must_use]
    pub fn bind_null(mut self, db_type: DbType) -> Self {
        self.parameters.push(Parameter {
            db_type,
            value: Value::Null(db_type),
        });
        self
    }

    /// Replaces the pool timeout for this operation.
    #[must_use]
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = TimeoutSetting::Set(timeout);
        self
    }

    /// Explicitly requests no timeout for this operation.
    #[must_use]
    pub fn no_timeout(mut self) -> Self {
        self.timeout = TimeoutSetting::Disabled;
        self
    }

    /// Replaces the pool fetch limit for this operation.
    #[must_use]
    pub fn limit(mut self, limit: RowLimit) -> Self {
        self.limit = LimitSetting::Set(limit);
        self
    }

    /// Sets telemetry inheritance/off/on.
    #[must_use]
    pub fn telemetry(mut self, setting: QuerySetting) -> Self {
        self.telemetry = setting;
        self
    }

    /// Sets SQL-capture inheritance/off/on.
    #[must_use]
    pub fn sql_capture(mut self, setting: QuerySetting) -> Self {
        self.sql_capture = setting;
        self
    }

    /// Sets retry inheritance/off/explicit policy.
    #[must_use]
    pub fn retry(mut self, setting: RetrySetting) -> Self {
        self.retry = setting;
        self
    }

    /// Replaces the per-operation SQL capture hook.
    #[must_use]
    pub fn sql_hook(mut self, hook: SqlCaptureHook) -> Self {
        self.sql_hook = Some(hook);
        self
    }

    /// Replaces the retry runtime for this operation.
    #[must_use]
    pub fn retry_runtime(mut self, runtime: RetryRuntime) -> Self {
        self.retry_runtime = Some(runtime);
        self
    }

    /// Marks the operation explicitly replay-safe. Defaults remain `Never`.
    #[must_use]
    pub fn replay_safe(mut self) -> Self {
        self.replay_safety = ReplaySafety::ExplicitlyReplaySafe;
        self
    }

    /// Resolves the timeout against pool defaults.
    #[must_use]
    pub fn resolved_timeout(&self, defaults: PoolDefaults) -> Option<Duration> {
        match self.timeout {
            TimeoutSetting::Inherit => defaults.timeout,
            TimeoutSetting::Disabled => None,
            TimeoutSetting::Set(timeout) => Some(timeout),
        }
    }

    /// Resolves the fetch limit against pool defaults.
    #[must_use]
    pub fn resolved_limit(&self, defaults: PoolDefaults) -> RowLimit {
        match self.limit {
            LimitSetting::Inherit => defaults.limit,
            LimitSetting::Set(limit) => limit,
        }
    }

    /// Resolve provider-facing timeout and reject policy options no wrapper handles.
    ///
    /// # Errors
    /// Returns `Unsupported` for policy facilities unavailable on a bare provider, or
    /// `InvalidArgument` if the timeout cannot be represented in milliseconds.
    pub fn prepare_provider(&mut self, defaults: PoolDefaults) -> Result<RowLimit, DbError> {
        if !self.policy_applied {
            reject_unhandled_pool_options(self)?;
        }
        let limit = self.resolved_limit(defaults);
        self.limit = LimitSetting::Set(limit);
        resolve_provider_timeout(self, defaults.timeout)?;
        Ok(limit)
    }

    /// Reject a fetch-only limit before dispatching a non-fetch statement.
    ///
    /// Inherited pool limits do not apply to execute operations.
    ///
    /// # Errors
    /// Returns `InvalidArgument` when a per-operation fetch limit was supplied.
    pub fn validate_execute(&self) -> Result<(), DbError> {
        if matches!(self.limit, LimitSetting::Set(_)) {
            return Err(DbError::new(ErrorKind::InvalidArgument));
        }
        Ok(())
    }

    /// Validate and resolve options for a provider's execute operation.
    ///
    /// # Errors
    /// Returns an error for an explicit fetch limit or unsupported provider settings.
    pub fn prepare_execute_provider(&mut self, defaults: PoolDefaults) -> Result<(), DbError> {
        self.validate_execute()?;
        self.prepare_provider(defaults)?;
        Ok(())
    }

    /// Reject settings that cannot apply to an unparameterized trusted script.
    ///
    /// # Errors
    /// Returns `InvalidArgument` if parameters or a fetch limit were supplied.
    pub fn validate_script(&self) -> Result<(), DbError> {
        if !self.parameters.is_empty() || !matches!(self.limit, LimitSetting::Inherit) {
            return Err(DbError::new(ErrorKind::InvalidArgument));
        }
        Ok(())
    }

    /// Resolve a trusted script's provider settings after rejecting inapplicable options.
    ///
    /// # Errors
    /// Returns an error for inapplicable or unsupported settings.
    pub fn prepare_script_provider(&mut self, defaults: PoolDefaults) -> Result<(), DbError> {
        self.validate_script()?;
        self.prepare_provider(defaults)?;
        Ok(())
    }
}

// The low-level provider boundary currently accepts millisecond timeouts. Round up so
// a positive sub-millisecond timeout never accidentally becomes an immediate timeout.
fn resolve_provider_timeout(
    options: &mut QueryOptions,
    default: Option<Duration>,
) -> Result<(), DbError> {
    let timeout = match options.timeout {
        TimeoutSetting::Inherit => default,
        TimeoutSetting::Disabled => None,
        TimeoutSetting::Set(timeout) => Some(timeout),
    };
    options.timeout = timeout.map_or(TimeoutSetting::Disabled, TimeoutSetting::Set);
    options.execute.timeout_ms = timeout
        .map(|value| {
            u64::try_from(
                value
                    .as_millis()
                    .saturating_add(u128::from(value.subsec_nanos() % 1_000_000 != 0)),
            )
            .map_err(|_| DbError::new(ErrorKind::InvalidArgument))
        })
        .transpose()?;
    Ok(())
}

// A bare pool has no observer or retry runtime. Reject requests requiring those
// facilities rather than silently dropping them at the provider boundary.
fn reject_unhandled_pool_options(options: &QueryOptions) -> Result<(), DbError> {
    if options.telemetry == QuerySetting::On
        || options.sql_capture == QuerySetting::On
        || options.sql_hook.is_some()
        || matches!(
            options.retry,
            RetrySetting::Use(RetryPolicy::Enabled { .. })
        )
        || options.retry_runtime.is_some()
    {
        return Err(DbError::new(ErrorKind::Unsupported));
    }
    Ok(())
}

fn reject_session_retry(options: &QueryOptions) -> Result<(), DbError> {
    if !options.policy_applied
        && (matches!(
            options.retry,
            RetrySetting::Use(RetryPolicy::Enabled { .. })
        ) || options.retry_runtime.is_some())
    {
        return Err(DbError::new(ErrorKind::Unsupported));
    }
    Ok(())
}

/// Hard maximums for detached fetch results.
///
/// `rows` and `bytes` are aggregate across every result set. `max_sets` also counts empty
/// sets, so a provider cannot return an unbounded number of empty boundaries.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RowLimit {
    rows: usize,
    bytes: usize,
    max_sets: usize,
}
impl RowLimit {
    /// Creates a single-result-set limit with non-zero hard row and byte bounds.
    ///
    /// Use [`RowLimit::with_max_sets`] when the statement may return multiple sets.
    ///
    /// # Errors
    /// Returns `InvalidArgument` if either bound is zero.
    pub fn new(rows: usize, bytes: usize) -> Result<Self, DbError> {
        Self::with_max_sets(rows, bytes, 1)
    }
    /// Creates a limit with non-zero row, byte, and aggregate result-set bounds.
    ///
    /// The set bound counts empty sets. Providers must fail, never truncate, when any bound is
    /// exceeded.
    ///
    /// # Errors
    /// Returns `InvalidArgument` with a static reason if any bound is zero.
    pub fn with_max_sets(rows: usize, bytes: usize, max_sets: usize) -> Result<Self, DbError> {
        if rows == 0 {
            return Err(DbError::invalid(InvalidArgumentReason::ZeroFetchRowLimit));
        }
        if bytes == 0 {
            return Err(DbError::invalid(InvalidArgumentReason::ZeroByteLimit));
        }
        if max_sets == 0 {
            return Err(DbError::invalid(InvalidArgumentReason::ZeroSetLimit));
        }
        Ok(Self {
            rows,
            bytes,
            max_sets,
        })
    }
    /// Returns the maximum number of rows across all sets.
    #[must_use]
    pub const fn rows(self) -> usize {
        self.rows
    }
    /// Returns the aggregate byte ceiling across all sets.
    #[must_use]
    pub const fn bytes(self) -> usize {
        self.bytes
    }
    /// Returns the maximum number of result sets, including empty sets.
    #[must_use]
    pub const fn max_sets(self) -> usize {
        self.max_sets
    }
}

impl Default for RowLimit {
    /// A bounded default: 1,000 rows, 1 MiB, one result set.
    fn default() -> Self {
        Self {
            rows: 1_000,
            bytes: 1_048_576,
            max_sets: 1,
        }
    }
}

/// Tri-state timeout override. `Inherit` uses the pool default; `Disabled` explicitly requests
/// no timeout; `Set` replaces the pool default for this operation.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum TimeoutSetting {
    /// Use the pool default.
    #[default]
    Inherit,
    /// Explicitly request no timeout. Use only when the caller owns a separate cancellation path.
    Disabled,
    /// Replace the pool default with this timeout.
    Set(Duration),
}

/// Bounded fetch limit override. There is deliberately no unbounded variant.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum LimitSetting {
    /// Use the pool default.
    #[default]
    Inherit,
    /// Replace the pool default for this operation.
    Set(RowLimit),
}

/// Defaults supplied by a pool for operations that inherit.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PoolDefaults {
    /// Default statement timeout. `None` means the pool chose no timeout.
    pub timeout: Option<Duration>,
    /// Default bounded fetch limit.
    pub limit: RowLimit,
    /// Whether telemetry is enabled by default.
    pub telemetry_enabled: bool,
    /// Whether exact SQL capture is enabled by default.
    pub sql_capture_enabled: bool,
    /// Default retry policy.
    pub retry: RetryPolicy,
}
impl Default for PoolDefaults {
    fn default() -> Self {
        Self {
            // A portable statement deadline cannot be promised by every provider. A provider
            // may choose an enforceable deadline; unsupported explicit deadlines must fail.
            timeout: None,
            limit: RowLimit::default(),
            telemetry_enabled: false,
            sql_capture_enabled: false,
            retry: RetryPolicy::Disabled,
        }
    }
}

/// Stable, sanitized error category. No driver message or query data is retained.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ErrorKind {
    /// Invalid argument, including a zero bound or invalid parameter value.
    InvalidArgument,
    /// Constraint violation.
    Constraint,
    /// Database is busy/locked.
    Busy,
    /// Deadline elapsed.
    Timeout,
    /// Operation was cancelled by provider.
    Cancelled,
    /// Requested capability is unavailable.
    Unsupported,
    /// A blocking adapter was called from the worker thread that would have to service it.
    ///
    /// The adapter refuses to wait on itself; a caller on its own worker thread must not block.
    /// This category is never reported by a native asynchronous or synchronous provider.
    Reentrant,
    /// Parameter or result conversion/shape error.
    Decode,
    /// Connection or transport failure.
    Connection,
    /// A dispatched standalone write did not produce an authoritative result.
    WriteOutcomeUnknown,
    /// Commit was dispatched but its result is unknown.
    CommitOutcomeUnknown,
    /// Requested column or ordinal is absent.
    Missing,
    /// A cardinality-one convenience found no row.
    NoRows,
    /// A cardinality-one convenience found more than one row in the first result set.
    TooManyRows,
    /// Name identifies more than one column.
    Ambiguous,
    /// A SQL null was requested as a non-null value.
    Null,
    /// Exact numeric conversion exceeds the destination range.
    Overflow,
    /// Supplied value has an incompatible type.
    WrongType,
}
/// Sanitized database error with a stable category and no source payload.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DbError {
    kind: ErrorKind,
    reason: Option<InvalidArgumentReason>,
}
/// Static, safe explanation for invalid caller-supplied arguments.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InvalidArgumentReason {
    /// Parameter's value variant did not match its declared type.
    ParameterTypeMismatch,
    /// Floating-point value was NaN or infinite.
    NonFiniteFloat,
    /// Fetch row limit was zero.
    ZeroFetchRowLimit,
    /// Fetch byte limit was zero.
    ZeroByteLimit,
    /// Fetch result-set limit was zero.
    ZeroSetLimit,
}
impl DbError {
    /// Creates an error containing only its stable category.
    #[must_use]
    pub const fn new(kind: ErrorKind) -> Self {
        Self { kind, reason: None }
    }
    /// Creates an invalid-argument error with a static, non-sensitive reason.
    #[must_use]
    pub const fn invalid(reason: InvalidArgumentReason) -> Self {
        Self {
            kind: ErrorKind::InvalidArgument,
            reason: Some(reason),
        }
    }
    /// Returns the safe static invalid-argument reason, if present.
    #[must_use]
    pub const fn reason(self) -> Option<InvalidArgumentReason> {
        self.reason
    }
    /// Returns the stable error category.
    #[must_use]
    pub const fn kind(self) -> ErrorKind {
        self.kind
    }
}
impl fmt::Display for DbError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.reason {
            Some(reason) => write!(f, "invalid database argument ({reason:?})"),
            None => write!(f, "database operation failed ({:?})", self.kind),
        }
    }
}
impl Error for DbError {}

/// A detached row of owned values in ordinal order with provider-supplied names.
#[derive(Clone, Debug, PartialEq)]
pub struct Row {
    columns: Vec<(String, Value)>,
}
impl Row {
    /// Creates a row from owned name/value pairs; duplicate names remain representable but name lookup is ambiguous.
    ///
    /// # Errors
    /// Returns `InvalidArgument` with a static reason if a cell contains a non-finite float.
    pub fn new(columns: Vec<(String, Value)>) -> Result<Self, DbError> {
        for (_, value) in &columns {
            value.validate()?;
        }
        Ok(Self { columns })
    }
    /// Returns the number of columns.
    #[must_use]
    pub fn len(&self) -> usize {
        self.columns.len()
    }
    /// Returns whether the row has no columns.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.columns.is_empty()
    }
    /// Returns a cell by zero-based ordinal, distinguishing missing columns and null values.
    ///
    /// # Errors
    /// Returns `Missing`, `Null`, `WrongType`, or `Overflow` as appropriate.
    pub fn get_ordinal<T: FromValue>(&self, ordinal: usize) -> Result<T, DbError> {
        let (_, value) = self
            .columns
            .get(ordinal)
            .ok_or_else(|| DbError::new(ErrorKind::Missing))?;
        T::from_value(value)
    }
    /// Returns a cell by exact name; missing and duplicate names are decode errors.
    ///
    /// # Errors
    /// Returns `Missing`, `Ambiguous`, `Null`, `WrongType`, or `Overflow` as appropriate.
    pub fn get<T: FromValue>(&self, name: &str) -> Result<T, DbError> {
        let mut found = self.columns.iter().filter(|(column, _)| column == name);
        let (_, value) = found
            .next()
            .ok_or_else(|| DbError::new(ErrorKind::Missing))?;
        if found.next().is_some() {
            return Err(DbError::new(ErrorKind::Ambiguous));
        }
        T::from_value(value)
    }
    /// Reads a cell by exact name with the same exact typing rules as [`Row::get`].
    ///
    /// # Errors
    /// Returns `Missing`, `Ambiguous`, `Null`, `WrongType`, or `Overflow` as appropriate.
    pub fn read<T: FromValue>(&self, name: &str) -> Result<T, DbError> {
        self.get(name)
    }
    /// Reads a cell by zero-based ordinal with the same exact typing rules as
    /// [`Row::get_ordinal`].
    ///
    /// # Errors
    /// Returns `Missing`, `Null`, `WrongType`, or `Overflow` as appropriate.
    pub fn read_at<T: FromValue>(&self, ordinal: usize) -> Result<T, DbError> {
        self.get_ordinal(ordinal)
    }
    /// Returns a cloned cell by zero-based ordinal, without typed conversion.
    ///
    /// # Errors
    /// Returns `Missing` when the ordinal is absent.
    pub fn value_at(&self, ordinal: usize) -> Result<Value, DbError> {
        self.columns
            .get(ordinal)
            .map(|(_, value)| value.clone())
            .ok_or_else(|| DbError::new(ErrorKind::Missing))
    }
    /// Returns the row's portable byte cost: column-name UTF-8 bytes plus value payload bytes.
    ///
    /// Payload accounting is `Null = 0`, `Bool = 1`, `I64`/`F64 = 8`, and text/bytes length.
    /// Providers and buffering use the same rule so a limit means the same thing at every layer.
    ///
    /// # Errors
    /// Returns `Overflow` if the aggregate cannot be represented in `usize`.
    pub fn encoded_bytes(&self) -> Result<usize, DbError> {
        self.columns
            .iter()
            .try_fold(0usize, |total, (name, value)| {
                let payload = match value {
                    Value::Null(_) => 0,
                    Value::Bool(_) => 1,
                    Value::I64(_) | Value::F64(_) => 8,
                    Value::Text(value) => value.len(),
                    Value::Bytes(value) => value.len(),
                };
                total
                    .checked_add(name.len())
                    .and_then(|total| total.checked_add(payload))
                    .ok_or_else(|| DbError::new(ErrorKind::Overflow))
            })
    }
    /// Reads a non-null value by exact name.
    ///
    /// # Errors
    /// Returns `Missing`, `Ambiguous`, `Null`, `WrongType`, or `Overflow`.
    pub fn require<T: FromValue>(&self, name: &str) -> Result<T, DbError> {
        let mut found = self.columns.iter().filter(|(column, _)| column == name);
        let (_, value) = found
            .next()
            .ok_or_else(|| DbError::new(ErrorKind::Missing))?;
        if found.next().is_some() {
            return Err(DbError::new(ErrorKind::Ambiguous));
        }
        if let Value::Null(actual_type) = value {
            return Err(DbError::new(if *actual_type == T::DB_TYPE {
                ErrorKind::Null
            } else {
                ErrorKind::WrongType
            }));
        }
        T::from_value(value)
    }
    /// Reads a non-null value by ordinal.
    ///
    /// # Errors
    /// Returns `Missing`, `Null`, `WrongType`, or `Overflow`.
    pub fn require_ordinal<T: FromValue>(&self, ordinal: usize) -> Result<T, DbError> {
        let (_, value) = self
            .columns
            .get(ordinal)
            .ok_or_else(|| DbError::new(ErrorKind::Missing))?;
        if let Value::Null(actual_type) = value {
            return Err(DbError::new(if *actual_type == T::DB_TYPE {
                ErrorKind::Null
            } else {
                ErrorKind::WrongType
            }));
        }
        T::from_value(value)
    }
}
/// Exact typed conversion from a row value; `Option<T>` represents a matching typed SQL null.
pub trait FromValue: Sized {
    /// SQL type accepted by this decoder.
    const DB_TYPE: DbType;
    /// Converts without lossy coercion.
    ///
    /// # Errors
    /// For non-nullable targets, returns `Null` for a matching typed SQL null, `WrongType` for
    /// incompatible types/null types, or `Overflow`.
    fn from_value(value: &Value) -> Result<Self, DbError>;
}
macro_rules! exact_from {
    ($ty:ty, $variant:ident, $db:ident) => {
        impl FromValue for $ty {
            const DB_TYPE: DbType = DbType::$db;
            fn from_value(value: &Value) -> Result<Self, DbError> {
                match value {
                    Value::Null(DbType::$db) => Err(DbError::new(ErrorKind::Null)),
                    Value::Null(_) => Err(DbError::new(ErrorKind::WrongType)),
                    Value::$variant(value) => Ok(value.clone()),
                    _ => Err(DbError::new(ErrorKind::WrongType)),
                }
            }
        }
    };
}
impl<T: FromValue> FromValue for Option<T> {
    const DB_TYPE: DbType = T::DB_TYPE;
    fn from_value(value: &Value) -> Result<Self, DbError> {
        match value {
            Value::Null(db_type) if *db_type == T::DB_TYPE => Ok(None),
            Value::Null(_) => Err(DbError::new(ErrorKind::WrongType)),
            non_null => T::from_value(non_null).map(Some),
        }
    }
}
exact_from!(bool, Bool, Bool);
exact_from!(i64, I64, I64);
exact_from!(f64, F64, F64);
exact_from!(String, Text, Text);
exact_from!(Vec<u8>, Bytes, Bytes);
/// Exact conversion from an SQL integer to a 32-bit integer.
impl FromValue for i32 {
    const DB_TYPE: DbType = DbType::I64;
    fn from_value(value: &Value) -> Result<Self, DbError> {
        match value {
            Value::Null(DbType::I64) => Err(DbError::new(ErrorKind::Null)),
            Value::I64(value) => {
                i32::try_from(*value).map_err(|_| DbError::new(ErrorKind::Overflow))
            }
            _ => Err(DbError::new(ErrorKind::WrongType)),
        }
    }
}

/// A statement's bounded result sets, consumed row by row.
///
/// A reader is not proof of a read: statements such as `INSERT ... RETURNING` can write.
/// `next_row_async` yields rows from the current result set and returns `None` at the end of that set.
/// `has_next_set_async` advances: it discards unread rows in the current set and reports whether another
/// set exists. `finish_async` drains and discards every remaining row and set, surfacing any later
/// statement error. A provider with unread results attached must discard its connection if an
/// unfinished reader is dropped. A provider that materializes the bounded batch before returning
/// may already have made the connection reusable and has nothing outstanding to discard.
///
/// `RowLimit` is a bound for the whole batch. A provider that supports multiple result sets counts
/// rows, bytes, and sets across the batch, including empty sets; a single-set provider may report
/// no next set instead of inventing a boundary it cannot observe. Buffering wrappers enforce the
/// same bounds while preserving boundaries.
pub trait Reader: Send {
    /// Returns the next row in the current result set, or `None` at the set's end.
    ///
    /// # Errors
    /// Returns a sanitized provider error. After an error the reader is finished; further calls
    /// return `None` or the same error. A provider with unread results attached must discard its
    /// connection when the reader is dropped.
    fn next_row_async(&mut self) -> DbFuture<'_, Option<Row>>;

    /// Advances to the next result set, discarding any unread rows in the current one.
    ///
    /// Returns `true` when a next set is current, or `false` at the end of the batch. A single-set
    /// provider returns `false`; it must not fabricate result boundaries.
    ///
    /// # Errors
    /// Returns a sanitized provider error while draining or advancing.
    fn has_next_set_async(&mut self) -> DbFuture<'_, bool>;

    /// Drains and discards every remaining row and result set.
    ///
    /// The reader is finished afterwards. A provider with unread results must give dropping the
    /// reader the same cleanup effect on the connection, but dropping cannot report a later
    /// statement error.
    ///
    /// # Errors
    /// Returns the first sanitized error encountered while draining.
    fn finish_async(&mut self) -> DbFuture<'_, ()>;

    /// Drains every result set and returns the owned rows, flattening set boundaries.
    ///
    /// This is a convenience for callers that want the whole bounded batch. It does not add a
    /// limit of its own; providers apply the `RowLimit` supplied to `fetch_async`. Use
    /// [`Reader::read_sets_async`] when boundaries matter.
    ///
    /// # Errors
    /// Returns the first sanitized error encountered while reading or draining.
    fn read_all_async(&mut self) -> DbFuture<'_, Vec<Row>> {
        Box::pin(async move {
            Ok(self
                .read_sets_async()
                .await?
                .into_iter()
                .flatten()
                .collect())
        })
    }

    /// Drains every result set and returns the rows grouped by set, including empty sets.
    ///
    /// This is a convenience for callers that want the whole bounded batch without losing
    /// boundaries. It does not add a limit of its own; providers apply the `RowLimit` supplied to
    /// `fetch_async`.
    ///
    /// # Errors
    /// Returns the first sanitized error encountered while reading or draining.
    fn read_sets_async(&mut self) -> DbFuture<'_, Vec<Vec<Row>>> {
        Box::pin(async move {
            let mut sets = Vec::new();
            loop {
                let mut set = Vec::new();
                while let Some(row) = self.next_row_async().await? {
                    set.push(row);
                }
                sets.push(set);
                if !self.has_next_set_async().await? {
                    break;
                }
            }
            self.finish_async().await?;
            Ok(sets)
        })
    }
}

/// A reader over an already-materialized batch, preserving result-set boundaries.
///
/// Providers and wrappers that materialize the bounded batch before returning use this. It has no
/// unread wire results, so `finish_async` and `has_next_set_async` only discard the in-memory rows. Empty sets
/// remain representable and countable.
#[derive(Debug)]
pub struct BufferedReader {
    sets: VecDeque<VecDeque<Row>>,
    done: bool,
}
impl BufferedReader {
    /// Wraps owned rows as one result set.
    #[must_use]
    pub fn new(rows: Vec<Row>) -> Self {
        Self::from_sets(vec![rows])
    }
    /// Wraps owned rows grouped by result set, including empty sets.
    #[must_use]
    pub fn from_sets(sets: Vec<Vec<Row>>) -> Self {
        Self {
            sets: sets.into_iter().map(VecDeque::from).collect(),
            done: false,
        }
    }
}
impl Reader for BufferedReader {
    fn next_row_async(&mut self) -> DbFuture<'_, Option<Row>> {
        let row = if self.done {
            None
        } else {
            self.sets
                .front_mut()
                .and_then(std::collections::VecDeque::pop_front)
        };
        Box::pin(async move { Ok(row) })
    }

    fn has_next_set_async(&mut self) -> DbFuture<'_, bool> {
        if self.done {
            return Box::pin(async { Ok(false) });
        }
        self.sets.pop_front();
        let more = !self.sets.is_empty();
        if !more {
            self.done = true;
        }
        Box::pin(async move { Ok(more) })
    }

    fn finish_async(&mut self) -> DbFuture<'_, ()> {
        self.sets.clear();
        self.done = true;
        Box::pin(async { Ok(()) })
    }
}

fn row_cardinality_error(more_than_one: bool) -> DbError {
    DbError::new(if more_than_one {
        ErrorKind::TooManyRows
    } else {
        ErrorKind::NoRows
    })
}

/// A pool able to acquire exclusive sessions and report truthful capabilities.
pub trait Pool: Send + Sync {
    /// Returns capabilities for this provider/configuration.
    fn capabilities(&self) -> Capabilities;
    /// Returns defaults used for options inherited by this pool.
    fn defaults(&self) -> PoolDefaults {
        PoolDefaults::default()
    }
    /// Acquires one exclusive session; the caller must drop it to release it.
    fn acquire_async(&self) -> DbFuture<'_, Box<dyn Session>>;
    /// Executes one statement through a newly acquired session with per-query policy overrides.
    /// Overrides must reject an explicit fetch limit before dispatch.
    ///
    /// # Errors
    /// Returns a sanitized provider error.
    fn execute_async<'a>(&'a self, sql: &'a str, mut options: QueryOptions) -> DbFuture<'a, u64> {
        Box::pin(async move {
            options.validate_execute()?;
            reject_unhandled_pool_options(&options)?;
            resolve_provider_timeout(&mut options, self.defaults().timeout)?;
            let mut session = self.acquire_async().await?;
            session.execute_async(sql, options).await
        })
    }
    /// Fetches one bounded batch through a newly acquired session with per-query overrides.
    ///
    /// The default implementation drains the batch and returns a reader over owned rows.
    ///
    /// # Errors
    /// Returns a sanitized provider error, including one found after the first row.
    fn fetch_async<'a>(
        &'a self,
        sql: &'a str,
        mut options: QueryOptions,
    ) -> DbFuture<'a, Box<dyn Reader + 'a>> {
        Box::pin(async move {
            reject_unhandled_pool_options(&options)?;
            let limit = options.resolved_limit(self.defaults());
            options.limit = LimitSetting::Set(limit);
            resolve_provider_timeout(&mut options, self.defaults().timeout)?;
            let mut session = self.acquire_async().await?;
            let mut reader = session.fetch_async(sql, options).await?;
            let sets = collect_sets(reader.as_mut(), limit).await?;
            Ok(Box::new(BufferedReader::from_sets(sets)) as Box<dyn Reader + 'a>)
        })
    }
    /// Fetches and drains the whole bounded batch using default options.
    ///
    /// # Errors
    /// Returns a sanitized provider error, including one found after the first row.
    fn fetch_all_async<'a>(
        &'a self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Vec<Row>> {
        Box::pin(async move {
            let limit = options.resolved_limit(self.defaults());
            let mut reader = self.fetch_async(sql, options).await?;
            let sets = collect_sets(reader.as_mut(), limit).await?;
            Ok(sets.into_iter().flatten().collect())
        })
    }
    /// Fetches at most the first row of the first result set, then drains the full batch.
    ///
    /// # Errors
    /// Returns a sanitized provider error, including one found after the first row.
    fn fetch_first_async<'a>(
        &'a self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Option<Row>> {
        Box::pin(async move {
            let mut reader = self.fetch_async(sql, options).await?;
            let first = reader.next_row_async().await?;
            reader.finish_async().await?;
            Ok(first)
        })
    }
    /// Fetches a statement whose first result set must contain exactly one row.
    ///
    /// Remaining rows and later result sets are drained before returning. A later error wins over
    /// the cardinality result.
    ///
    /// # Errors
    /// Returns `NoRows`, `TooManyRows`, or a sanitized provider error found while reading.
    fn fetch_one_async<'a>(&'a self, sql: &'a str, options: QueryOptions) -> DbFuture<'a, Row> {
        Box::pin(async move {
            let mut reader = self.fetch_async(sql, options).await?;
            let first = reader.next_row_async().await?;
            let more = reader.next_row_async().await?.is_some();
            reader.finish_async().await?;
            match first {
                None => Err(row_cardinality_error(false)),
                Some(_) if more => Err(row_cardinality_error(true)),
                Some(row) => Ok(row),
            }
        })
    }
    /// Fetches exactly one row and returns its first column.
    ///
    /// # Errors
    /// Returns `NoRows`, `TooManyRows`, `Missing` when the row has no columns, or a sanitized
    /// provider error found while reading.
    fn fetch_scalar_async<'a>(
        &'a self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Value> {
        Box::pin(async move {
            let row = self.fetch_one_async(sql, options).await?;
            row.value_at(0)
        })
    }
}
/// An exclusively borrowed database session.
pub trait Session: Send {
    /// Defaults inherited by operations on this session.
    fn defaults(&self) -> PoolDefaults {
        PoolDefaults::default()
    }
    /// Executes one positional-parameter statement; parameters follow placeholder order, with no SQL rewriting.
    /// Providers must reject an explicit fetch limit before dispatch, e.g. via
    /// [`QueryOptions::prepare_execute_provider`]. An inherited limit is permitted.
    /// Providers reject placeholder/parameter count mismatches. SQLite uses `?`; PostgreSQL uses `$1`, `$2`.
    fn execute_async<'a>(&'a mut self, sql: &'a str, options: QueryOptions) -> DbFuture<'a, u64>;
    /// Fetches one bounded batch as a reader.
    ///
    /// The reader may borrow this session until it is finished or dropped; a provider that
    /// materializes the batch may release the session earlier. The limit applies to the whole
    /// batch; providers that support multiple result sets count rows and bytes across sets.
    /// Parameters are positional and ordered; providers reject count mismatches and do not rewrite
    /// SQL. Fetching does not imply read-only SQL: `INSERT ... RETURNING` can write. Providers must
    /// report uncertain write results as `WriteOutcomeUnknown`, not retryable transport errors.
    fn fetch_async<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Box<dyn Reader + 'a>>;
    /// Fetches and drains the whole bounded batch.
    ///
    /// # Errors
    /// Returns a sanitized provider error, including one found after the first row.
    fn fetch_all_async<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Vec<Row>> {
        let limit = options.resolved_limit(self.defaults());
        Box::pin(async move {
            let mut reader = self.fetch_async(sql, options).await?;
            let sets = collect_sets(reader.as_mut(), limit).await?;
            Ok(sets.into_iter().flatten().collect())
        })
    }
    /// Fetches at most the first row of the first result set, then drains the full batch.
    ///
    /// # Errors
    /// Returns a sanitized provider error, including one found after the first row.
    fn fetch_first_async<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Option<Row>> {
        Box::pin(async move {
            let mut reader = self.fetch_async(sql, options).await?;
            let first = reader.next_row_async().await?;
            reader.finish_async().await?;
            Ok(first)
        })
    }
    /// Fetches a statement whose first result set must contain exactly one row.
    ///
    /// Remaining rows and later result sets are drained before returning. A later error wins over
    /// the cardinality result.
    ///
    /// # Errors
    /// Returns `NoRows`, `TooManyRows`, or a sanitized provider error found while reading.
    fn fetch_one_async<'a>(&'a mut self, sql: &'a str, options: QueryOptions) -> DbFuture<'a, Row> {
        Box::pin(async move {
            let mut reader = self.fetch_async(sql, options).await?;
            let first = reader.next_row_async().await?;
            let more = reader.next_row_async().await?.is_some();
            reader.finish_async().await?;
            match first {
                None => Err(row_cardinality_error(false)),
                Some(_) if more => Err(row_cardinality_error(true)),
                Some(row) => Ok(row),
            }
        })
    }
    /// Fetches exactly one row and returns its first column.
    ///
    /// # Errors
    /// Returns `NoRows`, `TooManyRows`, `Missing` when the row has no columns, or a sanitized
    /// provider error found while reading.
    fn fetch_scalar_async<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Value> {
        Box::pin(async move {
            let row = self.fetch_one_async(sql, options).await?;
            row.value_at(0)
        })
    }
    /// Executes a multi-statement unparameterized script. Input must be trusted; scripts must not manage transactions.
    /// An interrupted operation requires provider cleanup before session reuse.
    fn execute_trusted_script_async<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, ()>;
    /// Begins an interactive transaction on this same session; unsupported requested options fail before dispatch.
    /// If dropped after begin dispatch, the provider must roll back or discard the connection before reuse.
    fn begin_async<'a>(
        &'a mut self,
        options: TxOptions,
    ) -> DbFuture<'a, Box<dyn Transaction<'a> + 'a>>;
}
/// A transaction that exclusively borrows its session until consumed or dropped.
///
/// Dropping without commit or rollback, or interrupting a begin/commit/rollback future, must not leave
/// a connection reusable while a transaction may remain open. The provider must complete rollback
/// or discard that connection before allowing reuse. Dropping a Rust value alone is not proof of
/// rollback. A dispatched commit with unconfirmed success is an unknown outcome, not a rollback.
pub trait Transaction<'session>: Send {
    /// Defaults inherited by operations on this transaction.
    fn defaults(&self) -> PoolDefaults {
        PoolDefaults::default()
    }
    /// Executes one statement with positional parameters in slice order, without SQL rewriting.
    /// Providers must reject an explicit fetch limit before dispatch, e.g. via
    /// [`QueryOptions::prepare_execute_provider`]. An inherited limit is permitted.
    /// Providers reject placeholder/parameter count mismatches.
    fn execute_async<'a>(&'a mut self, sql: &'a str, options: QueryOptions) -> DbFuture<'a, u64>;
    /// Fetches one bounded batch as a reader.
    ///
    /// The reader may borrow this transaction until it is finished or dropped; a provider that
    /// materializes the batch may release it earlier. The limit applies to the whole batch;
    /// providers that support multiple result sets count rows and bytes across sets. Parameters are
    /// positional and ordered.
    fn fetch_async<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Box<dyn Reader + 'a>>;
    /// Fetches and drains the whole bounded batch.
    ///
    /// # Errors
    /// Returns a sanitized provider error, including one found after the first row.
    fn fetch_all_async<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Vec<Row>> {
        let limit = options.resolved_limit(self.defaults());
        Box::pin(async move {
            let mut reader = self.fetch_async(sql, options).await?;
            let sets = collect_sets(reader.as_mut(), limit).await?;
            Ok(sets.into_iter().flatten().collect())
        })
    }
    /// Fetches at most the first row of the first result set, then drains the full batch.
    ///
    /// # Errors
    /// Returns a sanitized provider error, including one found after the first row.
    fn fetch_first_async<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Option<Row>> {
        Box::pin(async move {
            let mut reader = self.fetch_async(sql, options).await?;
            let first = reader.next_row_async().await?;
            reader.finish_async().await?;
            Ok(first)
        })
    }
    /// Fetches a statement whose first result set must contain exactly one row.
    ///
    /// Remaining rows and later result sets are drained before returning. A later error wins over
    /// the cardinality result.
    ///
    /// # Errors
    /// Returns `NoRows`, `TooManyRows`, or a sanitized provider error found while reading.
    fn fetch_one_async<'a>(&'a mut self, sql: &'a str, options: QueryOptions) -> DbFuture<'a, Row> {
        Box::pin(async move {
            let mut reader = self.fetch_async(sql, options).await?;
            let first = reader.next_row_async().await?;
            let more = reader.next_row_async().await?.is_some();
            reader.finish_async().await?;
            match first {
                None => Err(row_cardinality_error(false)),
                Some(_) if more => Err(row_cardinality_error(true)),
                Some(row) => Ok(row),
            }
        })
    }
    /// Fetches exactly one row and returns its first column.
    ///
    /// # Errors
    /// Returns `NoRows`, `TooManyRows`, `Missing` when the row has no columns, or a sanitized
    /// provider error found while reading.
    fn fetch_scalar_async<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Value> {
        Box::pin(async move {
            let row = self.fetch_one_async(sql, options).await?;
            row.value_at(0)
        })
    }
    /// Executes a trusted multi-statement script that must not manage transactions. Interrupted work
    /// requires provider cleanup before transaction reuse.
    fn execute_trusted_script_async<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, ()>;
    /// Commits and consumes this transaction. Before reuse, the provider must establish transaction
    /// closure and cleanup or discard the connection, including if this future is interrupted. If
    /// commit was dispatched but success is unknown, return `CommitOutcomeUnknown`; this does not
    /// imply rollback.
    fn commit_async(self: Box<Self>) -> DbFuture<'session, ()>;
    /// Rolls back and consumes this transaction. The provider must finish rollback or discard the
    /// connection before reuse, including if this future is interrupted; it must never return a
    /// possibly active connection.
    fn rollback_async(self: Box<Self>) -> DbFuture<'session, ()>;
}
/// Optional provider extension for SQLite-style immediate transaction starts without leaking driver types.
pub trait ImmediateTransaction: Session {
    /// Begins an immediate transaction on the same session or returns `Unsupported` before beginning.
    fn begin_immediate_async<'a>(&'a mut self) -> DbFuture<'a, Box<dyn Transaction<'a> + 'a>>;
}

/// SQLite-specific pool capabilities shared by SQLite consumers without exposing a driver.
///
/// Implementations must apply `busy_timeout` to each acquired connection before returning the
/// immediate-transaction-capable session.
pub trait SqlitePool: Pool {
    /// Returns the configured SQLite busy timeout.
    fn busy_timeout(&self) -> Duration;

    /// Acquires a session capable of starting `BEGIN IMMEDIATE`.
    fn acquire_sqlite_async(&self) -> DbFuture<'_, Box<dyn ImmediateTransaction>>;
}

/// Policy and telemetry decorator for pool, session, transaction, and script operations.
///
/// Pool methods `execute_async`/`fetch_async` acquire a fresh session for each configured retry attempt.
/// Session, transaction, and trusted-script methods do not retry.
/// The total-budget timer is constructed once before acquisition and checked again before each
/// statement dispatch, including after every synchronous hook in nested policy session wrappers.
/// A pre-dispatch rejection remains `Cancelled`/`Timeout` and emits no statement events.
/// Already-dispatched success wins simultaneous cancellation/deadline.
/// Other interruptions during dispatch are `WriteOutcomeUnknown`, even for fetches; authoritative
/// provider unknown-outcome errors are never replaced by cancellation/deadline or retried.
/// Dropping an in-flight observed operation emits one `WriteOutcomeUnknown` event because dispatch
/// completion cannot be established.
/// Statement events cover execution only; acquisition and transaction begin/commit/rollback
/// boundaries are not separately observed.
pub struct PolicyPool {
    inner: Arc<dyn Pool>,
    policy: Arc<PoolPolicy>,
}
impl fmt::Debug for PolicyPool {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PolicyPool")
            .field("policy", &self.policy)
            .finish_non_exhaustive()
    }
}
impl PolicyPool {
    /// Wraps a provider and applies the supplied defaults to operations made through this wrapper.
    pub fn new<P: Pool + 'static>(pool: P, policy: PoolPolicy) -> Self {
        Self::from_shared(Arc::new(pool), policy)
    }

    /// Wraps a shared provider trait object with per-pool policy.
    #[must_use]
    pub fn from_shared(pool: Arc<dyn Pool>, mut policy: PoolPolicy) -> Self {
        if policy.default_timeout.is_none() {
            policy.default_timeout = pool.defaults().timeout;
        }
        Self {
            inner: pool,
            policy: Arc::new(policy),
        }
    }
}

impl Pool for PolicyPool {
    fn capabilities(&self) -> Capabilities {
        self.inner.capabilities()
    }

    fn defaults(&self) -> PoolDefaults {
        policy_defaults(&self.policy)
    }

    fn acquire_async(&self) -> DbFuture<'_, Box<dyn Session>> {
        let inner = Arc::clone(&self.inner);
        let policy = Arc::clone(&self.policy);
        Box::pin(async move {
            let session = inner.acquire_async().await?;
            Ok(Box::new(PolicySession { session, policy }) as Box<dyn Session>)
        })
    }

    fn execute_async<'a>(&'a self, sql: &'a str, mut options: QueryOptions) -> DbFuture<'a, u64> {
        let pool = Arc::clone(&self.inner);
        let policy = Arc::clone(&self.policy);
        Box::pin(async move {
            options.validate_execute()?;
            resolve_provider_timeout(&mut options, policy.default_timeout)?;
            let retry = resolve_retry(options.retry, policy.retry);
            if options.replay_safety == ReplaySafety::ExplicitlyReplaySafe
                && let RetryPolicy::Enabled {
                    policy: retry_policy,
                    retry_timeouts,
                } = retry
            {
                let Some(mut runtime) = options
                    .retry_runtime
                    .clone()
                    .or_else(|| policy.retry_runtime.clone())
                else {
                    return Err(DbError::new(ErrorKind::Unsupported));
                };
                let dispatch = RetryDispatch::for_operation(&mut runtime);
                let cancellation = Arc::clone(&dispatch.cancellation);
                let deadline = dispatch.deadline.clone();
                let attempt_state = Arc::clone(&dispatch.state);
                let mut options = options;
                options.retry_runtime = Some(runtime.clone());
                let retry_if = |error: &DbError| {
                    retry_allowed(*error, retry_timeouts, &cancellation, &deadline)
                };
                let outcome = execute_idempotent_retry(
                    retry_policy,
                    &cancellation,
                    deadline.clone(),
                    || {
                        let pool = Arc::clone(&pool);
                        let policy = Arc::clone(&policy);
                        let cancellation = Arc::clone(&cancellation);
                        let deadline = deadline.clone();
                        let attempt_state = Arc::clone(&attempt_state);
                        let options = options.clone();
                        async move {
                            attempt_state.reset();
                            deadline.check(&cancellation).await?;
                            let mut session = pool.acquire_async().await?;
                            let result = execute_observed(
                                session.as_mut(),
                                sql,
                                options,
                                Arc::clone(&policy),
                            )
                            .await;
                            drop(session);
                            result
                        }
                    },
                    retry_if,
                    || (runtime.random)(),
                    |delay| (runtime.sleep)(delay),
                )
                .await;
                drop(dispatch);
                return retry_result_with_interrupt(outcome, &attempt_state);
            }

            let mut session = pool.acquire_async().await?;
            let result = execute_observed(session.as_mut(), sql, options, policy).await;
            drop(session);
            result
        })
    }

    fn fetch_async<'a>(
        &'a self,
        sql: &'a str,
        mut options: QueryOptions,
    ) -> DbFuture<'a, Box<dyn Reader + 'a>> {
        let pool = Arc::clone(&self.inner);
        let policy = Arc::clone(&self.policy);
        Box::pin(async move {
            let limit = match options.limit {
                LimitSetting::Inherit => policy.default_limit,
                LimitSetting::Set(limit) => limit,
            };
            options.limit = LimitSetting::Set(limit);
            resolve_provider_timeout(&mut options, policy.default_timeout)?;
            let retry = resolve_retry(options.retry, policy.retry);
            if options.replay_safety == ReplaySafety::ExplicitlyReplaySafe
                && let RetryPolicy::Enabled {
                    policy: retry_policy,
                    retry_timeouts,
                } = retry
            {
                let Some(mut runtime) = options
                    .retry_runtime
                    .clone()
                    .or_else(|| policy.retry_runtime.clone())
                else {
                    return Err(DbError::new(ErrorKind::Unsupported));
                };
                let dispatch = RetryDispatch::for_operation(&mut runtime);
                let cancellation = Arc::clone(&dispatch.cancellation);
                let deadline = dispatch.deadline.clone();
                let attempt_state = Arc::clone(&dispatch.state);
                let mut options = options;
                options.retry_runtime = Some(runtime.clone());
                let retry_if = |error: &DbError| {
                    retry_allowed(*error, retry_timeouts, &cancellation, &deadline)
                };
                let outcome = execute_idempotent_retry(
                    retry_policy,
                    &cancellation,
                    deadline.clone(),
                    || {
                        let pool = Arc::clone(&pool);
                        let policy = Arc::clone(&policy);
                        let cancellation = Arc::clone(&cancellation);
                        let deadline = deadline.clone();
                        let attempt_state = Arc::clone(&attempt_state);
                        let options = options.clone();
                        async move {
                            attempt_state.reset();
                            deadline.check(&cancellation).await?;
                            let mut session = pool.acquire_async().await?;
                            let result = fetch_observed(
                                session.as_mut(),
                                sql,
                                limit,
                                options,
                                Arc::clone(&policy),
                            )
                            .await;
                            drop(session);
                            result
                        }
                    },
                    retry_if,
                    || (runtime.random)(),
                    |delay| (runtime.sleep)(delay),
                )
                .await;
                drop(dispatch);
                return retry_result_with_interrupt(outcome, &attempt_state)
                    .map(|reader| Box::new(reader) as Box<dyn Reader + 'a>);
            }

            let mut session = pool.acquire_async().await?;
            let result = fetch_observed(session.as_mut(), sql, limit, options, policy).await;
            drop(session);
            result.map(|reader| Box::new(reader) as Box<dyn Reader + 'a>)
        })
    }
}

fn resolve_retry(setting: RetrySetting, default: RetryPolicy) -> RetryPolicy {
    match setting {
        RetrySetting::Inherit => default,
        RetrySetting::Off => RetryPolicy::Disabled,
        RetrySetting::Use(policy) => policy,
    }
}

fn retry_allowed(
    error: DbError,
    retry_timeouts: bool,
    cancellation: &Cancellation,
    deadline: &RetryDeadline,
) -> bool {
    !cancellation.is_cancelled()
        && !deadline.is_expired()
        && (matches!(error.kind(), ErrorKind::Busy | ErrorKind::Connection)
            || (retry_timeouts && error.kind() == ErrorKind::Timeout))
}

fn retry_result<T>(outcome: RetryOutcome<T, DbError>) -> Result<T, DbError> {
    match outcome {
        RetryOutcome::Success { value, .. } => Ok(value),
        RetryOutcome::Failure { error, .. } => Err(error),
        RetryOutcome::Cancelled { .. } => Err(DbError::new(ErrorKind::Cancelled)),
        RetryOutcome::TimedOut { .. } => Err(DbError::new(ErrorKind::Timeout)),
    }
}

fn mutex_lock<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
    mutex
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

#[derive(Default)]
struct RetryAttemptState {
    dispatch_in_flight: AtomicBool,
    last_error: Mutex<Option<ErrorKind>>,
    dispatch_rejection: Mutex<Option<DbError>>,
}

impl RetryAttemptState {
    fn reset(&self) {
        self.dispatch_in_flight.store(false, Ordering::Release);
        *mutex_lock(&self.last_error) = None;
        *mutex_lock(&self.dispatch_rejection) = None;
    }
}

// One pinned total-budget timer, shared by the resilience race and the SQL dispatch gate.
// Latch completion so neither consumer ever polls a completed timer again.
#[derive(Clone)]
struct RetryDeadline(Arc<Mutex<Option<RetryWait>>>);

impl RetryDeadline {
    fn new(timer: RetryWait) -> Self {
        Self(Arc::new(Mutex::new(Some(timer))))
    }

    fn is_expired(&self) -> bool {
        mutex_lock(&self.0).is_none()
    }

    async fn check(&self, cancellation: &Cancellation) -> Result<(), DbError> {
        if cancellation.is_cancelled() {
            return Err(DbError::new(ErrorKind::Cancelled));
        }
        let mut deadline = self.clone();
        let expired = poll_fn(|cx| Poll::Ready(Pin::new(&mut deadline).poll(cx).is_ready())).await;
        // Polling the caller's timer can itself make cancellation ready.
        if cancellation.is_cancelled() {
            Err(DbError::new(ErrorKind::Cancelled))
        } else if expired {
            Err(DbError::new(ErrorKind::Timeout))
        } else {
            Ok(())
        }
    }
}

impl Future for RetryDeadline {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        let mut timer = mutex_lock(&self.0);
        if let Some(wait) = timer.as_mut() {
            if wait.as_mut().poll(cx).is_pending() {
                return Poll::Pending;
            }
            *timer = None;
        }
        Poll::Ready(())
    }
}

struct RetryDispatch {
    deadline: RetryDeadline,
    cancellation: Arc<Cancellation>,
    state: Arc<RetryAttemptState>,
}

impl RetryDispatch {
    fn for_operation(runtime: &mut RetryRuntime) -> Arc<Self> {
        let dispatch = Arc::new(Self {
            // Construct the only timer before the first acquisition, not lazily inside a layer.
            deadline: RetryDeadline::new((runtime.timer)()),
            cancellation: Arc::clone(&runtime.cancellation),
            state: Arc::new(RetryAttemptState::default()),
        });
        runtime.dispatch = Some(Arc::downgrade(&dispatch));
        dispatch
    }

    async fn start(&self) -> Result<(), DbError> {
        // Acquisition may become ready in the engine's success probe. Check the actual timer,
        // not an expired flag that depends on the engine having polled its timer first.
        // A deeper policy layer is still preparing SQL, so no provider has been invoked yet.
        self.state
            .dispatch_in_flight
            .store(false, Ordering::Release);
        if let Err(error) = self.deadline.check(&self.cancellation).await {
            *mutex_lock(&self.state.dispatch_rejection) = Some(error);
            return Err(error);
        }
        self.state.dispatch_in_flight.store(true, Ordering::Release);
        Ok(())
    }

    fn rejection(&self) -> Option<DbError> {
        *mutex_lock(&self.state.dispatch_rejection)
    }

    fn finish(&self, result: Result<(), DbError>) {
        self.state
            .dispatch_in_flight
            .store(false, Ordering::Release);
        if let Err(error) = result {
            *mutex_lock(&self.state.last_error) = Some(error.kind());
        }
    }
}

fn retry_result_with_interrupt<T>(
    outcome: RetryOutcome<T, DbError>,
    state: &RetryAttemptState,
) -> Result<T, DbError> {
    match outcome {
        RetryOutcome::Cancelled { .. } => Err(interruption_error(state, ErrorKind::Cancelled)),
        RetryOutcome::TimedOut { .. } => Err(interruption_error(state, ErrorKind::Timeout)),
        other => retry_result(other),
    }
}

fn interruption_error(state: &RetryAttemptState, interruption: ErrorKind) -> DbError {
    // Fetch is not proof of a read: INSERT ... RETURNING can have changed the database.
    if state.dispatch_in_flight.load(Ordering::Acquire) {
        return DbError::new(ErrorKind::WriteOutcomeUnknown);
    }
    let last_error = *mutex_lock(&state.last_error);
    match last_error {
        Some(kind @ (ErrorKind::WriteOutcomeUnknown | ErrorKind::CommitOutcomeUnknown)) => {
            DbError::new(kind)
        }
        _ => DbError::new(interruption),
    }
}

/// Policy decorator that retains a pool's SQLite immediate-transaction capability.
///
/// It shares the observer, capture, timeout, and pool-operation retry implementation of
/// [`PolicyPool`]. Acquired SQLite sessions use the same observer helpers but intentionally do not
/// retry, matching ordinary policy sessions and transactions.
pub struct SqlitePolicyPool {
    inner: Arc<dyn SqlitePool>,
    policy_pool: PolicyPool,
}

impl fmt::Debug for SqlitePolicyPool {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("SqlitePolicyPool")
            .field("policy", &self.policy_pool.policy)
            .finish_non_exhaustive()
    }
}

impl SqlitePolicyPool {
    /// Wraps a SQLite provider and preserves its immediate-transaction extension.
    pub fn new<P: SqlitePool + 'static>(pool: P, policy: PoolPolicy) -> Self {
        Self::from_shared(Arc::new(pool), policy)
    }

    /// Wraps a shared SQLite provider while retaining one underlying pool for both APIs.
    #[must_use]
    pub fn from_shared(inner: Arc<dyn SqlitePool>, policy: PoolPolicy) -> Self {
        let pool: Arc<dyn Pool> = inner.clone();
        Self {
            inner,
            policy_pool: PolicyPool::from_shared(pool, policy),
        }
    }
}

impl Pool for SqlitePolicyPool {
    fn capabilities(&self) -> Capabilities {
        self.policy_pool.capabilities()
    }

    fn defaults(&self) -> PoolDefaults {
        self.policy_pool.defaults()
    }

    fn acquire_async(&self) -> DbFuture<'_, Box<dyn Session>> {
        self.policy_pool.acquire_async()
    }

    fn execute_async<'a>(&'a self, sql: &'a str, options: QueryOptions) -> DbFuture<'a, u64> {
        self.policy_pool.execute_async(sql, options)
    }

    fn fetch_async<'a>(
        &'a self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Box<dyn Reader + 'a>> {
        self.policy_pool.fetch_async(sql, options)
    }
}

impl SqlitePool for SqlitePolicyPool {
    fn busy_timeout(&self) -> Duration {
        self.inner.busy_timeout()
    }

    fn acquire_sqlite_async(&self) -> DbFuture<'_, Box<dyn ImmediateTransaction>> {
        let inner = Arc::clone(&self.inner);
        let policy = Arc::clone(&self.policy_pool.policy);
        Box::pin(async move {
            let session = inner.acquire_sqlite_async().await?;
            Ok(Box::new(PolicySqliteSession { session, policy }) as Box<dyn ImmediateTransaction>)
        })
    }
}

struct PolicySession {
    session: Box<dyn Session>,
    policy: Arc<PoolPolicy>,
}
impl Session for PolicySession {
    fn defaults(&self) -> PoolDefaults {
        policy_defaults(&self.policy)
    }

    fn execute_async<'a>(
        &'a mut self,
        sql: &'a str,
        mut options: QueryOptions,
    ) -> DbFuture<'a, u64> {
        Box::pin(async move {
            options.validate_execute()?;
            reject_session_retry(&options)?;
            resolve_provider_timeout(&mut options, self.policy.default_timeout)?;
            execute_observed(
                self.session.as_mut(),
                sql,
                options,
                Arc::clone(&self.policy),
            )
            .await
        })
    }

    fn fetch_async<'a>(
        &'a mut self,
        sql: &'a str,
        mut options: QueryOptions,
    ) -> DbFuture<'a, Box<dyn Reader + 'a>> {
        Box::pin(async move {
            reject_session_retry(&options)?;
            let limit = options.resolved_limit(policy_defaults(&self.policy));
            options.limit = LimitSetting::Set(limit);
            resolve_provider_timeout(&mut options, self.policy.default_timeout)?;
            let reader = fetch_observed(
                self.session.as_mut(),
                sql,
                limit,
                options,
                Arc::clone(&self.policy),
            )
            .await?;
            Ok(Box::new(reader) as Box<dyn Reader + 'a>)
        })
    }

    fn execute_trusted_script_async<'a>(
        &'a mut self,
        sql: &'a str,
        mut options: QueryOptions,
    ) -> DbFuture<'a, ()> {
        Box::pin(async move {
            options.validate_script()?;
            reject_session_retry(&options)?;
            resolve_provider_timeout(&mut options, self.policy.default_timeout)?;
            script_observed(
                self.session.as_mut(),
                sql,
                options,
                Arc::clone(&self.policy),
            )
            .await
        })
    }

    fn begin_async<'a>(
        &'a mut self,
        options: TxOptions,
    ) -> DbFuture<'a, Box<dyn Transaction<'a> + 'a>> {
        let session = &mut self.session;
        let policy = Arc::clone(&self.policy);
        Box::pin(async move {
            let transaction = session.begin_async(options).await?;
            Ok(Box::new(PolicyTransaction {
                transaction,
                policy,
            }) as Box<dyn Transaction<'a> + 'a>)
        })
    }
}

struct PolicySqliteSession {
    session: Box<dyn ImmediateTransaction>,
    policy: Arc<PoolPolicy>,
}

impl Session for PolicySqliteSession {
    fn defaults(&self) -> PoolDefaults {
        policy_defaults(&self.policy)
    }

    fn execute_async<'a>(
        &'a mut self,
        sql: &'a str,
        mut options: QueryOptions,
    ) -> DbFuture<'a, u64> {
        Box::pin(async move {
            options.validate_execute()?;
            reject_session_retry(&options)?;
            resolve_provider_timeout(&mut options, self.policy.default_timeout)?;
            execute_observed(
                self.session.as_mut(),
                sql,
                options,
                Arc::clone(&self.policy),
            )
            .await
        })
    }

    fn fetch_async<'a>(
        &'a mut self,
        sql: &'a str,
        mut options: QueryOptions,
    ) -> DbFuture<'a, Box<dyn Reader + 'a>> {
        Box::pin(async move {
            reject_session_retry(&options)?;
            let limit = options.resolved_limit(policy_defaults(&self.policy));
            options.limit = LimitSetting::Set(limit);
            resolve_provider_timeout(&mut options, self.policy.default_timeout)?;
            let reader = fetch_observed(
                self.session.as_mut(),
                sql,
                limit,
                options,
                Arc::clone(&self.policy),
            )
            .await?;
            Ok(Box::new(reader) as Box<dyn Reader + 'a>)
        })
    }

    fn execute_trusted_script_async<'a>(
        &'a mut self,
        sql: &'a str,
        mut options: QueryOptions,
    ) -> DbFuture<'a, ()> {
        Box::pin(async move {
            options.validate_script()?;
            reject_session_retry(&options)?;
            resolve_provider_timeout(&mut options, self.policy.default_timeout)?;
            script_observed(
                self.session.as_mut(),
                sql,
                options,
                Arc::clone(&self.policy),
            )
            .await
        })
    }

    fn begin_async<'a>(
        &'a mut self,
        options: TxOptions,
    ) -> DbFuture<'a, Box<dyn Transaction<'a> + 'a>> {
        let session = &mut self.session;
        let policy = Arc::clone(&self.policy);
        Box::pin(async move {
            let transaction = session.begin_async(options).await?;
            Ok(Box::new(PolicyTransaction {
                transaction,
                policy,
            }) as Box<dyn Transaction<'a> + 'a>)
        })
    }
}

impl ImmediateTransaction for PolicySqliteSession {
    fn begin_immediate_async<'a>(&'a mut self) -> DbFuture<'a, Box<dyn Transaction<'a> + 'a>> {
        let session = &mut self.session;
        let policy = Arc::clone(&self.policy);
        Box::pin(async move {
            let transaction = session.begin_immediate_async().await?;
            Ok(Box::new(PolicyTransaction {
                transaction,
                policy,
            }) as Box<dyn Transaction<'a> + 'a>)
        })
    }
}

struct PolicyTransaction<'session> {
    transaction: Box<dyn Transaction<'session> + 'session>,
    policy: Arc<PoolPolicy>,
}
impl<'session> Transaction<'session> for PolicyTransaction<'session> {
    fn defaults(&self) -> PoolDefaults {
        policy_defaults(&self.policy)
    }

    fn execute_async<'a>(
        &'a mut self,
        sql: &'a str,
        mut options: QueryOptions,
    ) -> DbFuture<'a, u64> {
        Box::pin(async move {
            options.validate_execute()?;
            reject_session_retry(&options)?;
            resolve_provider_timeout(&mut options, self.policy.default_timeout)?;
            execute_observed(
                self.transaction.as_mut(),
                sql,
                options,
                Arc::clone(&self.policy),
            )
            .await
        })
    }

    fn fetch_async<'a>(
        &'a mut self,
        sql: &'a str,
        mut options: QueryOptions,
    ) -> DbFuture<'a, Box<dyn Reader + 'a>> {
        Box::pin(async move {
            reject_session_retry(&options)?;
            let limit = options.resolved_limit(policy_defaults(&self.policy));
            options.limit = LimitSetting::Set(limit);
            resolve_provider_timeout(&mut options, self.policy.default_timeout)?;
            let reader = fetch_observed(
                self.transaction.as_mut(),
                sql,
                limit,
                options,
                Arc::clone(&self.policy),
            )
            .await?;
            Ok(Box::new(reader) as Box<dyn Reader + 'a>)
        })
    }

    fn execute_trusted_script_async<'a>(
        &'a mut self,
        sql: &'a str,
        mut options: QueryOptions,
    ) -> DbFuture<'a, ()> {
        Box::pin(async move {
            options.validate_script()?;
            reject_session_retry(&options)?;
            resolve_provider_timeout(&mut options, self.policy.default_timeout)?;
            script_observed(
                self.transaction.as_mut(),
                sql,
                options,
                Arc::clone(&self.policy),
            )
            .await
        })
    }

    fn commit_async(self: Box<Self>) -> DbFuture<'session, ()> {
        let transaction = self.transaction;
        transaction.commit_async()
    }

    fn rollback_async(self: Box<Self>) -> DbFuture<'session, ()> {
        let transaction = self.transaction;
        transaction.rollback_async()
    }
}

fn resolve_setting(setting: QuerySetting, pool_default: bool) -> bool {
    match setting {
        QuerySetting::Inherit => pool_default,
        QuerySetting::Off => false,
        QuerySetting::On => true,
    }
}

fn captured_sql(sql: &str, policy: &PoolPolicy, options: &QueryOptions) -> Option<String> {
    if !resolve_setting(options.sql_capture, policy.sql_capture_enabled)
        || !resolve_setting(options.telemetry, policy.telemetry_enabled)
    {
        return None;
    }
    policy.observer.as_ref()?;
    let hook = options.sql_hook.as_ref().or(policy.sql_hook.as_ref());
    match hook {
        Some(hook) => {
            contain_callback_panic(catch_unwind(AssertUnwindSafe(|| hook(sql)))).flatten()
        }
        None => Some(sql.to_owned()),
    }
}

fn contain_callback_panic<T>(result: Result<T, Box<dyn std::any::Any + Send>>) -> Option<T> {
    match result {
        Ok(value) => Some(value),
        Err(payload) => {
            // A panic payload may have a destructor that panics. Forget it so this callback
            // boundary cannot unwind while unwinding a post-operation observer/hook.
            std::mem::forget(payload);
            None
        }
    }
}

fn emit(
    operation: Operation,
    started: Instant,
    result: Result<(), DbError>,
    sql: Option<String>,
    policy: &PoolPolicy,
    options: &QueryOptions,
) {
    if !resolve_setting(options.telemetry, policy.telemetry_enabled) {
        return;
    }
    let Some(observer) = &policy.observer else {
        return;
    };
    let outcome = match result {
        Ok(()) => TelemetryOutcome::Success,
        Err(error) => TelemetryOutcome::Error(error.kind()),
    };
    let event = DbEvent {
        operation,
        duration: started.elapsed(),
        outcome,
        sql,
    };
    let _ = contain_callback_panic(catch_unwind(AssertUnwindSafe(|| observer(&event))));
}

struct ObservationGuard {
    operation: Operation,
    started: Instant,
    captured: Option<String>,
    policy: Arc<PoolPolicy>,
    options: QueryOptions,
    active: bool,
}

impl ObservationGuard {
    fn new(
        operation: Operation,
        started: Instant,
        captured: Option<String>,
        policy: Arc<PoolPolicy>,
        options: QueryOptions,
    ) -> Self {
        Self {
            operation,
            started,
            captured,
            policy,
            options,
            active: true,
        }
    }

    fn finish(&mut self, result: Result<(), DbError>) {
        if self.active {
            self.active = false;
            emit(
                self.operation,
                self.started,
                result,
                self.captured.take(),
                &self.policy,
                &self.options,
            );
        }
    }

    fn disarm(&mut self) {
        self.active = false;
    }
}

impl Drop for ObservationGuard {
    fn drop(&mut self) {
        if self.active {
            self.finish(Err(DbError::new(ErrorKind::WriteOutcomeUnknown)));
        }
    }
}

trait StatementExecutor: Send {
    fn execute_statement<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, u64>;
    fn fetch_statement<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Box<dyn Reader + 'a>>;
    fn execute_script<'a>(&'a mut self, sql: &'a str, options: QueryOptions) -> DbFuture<'a, ()>;
}
impl StatementExecutor for dyn Session + '_ {
    fn execute_statement<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, u64> {
        self.execute_async(sql, options)
    }
    fn fetch_statement<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Box<dyn Reader + 'a>> {
        self.fetch_async(sql, options)
    }
    fn execute_script<'a>(&'a mut self, sql: &'a str, options: QueryOptions) -> DbFuture<'a, ()> {
        self.execute_trusted_script_async(sql, options)
    }
}
impl StatementExecutor for dyn ImmediateTransaction + '_ {
    fn execute_statement<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, u64> {
        self.execute_async(sql, options)
    }
    fn fetch_statement<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Box<dyn Reader + 'a>> {
        self.fetch_async(sql, options)
    }
    fn execute_script<'a>(&'a mut self, sql: &'a str, options: QueryOptions) -> DbFuture<'a, ()> {
        self.execute_trusted_script_async(sql, options)
    }
}
impl StatementExecutor for dyn Transaction<'_> + '_ {
    fn execute_statement<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, u64> {
        self.execute_async(sql, options)
    }
    fn fetch_statement<'a>(
        &'a mut self,
        sql: &'a str,
        options: QueryOptions,
    ) -> DbFuture<'a, Box<dyn Reader + 'a>> {
        self.fetch_async(sql, options)
    }
    fn execute_script<'a>(&'a mut self, sql: &'a str, options: QueryOptions) -> DbFuture<'a, ()> {
        self.execute_trusted_script_async(sql, options)
    }
}

fn script_observed<'a, S: StatementExecutor + ?Sized>(
    session: &'a mut S,
    sql: &'a str,
    query_options: QueryOptions,
    policy: Arc<PoolPolicy>,
) -> DbFuture<'a, ()> {
    Box::pin(async move {
        let captured = captured_sql(sql, &policy, &query_options);
        let started = Instant::now();
        let mut observation = ObservationGuard::new(
            Operation::Execute,
            started,
            captured,
            Arc::clone(&policy),
            query_options.clone(),
        );
        let mut provider_options = query_options.clone();
        provider_options.policy_applied = true;
        let result = session.execute_script(sql, provider_options).await;
        let observer_result = match &result {
            Ok(()) => Ok(()),
            Err(error) => Err(DbError::new(error.kind())),
        };
        observation.finish(observer_result);
        result
    })
}

fn operation_dispatch(options: &QueryOptions) -> Option<Arc<RetryDispatch>> {
    options.retry_runtime.as_ref()?.dispatch.as_ref()?.upgrade()
}

fn execute_observed<'a, S: StatementExecutor + ?Sized>(
    session: &'a mut S,
    sql: &'a str,
    options: QueryOptions,
    policy: Arc<PoolPolicy>,
) -> DbFuture<'a, u64> {
    Box::pin(async move {
        let dispatch = operation_dispatch(&options);
        let captured = captured_sql(sql, &policy, &options);
        if let Some(dispatch) = &dispatch {
            dispatch.start().await?;
        }
        let started = Instant::now();
        let mut observation = ObservationGuard::new(
            Operation::Execute,
            started,
            captured,
            Arc::clone(&policy),
            options.clone(),
        );
        let mut provider_options = options.clone();
        provider_options.policy_applied = true;
        let result = session.execute_statement(sql, provider_options).await;
        // An inner hook can stop this operation before reaching the provider. Preserve that
        // rejection through every outer wrapper: it is neither a write nor a statement event.
        if let Some(error) = dispatch.as_ref().and_then(|dispatch| dispatch.rejection()) {
            observation.disarm();
            return Err(error);
        }
        let result = result.map_err(normalize_write_outcome);
        if let Some(dispatch) = &dispatch {
            dispatch.finish(result.as_ref().map(|_| ()).map_err(|error| *error));
        }
        observation.finish(result.as_ref().map(|_| ()).map_err(|error| *error));
        result
    })
}

fn normalize_write_outcome(error: DbError) -> DbError {
    match error.kind() {
        ErrorKind::Connection | ErrorKind::Timeout | ErrorKind::Cancelled => {
            DbError::new(ErrorKind::WriteOutcomeUnknown)
        }
        _ => error,
    }
}

async fn collect_sets(reader: &mut dyn Reader, limit: RowLimit) -> Result<Vec<Vec<Row>>, DbError> {
    let mut sets = Vec::new();
    let mut rows = 0usize;
    let mut bytes = 0usize;
    loop {
        let mut set = Vec::new();
        while let Some(row) = reader.next_row_async().await? {
            if rows == limit.rows() {
                return Err(DbError::new(ErrorKind::Overflow));
            }
            let size = row.encoded_bytes()?;
            bytes = bytes
                .checked_add(size)
                .ok_or_else(|| DbError::new(ErrorKind::Overflow))?;
            if bytes > limit.bytes() {
                return Err(DbError::new(ErrorKind::Overflow));
            }
            rows = rows
                .checked_add(1)
                .ok_or_else(|| DbError::new(ErrorKind::Overflow))?;
            set.push(row);
        }
        sets.push(set);
        if !reader.has_next_set_async().await? {
            break;
        }
        if sets.len() == limit.max_sets() {
            return Err(DbError::new(ErrorKind::Overflow));
        }
    }
    reader.finish_async().await?;
    Ok(sets)
}

fn fetch_observed<'a, S: StatementExecutor + ?Sized>(
    session: &'a mut S,
    sql: &'a str,
    limit: RowLimit,
    options: QueryOptions,
    policy: Arc<PoolPolicy>,
) -> DbFuture<'a, BufferedReader> {
    Box::pin(async move {
        let dispatch = operation_dispatch(&options);
        let captured = captured_sql(sql, &policy, &options);
        if let Some(dispatch) = &dispatch {
            dispatch.start().await?;
        }
        let started = Instant::now();
        let mut observation = ObservationGuard::new(
            Operation::Fetch,
            started,
            captured,
            Arc::clone(&policy),
            options.clone(),
        );
        let result = async {
            let mut provider_options = options.clone();
            provider_options.policy_applied = true;
            let mut reader = session.fetch_statement(sql, provider_options).await?;
            collect_sets(reader.as_mut(), limit).await
        }
        .await;
        if let Some(error) = dispatch.as_ref().and_then(|dispatch| dispatch.rejection()) {
            observation.disarm();
            return Err(error);
        }
        if let Some(dispatch) = &dispatch {
            dispatch.finish(result.as_ref().map(|_| ()).map_err(|error| *error));
        }
        observation.finish(result.as_ref().map(|_| ()).map_err(|error| *error));
        result.map(BufferedReader::from_sets)
    })
}