Files
turso/serverless/python/turso_serverless/__init__.py
Glauber Costa e29fe1033d add a parameterized batch API to the embedded and serverless Python drivers
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>
2026-08-28 10:40:26 -07:00

59 lines
1.2 KiB
Python

"""Pure Python serverless Turso client (DB-API 2.0).
Connects to a Turso database over HTTP using the SQL over HTTP protocol.
No Rust FFI required — uses urllib.request from stdlib.
Usage:
import turso_serverless
conn = turso_serverless.connect("libsql://my-db.turso.io", auth_token="...")
cursor = conn.execute("SELECT * FROM users")
rows = cursor.fetchall()
conn.close()
"""
from .connection import BatchResult, Connection, Cursor, connect
from .dbapi import (
# Exception classes
DatabaseError,
DataError,
Error,
IntegrityError,
InterfaceError,
InternalError,
NotSupportedError,
OperationalError,
ProgrammingError,
# Helpers
Row,
Warning,
)
# DB-API 2.0 module-level attributes
apilevel = "2.0"
threadsafety = 1
paramstyle = "qmark"
__all__ = [
"connect",
"BatchResult",
"Connection",
"Cursor",
"Row",
# DB-API 2.0 module attributes
"apilevel",
"paramstyle",
"threadsafety",
# Exception classes
"Warning",
"Error",
"InterfaceError",
"DatabaseError",
"DataError",
"OperationalError",
"IntegrityError",
"InternalError",
"ProgrammingError",
"NotSupportedError",
]