Files
turso/testing/conformance/javascript
Preston Thorpe 6ac915124f Merge 'batch API: full per-statement results in every driver, fixing a complaint we've carried since libSQL' from Glauber Costa
Ever since the libSQL days, the batch API has had the same complaint:
you
could not get the result of each individual statement, and when a batch
failed you could not tell which statement failed — or what had already
taken effect. With Turso we have a blank slate, and it turns out the
protocol never was the limitation: a batch response carries complete
per-statement information (results, last insert rowid, error codes, and
the execution statistics `rows_read`, `rows_written`,
`query_duration_ms`; PROTOCOL.md §6.2, §8.4, §9.1). The drivers just
dropped it. This PR takes the opportunity to do it right.
Every driver now exposes one `batch()` API that returns everything the
protocol reports, per statement, in one HTTP round trip:
- **Per-statement results**: rows, affected row count, last insert
  rowid, and the server-side execution statistics.
- **Failure visibility**: statements execute in order and stop at the
  first failure; the error identifies the failing statement by index
  *and* carries one entry per statement — the completed statement's
  result, or null/None for the failing statement and the ones that never
  ran. In a non-atomic batch the completed statements are committed, and
  the caller can finally see exactly what took effect.
- **Atomic mode**: the transactional variant wraps the statements in
  `BEGIN <mode>` / `COMMIT` with a conditional `ROLLBACK`, carried by
the
  same single request.
Per driver:
- **JavaScript** (`serverless/javascript`, existing public API —
extended
  compatibly): each `ResultSet` gains `lastInsertRowid`, `rowsRead`,
  `rowsWritten`, `queryDurationMs`; the thrown `DatabaseError` gains
  `batchIndex` and `batchResults`. `batch()` moves from the cursor
  endpoint to a pipeline batch request because the cursor stream's
  `step_end` does not carry the statistics. Non-atomic batches are now
  gated on the previous statement succeeding — previously statements
  after a failure kept executing server-side.
- **Rust** (new API, `bindings/rust` + `serverless/rust` in one commit
  per the driver-parity rule): `Connection::batch` /
  `Connection::transactional_batch`,
`BatchStatement`/`IntoBatchStatement`
  inputs, `BatchResult` with stats accessors, and
  `Error::BatchStatementFailed { index, error, results }`. The sync
  driver shares the embedded `Connection`, so all three backends expose
  the same API.
- **Python** (new API, `bindings/python` + `serverless/python` in one
  commit): `Connection.batch(statements, mode=None)` on the DB-API
  surface (sync, aio, and the sync-database driver via inheritance),
  raising the classified DB-API exception with `batch_index` and
  `batch_results` attributes.
The parity requirement itself is codified first, in
`serverless/conformance/differential/README.md` ("Driver API parity")
with a pointer from the AGENTS.md core principles.
**Deliberately skipped: Go.** The Go serverless driver is a plain
`database/sql` driver, and `database/sql` has no batch surface, so it is
less obvious what to do there: libsql-client-go's precedent is splitting
multi-statement SQL client-side (with a vendored ANTLR SQLite parser)
inside `Exec`, while the alternative is a driver-specific `Batch` API
outside the standard interface. That trade-off deserves its own
discussion and PR.
The batch behavior is property-tested: a `param_batch` operation in the
shared differential vocabulary (`spec/ops.json`) generates 1-4
parameterized statements — including planted failures and atomic modes —
and all three language harnesses (fast-check, hypothesis, hegel) assert
that the embedded and serverless drivers agree on every statement's
result, the failing index, and the partial results carried by the error.
Building those harnesses caught two real driver bugs before the tests
ever ran: the embedded JS driver had no batch error surface at all, and
the embedded Rust driver reported the connection's stale change counter
for row-returning statements inside a batch.
Follow-ups, out of scope here:
- Whether the JS `compat` (libsql-compat) layer should surface the new
  per-statement `ResultSet` fields on success; it is kept libsql-shaped
  here (the error path does preserve `batchIndex`/`batchResults`).
- Driver-wide preservation of `extended_code` on errors (today only the
  Python serverless `ServerError` keeps it).
- A batch entry containing multi-statement SQL ("a; b") errors on the
  embedded drivers but the server prepares only the first statement — an
  embedded/serverless divergence worth its own discussion.
Also rides along in the JS commit: four env-gated integration tests
still
asserted positional access on expanded rows, which f52724d45 removed on
purpose; CI never runs those tests, so they had drifted.
Tested against a live libsql-server and against Turso Cloud: Rust 44
unit + 58 conformance tests plus the hegel parity property, Python 73
serverless + 236 embedded plus the hypothesis parity property,
JavaScript 42 integration tests plus the fast-check parity property, and
protocol-level coverage asserting a parameterized multi-statement batch
in a single pipeline POST.
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Closes #8535
2026-08-29 08:36:30 -07:00
..