day-sqlite-worker 0.3.0

Typed SQLite persistence and browser storage for Day apps
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
// Copyright © The Daybrite Project
// SPDX-License-Identifier: MPL-2.0

//! The SQLite engine behind day-persistence on web-dom (docs/persistence.md).
//!
//! The vendored amalgamation compiles with no libc and no wasm-bindgen: `vendor/shim` renames
//! every libc symbol it needs onto the freestanding C subset compiled beside it, and the
//! handful of Rust-side shims in this file (allocator, UTC localtime, entropy, abort). The
//! only imports the wasm module gains are `day_sql_*`: time, entropy and a log line, the
//! three channel calls, and, for the worker instance, the ten `day_sql_fs_*` OPFS primitives.
//!
//! The browser half ships with the crate, so nothing in the Day host page knows about SQL.
//! `[package.metadata.day.web]` (docs/web.md) declares `web/day-sql.js`, a web module the
//! page imports before it instantiates the app: it supplies those imports and boots
//! `web/day-sql-worker.js` beside it. The module instantiates in Day's raw-wasm pipeline.
//!
//! One wasm, two instantiations. The **app instance** may use [`Connection::open_memory`]
//! in-process (`:memory:` engines never touch a file) and reaches file databases through
//! `channel`. The **worker instance** services the SharedArrayBuffer channel: the worker
//! page copies each request into wasm memory and calls [`day_sql_exec`], which runs it
//! against real OPFS files through the day-opfs VFS and returns the reply bytes. The
//! [`protocol`] module is the wire format both sides share.
//!
//! Everything (VFS, connection layer, worker loop) compiles and unit-tests on native hosts
//! against an in-memory OPFS fake; only the browser glue is wasm-specific.
//!
//! The C tree under `vendor/` and the shim recipe come from sqlite-wasm-rs (MIT, see
//! `vendor/LICENSE`); SQLite itself is public domain.

#[allow(
    non_upper_case_globals,
    non_camel_case_types,
    non_snake_case,
    dead_code,
    improper_ctypes,
    clippy::all
)]
#[rustfmt::skip]
mod bindings;
pub mod protocol;
mod vfs;

use core::ffi::{c_char, c_int, c_long, c_void};
use std::cell::{Cell, RefCell};
use std::collections::HashMap;

use bindings as b;
use protocol::{Reply, Req, Value};

// -------------------------------------------------------------------------------------------
// Host services: time and entropy
// -------------------------------------------------------------------------------------------

mod os {
    #[cfg(all(target_family = "wasm", target_os = "unknown"))]
    mod imp {
        #[link(wasm_import_module = "env")]
        unsafe extern "C" {
            fn day_sql_now_ms() -> f64;
            fn day_sql_entropy(buf: *mut u8, len: usize);
            fn day_sql_log(msg: *const u8, len: usize);
        }
        pub fn now_ms() -> f64 {
            unsafe { day_sql_now_ms() }
        }
        pub fn entropy(buf: &mut [u8]) {
            unsafe { day_sql_entropy(buf.as_mut_ptr(), buf.len()) }
        }
        /// A traced statement, to the worker page's console (`[day-sql]` lines in devtools).
        pub fn log_line(s: &str) {
            unsafe { day_sql_log(s.as_ptr(), s.len()) }
        }
    }

    #[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
    mod imp {
        pub fn now_ms() -> f64 {
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_millis() as f64)
                .unwrap_or(0.0)
        }
        pub fn log_line(s: &str) {
            log::warn!("[day-sql] {s}");
        }
        pub fn entropy(buf: &mut [u8]) {
            // SQLite wants seed material, not key material. RandomState is std's OS-seeded
            // hasher, enough for temp names and PRNG seeding in the native test build.
            use std::hash::{BuildHasher, Hasher};
            let state = std::collections::hash_map::RandomState::new();
            for (i, chunk) in buf.chunks_mut(8).enumerate() {
                let mut h = state.build_hasher();
                h.write_usize(i);
                let bytes = h.finish().to_le_bytes();
                chunk.copy_from_slice(&bytes[..chunk.len()]);
            }
        }
    }

    pub(crate) use imp::*;
}

// -------------------------------------------------------------------------------------------
// The page side of the channel (web/day-sql.js)
// -------------------------------------------------------------------------------------------

/// The app instance's end of the SharedArrayBuffer channel: one synchronous round trip of
/// [`protocol`] bytes to the worker instance. Implemented by `web/day-sql.js`, the web module
/// this crate ships; a driver encodes a [`protocol::Req`], calls, and decodes the reply.
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
pub mod channel {
    #[link(wasm_import_module = "env")]
    unsafe extern "C" {
        /// 1 while the day-sql worker is up and the SharedArrayBuffer channel is live.
        fn day_sql_ready() -> i32;
        /// Send one request; answers the reply's byte length, or -1 with no channel.
        fn day_sql_call(req: *const u8, len: usize) -> f64;
        /// Copy the staged reply (`day_sql_call`'s length) into `dst`.
        fn day_sql_reply(dst: *mut u8);
    }

    /// Whether the worker is up. False without cross-origin isolation (COOP/COEP), where the
    /// page cannot share memory with a worker at all.
    pub fn ready() -> bool {
        unsafe { day_sql_ready() == 1 }
    }

    /// One round trip: the encoded request in, the encoded reply out. `None` when there is no
    /// channel, or when the worker stopped answering.
    pub fn call(req: &[u8]) -> Option<Vec<u8>> {
        let len = unsafe { day_sql_call(req.as_ptr(), req.len()) };
        if len < 0.0 {
            return None;
        }
        let mut reply = vec![0u8; len as usize];
        unsafe { day_sql_reply(reply.as_mut_ptr()) };
        Some(reply)
    }
}

