Skip to content

Commit

Permalink
(tentative) add support for sqlalchemy 2.0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
adnam committed Dec 5, 2023
1 parent ec98791 commit 96e15fd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 59 deletions.
18 changes: 4 additions & 14 deletions databases/backends/aiopg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import aiopg
from sqlalchemy.engine.cursor import CursorResultMetaData
from sqlalchemy.engine.interfaces import Dialect, ExecutionContext
from sqlalchemy.engine.row import Row
from sqlalchemy.sql import ClauseElement
from sqlalchemy.sql.ddl import DDLElement

from databases.backends.common.records import Record, Row, create_column_maps
from databases.backends.common.records import Record, create_column_maps, make_row
from databases.backends.compilers.psycopg import PGCompiler_psycopg
from databases.backends.dialects.psycopg import PGDialect_psycopg
from databases.core import LOG_EXTRA, DatabaseURL
Expand Down Expand Up @@ -130,11 +129,8 @@ async def fetch_all(self, query: ClauseElement) -> typing.List[RecordInterface]:
rows = await cursor.fetchall()
metadata = CursorResultMetaData(context, cursor.description)
rows = [
Row(
make_row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
for row in rows
Expand All @@ -155,11 +151,8 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[RecordInterfa
if row is None:
return None
metadata = CursorResultMetaData(context, cursor.description)
row = Row(
row = make_row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
return Record(row, result_columns, dialect, column_maps)
Expand Down Expand Up @@ -198,11 +191,8 @@ async def iterate(
await cursor.execute(query_str, args)
metadata = CursorResultMetaData(context, cursor.description)
async for row in cursor:
record = Row(
record = make_row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
yield Record(record, result_columns, dialect, column_maps)
Expand Down
17 changes: 4 additions & 13 deletions databases/backends/asyncmy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sqlalchemy.sql import ClauseElement
from sqlalchemy.sql.ddl import DDLElement

from databases.backends.common.records import Record, Row, create_column_maps
from databases.backends.common.records import Record, create_column_maps, make_row
from databases.core import LOG_EXTRA, DatabaseURL
from databases.interfaces import (
ConnectionBackend,
Expand Down Expand Up @@ -117,11 +117,8 @@ async def fetch_all(self, query: ClauseElement) -> typing.List[RecordInterface]:
rows = await cursor.fetchall()
metadata = CursorResultMetaData(context, cursor.description)
rows = [
Row(
make_row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
for row in rows
Expand All @@ -144,11 +141,8 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[RecordInterfa
if row is None:
return None
metadata = CursorResultMetaData(context, cursor.description)
row = Row(
row = make_row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
return Record(row, result_columns, dialect, column_maps)
Expand Down Expand Up @@ -189,11 +183,8 @@ async def iterate(
await cursor.execute(query_str, args)
metadata = CursorResultMetaData(context, cursor.description)
async for row in cursor:
record = Row(
record = make_row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
yield Record(record, result_columns, dialect, column_maps)
Expand Down
25 changes: 25 additions & 0 deletions databases/backends/common/records.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json
import typing
from datetime import date, datetime
from packaging import version

from sqlalchemy import __version__ as sqlalchemy_version
from sqlalchemy.engine.interfaces import Dialect
from sqlalchemy.engine.row import Row as SQLRow
from sqlalchemy.sql.compiler import _CompileLabel
Expand Down Expand Up @@ -84,6 +86,7 @@ def __getattr__(self, name: str) -> typing.Any:


class Row(SQLRow):

def __getitem__(self, key: typing.Any) -> typing.Any:
"""
An instance of a Row in SQLAlchemy allows the access
Expand Down Expand Up @@ -140,3 +143,25 @@ def create_column_maps(
else:
column_map_full[str(column[0])] = (idx, datatype)
return column_map, column_map_int, column_map_full


if version.parse(sqlalchemy_version) >= version.parse("2.0.10"):

def make_row(metadata, row):
return Row(
metadata,
metadata._effective_processors,
metadata._key_to_index,
row,
)

else:

def make_row(metadata, row):
return Row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)

This comment has been minimized.

17 changes: 4 additions & 13 deletions databases/backends/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sqlalchemy.sql import ClauseElement
from sqlalchemy.sql.ddl import DDLElement

from databases.backends.common.records import Record, Row, create_column_maps
from databases.backends.common.records import Record, create_column_maps, make_row
from databases.core import LOG_EXTRA, DatabaseURL
from databases.interfaces import (
ConnectionBackend,
Expand Down Expand Up @@ -116,11 +116,8 @@ async def fetch_all(self, query: ClauseElement) -> typing.List[RecordInterface]:
rows = await cursor.fetchall()
metadata = CursorResultMetaData(context, cursor.description)
rows = [
Row(
make_row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
for row in rows
Expand All @@ -141,11 +138,8 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[RecordInterfa
if row is None:
return None
metadata = CursorResultMetaData(context, cursor.description)
row = Row(
row = make_row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
return Record(row, result_columns, dialect, column_maps)
Expand Down Expand Up @@ -186,11 +180,8 @@ async def iterate(
await cursor.execute(query_str, args)
metadata = CursorResultMetaData(context, cursor.description)
async for row in cursor:
record = Row(
record = make_row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
yield Record(record, result_columns, dialect, column_maps)
Expand Down
23 changes: 4 additions & 19 deletions databases/backends/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from sqlalchemy.sql import ClauseElement
from sqlalchemy.sql.ddl import DDLElement

from databases.backends.common.records import Record, Row, create_column_maps
from databases.backends.common.records import Record, create_column_maps, make_row
from databases.core import LOG_EXTRA, DatabaseURL
from databases.interfaces import ConnectionBackend, DatabaseBackend, TransactionBackend

Expand Down Expand Up @@ -82,16 +82,7 @@ async def fetch_all(self, query: ClauseElement) -> typing.List[Record]:
async with self._connection.execute(query_str, args) as cursor:
rows = await cursor.fetchall()
metadata = CursorResultMetaData(context, cursor.description)
rows = [
Row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
for row in rows
]
rows = [make_row(metadata, row) for row in rows]
return [Record(row, result_columns, dialect, column_maps) for row in rows]

async def fetch_one(self, query: ClauseElement) -> typing.Optional[Record]:
Expand All @@ -105,11 +96,8 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[Record]:
if row is None:
return None
metadata = CursorResultMetaData(context, cursor.description)
row = Row(
row = make_row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
return Record(row, result_columns, dialect, column_maps)
Expand Down Expand Up @@ -139,11 +127,8 @@ async def iterate(
async with self._connection.execute(query_str, args) as cursor:
metadata = CursorResultMetaData(context, cursor.description)
async for row in cursor:
record = Row(
record = make_row(
metadata,
metadata._processors,
metadata._keymap,
Row._default_key_style,
row,
)
yield Record(record, result_columns, dialect, column_maps)
Expand Down

0 comments on commit 96e15fd

Please sign in to comment.