Skip to content

Commit 799a0c9

Browse files
authored
Merge pull request #46 from qaspen-python/feature/cursor_from_connection
Started implementing chain to create pool
2 parents 84751b8 + 0a8f9a4 commit 799a0c9

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

python/psqlpy/_internal/__init__.pyi

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,50 @@ class Connection:
942942
- `read_variant`: configure read variant of the transaction.
943943
- `deferrable`: configure deferrable of the transaction.
944944
"""
945+
def cursor(
946+
self: Self,
947+
querystring: str,
948+
parameters: Sequence[Any] | None = None,
949+
fetch_number: int | None = None,
950+
scroll: bool | None = None,
951+
prepared: bool = True,
952+
) -> Cursor:
953+
"""Create new cursor object.
954+
955+
Cursor can be used as an asynchronous iterator.
956+
957+
### Parameters:
958+
- `querystring`: querystring to execute.
959+
- `parameters`: list of parameters to pass in the query.
960+
- `fetch_number`: how many rows need to fetch.
961+
- `scroll`: SCROLL or NO SCROLL cursor.
962+
- `prepared`: should the querystring be prepared before the request.
963+
By default any querystring will be prepared.
964+
965+
### Returns:
966+
new initialized cursor.
967+
968+
### Example:
969+
```python
970+
import asyncio
971+
972+
from psqlpy import PSQLPool, QueryResult
973+
974+
975+
async def main() -> None:
976+
db_pool = PSQLPool()
977+
connection = await db_pool.connection()
978+
async with connection.transaction():
979+
async with connection.cursor(
980+
querystring="SELECT * FROM users WHERE username = $1",
981+
parameters=["Some_Username"],
982+
fetch_number=5,
983+
) as cursor:
984+
async for fetched_result in cursor:
985+
dict_result: List[Dict[Any, Any]] = fetched_result.result()
986+
... # do something with this result.
987+
```
988+
"""
945989

946990
class ConnectionPool:
947991
"""Connection pool for executing queries.

python/tests/test_connection.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66
from tests.helpers import count_rows_in_test_table
77

8-
from psqlpy import ConnectionPool, QueryResult, Transaction
8+
from psqlpy import ConnectionPool, Cursor, QueryResult, Transaction
99
from psqlpy.exceptions import ConnectionExecuteError, TransactionExecuteError
1010

1111
pytestmark = pytest.mark.anyio
@@ -128,3 +128,22 @@ async def test_connection_fetch_val_more_than_one_row(
128128
f"SELECT * FROM {table_name}",
129129
[],
130130
)
131+
132+
133+
async def test_connection_cursor(
134+
psql_pool: ConnectionPool,
135+
table_name: str,
136+
number_database_records: int,
137+
) -> None:
138+
"""Test cursor from Connection."""
139+
connection = await psql_pool.connection()
140+
cursor: Cursor
141+
all_results: list[dict[typing.Any, typing.Any]] = []
142+
143+
async with connection.transaction(), connection.cursor(
144+
querystring=f"SELECT * FROM {table_name}",
145+
) as cursor:
146+
async for cur_res in cursor:
147+
all_results.extend(cur_res.result())
148+
149+
assert len(all_results) == number_database_records

src/driver/connection.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
};
1010

1111
use super::{
12+
cursor::Cursor,
1213
transaction::Transaction,
1314
transaction_options::{IsolationLevel, ReadVariant},
1415
};
@@ -396,4 +397,24 @@ impl Connection {
396397
HashSet::new(),
397398
)
398399
}
400+
401+
#[must_use]
402+
pub fn cursor(
403+
&self,
404+
querystring: String,
405+
parameters: Option<Py<PyAny>>,
406+
fetch_number: Option<usize>,
407+
scroll: Option<bool>,
408+
prepared: Option<bool>,
409+
) -> Cursor {
410+
Cursor::new(
411+
self.db_client.clone(),
412+
querystring,
413+
parameters,
414+
"cur_name".into(),
415+
fetch_number.unwrap_or(10),
416+
scroll,
417+
prepared,
418+
)
419+
}
399420
}

0 commit comments

Comments
 (0)