|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from sqlspec import SQLSpec |
| 4 | +from sqlspec.adapters.aiosqlite import AiosqliteConfig |
| 5 | +from sqlspec.adapters.sqlite import SqliteConfig |
| 6 | +from sqlspec.core import StatementStack |
| 7 | + |
| 8 | + |
| 9 | +def build_stack(user_id: int, action: str) -> "StatementStack": |
| 10 | + stack = ( |
| 11 | + StatementStack() |
| 12 | + .push_execute( |
| 13 | + "INSERT INTO audit_log (user_id, action) VALUES (:user_id, :action)", {"user_id": user_id, "action": action} |
| 14 | + ) |
| 15 | + .push_execute( |
| 16 | + "UPDATE users SET last_action = :action WHERE id = :user_id", {"action": action, "user_id": user_id} |
| 17 | + ) |
| 18 | + .push_execute("SELECT role FROM user_roles WHERE user_id = :user_id ORDER BY role", {"user_id": user_id}) |
| 19 | + ) |
| 20 | + return stack |
| 21 | + |
| 22 | + |
| 23 | +def run_sync_example() -> None: |
| 24 | + sql = SQLSpec() |
| 25 | + config = SqliteConfig(pool_config={"database": ":memory:"}) |
| 26 | + registry = sql.add_config(config) |
| 27 | + |
| 28 | + with sql.provide_session(registry) as session: |
| 29 | + session.execute_script( |
| 30 | + """ |
| 31 | + CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, last_action TEXT); |
| 32 | + CREATE TABLE IF NOT EXISTS audit_log ( |
| 33 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 34 | + user_id INTEGER NOT NULL, |
| 35 | + action TEXT NOT NULL |
| 36 | + ); |
| 37 | + CREATE TABLE IF NOT EXISTS user_roles ( |
| 38 | + user_id INTEGER NOT NULL, |
| 39 | + role TEXT NOT NULL |
| 40 | + ); |
| 41 | + INSERT INTO users (id, last_action) VALUES (1, 'start'); |
| 42 | + INSERT INTO user_roles (user_id, role) VALUES (1, 'admin'), (1, 'editor'); |
| 43 | + """ |
| 44 | + ) |
| 45 | + |
| 46 | + stack = build_stack(user_id=1, action="sync-login") |
| 47 | + results = session.execute_stack(stack) |
| 48 | + |
| 49 | + audit_insert, user_update, role_select = results |
| 50 | + print("[sync] rows inserted:", audit_insert.rowcount) |
| 51 | + print("[sync] rows updated:", user_update.rowcount) |
| 52 | + if role_select.raw_result is not None: |
| 53 | + print("[sync] roles:", [row["role"] for row in role_select.raw_result.data]) |
| 54 | + |
| 55 | + |
| 56 | +def run_async_example() -> None: |
| 57 | + async def _inner() -> None: |
| 58 | + sql = SQLSpec() |
| 59 | + config = AiosqliteConfig(pool_config={"database": ":memory:"}) |
| 60 | + registry = sql.add_config(config) |
| 61 | + |
| 62 | + async with sql.provide_session(registry) as session: |
| 63 | + await session.execute_script( |
| 64 | + """ |
| 65 | + CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, last_action TEXT); |
| 66 | + CREATE TABLE IF NOT EXISTS audit_log ( |
| 67 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 68 | + user_id INTEGER NOT NULL, |
| 69 | + action TEXT NOT NULL |
| 70 | + ); |
| 71 | + CREATE TABLE IF NOT EXISTS user_roles ( |
| 72 | + user_id INTEGER NOT NULL, |
| 73 | + role TEXT NOT NULL |
| 74 | + ); |
| 75 | + INSERT INTO users (id, last_action) VALUES (2, 'start'); |
| 76 | + INSERT INTO user_roles (user_id, role) VALUES (2, 'viewer'); |
| 77 | + """ |
| 78 | + ) |
| 79 | + |
| 80 | + stack = build_stack(user_id=2, action="async-login") |
| 81 | + results = await session.execute_stack(stack, continue_on_error=False) |
| 82 | + |
| 83 | + audit_insert, user_update, role_select = results |
| 84 | + print("[async] rows inserted:", audit_insert.rowcount) |
| 85 | + print("[async] rows updated:", user_update.rowcount) |
| 86 | + if role_select.raw_result is not None: |
| 87 | + print("[async] roles:", [row["role"] for row in role_select.raw_result.data]) |
| 88 | + |
| 89 | + asyncio.run(_inner()) |
| 90 | + |
| 91 | + |
| 92 | +def main() -> None: |
| 93 | + run_sync_example() |
| 94 | + run_async_example() |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + main() |
0 commit comments