// -------------------------------------------------------------------------------------------
// The C shims (vendor/shim/wasm-shim.h renames; recipe from sqlite-wasm-rs, MIT)
// -------------------------------------------------------------------------------------------

// dlmalloc's alignment: enough for every SQLite allocation.
const ALIGN: usize = core::mem::size_of::<usize>() * 2;

/// # Safety
/// C `malloc` contract; the size is stored ahead of the returned block for `free`/`realloc`.
#[no_mangle]
pub unsafe extern "C" fn rust_sqlite_wasm_malloc(size: usize) -> *mut c_void {
    let layout = core::alloc::Layout::from_size_align_unchecked(size + ALIGN, ALIGN);
    let ptr = std::alloc::alloc(layout);
    if ptr.is_null() {
        return core::ptr::null_mut();
    }
    *ptr.cast::<usize>() = size;
    ptr.add(ALIGN).cast()
}

/// # Safety
/// Only pointers from `rust_sqlite_wasm_malloc`/`realloc`.
#[no_mangle]
pub unsafe extern "C" fn rust_sqlite_wasm_free(ptr: *mut c_void) {
    let ptr: *mut u8 = ptr.sub(ALIGN).cast();
    let size = *(ptr.cast::<usize>());
    let layout = core::alloc::Layout::from_size_align_unchecked(size + ALIGN, ALIGN);
    std::alloc::dealloc(ptr, layout);
}

/// # Safety
/// Only pointers from `rust_sqlite_wasm_malloc`/`realloc`.
#[no_mangle]
pub unsafe extern "C" fn rust_sqlite_wasm_realloc(
    ptr: *mut c_void,
    new_size: usize,
) -> *mut c_void {
    let ptr: *mut u8 = ptr.sub(ALIGN).cast();
    let size = *(ptr.cast::<usize>());
    let layout = core::alloc::Layout::from_size_align_unchecked(size + ALIGN, ALIGN);
    let ptr = std::alloc::realloc(ptr, layout, new_size + ALIGN);
    if ptr.is_null() {
        return core::ptr::null_mut();
    }
    *ptr.cast::<usize>() = new_size;
    ptr.add(ALIGN).cast()
}

/// # Safety
/// C `calloc` contract.
#[no_mangle]
pub unsafe extern "C" fn rust_sqlite_wasm_calloc(num: usize, size: usize) -> *mut c_void {
    let Some(total) = num.checked_mul(size) else {
        return core::ptr::null_mut();
    };
    let ptr: *mut u8 = rust_sqlite_wasm_malloc(total).cast();
    if !ptr.is_null() {
        core::ptr::write_bytes(ptr, 0, total);
    }
    ptr.cast()
}

/// # Safety
/// wasi `getentropy` contract: fills `buf_len` bytes.
#[no_mangle]
pub unsafe extern "C" fn rust_sqlite_wasm_getentropy(buf: *mut u8, buf_len: usize) -> u16 {
    os::entropy(core::slice::from_raw_parts_mut(buf, buf_len));
    0
}

/// # Safety
/// C `__assert_fail` contract; an assertion in the C tree is a bug worth stopping on.
#[no_mangle]
pub unsafe extern "C" fn rust_sqlite_wasm_assert_fail(
    expr: *const c_char,
    file: *const c_char,
    line: c_int,
    _func: *const c_char,
) {
    let expr = core::ffi::CStr::from_ptr(expr).to_string_lossy();
    let file = core::ffi::CStr::from_ptr(file).to_string_lossy();
    panic!("sqlite assertion failed: {expr} ({file}:{line})");
}

/// # Safety
/// C `abort` contract.
#[no_mangle]
pub unsafe extern "C" fn rust_sqlite_wasm_abort() -> ! {
    #[cfg(all(target_family = "wasm", target_os = "unknown"))]
    core::arch::wasm32::unreachable();
    #[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
    std::process::abort();
}

/// musl's `struct tm` (what `<time.h>` in the vendored headers declares).
#[repr(C)]
pub struct Tm {
    tm_sec: c_int,
    tm_min: c_int,
    tm_hour: c_int,
    tm_mday: c_int,
    tm_mon: c_int,
    tm_year: c_int,
    tm_wday: c_int,
    tm_yday: c_int,
    tm_isdst: c_int,
    tm_gmtoff: c_long,
    tm_zone: *mut c_char,
}

/// UTC civil breakdown for `t` (Howard Hinnant's `civil_from_days`). No timezone database
/// exists in the browser sandbox, so SQLite's `'localtime'` modifier resolves to UTC here;
/// apps that need the viewer's zone format in the UI layer, where the platform knows it.
fn utc_breakdown(t: i64) -> Tm {
    let days = t.div_euclid(86_400);
    let secs = t.rem_euclid(86_400);
    let z = days + 719_468;
    let era = z.div_euclid(146_097);
    let doe = z - era * 146_097;
    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = yoe + era * 400 + i64::from(m <= 2);
    let leap = y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
    const CUM: [i64; 12] = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
    let yday = CUM[(m - 1) as usize] + i64::from(leap && m > 2) + d - 1;
    Tm {
        tm_sec: (secs % 60) as c_int,
        tm_min: ((secs / 60) % 60) as c_int,
        tm_hour: (secs / 3600) as c_int,
        tm_mday: d as c_int,
        tm_mon: (m - 1) as c_int,
        tm_year: (y - 1900) as c_int,
        tm_wday: (days + 4).rem_euclid(7) as c_int,
        tm_yday: yday as c_int,
        tm_isdst: 0,
        tm_gmtoff: 0,
        tm_zone: core::ptr::null_mut(),
    }
}

