Parameterized batches could produce different effects across embedded and
serverless drivers when a later bind value was invalid. Embedded Rust also
allowed cloned connections to interleave work with a batch, and managed
batches allowed transaction-control statements to escape their wrapper.
Rollback failures were hidden, while serverless Rust could retain a stale
last-insert rowid when COMMIT failed after a successful insert.
Validate every batch input before execution, reject transaction-control SQL
before opening a managed transaction, and hold an operation-wide gate for
embedded Rust batches. Preserve both the primary and rollback errors, and
carry successful statement rowids through serverless batch decoding even
when a synthetic transaction step fails. Reject malformed serverless batch
responses instead of treating incomplete transaction steps as success.
Add Connection.batch(statements, mode=None) to turso (bindings/python,
inherited by the sync driver's ConnectionSync and exposed on the aio
wrapper) and to turso_serverless, with the same signature and semantics
per the driver API parity requirement.
Statements are SQL strings or (sql, parameters) pairs and execute in
order, stopping at the first failure; the raised exception keeps its
DB-API class, prefixes the message with the zero-based statement index,
and carries batch_index and batch_results attributes — the latter with
one entry per statement, so callers can tell exactly which statements
took effect before the failure. A client-side value encoding failure is
reported the same way. With mode set (deferred, immediate, exclusive,
concurrent) the statements are wrapped in BEGIN/COMMIT with a ROLLBACK
on failure for all-or-nothing execution. batch() never opens a legacy
implicit transaction; statements join an already-open transaction, in
which case mode is ignored.
Each statement's BatchResult carries rows, description, rowcount, and
lastrowid, plus the server-side execution statistics rows_read,
rows_written, and query_duration_ms reported by the serverless protocol
(the embedded engine does not report them per statement). The
serverless driver sends the whole batch, including the transaction
wrapper, as a single pipeline request, so a multi-statement
parameterized batch is one HTTP round trip.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Implement the client side of the SQL over HTTP protocol
(serverless/PROTOCOL.md) so Python applications can talk to Turso Cloud
from serverless and edge environments, where the only networking
primitive is an HTTP request. The driver is pure Python on top of
urllib, implements DB-API 2.0, and mirrors the embedded turso driver,
so the same code runs against a local database or Turso Cloud.
The session layer follows the JavaScript and Rust serverless drivers:
the baton is tracked on every request, transaction state comes from the
server's authoritative get_autocommit answer piggybacked on every
pipeline (and an is_autocommit-gated probe step on cursor batches), and
statements run over the cursor endpoint. Pipeline responses are
validated to carry one result per request. A failed request is never
transparently retried: the stream is reset and the error surfaces, so
the application decides whether re-running a statement is safe.
Server-returned base URLs go through the same normalization as
user-supplied ones, including trailing-slash stripping.
The conformance suite runs against a live database configured with
TURSO_DATABASE_URL and TURSO_AUTH_TOKEN and skips when they are unset,
covering queries, parameter binding, value round-trips, transactions,
executescript, and DB-API error mapping.
Tests: pytest in serverless/python; protocol and URL handling unit
tests run standalone, the conformance suite needs the environment
variables set.