diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-11-26 10:17:38 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-11-29 13:46:23 -0500 |
commit | db85d28a857945ce021e27a187a14999eeb5c89e (patch) | |
tree | 3e402d745aa1d01a51198154ddda25fe96296056 /lib/sqlalchemy/dialects/postgresql/psycopg2.py | |
parent | 5eb407f84bdabdbcd68975dbf76dc4c0809d7373 (diff) | |
download | sqlalchemy-db85d28a857945ce021e27a187a14999eeb5c89e.tar.gz |
provide connectionfairy on initialize
This is so that dialect methods that are called within init
can assume the same argument structure as when they are called
in other places; we can nail down the type of object as well.
This change seems to mostly impact the isolation level routines
in the dialects, as these are called during initialize()
as well as on established connections. these methods can now
assume a non-proxied DBAPI connection object in all cases,
as it is commonly required that attributes like ".autocommit"
are set on the object which don't work well in a proxied
situation.
Other changes:
* adds an interface for the "connectionfairy" concept
called PoolProxiedConnection.
* Removes ``Connectable`` superclass of Connection.
``Connectable`` was originally meant to provide for the
"method which accepts connection or engine" theme. As this
pattern is greatly reduced in 2.0 and Engine no longer extends
from it, the ``Connectable`` superclass doesnt serve any real
purpose.
Leading from that, to set this in I also applied pep 484 annotations
to the Dialect base, and then in the interests of seeing some
of the typing information show up in my IDE did a little bit for Engine,
Connection and others. I hope that it's feasible that we can
add annotations to specific classes and attributes ahead of when we
actually try to mass-populate the whole library. This was
the original spirit of pep-484 that we can apply annotations
gradually. I do of course want to try to do a mass-populate
although i think even in that case we will end up doing a lot
of manual work anyway (in particular for the changes here which
are distinct from what the stubs have).
Fixes: #7122
Change-Id: I5dd7fbff8a7ae520a81c165091af12a6a68826db
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/psycopg2.py')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/psycopg2.py | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py index 3d9f90a29..0bbad3257 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py @@ -597,7 +597,8 @@ class PGDialect_psycopg2(_PGDialect_common_psycopg): super(PGDialect_psycopg2, self).initialize(connection) self._has_native_hstore = ( self.use_native_hstore - and self._hstore_oids(connection.connection) is not None + and self._hstore_oids(connection.connection.dbapi_connection) + is not None ) # PGDialect.initialize() checks server version for <= 8.2 and sets @@ -639,8 +640,8 @@ class PGDialect_psycopg2(_PGDialect_common_psycopg): "SERIALIZABLE": extensions.ISOLATION_LEVEL_SERIALIZABLE, } - def set_isolation_level(self, connection, level): - connection.set_isolation_level(self._isolation_lookup[level]) + def set_isolation_level(self, dbapi_connection, level): + dbapi_connection.set_isolation_level(self._isolation_lookup[level]) def set_readonly(self, connection, value): connection.readonly = value @@ -660,47 +661,47 @@ class PGDialect_psycopg2(_PGDialect_common_psycopg): fns = [] if self.client_encoding is not None: - def on_connect(conn): - conn.set_client_encoding(self.client_encoding) + def on_connect(dbapi_conn): + dbapi_conn.set_client_encoding(self.client_encoding) fns.append(on_connect) if self.dbapi and self.use_native_uuid: - def on_connect(conn): - extras.register_uuid(None, conn) + def on_connect(dbapi_conn): + extras.register_uuid(None, dbapi_conn) fns.append(on_connect) if self.dbapi and self.use_native_hstore: - def on_connect(conn): - hstore_oids = self._hstore_oids(conn) + def on_connect(dbapi_conn): + hstore_oids = self._hstore_oids(dbapi_conn) if hstore_oids is not None: oid, array_oid = hstore_oids kw = {"oid": oid} kw["array_oid"] = array_oid - extras.register_hstore(conn, **kw) + extras.register_hstore(dbapi_conn, **kw) fns.append(on_connect) if self.dbapi and self._json_deserializer: - def on_connect(conn): + def on_connect(dbapi_conn): extras.register_default_json( - conn, loads=self._json_deserializer + dbapi_conn, loads=self._json_deserializer ) extras.register_default_jsonb( - conn, loads=self._json_deserializer + dbapi_conn, loads=self._json_deserializer ) fns.append(on_connect) if fns: - def on_connect(conn): + def on_connect(dbapi_conn): for fn in fns: - fn(conn) + fn(dbapi_conn) return on_connect else: @@ -781,11 +782,10 @@ class PGDialect_psycopg2(_PGDialect_common_psycopg): ) @util.memoized_instancemethod - def _hstore_oids(self, conn): + def _hstore_oids(self, dbapi_connection): + extras = self._psycopg2_extras - if hasattr(conn, "dbapi_connection"): - conn = conn.dbapi_connection - oids = extras.HstoreAdapter.get_oids(conn) + oids = extras.HstoreAdapter.get_oids(dbapi_connection) if oids is not None and oids[0]: return oids[0:2] else: |