/// # Safety
/// C `localtime` contract: returns a shared static buffer, single-threaded callers only,
/// which this engine is by construction (SQLITE_THREADSAFE=0).
#[no_mangle]
pub unsafe extern "C" fn rust_sqlite_wasm_localtime(t: *const i64) -> *mut Tm {
    static mut TM: Tm = Tm {
        tm_sec: 0,
        tm_min: 0,
        tm_hour: 0,
        tm_mday: 0,
        tm_mon: 0,
        tm_year: 0,
        tm_wday: 0,
        tm_yday: 0,
        tm_isdst: 0,
        tm_gmtoff: 0,
        tm_zone: core::ptr::null_mut(),
    };
    let tm = core::ptr::addr_of_mut!(TM);
    *tm = utc_breakdown(*t);
    tm
}

/// # Safety
/// Called once by `sqlite3_initialize` (SQLITE_OS_OTHER contract): registers day-mem
/// (default) and day-opfs.
#[no_mangle]
pub unsafe extern "C" fn sqlite3_os_init() -> c_int {
    vfs::register_all()
}

/// # Safety
/// `sqlite3_shutdown` counterpart; nothing to unwind.
#[no_mangle]
pub unsafe extern "C" fn sqlite3_os_end() -> c_int {
    b::SQLITE_OK
}

// -------------------------------------------------------------------------------------------
// The connection layer
// -------------------------------------------------------------------------------------------

/// An engine failure: SQLite's result code and its message.
#[derive(Clone, Debug, PartialEq)]
pub struct SqlError {
    pub code: i32,
    pub message: String,
}

impl std::fmt::Display for SqlError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} (sqlite code {})", self.message, self.code)
    }
}

impl std::error::Error for SqlError {}

fn err(code: c_int, message: impl Into<String>) -> SqlError {
    SqlError {
        code,
        message: message.into(),
    }
}

/// One open database. `open_memory` works in any instance (no file I/O at all);
/// `open_opfs` routes through the day-opfs VFS and is the worker's shape.
/// A statement-trace sink (`Connection::trace_stmt`).
pub type TraceFn = Box<dyn Fn(&str)>;

pub struct Connection {
    db: *mut b::sqlite3,
    /// The installed statement-trace closure (`trace_stmt`), heap-boxed so the pointer the
    /// engine holds stays put while the Connection moves; dropped after `db` closes.
    trace: Option<Box<TraceFn>>,
}

unsafe extern "C" fn sql_trace_cb(
    ev: core::ffi::c_uint,
    ctx: *mut core::ffi::c_void,
    p: *mut core::ffi::c_void,
    x: *mut core::ffi::c_void,
) -> c_int {
    if ev != b::SQLITE_TRACE_STMT || ctx.is_null() {
        return 0;
    }
    // SAFETY: ctx is the connection's boxed trace closure, alive until after sqlite3_close
    // (field order in Connection); SQLITE_THREADSAFE=0 means no concurrent invocation.
    let f = unsafe { &*(ctx as *const TraceFn) };
    unsafe {
        let expanded = b::sqlite3_expanded_sql(p.cast());
        if !expanded.is_null() {
            if let Ok(s) = core::ffi::CStr::from_ptr(expanded).to_str() {
                f(s);
            }
            b::sqlite3_free(expanded.cast());
        } else if !x.is_null() {
            // The engine could not expand (OOM, or a trigger frame): the unexpanded text.
            if let Ok(s) = core::ffi::CStr::from_ptr(x.cast()).to_str() {
                f(s);
            }
        }
    }
    0
}

impl Connection {
    fn open_with(path: &str, vfs: Option<&core::ffi::CStr>) -> Result<Connection, SqlError> {
        let path = std::ffi::CString::new(path).map_err(|_| err(1, "NUL in database name"))?;
        let mut db: *mut b::sqlite3 = core::ptr::null_mut();
        let flags = b::SQLITE_OPEN_READWRITE | b::SQLITE_OPEN_CREATE;
        let rc = unsafe {
            b::sqlite3_open_v2(
                path.as_ptr(),
                &mut db,
                flags,
                vfs.map_or(core::ptr::null(), |v| v.as_ptr()),
            )
        };
        if rc != b::SQLITE_OK {
            let message = if db.is_null() {
                "out of memory opening database".to_string()
            } else {
                let m = unsafe { errmsg(db) };
                unsafe { b::sqlite3_close(db) };
                m
            };
            return Err(err(rc, message));
        }
        Ok(Connection { db, trace: None })
    }

    /// Install the engine's per-statement trace (`sqlite3_trace_v2`, SQLITE_TRACE_STMT): `f`
    /// sees every statement this connection executes, with bound parameters expanded.
    pub fn trace_stmt(&mut self, f: TraceFn) {
        let boxed: Box<TraceFn> = Box::new(f);
        let ctx = &*boxed as *const TraceFn as *mut core::ffi::c_void;
        // SAFETY: ctx points into `boxed`, stored on self below, so it is stable while the
        // engine holds it, and cleared implicitly when the connection closes.
        unsafe {
            b::sqlite3_trace_v2(self.db, b::SQLITE_TRACE_STMT, Some(sql_trace_cb), ctx);
        }
        self.trace = Some(boxed);
    }

    /// A private in-memory database, the only kind the main-thread app instance may open.
    pub fn open_memory() -> Result<Connection, SqlError> {
        Self::open_with(":memory:", None)
    }

    /// A named database through the day-opfs VFS (real OPFS in the worker, the in-memory
    /// fake in native tests).
    pub fn open_opfs(name: &str) -> Result<Connection, SqlError> {
        Self::open_with(name, Some(vfs::OPFS_VFS))
    }

