Commit 0f88028
feat(stdlib): Sqlite prepared statements — db-theory #1b (Stmt + 10 typed externs) (#524)
## Summary
Layers the prepared-statement surface on top of the #522 convenience
surface. Use this for anything carrying **user input**, anything
iterating over **more than a handful of rows**, and anything where
**typed value marshalling** matters (Int / Text / NULL).
## New stdlib surface (`stdlib/Sqlite.affine`)
| extern | purpose |
|---|---|
| `extern type Stmt` | opaque statement handle |
| `db_prepare(d, sql) -> Stmt` | compile SQL with `?` placeholders |
| `db_bind_int(s, idx, v) -> Int` | bind 1-indexed param (sqlite3
convention) |
| `db_bind_text(s, idx, v) -> Int` | bind text |
| `db_bind_null(s, idx) -> Int` | bind NULL |
| `db_step(s) -> Int` | `1` = `SQLITE_ROW`, `0` = `SQLITE_DONE` |
| `db_column_count(s) -> Int` | current-row width |
| `db_column_int(s, idx) -> Int` | 0-indexed read; NULL → 0 |
| `db_column_text(s, idx) -> String` | 0-indexed read; NULL → "" |
| `db_reset(s) -> Int` | re-step from row 0 without recompiling |
| `db_finalize(s) -> Int` | release |
Bind-index is 1-based, column-index is 0-based — matches both
`jsr:@db/sqlite` and `better-sqlite3` (the two reference `__as_sqlite`
adapters). NULL coercion happens at the JS-helper layer; callers needing
NULL/value-zero discrimination use the convenience `db_query_one` path.
## Smoke harness
(`tests/codegen-deno/sqlite_prepared.{affine,harness.mjs}`)
| smoke fn | exercises |
|---|---|
| `smoke_prepare_bind_int_step_finalize` | `INSERT(?, ?)` with two int
binds + finalize + query-back |
| `smoke_step_iteration` | `SELECT` N rows, loop `db_step` until 0,
accumulate via `db_column_int` |
| `smoke_text_bind_and_column` | bind_text → step → column_text
round-trip |
| `smoke_null_bind` | bind_null → column_int coerces to 0 |
| `smoke_reset_and_reuse` | prepare once + bind/step + reset +
re-bind/step → 2 rows |
| `smoke_column_count_basic` | `SELECT a, b, c FROM t` → column_count =
3 |
Mock extends #522's in-memory adapter with 10 statement-side methods;
production adapters (`jsr:@db/sqlite` / `better-sqlite3`) provide
near-1:1 wrappers.
## Verification
- `dune build bin/main.exe`: clean
- `dune runtest`: **363 / 363** green (codegen ⊆ stdlib consistency
picks up the 10 new builtins; all match `stdlib/Sqlite.affine`)
- `tools/run_codegen_deno_tests.sh`: **32 / 32** harnesses pass (was 31)
## Stacked on #522
#522 merged 12:43Z; this branch forks from the resulting main HEAD.
Clean diff.
## db-theory #1c (next PR, separate)
- Schema introspection (`db_schema_tables`, `db_schema_columns`)
- Bulk I/O (`db_import_csv`, `db_export_csv`)
- Typed `DbError` + `Result<T, DbError>` variants
## Test plan
- [x] dune build clean
- [x] dune runtest 363/363 green
- [x] tools/run_codegen_deno_tests.sh 32/32 pass
- [x] 6 prepared-statement smoke assertions pass
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 5ebdba2 commit 0f88028
4 files changed
Lines changed: 485 additions & 4 deletions
File tree
- lib
- stdlib
- tests/codegen-deno
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
516 | 516 | | |
517 | 517 | | |
518 | 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 | + | |
519 | 544 | | |
520 | 545 | | |
521 | 546 | | |
| |||
804 | 829 | | |
805 | 830 | | |
806 | 831 | | |
807 | | - | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
808 | 844 | | |
809 | 845 | | |
810 | 846 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
9 | 23 | | |
10 | 24 | | |
11 | 25 | | |
12 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
13 | 30 | | |
14 | 31 | | |
15 | 32 | | |
16 | 33 | | |
17 | 34 | | |
18 | 35 | | |
19 | 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments