Commit 7053a78
feat(stdlib): Sqlite prepared statements — db-theory #1b (Stmt + 10 typed externs)
Layers the prepared-statement surface on top of the #1a 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 extern surface (`stdlib/Sqlite.affine`)
- `pub 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 conv).
- `db_bind_text(s, idx, v) -> Int`
- `db_bind_null(s, idx) -> Int`
- `db_step(s) -> Int` — 1 if a row is available (`SQLITE_ROW`), 0 if
iteration is complete (`SQLITE_DONE`).
- `db_column_count(s) -> Int` — width of the current row.
- `db_column_int(s, idx) -> Int` — 0-indexed column read; NULL → 0.
- `db_column_text(s, idx) -> String` — 0-indexed column read; NULL → "".
- `db_reset(s) -> Int` — re-step from row 0 without recompiling.
- `db_finalize(s) -> Int` — release; handle invalid after.
Lifecycle + return-value conventions documented inline at the call
site. Bind-index is 1-based, column-index is 0-based — matches both
`jsr:@db/sqlite` and `better-sqlite3` (the two reference adapters
the `__as_sqlite` contract supports).
## Codegen lowerings (`lib/codegen_deno.ml`)
10 new entries in `deno_builtins`, each mapping the AffineScript extern
to a `__as_db*` JS helper. Each helper delegates to one
`globalThis.__as_sqlite.<method>` call, keeping the adapter contract
flat — the host adapter's `prepare`/`bindInt`/`bindText`/`bindNull`/
`step`/`columnCount`/`columnInt`/`columnText`/`reset`/`finalize`
methods are direct one-line passes through to the underlying sqlite
library.
NULL semantics handled at the JS-helper layer: `columnInt` / `columnText`
on a NULL column return `null` from the adapter, then coerce to `0` /
`""` respectively in the helper — matches the AffineScript-side type
signature (`Int` / `String`, no `Option`). Callers that need to
distinguish NULL from 0 / "" use the convenience `db_query_one` path
and inspect the JSON null in the result.
## Smoke harness (`tests/codegen-deno/sqlite_prepared.{affine,harness.mjs}`)
Six new smoke functions exercise the full prepared-statement lifecycle:
| 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 `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 + bind/step → 2 rows |
| `smoke_column_count_basic` | SELECT a, b, c → column_count = 3 |
The harness extends the #1a in-memory mock with the 10 statement-side
methods. Mock SQL parser stays narrow (CREATE / INSERT-with-binds /
SELECT / WHERE / ORDER BY / COUNT(*) / single-expression `SELECT a +
b`); harness commentary directs production deployments to the real
`__as_sqlite` adapter.
## 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`: **all 32** harnesses pass (was 31)
## Stacked on #522 (db-theory #1a)
#522 merged 12:43Z; this branch forks from the resulting main HEAD.
## db-theory #1c scope (next PR, separate)
- Schema introspection: `db_schema_tables() -> [String]`,
`db_schema_columns(table: String) -> [String]`
- Bulk I/O: `db_import_csv(path) -> Int`, `db_export_csv(path) -> Int`
- Typed error: `extern type DbError` + `Result<T, DbError>`-shaped
variants of the convenience surface
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent fffae8b commit 7053a78
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