    /// Run semicolon-separated statements with no parameters and no rows.
    pub fn execute_batch(&self, sql: &str) -> Result<(), SqlError> {
        let mut rest = sql.as_bytes();
        while !rest.is_empty() {
            let (stmt, tail) = self.prepare(rest)?;
            let Some(stmt) = stmt else {
                rest = tail;
                continue; // whitespace / comments
            };
            let rc = loop {
                match unsafe { b::sqlite3_step(stmt.0) } {
                    b::SQLITE_ROW => continue,
                    code => break code,
                }
            };
            if rc != b::SQLITE_DONE {
                return Err(err(rc, unsafe { errmsg(self.db) }));
            }
            drop(stmt);
            rest = tail;
        }
        Ok(())
    }

    /// Run one statement; the count of changed rows comes back.
    pub fn execute(&self, sql: &str, params: &[Value]) -> Result<u64, SqlError> {
        let (stmt, _) = self.prepare(sql.as_bytes())?;
        let stmt = stmt.ok_or_else(|| err(1, "empty statement"))?;
        self.bind(&stmt, params)?;
        match unsafe { b::sqlite3_step(stmt.0) } {
            b::SQLITE_DONE | b::SQLITE_ROW => Ok(unsafe { b::sqlite3_changes(self.db) } as u64),
            rc => Err(err(rc, unsafe { errmsg(self.db) })),
        }
    }

    /// Run one statement, delivering each row to `f`.
    pub fn query(
        &self,
        sql: &str,
        params: &[Value],
        f: &mut dyn FnMut(Vec<Value>),
    ) -> Result<(), SqlError> {
        self.query_impl(sql, params, false, &mut |_, row| f(row))
            .map(|_| ())
    }

    /// Query with result column names, including aliases. Names are returned even for no rows.
    pub fn query_named(
        &self,
        sql: &str,
        params: &[Value],
        f: &mut dyn FnMut(&[String], Vec<Value>),
    ) -> Result<Vec<String>, SqlError> {
        self.query_impl(sql, params, true, f)
    }

    fn query_impl(
        &self,
        sql: &str,
        params: &[Value],
        named: bool,
        f: &mut dyn FnMut(&[String], Vec<Value>),
    ) -> Result<Vec<String>, SqlError> {
        let (stmt, _) = self.prepare(sql.as_bytes())?;
        let stmt = stmt.ok_or_else(|| err(1, "empty statement"))?;
        self.bind(&stmt, params)?;
        // Step before reading metadata: SQLite may automatically reprepare on schema changes.
        let mut rc = unsafe { b::sqlite3_step(stmt.0) };
        let n = unsafe { b::sqlite3_column_count(stmt.0) };
        let names = if named {
            (0..n)
                .map(|i| {
                    let ptr = unsafe { b::sqlite3_column_name(stmt.0, i) };
                    if ptr.is_null() {
                        Err(err(
                            b::SQLITE_NOMEM,
                            "SQLite could not allocate a column name",
                        ))
                    } else {
                        Ok(unsafe { core::ffi::CStr::from_ptr(ptr) }
                            .to_string_lossy()
                            .into_owned())
                    }
                })
                .collect::<Result<Vec<_>, _>>()?
        } else {
            Vec::new()
        };
        loop {
            match rc {
                b::SQLITE_ROW => {
                    let mut row = Vec::with_capacity(n as usize);
                    for i in 0..n {
                        row.push(unsafe { column_value(stmt.0, i) });
                    }
                    f(&names, row);
                }
                b::SQLITE_DONE => return Ok(names),
                rc => return Err(err(rc, unsafe { errmsg(self.db) })),
            }
            rc = unsafe { b::sqlite3_step(stmt.0) };
        }
    }

    fn prepare<'a>(&self, sql: &'a [u8]) -> Result<(Option<Stmt>, &'a [u8]), SqlError> {
        let mut stmt: *mut b::sqlite3_stmt = core::ptr::null_mut();
        let mut tail: *const c_char = core::ptr::null();
        let rc = unsafe {
            b::sqlite3_prepare_v2(
                self.db,
                sql.as_ptr().cast(),
                sql.len() as c_int,
                &mut stmt,
                &mut tail,
            )
        };
        if rc != b::SQLITE_OK {
            return Err(err(rc, unsafe { errmsg(self.db) }));
        }
        let consumed = if tail.is_null() {
            sql.len()
        } else {
            unsafe { tail.offset_from(sql.as_ptr().cast()) as usize }
        };
        Ok((
            (!stmt.is_null()).then_some(Stmt(stmt)),
            &sql[consumed.min(sql.len())..],
        ))
    }

    fn bind(&self, stmt: &Stmt, params: &[Value]) -> Result<(), SqlError> {
        // SQLITE_TRANSIENT: SQLite copies the buffer before the call returns.
        let transient: b::sqlite3_destructor_type = unsafe { core::mem::transmute(-1isize) };
        for (i, v) in params.iter().enumerate() {
            let i = i as c_int + 1;
            let rc = unsafe {
                match v {
                    Value::Null => b::sqlite3_bind_null(stmt.0, i),
                    Value::Int(n) => b::sqlite3_bind_int64(stmt.0, i, *n),
                    Value::Real(r) => b::sqlite3_bind_double(stmt.0, i, *r),
                    Value::Text(t) => b::sqlite3_bind_text(
                        stmt.0,
                        i,
                        t.as_ptr().cast(),
                        t.len() as c_int,
                        transient,
                    ),
                    Value::Blob(bl) => b::sqlite3_bind_blob(
                        stmt.0,
                        i,
                        bl.as_ptr().cast(),
                        bl.len() as c_int,
                        transient,
                    ),
                }
            };
            if rc != b::SQLITE_OK {
                return Err(err(rc, unsafe { errmsg(self.db) }));
            }
        }
        Ok(())
    }
}

impl Drop for Connection {
    fn drop(&mut self) {
        unsafe { b::sqlite3_close(self.db) };
    }
}

/// Finalized on drop, so every prepare path cleans up.
struct Stmt(*mut b::sqlite3_stmt);

