|
1 | | -from spin_sdk import http, sqlite |
| 1 | +from spin_sdk import http |
2 | 2 | from spin_sdk.http import Request, Response |
3 | | -from spin_sdk.sqlite import Value_Integer |
| 3 | +from spin_sdk.sqlite import Connection, Value_Text, Value_Integer |
| 4 | +from spin_sdk.util import collect |
4 | 5 |
|
5 | 6 | class HttpHandler(http.Handler): |
6 | 7 | async def handle_request(self, request: Request) -> Response: |
7 | | - with await sqlite.open_default() as db: |
8 | | - result = db.execute("SELECT * FROM todos WHERE id > (?);", [Value_Integer(1)]) |
9 | | - rows = result.rows |
10 | | - |
| 8 | + with await Connection.open_default() as db: |
| 9 | + _, stream, result = await db.execute( |
| 10 | + "CREATE TABLE IF NOT EXISTS example (id INTEGER NOT NULL PRIMARY KEY, value TEXT NOT NULL)", |
| 11 | + [] |
| 12 | + ) |
| 13 | + await collect((stream, result)) |
| 14 | + |
| 15 | + insert = "INSERT INTO example (id, value) VALUES (?, ?) ON CONFLICT (id) DO UPDATE SET value=excluded.value" |
| 16 | + |
| 17 | + _, stream, result = await db.execute(insert, [Value_Integer(1), Value_Text("foo")]) |
| 18 | + await collect((stream, result)) |
| 19 | + |
| 20 | + _, stream, result = await db.execute(insert, [Value_Integer(2), Value_Text("bar")]) |
| 21 | + await collect((stream, result)) |
| 22 | + |
| 23 | + columns, stream, result = await db.execute("SELECT * FROM example WHERE id > (?);", [Value_Integer(0)]) |
| 24 | + rows = await collect((stream, result)) |
| 25 | + |
| 26 | + assert columns == ["id", "value"] |
| 27 | + assert len(rows) == 1 |
| 28 | + assert isinstance(rows[0].values[0], Value_Integer) |
| 29 | + assert rows[0].values[0].value == 2 |
| 30 | + assert isinstance(rows[0].values[1], Value_Text) |
| 31 | + assert rows[0].values[1].value == "bar" |
| 32 | + |
11 | 33 | return Response( |
12 | 34 | 200, |
13 | 35 | {"content-type": "text/plain"}, |
|
0 commit comments