@@ -160,6 +160,25 @@ class Cursor:
160
160
It can be used as an asynchronous iterator.
161
161
"""
162
162
163
+ def __aiter__ (self : Self ) -> Self : ...
164
+ async def __anext__ (self : Self ) -> QueryResult : ...
165
+ async def __aenter__ (self : Self ) -> Self : ...
166
+ async def __aexit__ (
167
+ self : Self ,
168
+ exception_type : type [BaseException ] | None ,
169
+ exception : BaseException | None ,
170
+ traceback : types .TracebackType | None ,
171
+ ) -> None : ...
172
+ async def start (self : Self ) -> None :
173
+ """Start the cursor.
174
+
175
+ Execute DECLARE command for the cursor.
176
+ """
177
+ async def close (self : Self ) -> None :
178
+ """Close the cursor.
179
+
180
+ Execute CLOSE command for the cursor.
181
+ """
163
182
async def fetch (
164
183
self : Self ,
165
184
fetch_number : int | None = None ,
@@ -267,13 +286,6 @@ class Cursor:
267
286
### Returns:
268
287
result as `QueryResult`.
269
288
"""
270
- async def close (self : Self ) -> None :
271
- """Close the cursor.
272
-
273
- Execute CLOSE command for the cursor.
274
- """
275
- def __aiter__ (self : Self ) -> Self : ...
276
- async def __anext__ (self : Self ) -> QueryResult : ...
277
289
278
290
class Transaction :
279
291
"""Single connection for executing queries.
@@ -330,7 +342,8 @@ class Transaction:
330
342
db_pool = PSQLPool()
331
343
await db_pool.startup()
332
344
333
- transaction = await db_pool.transaction()
345
+ connection = await db_pool.connection()
346
+ transaction = connection.transaction()
334
347
await transaction.begin()
335
348
query_result: QueryResult = await transaction.execute(
336
349
"SELECT username FROM users WHERE id = $1",
@@ -339,21 +352,6 @@ class Transaction:
339
352
dict_result: List[Dict[Any, Any]] = query_result.result()
340
353
# You must call commit manually
341
354
await transaction.commit()
342
-
343
- # Or you can transaction as a async context manager
344
-
345
- async def main() -> None:
346
- db_pool = PSQLPool()
347
- await psqlpy.startup()
348
-
349
- transaction = await db_pool.transaction()
350
- async with transaction:
351
- query_result: QueryResult = await transaction.execute(
352
- "SELECT username FROM users WHERE id = $1",
353
- [100],
354
- )
355
- dict_result: List[Dict[Any, Any]] = query_result.result()
356
- # This way transaction begins and commits by itself.
357
355
```
358
356
"""
359
357
async def execute_many (
@@ -381,7 +379,8 @@ class Transaction:
381
379
db_pool = PSQLPool()
382
380
await db_pool.startup()
383
381
384
- transaction = await db_pool.transaction()
382
+ connection = await db_pool.connection()
383
+ transaction = connection.transaction()
385
384
await transaction.begin()
386
385
query_result: QueryResult = await transaction.execute_many(
387
386
"INSERT INTO users (name, age) VALUES ($1, $2)",
@@ -390,21 +389,6 @@ class Transaction:
390
389
dict_result: List[Dict[Any, Any]] = query_result.result()
391
390
# You must call commit manually
392
391
await transaction.commit()
393
-
394
- # Or you can transaction as a async context manager
395
-
396
- async def main() -> None:
397
- db_pool = PSQLPool()
398
- await psqlpy.startup()
399
-
400
- transaction = await db_pool.transaction()
401
- async with transaction:
402
- query_result: QueryResult = await transaction.execute(
403
- "SELECT username FROM users WHERE id = $1",
404
- [100],
405
- )
406
- dict_result: List[Dict[Any, Any]] = query_result.result()
407
- # This way transaction begins and commits by itself.
408
392
```
409
393
"""
410
394
async def fetch_row (
@@ -434,30 +418,16 @@ class Transaction:
434
418
db_pool = PSQLPool()
435
419
await db_pool.startup()
436
420
437
- transaction = await db_pool.transaction()
421
+ connection = await db_pool.connection()
422
+ transaction = connection.transaction()
438
423
await transaction.begin()
439
- query_result: SingleQueryResult = await transaction.execute (
424
+ query_result: SingleQueryResult = await transaction.fetch_row (
440
425
"SELECT username FROM users WHERE id = $1",
441
426
[100],
442
427
)
443
428
dict_result: Dict[Any, Any] = query_result.result()
444
429
# You must call commit manually
445
430
await transaction.commit()
446
-
447
- # Or you can transaction as a async context manager
448
-
449
- async def main() -> None:
450
- db_pool = PSQLPool()
451
- await psqlpy.startup()
452
-
453
- transaction = await db_pool.transaction()
454
- async with transaction:
455
- query_result: SingleQueryResult = await transaction.execute(
456
- "SELECT username FROM users WHERE id = $1 LIMIT 1",
457
- [100],
458
- )
459
- dict_result: Dict[Any, Any] = query_result.result()
460
- # This way transaction begins and commits by itself.
461
431
```
462
432
"""
463
433
async def fetch_val (
@@ -485,27 +455,13 @@ class Transaction:
485
455
db_pool = PSQLPool()
486
456
await db_pool.startup()
487
457
488
- transaction = await db_pool.transaction()
458
+ connection = await db_pool.connection()
459
+ transaction = connection.transaction()
489
460
await transaction.begin()
490
461
value: Any | None = await transaction.execute(
491
462
"SELECT username FROM users WHERE id = $1",
492
463
[100],
493
464
)
494
-
495
- # Or you can transaction as a async context manager
496
-
497
- async def main() -> None:
498
- db_pool = PSQLPool()
499
- await psqlpy.startup()
500
-
501
- transaction = await db_pool.transaction()
502
- async with transaction:
503
- query_result: SingleQueryResult = await transaction.execute(
504
- "SELECT username FROM users WHERE id = $1",
505
- [100],
506
- )
507
- dict_result: Dict[Any, Any] = query_result.result()
508
- # This way transaction begins and commits by itself.
509
465
```
510
466
"""
511
467
async def pipeline (
@@ -547,7 +503,8 @@ class Transaction:
547
503
db_pool = PSQLPool()
548
504
await db_pool.startup()
549
505
550
- transaction = await db_pool.transaction()
506
+ connection = await db_pool.connection()
507
+ transaction = connection.transaction()
551
508
552
509
results: list[QueryResult] = await transaction.pipeline(
553
510
queries=[
@@ -591,7 +548,8 @@ class Transaction:
591
548
db_pool = PSQLPool()
592
549
await db_pool.startup()
593
550
594
- transaction = await db_pool.transaction()
551
+ connection = await db_pool.connection()
552
+ transaction = connection.transaction()
595
553
596
554
await transaction.savepoint("my_savepoint")
597
555
await transaction.execute(...)
@@ -615,7 +573,8 @@ class Transaction:
615
573
db_pool = PSQLPool()
616
574
await db_pool.startup()
617
575
618
- transaction = await db_pool.transaction()
576
+ connection = await db_pool.connection()
577
+ transaction = connection.transaction()
619
578
await transaction.execute(...)
620
579
await transaction.rollback()
621
580
```
@@ -640,7 +599,8 @@ class Transaction:
640
599
db_pool = PSQLPool()
641
600
await db_pool.startup()
642
601
643
- transaction = await db_pool.transaction()
602
+ connection = await db_pool.connection()
603
+ transaction = connection.transaction()
644
604
645
605
await transaction.savepoint("my_savepoint")
646
606
await transaction.execute(...)
@@ -667,13 +627,14 @@ class Transaction:
667
627
db_pool = PSQLPool()
668
628
await db_pool.startup()
669
629
670
- transaction = await db_pool.transaction()
630
+ connection = await db_pool.connection()
631
+ transaction = connection.transaction()
671
632
672
633
await transaction.savepoint("my_savepoint")
673
634
await transaction.release_savepoint
674
635
```
675
636
"""
676
- async def cursor (
637
+ def cursor (
677
638
self : Self ,
678
639
querystring : str ,
679
640
parameters : list [Any ] | None = None ,
@@ -707,15 +668,18 @@ class Transaction:
707
668
connection = await db_pool.connection()
708
669
transaction = await connection.transaction()
709
670
710
- cursor = await transaction.cursor(
671
+ cursor = transaction.cursor(
711
672
querystring="SELECT * FROM users WHERE username = $1",
712
673
parameters=["Some_Username"],
713
674
fetch_number=5,
714
675
)
676
+ await cursor.start()
715
677
716
678
async for fetched_result in cursor:
717
679
dict_result: List[Dict[Any, Any]] = fetched_result.result()
718
680
... # do something with this result.
681
+
682
+ await cursor.close()
719
683
```
720
684
"""
721
685
@@ -772,6 +736,7 @@ class Connection:
772
736
### Parameters:
773
737
- `isolation_level`: configure isolation level of the transaction.
774
738
- `read_variant`: configure read variant of the transaction.
739
+ - `deferrable`: configure deferrable of the transaction.
775
740
"""
776
741
777
742
class PSQLPool :
@@ -842,7 +807,7 @@ class PSQLPool:
842
807
843
808
async def main() -> None:
844
809
db_pool = PSQLPool()
845
- await psqlpy .startup()
810
+ await db_pool .startup()
846
811
query_result: QueryResult = await psqlpy.execute(
847
812
"SELECT username FROM users WHERE id = $1",
848
813
[100],
0 commit comments