-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use psycopg instead of django connection to connect into chats db
- Loading branch information
1 parent
50f07b2
commit 7a259ad
Showing
6 changed files
with
79 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,38 @@ | ||
from contextlib import contextmanager | ||
|
||
from django.db import connections | ||
import settings | ||
from psycopg import connect | ||
from psycopg.rows import dict_row | ||
from psycopg_pool import ConnectionPool, NullConnectionPool | ||
|
||
|
||
def dictfetchall(cursor): | ||
""" | ||
Return all rows from a cursor as a dict. | ||
Assume the column names are unique. | ||
""" | ||
columns = [col[0] for col in cursor.description] | ||
return [dict(zip(columns, row)) for row in cursor.fetchall()] | ||
return cursor.fetchall() | ||
|
||
|
||
def dictfetchone(cursor): | ||
""" | ||
Return all rows from a cursor as a dict. | ||
Assume the column names are unique. | ||
""" | ||
columns = [col[0] for col in cursor.description] | ||
row = cursor.fetchone() | ||
return dict(zip(columns, row)) | ||
return cursor.fetchone() | ||
|
||
|
||
def pg_execute_query(db_name: str, query: str, *args, **kwargs): | ||
with connections[db_name].cursor() as cursor: | ||
return cursor.execute(query, args).fetchall() | ||
chats_pool = NullConnectionPool( | ||
max_size=5, | ||
conninfo=settings.CHATS_PG, | ||
check=ConnectionPool.check_connection, | ||
) | ||
|
||
|
||
@contextmanager | ||
def get_cursor(db_name: str): | ||
with connections[db_name].cursor() as cur: | ||
yield cur | ||
def get_connection(): | ||
# if settings.CONNECTION_TYPE == "pool": | ||
# with chats_pool.connection() as conn: | ||
# yield conn | ||
# else: | ||
with connect(settings.CHATS_PG) as conn: | ||
yield conn | ||
|
||
|
||
@contextmanager | ||
def get_cursor(*args, **kwargs): | ||
with get_connection() as conn: | ||
with conn.cursor(row_factory=dict_row) as cur: | ||
yield cur |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from contextlib import contextmanager | ||
|
||
from django.db import connections | ||
|
||
|
||
def dictfetchall(cursor): | ||
""" | ||
Return all rows from a cursor as a dict. | ||
Assume the column names are unique. | ||
""" | ||
columns = [col[0] for col in cursor.description] | ||
return [dict(zip(columns, row)) for row in cursor.fetchall()] | ||
|
||
|
||
def dictfetchone(cursor): | ||
""" | ||
Return all rows from a cursor as a dict. | ||
Assume the column names are unique. | ||
""" | ||
columns = [col[0] for col in cursor.description] | ||
row = cursor.fetchone() | ||
return dict(zip(columns, row)) | ||
|
||
|
||
@contextmanager | ||
def get_cursor(db_name: str): | ||
with connections[db_name].cursor() as cur: | ||
yield cur |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters