Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 115 additions & 17 deletions docs/v4/postgres.html

Large diffs are not rendered by default.

172 changes: 143 additions & 29 deletions docs/v4/sqlite.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions examples/spin-postgres/app.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from spin_sdk import http, postgres
from spin_sdk import http, postgres, util
from spin_sdk.http import Request, Response
from spin_sdk.postgres import RowSet, DbValue

from spin_sdk.postgres import Connection, RowSet, DbValue

def format_value(db_value: DbValue) -> str:
if hasattr(db_value, "value"):
return str(db_value.value)
return "NULL"


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


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

return Response(200, {"content-type": "text/plain"}, bytes(result, "utf-8"))
13 changes: 7 additions & 6 deletions examples/spin-sqlite/app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from spin_sdk import http, sqlite
from spin_sdk import http, util
from spin_sdk.http import Request, Response
from spin_sdk.sqlite import Value_Integer
from spin_sdk.sqlite import Connection, Value_Text, Value_Integer

class HttpHandler(http.Handler):
async def handle_request(self, request: Request) -> Response:
with await sqlite.open_default() as db:
result = db.execute("SELECT * FROM todos WHERE id > (?);", [Value_Integer(1)])
rows = result.rows

with await Connection.open_default() as db:
await db.execute("INSERT INTO todos (description, due) VALUES (?, ?)", [Value_Text("Try out Spin SQLite"), Value_Text("Friday")])
columns, stream, future = await db.execute("SELECT * FROM todos WHERE id > (?);", [Value_Integer(1)])
rows = await util.collect((stream, future))

return Response(
200,
{"content-type": "text/plain"},
Expand Down
Loading
Loading