|
1 | 1 | """Module for interacting with an SQLite database""" |
2 | 2 |
|
3 | | -from typing import List |
| 3 | +from typing import List, Tuple |
| 4 | +from types import TracebackType |
| 5 | +from componentize_py_types import Result |
| 6 | +from componentize_py_async_support.streams import StreamReader |
| 7 | +from componentize_py_async_support.futures import FutureReader |
4 | 8 | from spin_sdk.wit.imports import spin_sqlite_sqlite_3_1_0 as sqlite |
5 | 9 |
|
6 | | -Connection = sqlite.Connection |
| 10 | +Error = sqlite.Error |
| 11 | +Value = sqlite.Value |
7 | 12 | Value_Integer = sqlite.Value_Integer |
8 | 13 | Value_Real = sqlite.Value_Real |
9 | 14 | Value_Text = sqlite.Value_Text |
10 | 15 | Value_Blob = sqlite.Value_Blob |
| 16 | +RowResult = sqlite.RowResult |
11 | 17 |
|
12 | | -async def open(name: str) -> Connection: |
13 | | - """Open a connection to a named database instance. |
| 18 | +class Connection: |
| 19 | + def __init__(self, connection: sqlite.Connection) -> None: |
| 20 | + self.connection = connection |
14 | 21 |
|
15 | | - If `database` is "default", the default instance is opened. |
| 22 | + def __enter__(self) -> Connection: |
| 23 | + return self |
16 | 24 |
|
17 | | - A `componentize_py_types.Err(spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Error_AccessDenied)` will be raised when the component does not have access to the specified database. |
| 25 | + def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) -> bool | None: |
| 26 | + return self.connection.__exit__(exc_type, exc_value, traceback) |
18 | 27 |
|
19 | | - A `componentize_py_types.Err(spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Error_NoSuchDatabase)` will be raised when the host does not recognize the database name requested. |
20 | | - |
21 | | - A `componentize_py_types.Err(spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Error_InvalidConnection)` will be raised when the provided connection string is not valid. |
22 | | - |
23 | | - A `componentize_py_types.Err(spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Error_Io(str))` will be raised when implementation-specific error occured (e.g. I/O) |
24 | | - """ |
25 | | - return await Connection.open_async(name) |
| 28 | + @classmethod |
| 29 | + async def open(cls, name: str) -> Connection: |
| 30 | + """Open a connection to a named database instance. |
26 | 31 |
|
27 | | -async def open_default() -> Connection: |
28 | | - """Open the default store. |
| 32 | + If `database` is "default", the default instance is opened. |
29 | 33 |
|
30 | | - A `componentize_py_types.Err(spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Error_AccessDenied)` will be raised when the component does not have access to the default database. |
| 34 | + A `componentize_py_types.Err(spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Error_AccessDenied)` will be raised when the component does not have access to the specified database. |
31 | 35 |
|
32 | | - A `componentize_py_types.Err(spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Error_Io(str))` will be raised when implementation-specific error occured (e.g. I/O) |
33 | | - """ |
34 | | - return await Connection.open_async("default") |
| 36 | + A `componentize_py_types.Err(spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Error_NoSuchDatabase)` will be raised when the host does not recognize the database name requested. |
| 37 | +
|
| 38 | + A `componentize_py_types.Err(spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Error_InvalidConnection)` will be raised when the provided connection string is not valid. |
| 39 | +
|
| 40 | + A `componentize_py_types.Err(spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Error_Io(str))` will be raised when implementation-specific error occured (e.g. I/O) |
| 41 | + """ |
| 42 | + return Connection(await sqlite.Connection.open_async(name)) |
| 43 | + |
| 44 | + @classmethod |
| 45 | + async def open_default(cls) -> Connection: |
| 46 | + """Open the default store. |
| 47 | +
|
| 48 | + A `componentize_py_types.Err(spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Error_AccessDenied)` will be raised when the component does not have access to the default database. |
| 49 | +
|
| 50 | + A `componentize_py_types.Err(spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Error_Io(str))` will be raised when implementation-specific error occured (e.g. I/O) |
| 51 | + """ |
| 52 | + return Connection(await sqlite.Connection.open_async("default")) |
| 53 | + |
| 54 | + async def execute(self, statement: str, params: List[Value]) -> Tuple[List[str], StreamReader[RowResult], FutureReader[Result[None, Error]]]: |
| 55 | + """ |
| 56 | + Execute a command to the database. |
| 57 | +
|
| 58 | + Returns a Tuple containing a List of columns, a List of RowResults encapsulated in an asynchronous iterator (`componentize_py_async_support.streams.StreamReader`), |
| 59 | + and a future (`componentize_py_async_support.futures.FutureReader`) containing the result of the operation. |
| 60 | +
|
| 61 | + Raises: `componentize_py_types.Err(spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Error)` |
| 62 | + """ |
| 63 | + return await self.connection.execute_async(statement, params) |
| 64 | + |
| 65 | + async def last_insert_rowid(self) -> int: |
| 66 | + """ |
| 67 | + The SQLite rowid of the most recent successful INSERT on the connection, or 0 if |
| 68 | + there has not yet been an INSERT on the connection. |
| 69 | + """ |
| 70 | + return await self.connection.last_insert_rowid_async() |
| 71 | + |
| 72 | + async def changes(self) -> int: |
| 73 | + """ |
| 74 | + The number of rows modified, inserted or deleted by the most recently completed |
| 75 | + INSERT, UPDATE or DELETE statement on the connection. |
| 76 | + """ |
| 77 | + return await self.connection.changes_async() |
0 commit comments