Skip to content

Commit 7ef996c

Browse files
authored
Merge pull request #148 from vdice/feat/opinionated-db-modules
feat(postgres/sqlite): update API to be fully async
2 parents 715e06b + 022f16b commit 7ef996c

6 files changed

Lines changed: 405 additions & 90 deletions

File tree

docs/v4/postgres.html

Lines changed: 115 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/v4/sqlite.html

Lines changed: 143 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/spin-postgres/app.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
from spin_sdk import http, postgres
1+
from spin_sdk import http, postgres, util
22
from spin_sdk.http import Request, Response
3-
from spin_sdk.postgres import RowSet, DbValue
4-
3+
from spin_sdk.postgres import Connection, RowSet, DbValue
54

65
def format_value(db_value: DbValue) -> str:
76
if hasattr(db_value, "value"):
87
return str(db_value.value)
98
return "NULL"
109

11-
1210
def format_rowset(rowset: RowSet) -> str:
1311
lines = []
1412
col_names = [col.name for col in rowset.columns]
@@ -19,11 +17,12 @@ def format_rowset(rowset: RowSet) -> str:
1917
lines.append(" | ".join(values))
2018
return "\n".join(lines)
2119

22-
2320
class HttpHandler(http.Handler):
2421
async def handle_request(self, request: Request) -> Response:
25-
with await postgres.open("user=postgres dbname=spin_dev host=localhost sslmode=disable password=password") as db:
26-
rowset = db.query("SELECT * FROM test", [])
22+
with await Connection.open("user=postgres dbname=spin_dev host=localhost sslmode=disable password=password") as db:
23+
columns, stream, future = await db.query("SELECT * FROM test", [])
24+
rows = await util.collect((stream, future))
25+
rowset = RowSet(columns, list(rows))
2726
result = format_rowset(rowset)
2827

2928
return Response(200, {"content-type": "text/plain"}, bytes(result, "utf-8"))

examples/spin-sqlite/app.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
1-
from spin_sdk import http, sqlite
1+
from spin_sdk import http
22
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
45

56
class HttpHandler(http.Handler):
67
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+
1133
return Response(
1234
200,
1335
{"content-type": "text/plain"},

0 commit comments

Comments
 (0)