bindings/python/py-bindings-db-aio.mdx
Turso - is the SQLite compatible database written in Rust. One of the important features of the Turso - is async IO execution which can be used with modern storage backend like IO uring.
Your task is to generate ASYNC Python driver with the API similar aiosqlite (which is similar to DB-API 2 of sqlite module) by REUSING current sync driver imiplementation.
General rules for driver implementation you MUST follow and never go against these rules:
# all imports must be at the beginning - no imports in the middle of function
from typing import ...
from queue import SimpleQueue
from .worker import Worker
from .lib import (
Connection as BlockingConnection,
Cursor as BlockingCursor,
...
)
# Connection goes FIRST
class Connection:
def __init__(connector: Callable[[], BlockingConnection]): ...
# internal worker MUST be stopped when connection goes out of context scope
# close must wait for worker to be stopped
async def close(...): ...
# make Connection instance awaitable
def __await__(...): ...
async def __aenter__(...): ...
# just close the connection - do not add any extra logic
async def __aexit__(...): ...
await self.close()
...
# Cursor goes SECOND
class Cursor: ...
# connect is not async because it returns awaitable Connection
# same signature as in the lib.py
def connect(...): ...
# create future for completion
future = asyncio.get_event_loop().create_future()
# put the work to the unbounded queue
queue.put_nowait((future, function))
loop parameter - ALWAYS use asyncio.get_event_loop()class T:
def f(self) -> 'T':
with ... as conn: ...For turso db execution you MUST reuse blocking implementation:
<File path="./turso/lib.py" /> <File path="./turso/worker.py" /> <File path="./turso/__init__.py" />Make driver API similar to the SQLite DB-API2 for the python.
Pay additional attention to the following aspects:
If autocommit is LEGACY_TRANSACTION_CONTROL, isolation_level is not None, sql is an INSERT, UPDATE, DELETE, or REPLACE statement, and there is no open transaction, a transaction is implicitly opened before executing sql.
executemany(...) methods as it must return rowcount of all executed statements (not just last statement)Also, make types and API compatible with aiosqlite (note, that aiosqlite implements some other methods, which can be omitted now, like interrupt)
<Shell cmd="python3 -m pydoc aiosqlite" /> </Code> </Output>