impl Drop for Stmt {
    fn drop(&mut self) {
        unsafe { b::sqlite3_finalize(self.0) };
    }
}

unsafe fn errmsg(db: *mut b::sqlite3) -> String {
    let p = b::sqlite3_errmsg(db);
    if p.is_null() {
        "unknown sqlite error".to_string()
    } else {
        core::ffi::CStr::from_ptr(p).to_string_lossy().into_owned()
    }
}

unsafe fn column_value(stmt: *mut b::sqlite3_stmt, i: c_int) -> Value {
    match b::sqlite3_column_type(stmt, i) {
        b::SQLITE_INTEGER => Value::Int(b::sqlite3_column_int64(stmt, i)),
        b::SQLITE_FLOAT => Value::Real(b::sqlite3_column_double(stmt, i)),
        b::SQLITE_TEXT => {
            let p = b::sqlite3_column_text(stmt, i);
            let n = b::sqlite3_column_bytes(stmt, i) as usize;
            let bytes = if p.is_null() {
                &[][..]
            } else {
                core::slice::from_raw_parts(p, n)
            };
            Value::Text(String::from_utf8_lossy(bytes).into_owned())
        }
        b::SQLITE_BLOB => {
            let p = b::sqlite3_column_blob(stmt, i);
            let n = b::sqlite3_column_bytes(stmt, i) as usize;
            let bytes = if p.is_null() {
                &[][..]
            } else {
                core::slice::from_raw_parts(p.cast::<u8>(), n)
            };
            Value::Blob(bytes.to_vec())
        }
        _ => Value::Null,
    }
}

// -------------------------------------------------------------------------------------------
// The worker loop
// -------------------------------------------------------------------------------------------

thread_local! {
    static CONNS: RefCell<HashMap<u32, Connection>> = RefCell::new(HashMap::new());
    static NEXT_CONN: Cell<u32> = const { Cell::new(1) };
    /// The staged reply `day_sql_exec` hands the worker page: `[len: u32 LE][bytes]`. Lives
    /// until the next request; the channel is strictly one request at a time.
    static REPLY: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
}

/// Service one request: the worker loop's whole body, and the entry point the native tests
/// drive.
pub fn handle_request(req: &[u8]) -> Vec<u8> {
    let reply = match protocol::decode_req(req) {
        Err(_) => Reply::Err("malformed request".to_string()),
        Ok(req) => dispatch(req),
    };
    protocol::encode_reply(&reply)
}

fn dispatch(req: Req) -> Reply {
    match req {
        Req::Open { name, trace } => match Connection::open_opfs(&name) {
            Ok(mut conn) => {
                if trace {
                    conn.trace_stmt(Box::new(os::log_line));
                }
                let id = NEXT_CONN.with(|n| {
                    let id = n.get();
                    n.set(id.wrapping_add(1).max(1));
                    id
                });
                CONNS.with(|c| c.borrow_mut().insert(id, conn));
                Reply::Conn(id)
            }
            Err(e) => Reply::Err(e.to_string()),
        },
        Req::Close { conn } => {
            CONNS.with(|c| c.borrow_mut().remove(&conn));
            Reply::Ok
        }
        Req::Batch { conn, sql } => with_conn(conn, |c| c.execute_batch(&sql).map(|_| Reply::Ok)),
        Req::Exec { conn, sql, params } => {
            with_conn(conn, |c| c.execute(&sql, &params).map(Reply::Changes))
        }
        Req::Query { conn, sql, params } => with_conn(conn, |c| {
            let mut rows = Vec::new();
            c.query(&sql, &params, &mut |row| rows.push(row))
                .map(|_| Reply::Rows(rows))
        }),
        Req::QueryNamed { conn, sql, params } => with_conn(conn, |c| {
            let mut rows = Vec::new();
            c.query_named(&sql, &params, &mut |_, row| rows.push(row))
                .map(|names| Reply::NamedRows { names, rows })
        }),
        Req::Exists { name } => Reply::Bool(vfs::fsx::exists(&name)),
        Req::List => Reply::Names(vfs::fsx::list()),
        Req::Delete { name } => {
            vfs::fsx::delete(&name);
            Reply::Ok
        }
        Req::Export { name } => match vfs::fsx::read_all(&name) {
            Some(bytes) => Reply::Bytes(bytes),
            None => Reply::Err(format!("no database named {name:?}")),
        },
        Req::Import { name, bytes } => {
            if vfs::fsx::write_all(&name, &bytes) {
                Reply::Ok
            } else {
                Reply::Err(format!("could not write {name:?}"))
            }
        }
    }
}

fn with_conn(id: u32, f: impl FnOnce(&Connection) -> Result<Reply, SqlError>) -> Reply {
    CONNS.with(|c| {
        // Take the connection out for the call: statement callbacks must not find the
        // registry borrowed.
        let Some(conn) = c.borrow_mut().remove(&id) else {
            return Reply::Err(format!("no connection {id}"));
        };
        let reply = match f(&conn) {
            Ok(r) => r,
            Err(e) => Reply::Err(e.to_string()),
        };
        c.borrow_mut().insert(id, conn);
        reply
    })
}

/// Allocate `len` bytes for the worker page to write a request into (consumed by
/// [`day_sql_exec`]).
#[no_mangle]
pub extern "C" fn day_sql_alloc(len: usize) -> *mut u8 {
    let mut v = Vec::<u8>::with_capacity(len);
    let ptr = v.as_mut_ptr();
    core::mem::forget(v);
    ptr
}

