Skip to content

Commit 2bb7a47

Browse files
Add SyncClient.
Replaces client with synchronous version of same API.
1 parent a421dd8 commit 2bb7a47

File tree

4 files changed

+683
-0
lines changed

4 files changed

+683
-0
lines changed

db_wrapper/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
"""
2020
from .connection import ConnectionParameters
2121
from .client import Client
22+
from .sync_client import Client as SyncClient
2223
from .model import Model, ModelData

db_wrapper/sync_client.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""Wrapper on aiopg to simplify connecting to & interacting with db."""
2+
3+
from __future__ import annotations
4+
from typing import (
5+
Any,
6+
TypeVar,
7+
Union,
8+
Optional,
9+
Hashable,
10+
List,
11+
Dict)
12+
13+
from psycopg2.extras import register_uuid
14+
# importing for the sole purpose of re-exporting
15+
# pylint: disable=unused-import
16+
from psycopg2 import sql
17+
from psycopg2._psycopg import cursor
18+
19+
from .connection import (
20+
sync_connect,
21+
ConnectionParameters,
22+
)
23+
24+
# add uuid support to psycopg2 & Postgres
25+
register_uuid()
26+
27+
28+
# Generic doesn't need a more descriptive name
29+
# pylint: disable=invalid-name
30+
T = TypeVar('T')
31+
32+
Query = Union[str, sql.Composed]
33+
34+
35+
class Client:
36+
"""Class to manage database connection & expose necessary methods to user.
37+
38+
Stores connection parameters on init, then exposes methods to
39+
asynchronously connect & disconnect the database, as well as execute SQL
40+
queries.
41+
"""
42+
43+
_connection_params: ConnectionParameters
44+
_connection: Any
45+
46+
def __init__(self, connection_params: ConnectionParameters) -> None:
47+
self._connection_params = connection_params
48+
49+
def connect(self) -> None:
50+
"""Connect to the database."""
51+
self._connection = sync_connect(self._connection_params)
52+
53+
def disconnect(self) -> None:
54+
"""Disconnect from the database."""
55+
self._connection.close()
56+
57+
@staticmethod
58+
def _execute_query(
59+
cursor: cursor,
60+
query: Query,
61+
params: Optional[Dict[Hashable, Any]] = None,
62+
) -> None:
63+
if params:
64+
cursor.execute(query, params) # type: ignore
65+
else:
66+
cursor.execute(query)
67+
68+
def execute(
69+
self,
70+
query: Query,
71+
params: Optional[Dict[Hashable, Any]] = None,
72+
) -> None:
73+
"""Execute the given SQL query.
74+
75+
Arguments:
76+
query (Query) -- the SQL query to execute
77+
params (dict) -- a dictionary of parameters to interpolate when
78+
executing the query
79+
80+
Returns:
81+
None
82+
"""
83+
with self._connection.cursor() as cursor:
84+
self._execute_query(cursor, query, params)
85+
86+
def execute_and_return(
87+
self,
88+
query: Query,
89+
params: Optional[Dict[Hashable, Any]] = None,
90+
) -> List[T]:
91+
"""Execute the given SQL query & return the result.
92+
93+
Arguments:
94+
query (Query) -- the SQL query to execute
95+
params (dict) -- a dictionary of parameters to interpolate when
96+
executing the query
97+
98+
Returns:
99+
List containing all the rows that matched the query.
100+
"""
101+
with self._connection.cursor() as cursor:
102+
self._execute_query(cursor, query, params)
103+
104+
result: List[T] = cursor.fetchall()
105+
return result

stubs/psycopg2/__init__.pyi

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from psycopg2._psycopg import (
2+
BINARY,
3+
Binary,
4+
connection,
5+
DATETIME,
6+
DataError,
7+
DatabaseError,
8+
Date,
9+
DateFromTicks,
10+
Error,
11+
IntegrityError,
12+
InterfaceError,
13+
InternalError,
14+
NUMBER,
15+
NotSupportedError,
16+
OperationalError,
17+
ProgrammingError,
18+
ROWID,
19+
STRING,
20+
Time,
21+
TimeFromTicks,
22+
Timestamp,
23+
TimestampFromTicks,
24+
Warning,
25+
__libpq_version__,
26+
apilevel,
27+
paramstyle,
28+
threadsafety
29+
)
30+
from typing import Any, Optional
31+
32+
connection = connection
33+
OperationalError = OperationalError
34+
35+
36+
def connect(
37+
dsn: Optional[Any] = ...,
38+
connection_factory: Optional[Any] = ...,
39+
cursor_factory: Optional[Any] = ...,
40+
**kwargs: Any
41+
) -> connection: ...

0 commit comments

Comments
 (0)