Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add psycopg3 as a new backend #586

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
S01E05
  • Loading branch information
ansipunk committed Mar 3, 2024
commit ddd8aaa3efbf960b33e1bdeb7d1b72a915d8bb41
1 change: 0 additions & 1 deletion databases/backends/common/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from sqlalchemy.engine.row import Row as SQLRow
from sqlalchemy.sql.compiler import _CompileLabel
from sqlalchemy.sql.schema import Column
from sqlalchemy.sql.sqltypes import JSON
from sqlalchemy.types import TypeEngine

from databases.interfaces import Record as RecordInterface
Expand Down
13 changes: 7 additions & 6 deletions databases/backends/psycopg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import psycopg
import psycopg_pool
from psycopg.rows import namedtuple_row
from sqlalchemy.engine.interfaces import Dialect
from sqlalchemy.dialects.postgresql.psycopg import PGDialect_psycopg
from sqlalchemy.sql import ClauseElement
Expand Down Expand Up @@ -58,12 +59,11 @@ def connection(self) -> "PsycopgConnection":
class PsycopgConnection(ConnectionBackend):
_database: PsycopgBackend
_dialect: Dialect
_connection: typing.Optional[psycopg.AsyncConnection]
_connection: typing.Optional[psycopg.AsyncConnection] = None

def __init__(self, database: PsycopgBackend, dialect: Dialect) -> None:
self._database = database
self._dialect = dialect
self._connection = None

async def acquire(self) -> None:
if self._connection is not None:
Expand All @@ -74,6 +74,7 @@ async def acquire(self) -> None:

# TODO: Add configurable timeouts
self._connection = await self._database._pool.getconn()
await self._connection.set_autocommit(True)

async def release(self) -> None:
if self._connection is None:
Expand All @@ -88,7 +89,7 @@ async def fetch_all(self, query: ClauseElement) -> typing.List[RecordInterface]:

query_str, args, result_columns = self._compile(query)

async with self._connection.cursor() as cursor:
async with self._connection.cursor(row_factory=namedtuple_row) as cursor:
await cursor.execute(query_str, args)
rows = await cursor.fetchall()

Expand All @@ -101,7 +102,7 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[RecordInterfa

query_str, args, result_columns = self._compile(query)

async with self._connection.cursor() as cursor:
async with self._connection.cursor(row_factory=namedtuple_row) as cursor:
await cursor.execute(query_str, args)
row = await cursor.fetchone()

Expand All @@ -127,7 +128,7 @@ async def execute(self, query: ClauseElement) -> typing.Any:

query_str, args, _ = self._compile(query)

async with self._connection.cursor() as cursor:
async with self._connection.cursor(row_factory=namedtuple_row) as cursor:
await cursor.execute(query_str, args)

async def execute_many(self, queries: typing.List[ClauseElement]) -> None:
Expand All @@ -144,7 +145,7 @@ async def iterate(
query_str, args, result_columns = self._compile(query)
column_maps = create_column_maps(result_columns)

async with self._connection.cursor() as cursor:
async with self._connection.cursor(row_factory=namedtuple_row) as cursor:
await cursor.execute(query_str, args)

while True:
Expand Down
Loading