database-wrapper-pgsql 0.2.32__tar.gz → 0.2.36__tar.gz

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.
Files changed (17) hide show
  1. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/PKG-INFO +2 -2
  2. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/database_wrapper_pgsql/connector.py +65 -19
  3. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/database_wrapper_pgsql/db_wrapper_pgsql_mixin.py +2 -1
  4. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/database_wrapper_pgsql/pg_introspector.py +4 -2
  5. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/database_wrapper_pgsql.egg-info/PKG-INFO +2 -2
  6. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/database_wrapper_pgsql.egg-info/requires.txt +1 -1
  7. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/pyproject.toml +2 -2
  8. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/README.md +0 -0
  9. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/database_wrapper_pgsql/__init__.py +0 -0
  10. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/database_wrapper_pgsql/db_wrapper_pgsql.py +0 -0
  11. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/database_wrapper_pgsql/db_wrapper_pgsql_async.py +0 -0
  12. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/database_wrapper_pgsql/py.typed +0 -0
  13. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/database_wrapper_pgsql/type_mapping.py +0 -0
  14. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/database_wrapper_pgsql.egg-info/SOURCES.txt +0 -0
  15. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/database_wrapper_pgsql.egg-info/dependency_links.txt +0 -0
  16. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/database_wrapper_pgsql.egg-info/top_level.txt +0 -0
  17. {database_wrapper_pgsql-0.2.32 → database_wrapper_pgsql-0.2.36}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: database_wrapper_pgsql
3
- Version: 0.2.32
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.32
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
 
@@ -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
- del self.cursor
185
- del self.connection
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']}@{self.config['hostname']}:{self.config['port']}"
382
- f"/{self.config['database']}?connect_timeout={self.connection_timeout}&application_name={self.name}"
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
- (self.connection, self.cursor) = res
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']}@{self.config['hostname']}:{self.config['port']}"
649
- f"/{self.config['database']}?connect_timeout={self.connection_timeout}&application_name={self.name}"
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
- del self.cursor
670
- del self.connection
671
- del self.pool_async
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
- (self.connection, self.cursor) = res
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
- if self.cursor:
707
- self.logger.debug("Closing cursor")
708
- await self.cursor.close()
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
- await self.return_connection(self.connection)
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
- self.shutdown_requested.wait(self.slow_down_timeout)
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
- sql.SQL(item[1] if len(item) > 1 and item[1] is not None else "ASC"),
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
- with self.conn.cursor() as cur:
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
- # enum_labels=list(row["enum_labels"]) if row["enum_labels"] else None,
71
+ enum_labels=list(row["enum_labels"]) if row["enum_labels"] else None,
70
72
  )
71
73
  )
72
74
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: database_wrapper_pgsql
3
- Version: 0.2.32
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.32
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
 
@@ -1,3 +1,3 @@
1
- database_wrapper==0.2.32
1
+ database_wrapper==0.2.36
2
2
  psycopg[binary]>=3.2.0
3
3
  psycopg[pool]>=3.2.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "database_wrapper_pgsql"
7
- version = "0.2.32"
7
+ version = "0.2.36"
8
8
  description = "database_wrapper for PostgreSQL database"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.8"
@@ -33,7 +33,7 @@ classifiers = [
33
33
  ]
34
34
  keywords = ["database", "wrapper", "python", "postgresql", "pgsql"]
35
35
  dependencies = [
36
- "database_wrapper == 0.2.32",
36
+ "database_wrapper == 0.2.36",
37
37
  "psycopg[binary] >= 3.2.0",
38
38
  "psycopg[pool] >= 3.2.0",
39
39
  ]