Skip to content

Commit 6910288

Browse files
author
ansipunk
committed
S01E10
1 parent cdbf97f commit 6910288

File tree

6 files changed

+71
-7
lines changed

6 files changed

+71
-7
lines changed

databases/backends/aiopg.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
import uuid
66

77
import aiopg
8-
from sqlalchemy.dialects.postgresql.psycopg import PGDialect_psycopg
98
from sqlalchemy.engine.cursor import CursorResultMetaData
109
from sqlalchemy.engine.interfaces import Dialect, ExecutionContext
1110
from sqlalchemy.sql import ClauseElement
1211
from sqlalchemy.sql.ddl import DDLElement
1312

1413
from databases.backends.common.records import Record, Row, create_column_maps
14+
from databases.backends.compilers.psycopg import PGCompiler_psycopg
15+
from databases.backends.dialects.psycopg import PGDialect_psycopg
1516
from databases.core import LOG_EXTRA, DatabaseURL
1617
from databases.interfaces import (
1718
ConnectionBackend,
@@ -36,10 +37,12 @@ def _get_dialect(self) -> Dialect:
3637
dialect = PGDialect_psycopg(
3738
json_serializer=json.dumps, json_deserializer=lambda x: x
3839
)
40+
dialect.statement_compiler = PGCompiler_psycopg
3941
dialect.implicit_returning = True
4042
dialect.supports_native_enum = True
4143
dialect.supports_smallserial = True # 9.2+
4244
dialect._backslash_escapes = False
45+
dialect.supports_sane_multi_rowcount = True # psycopg 2.0.9+
4346
dialect._has_native_hstore = True
4447
dialect.supports_native_decimal = True
4548

databases/backends/asyncpg.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import typing
33

44
import asyncpg
5-
from sqlalchemy.dialects.postgresql.psycopg import PGDialect_psycopg
65
from sqlalchemy.engine.interfaces import Dialect
76
from sqlalchemy.sql import ClauseElement
87
from sqlalchemy.sql.ddl import DDLElement
98

109
from databases.backends.common.records import Record, create_column_maps
10+
from databases.backends.dialects.psycopg import dialect as psycopg_dialect
1111
from databases.core import LOG_EXTRA, DatabaseURL
1212
from databases.interfaces import (
1313
ConnectionBackend,
@@ -29,14 +29,16 @@ def __init__(
2929
self._pool = None
3030

3131
def _get_dialect(self) -> Dialect:
32-
dialect = PGDialect_psycopg(paramstyle="pyformat")
32+
dialect = psycopg_dialect(paramstyle="pyformat")
33+
3334
dialect.implicit_returning = True
3435
dialect.supports_native_enum = True
3536
dialect.supports_smallserial = True # 9.2+
3637
dialect._backslash_escapes = False
3738
dialect.supports_sane_multi_rowcount = True # psycopg 2.0.9+
3839
dialect._has_native_hstore = True
3940
dialect.supports_native_decimal = True
41+
4042
return dialect
4143

4244
def _get_connection_kwargs(self) -> dict:
@@ -189,10 +191,10 @@ def _compile(self, query: ClauseElement) -> typing.Tuple[str, list, tuple]:
189191
)
190192
return compiled_query, args, result_map
191193

192-
@property
193-
def raw_connection(self) -> asyncpg.connection.Connection:
194-
assert self._connection is not None, "Connection is not acquired"
195-
return self._connection
194+
@property
195+
def raw_connection(self) -> asyncpg.connection.Connection:
196+
assert self._connection is not None, "Connection is not acquired"
197+
return self._connection
196198

197199

198200
class AsyncpgTransaction(TransactionBackend):

databases/backends/compilers/__init__.py

Whitespace-only changes.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from sqlalchemy.dialects.postgresql.psycopg import PGCompiler_psycopg
2+
3+
4+
class APGCompiler_psycopg2(PGCompiler_psycopg):
5+
def construct_params(self, *args, **kwargs):
6+
pd = super().construct_params(*args, **kwargs)
7+
8+
for column in self.prefetch:
9+
pd[column.key] = self._exec_default(column.default)
10+
11+
return pd
12+
13+
def _exec_default(self, default):
14+
if default.is_callable:
15+
return default.arg(self.dialect)
16+
else:
17+
return default.arg

databases/backends/dialects/__init__.py

Whitespace-only changes.
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
All the unique changes for the databases package
3+
with the custom Numeric as the deprecated pypostgresql
4+
for backwards compatibility and to make sure the
5+
package can go to SQLAlchemy 2.0+.
6+
"""
7+
8+
import typing
9+
10+
from sqlalchemy.dialects.postgresql.base import PGDialect, PGExecutionContext
11+
from sqlalchemy.engine import processors
12+
from sqlalchemy.types import Numeric
13+
14+
15+
class PGExecutionContext_psycopg(PGExecutionContext):
16+
...
17+
18+
19+
class PGNumeric(Numeric):
20+
def bind_processor(
21+
self, dialect: typing.Any
22+
) -> typing.Union[str, None]: # pragma: no cover
23+
return processors.to_str
24+
25+
def result_processor(
26+
self, dialect: typing.Any, coltype: typing.Any
27+
) -> typing.Union[float, None]: # pragma: no cover
28+
if self.asdecimal:
29+
return None
30+
else:
31+
return processors.to_float
32+
33+
34+
class PGDialect_psycopg(PGDialect):
35+
colspecs = {
36+
**PGDialect.colspecs,
37+
Numeric: PGNumeric,
38+
}
39+
execution_ctx_cls = PGExecutionContext_psycopg
40+
41+
42+
dialect = PGDialect_psycopg

0 commit comments

Comments
 (0)