Skip to content

Commit aed5e35

Browse files
authored
Merge pull request #138 from psqlpy-python/support_dbapi
Changes to connection, transaction and cursor management
2 parents d80e558 + 359736d commit aed5e35

27 files changed

+692
-1046
lines changed

docs/components/connection.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ async def main() -> None:
200200
)
201201
```
202202

203-
### Back To Pool
203+
### Close
204204
Returns connection to the pool.
205205
It's crucial to commit all transactions and close all cursor which are made from the connection.
206206
Otherwise, this method won't do anything useful.
@@ -213,5 +213,5 @@ There is no need in this method if you use async context manager.
213213
async def main() -> None:
214214
...
215215
connection = await db_pool.connection()
216-
connection.back_to_pool()
216+
connection.close()
217217
```

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,6 @@ ignore = [
106106
[tool.ruff.pydocstyle]
107107
convention = "pep257"
108108
ignore-decorators = ["typing.overload"]
109+
110+
[project.entry-points."sqlalchemy.dialects"]
111+
psqlpy = "psqlpy_sqlalchemy.dialect:PSQLPyAsyncDialect"

python/psqlpy/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
connect,
1919
connect_pool,
2020
)
21+
from psqlpy.exceptions import (
22+
Error,
23+
)
2124

2225
__all__ = [
2326
"ConnRecyclingMethod",
2427
"Connection",
2528
"ConnectionPool",
2629
"ConnectionPoolBuilder",
2730
"Cursor",
31+
"Error",
2832
"IsolationLevel",
2933
"KeepaliveConfig",
3034
"Listener",

python/psqlpy/_internal/__init__.pyi

Lines changed: 16 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,12 @@ class KeepaliveConfig:
253253
"""Initialize new config."""
254254

255255
class Cursor:
256-
"""Represent opened cursor in a transaction.
256+
"""Represent binary cursor in a transaction.
257257
258258
It can be used as an asynchronous iterator.
259259
"""
260260

261+
array_size: int
261262
cursor_name: str
262263
querystring: str
263264
parameters: ParamsT = None
@@ -282,118 +283,27 @@ class Cursor:
282283
283284
Execute DECLARE command for the cursor.
284285
"""
285-
async def close(self: Self) -> None:
286+
def close(self: Self) -> None:
286287
"""Close the cursor.
287288
288289
Execute CLOSE command for the cursor.
289290
"""
290-
async def fetch(
291-
self: Self,
292-
fetch_number: int | None = None,
293-
) -> QueryResult:
294-
"""Fetch next <fetch_number> rows.
295-
296-
By default fetches 10 next rows.
297-
298-
### Parameters:
299-
- `fetch_number`: how many rows need to fetch.
300-
301-
### Returns:
302-
result as `QueryResult`.
303-
"""
304-
async def fetch_next(
305-
self: Self,
306-
) -> QueryResult:
307-
"""Fetch next row.
308-
309-
Execute FETCH NEXT
310-
311-
### Returns:
312-
result as `QueryResult`.
313-
"""
314-
async def fetch_prior(
315-
self: Self,
316-
) -> QueryResult:
317-
"""Fetch previous row.
318-
319-
Execute FETCH PRIOR
320-
321-
### Returns:
322-
result as `QueryResult`.
323-
"""
324-
async def fetch_first(
325-
self: Self,
326-
) -> QueryResult:
327-
"""Fetch first row.
328-
329-
Execute FETCH FIRST
330-
331-
### Returns:
332-
result as `QueryResult`.
333-
"""
334-
async def fetch_last(
335-
self: Self,
336-
) -> QueryResult:
337-
"""Fetch last row.
338-
339-
Execute FETCH LAST
340-
341-
### Returns:
342-
result as `QueryResult`.
343-
"""
344-
async def fetch_absolute(
345-
self: Self,
346-
absolute_number: int,
347-
) -> QueryResult:
348-
"""Fetch absolute rows.
349-
350-
Execute FETCH ABSOLUTE <absolute_number>.
351-
352-
### Returns:
353-
result as `QueryResult`.
354-
"""
355-
async def fetch_relative(
356-
self: Self,
357-
relative_number: int,
358-
) -> QueryResult:
359-
"""Fetch absolute rows.
360-
361-
Execute FETCH RELATIVE <relative_number>.
362-
363-
### Returns:
364-
result as `QueryResult`.
365-
"""
366-
async def fetch_forward_all(
367-
self: Self,
368-
) -> QueryResult:
369-
"""Fetch forward all rows.
370-
371-
Execute FETCH FORWARD ALL.
372-
373-
### Returns:
374-
result as `QueryResult`.
375-
"""
376-
async def fetch_backward(
377-
self: Self,
378-
backward_count: int,
379-
) -> QueryResult:
380-
"""Fetch backward rows.
381-
382-
Execute FETCH BACKWARD <backward_count>.
383-
384-
### Returns:
385-
result as `QueryResult`.
386-
"""
387-
async def fetch_backward_all(
291+
async def execute(
388292
self: Self,
293+
querystring: str,
294+
parameters: ParamsT = None,
389295
) -> QueryResult:
390-
"""Fetch backward all rows.
391-
392-
Execute FETCH BACKWARD ALL.
296+
"""Start cursor with querystring and parameters.
393297
394-
### Returns:
395-
result as `QueryResult`.
298+
Method should be used instead of context manager
299+
and `start` method.
396300
"""
301+
async def fetchone(self: Self) -> QueryResult:
302+
"""Return next one row from the cursor."""
303+
async def fetchmany(self: Self, size: int | None = None) -> QueryResult:
304+
"""Return <size> rows from the cursor."""
305+
async def fetchall(self: Self, size: int | None = None) -> QueryResult:
306+
"""Return all remaining rows from the cursor."""
397307

398308
class Transaction:
399309
"""Single connection for executing queries.
@@ -1098,8 +1008,6 @@ class Connection:
10981008
querystring: str,
10991009
parameters: ParamsT = None,
11001010
fetch_number: int | None = None,
1101-
scroll: bool | None = None,
1102-
prepared: bool = True,
11031011
) -> Cursor:
11041012
"""Create new cursor object.
11051013
@@ -1136,7 +1044,7 @@ class Connection:
11361044
... # do something with this result.
11371045
```
11381046
"""
1139-
def back_to_pool(self: Self) -> None:
1047+
def close(self: Self) -> None:
11401048
"""Return connection back to the pool.
11411049
11421050
It necessary to commit all transactions and close all cursor

python/tests/test_connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ async def test_connection_cursor(
145145
await transaction.begin()
146146
cursor = connection.cursor(querystring=f"SELECT * FROM {table_name}")
147147
await cursor.start()
148-
await cursor.close()
148+
cursor.close()
149149
await transaction.commit()
150150

151151

@@ -172,7 +172,7 @@ async def test_closed_connection_error(
172172
) -> None:
173173
"""Test exception when connection is closed."""
174174
connection = await psql_pool.connection()
175-
connection.back_to_pool()
175+
connection.close()
176176

177177
with pytest.raises(expected_exception=ConnectionClosedError):
178178
await connection.execute("SELECT 1")

0 commit comments

Comments
 (0)