Skip to content

Commit ef3a703

Browse files
committed
docs(v4): postgres and sqlite python updates
Signed-off-by: Vaughn Dice <vdice@akamai.com>
1 parent 522fe95 commit ef3a703

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

content/v4/rdbms-storage.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,31 @@ addEventListener('fetch', async (event: FetchEvent) => {
149149
The code below shows use of the [Postgres](https://spinframework.github.io/spin-python-sdk/v4/postgres.html) module and its [open](https://spinframework.github.io/spin-python-sdk/v4/postgres.html#spin_sdk.postgres.open) function for opening a connection to the database:
150150

151151
```python
152-
from spin_sdk import http, postgres
152+
from spin_sdk import http, util
153153
from spin_sdk.http import Request, Response
154+
from spin_sdk.postgres import Connection
154155

155156
class HttpHandler(http.Handler):
156157
async def handle_request(self, request: Request) -> Response:
157-
with await postgres.open("user=postgres dbname=spin_dev host=localhost sslmode=disable password=password") as db:
158-
print(db.query("SELECT * FROM test", []))
158+
with await Connection.open("user=postgres dbname=spin_dev host=localhost sslmode=disable password=password") as db:
159+
columns, stream, future = await db.query("SELECT * FROM test", [])
160+
rows = await util.collect((stream, future))
159161

160162
return Response(
161163
200,
162164
{"content-type": "text/plain"},
163-
bytes("Hello from Python!", "utf-8")
165+
bytes(str(rows), "utf-8")
164166
)
165167
```
166168

169+
**General Notes**
170+
* The `query` method returns a Tuple containing a list of `columns`, a list of `rows` encapsulated via a [StreamReader](https://github.com/bytecodealliance/componentize-py/blob/1b3d2e936868307a48fb70941dcad71b54e844f8/bundled/componentize_py_async_support/streams.py#L101), and a [FutureReader](https://github.com/bytecodealliance/componentize-py/blob/1b3d2e936868307a48fb70941dcad71b54e844f8/bundled/componentize_py_async_support/futures.py#L11). You _must_ check when the stream ends, to determine if the stream ended normally, or was terminated prematurely due to an error.
171+
172+
> As seen in the example above, you can utilize the [collect](https://spinframework.github.io/spin-python-sdk/v4/util.html#spin_sdk.util.collect) method from the `util` package to handle the `StreamReader` and `FutureReader` pair, aggregating the resulting rows into memory.
173+
174+
* The `Connection` object doesn't surface the `close` function.
175+
* Errors are surfaced as exceptions.
176+
167177
You can find a complete outbound PostgreSQL example in the [Spin Python SDK repository on GitHub](https://github.com/spinframework/spin-python-sdk/tree/main/examples/spin-postgres). There is also an [Outbound MySQL example](https://github.com/spinframework/spin-python-sdk/tree/main/examples/spin-mysql) available.
168178

169179
{{ blockEnd }}

content/v4/sqlite-api-guide.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,20 @@ addEventListener('fetch', async (event: FetchEvent) => {
158158

159159
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html)
160160
161-
To use SQLite functions, use the `sqlite` module in the Python SDK. The [`open`](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html#spin_sdk.sqlite.open) and [`open_default`](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html#spin_sdk.sqlite.open_default) functions return a [`Connection` object](https://spinframework.github.io/spin-python-sdk/v4/wit/imports/spin_sqlite_sqlite_3_1_0.html#spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Connection). The `Connection` object provides the [`execute` method](https://spinframework.github.io/spin-python-sdk/v4/wit/imports/spin_sqlite_sqlite_3_1_0.html#spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Connection.execute) as described above. For example:
161+
To use SQLite functions, use the `sqlite` module in the Python SDK. The [`open`](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html#spin_sdk.sqlite.open) and [`open_default`](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html#spin_sdk.sqlite.open_default) functions return a [`Connection` object](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html#spin_sdk.sqlite.Connection). The `Connection` object provides the [`execute` method](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html#spin_sdk.sqlite.Connection.execute) as described above. For example:
162162

163163
```python
164-
from spin_sdk import http, sqlite
164+
from spin_sdk import http, util
165165
from spin_sdk.http import Request, Response
166-
from spin_sdk.sqlite import Value_Integer
166+
from spin_sdk.sqlite import Connection, Value_Text, Value_Integer
167167

168168
class HttpHandler(http.Handler):
169169
async def handle_request(self, request: Request) -> Response:
170-
with await sqlite.open_default() as db:
171-
result = db.execute("SELECT * FROM todos WHERE id > (?);", [Value_Integer(1)])
172-
rows = result.rows
173-
170+
with await Connection.open_default() as db:
171+
await db.execute("INSERT INTO todos (description, due) VALUES (?, ?)", [Value_Text("Try out Spin SQLite"), Value_Text("Friday")])
172+
columns, stream, future = await db.execute("SELECT * FROM todos WHERE id > (?);", [Value_Integer(1)])
173+
rows = await util.collect((stream, future))
174+
174175
return Response(
175176
200,
176177
{"content-type": "text/plain"},
@@ -179,7 +180,10 @@ class HttpHandler(http.Handler):
179180
```
180181

181182
**General Notes**
182-
* The `execute` method returns [a `QueryResult` object](https://spinframework.github.io/spin-python-sdk/v4/wit/imports/spin_sqlite_sqlite_3_1_0.html#spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.QueryResult) with `rows` and `columns` methods. `columns` returns a list of strings representing column names. `rows` is an array of rows, each of which is an array of [`RowResult`](https://spinframework.github.io/spin-python-sdk/v4/wit/imports/spin_sqlite_sqlite_3_1_0.html#spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.RowResult) in the same order as `columns`.
183+
* The `execute` method returns a Tuple containing a list of `columns`, a list of `rows` encapsulated via a [StreamReader](https://github.com/bytecodealliance/componentize-py/blob/1b3d2e936868307a48fb70941dcad71b54e844f8/bundled/componentize_py_async_support/streams.py#L101), and a [FutureReader](https://github.com/bytecodealliance/componentize-py/blob/1b3d2e936868307a48fb70941dcad71b54e844f8/bundled/componentize_py_async_support/futures.py#L11). You _must_ check when the stream ends, to determine if the stream ended normally, or was terminated prematurely due to an error.
184+
185+
> As seen in the example above, you can utilize the [collect](https://spinframework.github.io/spin-python-sdk/v4/util.html#spin_sdk.util.collect) method from the `util` package to handle the `StreamReader` and `FutureReader` pair, aggregating the resulting rows into memory.
186+
183187
* The `Connection` object doesn't surface the `close` function.
184188
* Errors are surfaced as exceptions.
185189

0 commit comments

Comments
 (0)