database-wrapper-pgsql 0.2.32__py3-none-any.whl → 0.2.36__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- database_wrapper_pgsql/connector.py +65 -19
- database_wrapper_pgsql/db_wrapper_pgsql_mixin.py +2 -1
- database_wrapper_pgsql/pg_introspector.py +4 -2
- {database_wrapper_pgsql-0.2.32.dist-info → database_wrapper_pgsql-0.2.36.dist-info}/METADATA +2 -2
- database_wrapper_pgsql-0.2.36.dist-info/RECORD +12 -0
- database_wrapper_pgsql-0.2.32.dist-info/RECORD +0 -12
- {database_wrapper_pgsql-0.2.32.dist-info → database_wrapper_pgsql-0.2.36.dist-info}/WHEEL +0 -0
- {database_wrapper_pgsql-0.2.32.dist-info → database_wrapper_pgsql-0.2.36.dist-info}/top_level.txt +0 -0
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import asyncio
|
|
1
2
|
from collections.abc import AsyncIterator, Iterator
|
|
2
3
|
from contextlib import asynccontextmanager, contextmanager
|
|
3
4
|
from contextvars import ContextVar
|
|
4
5
|
from typing import Any, NotRequired, TypedDict, cast
|
|
6
|
+
from urllib.parse import quote
|
|
5
7
|
|
|
6
8
|
from psycopg import AsyncConnection as PgConnectionAsync # Async
|
|
7
9
|
from psycopg import AsyncCursor as PgCursorAsync
|
|
@@ -181,8 +183,11 @@ class PgsqlAsync(DatabaseBackend):
|
|
|
181
183
|
"""Destructor"""
|
|
182
184
|
|
|
183
185
|
# Just to be sure as async does not have __del__
|
|
184
|
-
|
|
185
|
-
|
|
186
|
+
if hasattr(self, "cursor"):
|
|
187
|
+
del self.cursor
|
|
188
|
+
|
|
189
|
+
if hasattr(self, "connection"):
|
|
190
|
+
del self.connection
|
|
186
191
|
|
|
187
192
|
async def open(self) -> None:
|
|
188
193
|
# Free resources
|
|
@@ -378,8 +383,10 @@ class PgsqlWithPooling(DatabaseBackend):
|
|
|
378
383
|
self.config["pool_kwargs"] = {}
|
|
379
384
|
|
|
380
385
|
conn_str = (
|
|
381
|
-
f"postgresql://{self.config['username']}:{self.config['password']}
|
|
382
|
-
f"
|
|
386
|
+
f"postgresql://{quote(self.config['username'])}:{quote(self.config['password'])}"
|
|
387
|
+
f"@{self.config['hostname']}:{self.config['port']}"
|
|
388
|
+
f"/{quote(self.config['database'])}?connect_timeout={self.connection_timeout}"
|
|
389
|
+
f"&application_name={quote(self.name)}"
|
|
383
390
|
f"&sslmode={self.config['ssl']}"
|
|
384
391
|
)
|
|
385
392
|
self.pool = ConnectionPool(
|
|
@@ -424,8 +431,29 @@ class PgsqlWithPooling(DatabaseBackend):
|
|
|
424
431
|
|
|
425
432
|
# Create new connection
|
|
426
433
|
res = self.new_connection()
|
|
427
|
-
if res:
|
|
428
|
-
(
|
|
434
|
+
if not res:
|
|
435
|
+
raise ConnectionError("Failed to get a connection from the pool")
|
|
436
|
+
|
|
437
|
+
(self.connection, self.cursor) = res
|
|
438
|
+
|
|
439
|
+
def close(self) -> None:
|
|
440
|
+
"""Close connection by returning it to the pool"""
|
|
441
|
+
try:
|
|
442
|
+
if self.cursor:
|
|
443
|
+
self.logger.debug("Closing cursor")
|
|
444
|
+
self.cursor.close()
|
|
445
|
+
except Exception as e:
|
|
446
|
+
self.logger.debug(f"Error while closing cursor: {e}")
|
|
447
|
+
finally:
|
|
448
|
+
self.cursor = None
|
|
449
|
+
|
|
450
|
+
if self.connection:
|
|
451
|
+
connection = self.connection
|
|
452
|
+
self.connection = None
|
|
453
|
+
try:
|
|
454
|
+
self.return_connection(connection)
|
|
455
|
+
except Exception as e:
|
|
456
|
+
self.logger.debug(f"Error while returning connection to the pool: {e}")
|
|
429
457
|
|
|
430
458
|
def new_connection(
|
|
431
459
|
self,
|
|
@@ -645,8 +673,10 @@ class PgsqlWithPoolingAsync(DatabaseBackend):
|
|
|
645
673
|
self.config["pool_kwargs"] = {}
|
|
646
674
|
|
|
647
675
|
conn_str = (
|
|
648
|
-
f"postgresql://{self.config['username']}:{self.config['password']}
|
|
649
|
-
f"
|
|
676
|
+
f"postgresql://{quote(self.config['username'])}:{quote(self.config['password'])}"
|
|
677
|
+
f"@{self.config['hostname']}:{self.config['port']}"
|
|
678
|
+
f"/{quote(self.config['database'])}?connect_timeout={self.connection_timeout}"
|
|
679
|
+
f"&application_name={quote(self.name)}"
|
|
650
680
|
f"&sslmode={self.config['ssl']}"
|
|
651
681
|
)
|
|
652
682
|
self.pool_async = AsyncConnectionPool(
|
|
@@ -666,9 +696,14 @@ class PgsqlWithPoolingAsync(DatabaseBackend):
|
|
|
666
696
|
|
|
667
697
|
def __del__(self) -> None:
|
|
668
698
|
"""Destructor"""
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
699
|
+
if hasattr(self, "cursor"):
|
|
700
|
+
del self.cursor
|
|
701
|
+
|
|
702
|
+
if hasattr(self, "connection"):
|
|
703
|
+
del self.connection
|
|
704
|
+
|
|
705
|
+
if hasattr(self, "pool_async"):
|
|
706
|
+
del self.pool_async
|
|
672
707
|
|
|
673
708
|
##################
|
|
674
709
|
### Connection ###
|
|
@@ -697,20 +732,29 @@ class PgsqlWithPoolingAsync(DatabaseBackend):
|
|
|
697
732
|
|
|
698
733
|
# Create new connection
|
|
699
734
|
res = await self.new_connection()
|
|
700
|
-
if res:
|
|
701
|
-
(
|
|
735
|
+
if not res:
|
|
736
|
+
raise ConnectionError("Failed to get a connection from the pool")
|
|
737
|
+
|
|
738
|
+
(self.connection, self.cursor) = res
|
|
702
739
|
|
|
703
740
|
async def close(self) -> None:
|
|
704
741
|
"""Close connection by returning it to the pool"""
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
742
|
+
try:
|
|
743
|
+
if self.cursor:
|
|
744
|
+
self.logger.debug("Closing cursor")
|
|
745
|
+
await self.cursor.close()
|
|
746
|
+
except Exception as e:
|
|
747
|
+
self.logger.debug(f"Error while closing cursor: {e}")
|
|
748
|
+
finally:
|
|
709
749
|
self.cursor = None
|
|
710
750
|
|
|
711
751
|
if self.connection:
|
|
712
|
-
|
|
752
|
+
connection = self.connection
|
|
713
753
|
self.connection = None
|
|
754
|
+
try:
|
|
755
|
+
await self.return_connection(connection)
|
|
756
|
+
except Exception as e:
|
|
757
|
+
self.logger.debug(f"Error while returning connection to the pool: {e}")
|
|
714
758
|
|
|
715
759
|
async def new_connection(
|
|
716
760
|
self,
|
|
@@ -743,7 +787,9 @@ class PgsqlWithPoolingAsync(DatabaseBackend):
|
|
|
743
787
|
await self.pool_async.putconn(connection)
|
|
744
788
|
|
|
745
789
|
self.logger.error(f"Error while getting connection from the pool: {e}")
|
|
746
|
-
|
|
790
|
+
# Event.wait() would block the whole event loop; the loop condition
|
|
791
|
+
# re-checks shutdown_requested after the sleep
|
|
792
|
+
await asyncio.sleep(self.slow_down_timeout)
|
|
747
793
|
tries += 1
|
|
748
794
|
if tries >= 3:
|
|
749
795
|
break
|
|
@@ -73,7 +73,8 @@ class DBWrapperPgsqlMixin:
|
|
|
73
73
|
order_list = [
|
|
74
74
|
sql.SQL("{} {}").format(
|
|
75
75
|
sql.Identifier(item[0]),
|
|
76
|
-
|
|
76
|
+
# order_direction (from DBWrapperMixin via MRO) validates against ASC/DESC
|
|
77
|
+
sql.SQL(self.order_direction(item)), # type: ignore[attr-defined]
|
|
77
78
|
)
|
|
78
79
|
for item in order_by
|
|
79
80
|
]
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import psycopg
|
|
2
|
+
from psycopg.rows import dict_row
|
|
2
3
|
|
|
3
4
|
from database_wrapper import ColumnMetaIntrospector, DBIntrospector
|
|
4
5
|
|
|
@@ -53,7 +54,8 @@ class PostgresIntrospector(DBIntrospector):
|
|
|
53
54
|
END AS enum_labels
|
|
54
55
|
FROM cols;
|
|
55
56
|
"""
|
|
56
|
-
|
|
57
|
+
# Explicit row factory - the default psycopg cursor returns tuples
|
|
58
|
+
with self.conn.cursor(row_factory=dict_row) as cur:
|
|
57
59
|
cur.execute(q, (schema, table))
|
|
58
60
|
rows = cur.fetchall()
|
|
59
61
|
|
|
@@ -66,7 +68,7 @@ class PostgresIntrospector(DBIntrospector):
|
|
|
66
68
|
is_nullable=bool(row["is_nullable"]),
|
|
67
69
|
has_default=(row["default_expr"] is not None),
|
|
68
70
|
default_expr=row["default_expr"],
|
|
69
|
-
|
|
71
|
+
enum_labels=list(row["enum_labels"]) if row["enum_labels"] else None,
|
|
70
72
|
)
|
|
71
73
|
)
|
|
72
74
|
|
{database_wrapper_pgsql-0.2.32.dist-info → database_wrapper_pgsql-0.2.36.dist-info}/METADATA
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: database_wrapper_pgsql
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.36
|
|
4
4
|
Summary: database_wrapper for PostgreSQL database
|
|
5
5
|
Author-email: Gints Murans <gm@gm.lv>
|
|
6
6
|
License: GNU General Public License v3.0 (GPL-3.0)
|
|
@@ -32,7 +32,7 @@ Classifier: Topic :: Software Development
|
|
|
32
32
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
33
33
|
Requires-Python: >=3.8
|
|
34
34
|
Description-Content-Type: text/markdown
|
|
35
|
-
Requires-Dist: database_wrapper==0.2.
|
|
35
|
+
Requires-Dist: database_wrapper==0.2.36
|
|
36
36
|
Requires-Dist: psycopg[binary]>=3.2.0
|
|
37
37
|
Requires-Dist: psycopg[pool]>=3.2.0
|
|
38
38
|
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
database_wrapper_pgsql/__init__.py,sha256=p1CkqOmXI61cy3uzpnRtukUG8vQrLfXCFPrwDfOEvFU,1161
|
|
2
|
+
database_wrapper_pgsql/connector.py,sha256=yY2Q95U8Jqty2L6eZ3pzQeWDExoP67Ejc7qFCPuKD84,27202
|
|
3
|
+
database_wrapper_pgsql/db_wrapper_pgsql.py,sha256=lrMN9lqfS0JapEfNu2sFeFxifK67eBhZZebU3MpvnRk,1909
|
|
4
|
+
database_wrapper_pgsql/db_wrapper_pgsql_async.py,sha256=cYYyBFWvVG3e24Ik0mJXnbxic9bskqnB-K3hEbJ6pk8,2025
|
|
5
|
+
database_wrapper_pgsql/db_wrapper_pgsql_mixin.py,sha256=_mLRkny8Outf3HaLcUCN7jDyZXMsaroK30U7c-6tbPs,5536
|
|
6
|
+
database_wrapper_pgsql/pg_introspector.py,sha256=-9_tTxOG7778gxaGuhsxcbCN42mEjr9kG-EAegxsU74,2684
|
|
7
|
+
database_wrapper_pgsql/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
database_wrapper_pgsql/type_mapping.py,sha256=KWIAT78Z6h5DEt8uQNfQP6t5ahBUPuLKgcPfQmIywM4,1625
|
|
9
|
+
database_wrapper_pgsql-0.2.36.dist-info/METADATA,sha256=MTP0ErpYJCHKw5jSjZAmW6HcP72q4Cymi3w22YP3JF8,3231
|
|
10
|
+
database_wrapper_pgsql-0.2.36.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
11
|
+
database_wrapper_pgsql-0.2.36.dist-info/top_level.txt,sha256=EQhZLk12wRdsSp-Lo3Jc4cXmNfG8y5EouNv_7OBCSGo,23
|
|
12
|
+
database_wrapper_pgsql-0.2.36.dist-info/RECORD,,
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
database_wrapper_pgsql/__init__.py,sha256=p1CkqOmXI61cy3uzpnRtukUG8vQrLfXCFPrwDfOEvFU,1161
|
|
2
|
-
database_wrapper_pgsql/connector.py,sha256=l1Dk35hnSDlGhgdzB4ldrJa6QL83CKn-VgD_R11P2Go,25548
|
|
3
|
-
database_wrapper_pgsql/db_wrapper_pgsql.py,sha256=lrMN9lqfS0JapEfNu2sFeFxifK67eBhZZebU3MpvnRk,1909
|
|
4
|
-
database_wrapper_pgsql/db_wrapper_pgsql_async.py,sha256=cYYyBFWvVG3e24Ik0mJXnbxic9bskqnB-K3hEbJ6pk8,2025
|
|
5
|
-
database_wrapper_pgsql/db_wrapper_pgsql_mixin.py,sha256=m6scMsSCAE7mhU2wV3D_V9hFS52zP1_SFwIiAIVKFKo,5448
|
|
6
|
-
database_wrapper_pgsql/pg_introspector.py,sha256=mMIosvQZZNE3ocAF7Wys50gYBjXrT9EaI-xcTWt-UnQ,2557
|
|
7
|
-
database_wrapper_pgsql/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
-
database_wrapper_pgsql/type_mapping.py,sha256=KWIAT78Z6h5DEt8uQNfQP6t5ahBUPuLKgcPfQmIywM4,1625
|
|
9
|
-
database_wrapper_pgsql-0.2.32.dist-info/METADATA,sha256=sj9pDZrWgRX-KAPV7yLBSdMkzscRQcKTLhQ80Bnz8o8,3231
|
|
10
|
-
database_wrapper_pgsql-0.2.32.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
11
|
-
database_wrapper_pgsql-0.2.32.dist-info/top_level.txt,sha256=EQhZLk12wRdsSp-Lo3Jc4cXmNfG8y5EouNv_7OBCSGo,23
|
|
12
|
-
database_wrapper_pgsql-0.2.32.dist-info/RECORD,,
|
|
File without changes
|
{database_wrapper_pgsql-0.2.32.dist-info → database_wrapper_pgsql-0.2.36.dist-info}/top_level.txt
RENAMED
|
File without changes
|