/// # Safety
/// `ptr` must be a `day_sql_alloc(len)` buffer holding a complete request; ownership
/// transfers here. Returns a pointer to `[reply_len: u32 LE][reply bytes]`, valid until the
/// next call.
#[no_mangle]
pub unsafe extern "C" fn day_sql_exec(ptr: *mut u8, len: usize) -> *const u8 {
    let req = Vec::from_raw_parts(ptr, len, len);
    let reply = handle_request(&req);
    REPLY.with(|r| {
        let mut r = r.borrow_mut();
        r.clear();
        r.extend_from_slice(&(reply.len() as u32).to_le_bytes());
        r.extend_from_slice(&reply);
        r.as_ptr()
    })
}

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

    /// The engine is compiled `SQLITE_THREADSAFE=0`, correct for the single-threaded worker,
    /// but the test harness runs on parallel threads, and concurrent first-opens race
    /// `sqlite3_initialize`'s unsynchronized VFS registration (a test then nondeterministically
    /// fails with "no such vfs: day-opfs"; first caught on the Linux CI leg). Every test takes
    /// this lock so the engine is only ever entered from one thread at a time, matching the
    /// worker's contract. Poisoning is ignored: one failing test must not cascade.
    static ENGINE: std::sync::Mutex<()> = std::sync::Mutex::new(());

    fn engine() -> std::sync::MutexGuard<'static, ()> {
        ENGINE.lock().unwrap_or_else(|e| e.into_inner())
    }

    fn ask(req: Req) -> Reply {
        protocol::decode_reply(&handle_request(&protocol::encode_req(&req))).expect("well-formed")
    }

    fn open(name: &str) -> u32 {
        match ask(Req::Open {
            name: name.into(),
            trace: false,
        }) {
            Reply::Conn(id) => id,
            other => panic!("open {name}: {other:?}"),
        }
    }

    #[test]
    fn memory_connection_round_trips_every_value_kind() {
        let _engine = engine();
        let conn = Connection::open_memory().expect("open");
        conn.execute_batch("CREATE TABLE t (a, b, c, d, e)")
            .expect("create");
        let params = vec![
            Value::Null,
            Value::Int(i64::MIN),
            Value::Real(2.5),
            Value::Text("héllo — quote' and \u{1f}".into()),
            Value::Blob(vec![0, 1, 255, 0]),
        ];
        let n = conn
            .execute("INSERT INTO t VALUES (?, ?, ?, ?, ?)", &params)
            .expect("insert");
        assert_eq!(n, 1);
        let mut rows = Vec::new();
        conn.query("SELECT a, b, c, d, e FROM t", &[], &mut |r| rows.push(r))
            .expect("select");
        assert_eq!(rows, vec![params]);
    }

    #[test]
    fn batch_runs_multiple_statements_and_reports_the_failing_one() {
        let _engine = engine();
        let conn = Connection::open_memory().expect("open");
        conn.execute_batch(
            "CREATE TABLE a (x); -- comment\nCREATE TABLE b (y);\nINSERT INTO a VALUES (1);",
        )
        .expect("batch");
        let mut count = Vec::new();
        conn.query("SELECT count(*) FROM a", &[], &mut |r| count.push(r))
            .expect("count");
        assert_eq!(count, vec![vec![Value::Int(1)]]);

        let e = conn
            .execute_batch("INSERT INTO a VALUES (2); INSERT INTO nope VALUES (3);")
            .expect_err("second statement is bad");
        assert!(e.message.contains("nope"), "{e}");
    }

    #[test]
    fn transactions_roll_back() {
        let _engine = engine();
        let conn = Connection::open_memory().expect("open");
        conn.execute_batch("CREATE TABLE t (x); BEGIN; INSERT INTO t VALUES (1); ROLLBACK;")
            .expect("batch");
        let mut n = Vec::new();
        conn.query("SELECT count(*) FROM t", &[], &mut |r| n.push(r))
            .expect("count");
        assert_eq!(n, vec![vec![Value::Int(0)]]);
    }

    #[test]
    fn fts5_and_rtree_are_compiled_in() {
        let _engine = engine();
        let conn = Connection::open_memory().expect("open");
        conn.execute_batch(
            "CREATE VIRTUAL TABLE ft USING fts5(body);\
             CREATE VIRTUAL TABLE geo USING rtree(id, minx, maxx, miny, maxy);",
        )
        .expect("both engines present");
        conn.execute(
            "INSERT INTO ft VALUES (?)",
            &[Value::Text("golden harbor light".into())],
        )
        .expect("insert");
        let mut hits = Vec::new();
        conn.query(
            "SELECT count(*) FROM ft WHERE ft MATCH ?",
            &[Value::Text("harbor".into())],
            &mut |r| hits.push(r),
        )
        .expect("match");
        assert_eq!(hits, vec![vec![Value::Int(1)]]);
    }

    #[test]
    fn dates_work_without_a_timezone_database() {
        let _engine = engine();
        let conn = Connection::open_memory().expect("open");
        let mut out = Vec::new();
        conn.query(
            "SELECT datetime(0, 'unixepoch'), date('now'), datetime(86400, 'unixepoch', 'localtime')",
            &[],
            &mut |r| out.push(r),
        )
        .expect("dates");
        let row = &out[0];
        assert_eq!(row[0], Value::Text("1970-01-01 00:00:00".into()));
        let Value::Text(today) = &row[1] else {
            panic!("date('now'): {row:?}")
        };
        assert!(today.starts_with("20"), "sane current year: {today}");
        // 'localtime' is UTC here (no tz database in the sandbox).
        assert_eq!(row[2], Value::Text("1970-01-02 00:00:00".into()));
    }

    #[test]
    fn utc_breakdown_matches_known_dates() {
        let _engine = engine();
        let tm = utc_breakdown(1_787_616_000); // 2026-08-25 00:00:00 UTC, a Tuesday
        assert_eq!(
            (tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_wday, tm.tm_yday),
            (126, 7, 25, 2, 236)
        );
        let tm = utc_breakdown(0);
        assert_eq!(
            (tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_wday, tm.tm_yday),
            (70, 0, 1, 4, 0)
        );
        // Leap-year boundary: 2024-12-31 is yday 365.
        let tm = utc_breakdown(1_735_603_200);
        assert_eq!(
            (tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_yday),
            (124, 11, 31, 365)
        );
    }

    #[test]
    fn opfs_database_persists_across_close_and_reopen() {
        let _engine = engine();
        let conn = Connection::open_opfs("persist.db").expect("open");
        conn.execute_batch("CREATE TABLE t (x)").expect("create");
        for i in 0..100 {
            conn.execute("INSERT INTO t VALUES (?)", &[Value::Int(i)])
                .expect("insert");
        }
        drop(conn);

        let conn = Connection::open_opfs("persist.db").expect("reopen");
        let mut n = Vec::new();
        conn.query("SELECT count(*), sum(x) FROM t", &[], &mut |r| n.push(r))
            .expect("count");
        assert_eq!(n, vec![vec![Value::Int(100), Value::Int(4950)]]);
        drop(conn);
        vfs::fsx::delete("persist.db");
    }

    #[test]
    fn commits_leave_no_journal_behind() {
        let _engine = engine();
        let conn = Connection::open_opfs("clean.db").expect("open");
        conn.execute_batch("CREATE TABLE t (x); BEGIN; INSERT INTO t VALUES (1); COMMIT;")
            .expect("batch");
        drop(conn);
        let names = vfs::fsx::list();
        assert!(
            names.iter().any(|n| n == "clean.db"),
            "database listed: {names:?}"
        );
        assert!(
            !names.iter().any(|n| n.contains("journal")),
            "journal deleted after commit: {names:?}"
        );
        vfs::fsx::delete("clean.db");
    }

    #[test]
    fn worker_batch_migration_is_atomic_and_survives_reopen() {
        let _engine = engine();
        let conn = open("migration.db");
        assert_eq!(ask(Req::Batch { conn, sql: "CREATE TABLE old_books(id TEXT PRIMARY KEY, title TEXT); INSERT INTO old_books VALUES ('fixture', 'Preserved');".into() }), Reply::Ok);
        assert_eq!(
            ask(Req::Exec {
                conn,
                sql: "BEGIN".into(),
                params: vec![]
            }),
            Reply::Changes(1)
        );
        let failed = ask(Req::Batch {
            conn,
            sql: "ALTER TABLE old_books RENAME TO books; INSERT INTO missing VALUES (1);".into(),
        });
        assert!(matches!(failed, Reply::Err(_)));
        assert!(matches!(
            ask(Req::Exec {
                conn,
                sql: "ROLLBACK".into(),
                params: vec![]
            }),
            Reply::Changes(_)
        ));
        assert_eq!(
            ask(Req::Query {
                conn,
                sql: "SELECT title FROM old_books".into(),
                params: vec![]
            }),
            Reply::Rows(vec![vec![Value::Text("Preserved".into())]])
        );
        assert_eq!(ask(Req::Batch { conn, sql: "BEGIN; CREATE TABLE books(id TEXT PRIMARY KEY, title TEXT, opened INTEGER NOT NULL DEFAULT 0); INSERT INTO books(id, title) SELECT id, title FROM old_books; DROP TABLE old_books; COMMIT;".into() }), Reply::Ok);
        assert_eq!(ask(Req::Close { conn }), Reply::Ok);
        let conn = open("migration.db");
        assert_eq!(
            ask(Req::Query {
                conn,
                sql: "SELECT id, title, opened FROM books".into(),
                params: vec![]
            }),
            Reply::Rows(vec![vec![
                Value::Text("fixture".into()),
                Value::Text("Preserved".into()),
                Value::Int(0)
            ]])
        );
        assert_eq!(ask(Req::Close { conn }), Reply::Ok);
        vfs::fsx::delete("migration.db");
    }

    #[test]
    fn worker_protocol_full_round_trip() {
        let _engine = engine();
        let conn = open("proto.db");
        assert_eq!(
            ask(Req::Batch {
                conn,
                sql: "CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT)".into()
            }),
            Reply::Ok
        );
        assert_eq!(
            ask(Req::Exec {
                conn,
                sql: "INSERT INTO notes (id, body) VALUES (?, ?)".into(),
                params: vec![Value::Int(1), Value::Text("rain later".into())],
            }),
            Reply::Changes(1)
        );
        assert_eq!(
            ask(Req::Query {
                conn,
                sql: "SELECT id, body FROM notes".into(),
                params: vec![],
            }),
            Reply::Rows(vec![vec![Value::Int(1), Value::Text("rain later".into())]])
        );
        assert_eq!(
            ask(Req::QueryNamed {
                conn,
                sql: "SELECT id AS key, body AS title FROM notes WHERE id=?".into(),
                params: vec![Value::Int(1)]
            }),
            Reply::NamedRows {
                names: vec!["key".into(), "title".into()],
                rows: vec![vec![Value::Int(1), Value::Text("rain later".into())]]
            }
        );
        assert_eq!(
            ask(Req::QueryNamed {
                conn,
                sql: "SELECT body AS title FROM notes WHERE id=?".into(),
                params: vec![Value::Int(99)]
            }),
            Reply::NamedRows {
                names: vec!["title".into()],
                rows: vec![]
            }
        );
        // Errors carry SQLite's message, and the connection survives them.
        let Reply::Err(msg) = ask(Req::Query {
            conn,
            sql: "SELECT nope FROM notes".into(),
            params: vec![],
        }) else {
            panic!("bad column must error")
        };
        assert!(msg.contains("nope"), "{msg}");
        assert_eq!(
            ask(Req::Query {
                conn,
                sql: "SELECT count(*) FROM notes".into(),
                params: vec![],
            }),
            Reply::Rows(vec![vec![Value::Int(1)]])
        );
        assert_eq!(ask(Req::Close { conn }), Reply::Ok);
        let Reply::Err(gone) = ask(Req::Exec {
            conn,
            sql: "INSERT INTO notes (id) VALUES (2)".into(),
            params: vec![],
        }) else {
            panic!("closed connection must error")
        };
        assert!(gone.contains("no connection"), "{gone}");
        assert_eq!(
            ask(Req::Delete {
                name: "proto.db".into()
            }),
            Reply::Ok
        );
    }

    #[test]
    fn pool_verbs_export_import_and_list() {
        let _engine = engine();
        let conn = open("source.db");
        assert_eq!(
            ask(Req::Batch {
                conn,
                sql: "CREATE TABLE t (x); INSERT INTO t VALUES (42);".into()
            }),
            Reply::Ok
        );
        assert_eq!(ask(Req::Close { conn }), Reply::Ok);

        assert_eq!(
            ask(Req::Exists {
                name: "source.db".into()
            }),
            Reply::Bool(true)
        );
        assert_eq!(
            ask(Req::Exists {
                name: "nope.db".into()
            }),
            Reply::Bool(false)
        );

        let Reply::Bytes(image) = ask(Req::Export {
            name: "source.db".into(),
        }) else {
            panic!("export must answer bytes")
        };
        assert!(
            image.starts_with(b"SQLite format 3\0"),
            "a real SQLite file image"
        );

        assert_eq!(
            ask(Req::Import {
                name: "copy.db".into(),
                bytes: image
            }),
            Reply::Ok
        );
        let copy = open("copy.db");
        assert_eq!(
            ask(Req::Query {
                conn: copy,
                sql: "SELECT x FROM t".into(),
                params: vec![],
            }),
            Reply::Rows(vec![vec![Value::Int(42)]])
        );
        assert_eq!(ask(Req::Close { conn: copy }), Reply::Ok);

        let Reply::Names(names) = ask(Req::List) else {
            panic!("list must answer names")
        };
        assert!(names.contains(&"source.db".to_string()), "{names:?}");
        assert!(names.contains(&"copy.db".to_string()), "{names:?}");

        assert_eq!(
            ask(Req::Delete {
                name: "source.db".into()
            }),
            Reply::Ok
        );
        assert_eq!(
            ask(Req::Delete {
                name: "copy.db".into()
            }),
            Reply::Ok
        );
        assert_eq!(
            ask(Req::Exists {
                name: "source.db".into()
            }),
            Reply::Bool(false)
        );
    }

    #[test]
    fn two_databases_are_independent() {
        let _engine = engine();
        let a = open("ind-a.db");
        let bconn = open("ind-b.db");
        assert_eq!(
            ask(Req::Batch {
                conn: a,
                sql: "CREATE TABLE only_a (x)".into()
            }),
            Reply::Ok
        );
        let Reply::Err(msg) = ask(Req::Query {
            conn: bconn,
            sql: "SELECT * FROM only_a".into(),
            params: vec![],
        }) else {
            panic!("b must not see a's table")
        };
        assert!(msg.contains("only_a"), "{msg}");
        assert_eq!(ask(Req::Close { conn: a }), Reply::Ok);
        assert_eq!(ask(Req::Close { conn: bconn }), Reply::Ok);
        assert_eq!(
            ask(Req::Delete {
                name: "ind-a.db".into()
            }),
            Reply::Ok
        );
        assert_eq!(
            ask(Req::Delete {
                name: "ind-b.db".into()
            }),
            Reply::Ok
        );
    }

    #[test]
    fn trace_stmt_sees_every_statement_with_parameters_expanded() {
        let _engine = engine();
        use std::cell::RefCell;
        use std::rc::Rc;
        let seen: Rc<RefCell<Vec<String>>> = Rc::new(RefCell::new(Vec::new()));
        let sink = seen.clone();
        let mut conn = Connection::open_memory().expect("open");
        conn.trace_stmt(Box::new(move |s| sink.borrow_mut().push(s.to_string())));
        conn.execute_batch("CREATE TABLE t (a, b)").expect("create");
        conn.execute(
            "INSERT INTO t VALUES (?, ?)",
            &[Value::Int(7), Value::Text("rain later".into())],
        )
        .expect("insert");
        let mut n = Vec::new();
        conn.query("SELECT count(*) FROM t", &[], &mut |r| n.push(r))
            .expect("count");
        let log = seen.borrow();
        assert!(log.iter().any(|s| s.contains("CREATE TABLE t")), "{log:?}");
        assert!(
            log.iter()
                .any(|s| s.contains("INSERT INTO t VALUES (7, 'rain later')")),
            "parameters expand: {log:?}"
        );
        assert!(log.iter().any(|s| s.contains("SELECT count(*)")), "{log:?}");
    }

    #[test]
    fn malformed_requests_answer_an_error() {
        let _engine = engine();
        let reply = protocol::decode_reply(&handle_request(&[9, 9, 9])).expect("well-formed");
        assert_eq!(reply, Reply::Err("malformed request".to_string()));
    }

    #[test]
    fn day_sql_exec_stages_a_length_prefixed_reply() {
        let _engine = engine();
        let req = protocol::encode_req(&Req::List);
        let ptr = day_sql_alloc(req.len());
        // SAFETY: writing exactly len bytes into a day_sql_alloc(len) buffer, then handing
        // ownership to day_sql_exec, its documented contract.
        let reply = unsafe {
            core::ptr::copy_nonoverlapping(req.as_ptr(), ptr, req.len());
            let out = day_sql_exec(ptr, req.len());
            let len = u32::from_le_bytes(core::slice::from_raw_parts(out, 4).try_into().unwrap());
            core::slice::from_raw_parts(out.add(4), len as usize).to_vec()
        };
        assert!(matches!(
            protocol::decode_reply(&reply),
            Ok(Reply::Names(_))
        ));
    }
}