Skip to content

Commit 5bcf10f

Browse files
committed
Added more documentation and added one test
1 parent c682a02 commit 5bcf10f

File tree

6 files changed

+48
-12
lines changed

6 files changed

+48
-12
lines changed

docs/components/connection.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ async def main() -> None:
2424
```python
2525
from psqlpy import connect
2626

27-
db_connection: Final = connect(
28-
dsn="postgres://postgres:postgres@localhost:5432/postgres",
29-
)
30-
3127
async def main() -> None:
28+
db_connection: Final = await connect(
29+
dsn="postgres://postgres:postgres@localhost:5432/postgres",
30+
)
3231
await db_connection.execute(...)
3332
```
3433

docs/components/cursor.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,34 @@ async def main() -> None:
6161
async with connection.cursor(
6262
querystring="SELECT * FROM users WHERE id > $1",
6363
parameters=[100],
64-
fetch_number=10,
64+
array_size=10,
6565
) as cursor:
6666
result: QueryResult = await cursor.fetchone()
6767
```
68+
69+
@tab Async Iterator
70+
```python
71+
from psqlpy import ConnectionPool, QueryResult
72+
73+
async def main() -> None:
74+
db_pool = ConnectionPool()
75+
connection = await db_pool.connection()
76+
77+
cursor = connection.cursor(
78+
querystring="SELECT * FROM users WHERE id > $1",
79+
parameters=[100],
80+
fetch_number=10,
81+
)
82+
await cursor.start()
83+
84+
async for result in cursor:
85+
print(result)
86+
```
6887
:::
6988

89+
## Cursor attributes
90+
- `array_size`: get and set attribute. Used in async iterator and `fetch_many` method.
91+
7092
## Cursor methods
7193
### Start
7294
Declare (create) cursor.
@@ -117,7 +139,7 @@ async def main() -> None:
117139

118140
### Fetchmany
119141

120-
Fetch N results from the cursor.
142+
Fetch N results from the cursor. Default is `array_size`.
121143

122144
#### Parameters:
123145
- `size`: number of records to fetch.

python/tests/test_cursor.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ async def test_cursor_as_async_context_manager(
6565
assert len(results.result()) == number_database_records
6666

6767

68+
async def test_cursor_as_async_iterator(
69+
psql_pool: ConnectionPool,
70+
table_name: str,
71+
number_database_records: int,
72+
) -> None:
73+
connection = await psql_pool.connection()
74+
all_results = []
75+
async with connection.cursor(
76+
querystring=f"SELECT * FROM {table_name}",
77+
) as cursor:
78+
async for results in cursor:
79+
all_results.extend(results.result())
80+
81+
assert len(all_results) == number_database_records
82+
83+
6884
async def test_cursor_send_underlying_connection_to_pool(
6985
psql_pool: ConnectionPool,
7086
table_name: str,

src/driver/common.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,18 @@ macro_rules! impl_cursor_method {
132132
($name:ident) => {
133133
#[pymethods]
134134
impl $name {
135-
#[pyo3(signature = (querystring=None, parameters=None, fetch_number=None))]
135+
#[pyo3(signature = (querystring=None, parameters=None, array_size=None))]
136136
pub fn cursor(
137137
&self,
138138
querystring: Option<String>,
139139
parameters: Option<Py<PyAny>>,
140-
fetch_number: Option<i32>,
140+
array_size: Option<i32>,
141141
) -> PSQLPyResult<Cursor> {
142142
Ok(Cursor::new(
143143
self.conn.clone(),
144144
querystring,
145145
parameters,
146-
fetch_number,
146+
array_size,
147147
self.pg_config.clone(),
148148
None,
149149
))

src/driver/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use deadpool_postgres::Pool;
2-
use pyo3::{ffi::PyObject, pyclass, pyfunction, pymethods, Py, PyAny, PyErr};
2+
use pyo3::{pyclass, pyfunction, pymethods, Py, PyAny, PyErr};
33
use std::sync::Arc;
44
use tokio::sync::RwLock;
55
use tokio_postgres::Config;

src/driver/cursor.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::sync::Arc;
22

33
use pyo3::{
4-
exceptions::PyStopAsyncIteration, pyclass, pymethods, types::PyNone, Py, PyAny, PyErr,
5-
PyObject, Python,
4+
exceptions::PyStopAsyncIteration, pyclass, pymethods, Py, PyAny, PyErr, PyObject, Python,
65
};
76
use tokio::sync::RwLock;
87
use tokio_postgres::{Config, Portal as tp_Portal};

0 commit comments

Comments
 (0)