summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/engine
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2020-05-21 18:18:39 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2020-05-22 10:30:38 -0400
commit5b2abf4d269b0c58bbec90ce0b16830e6e048a17 (patch)
treeef70ef91f0a891acd0f29397763008ff3d296b42 /lib/sqlalchemy/engine
parentd45657a2f5b880dc22dda2d1eb1687af5234a470 (diff)
downloadsqlalchemy-5b2abf4d269b0c58bbec90ce0b16830e6e048a17.tar.gz
Structural / performance refinements
* state connection schema_translate_map entirely in terms of execution options, support for per-execution options as well * use slots for role impls, remove superclass of the roles themselves as this is not needed * tighten loop in resolve, might become a C function Change-Id: Ib98ac9b65022fbf976e49c6060e4c37573528c5f
Diffstat (limited to 'lib/sqlalchemy/engine')
-rw-r--r--lib/sqlalchemy/engine/base.py30
-rw-r--r--lib/sqlalchemy/engine/default.py35
2 files changed, 26 insertions, 39 deletions
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py
index bbfafe8f1..c477c4292 100644
--- a/lib/sqlalchemy/engine/base.py
+++ b/lib/sqlalchemy/engine/base.py
@@ -52,7 +52,6 @@ class Connection(Connectable):
"""
- _schema_translate_map = None
_is_future = False
_sqla_logger_namespace = "sqlalchemy.engine.Connection"
@@ -82,7 +81,6 @@ class Connection(Connectable):
self.should_close_with_result = False
self.dispatch = _dispatch
self._has_events = _branch_from._has_events
- self._schema_translate_map = _branch_from._schema_translate_map
else:
self._dbapi_connection = (
connection
@@ -112,6 +110,10 @@ class Connection(Connectable):
if self._has_events or self.engine._has_events:
self.dispatch.engine_connect(self, _branch_from is not None)
+ @property
+ def _schema_translate_map(self):
+ return self._execution_options.get("schema_translate_map", None)
+
def schema_for_object(self, obj):
"""return the schema name for the given schema item taking into
account current schema translate map.
@@ -119,7 +121,9 @@ class Connection(Connectable):
"""
name = obj.schema
- schema_translate_map = self._schema_translate_map
+ schema_translate_map = self._execution_options.get(
+ "schema_translate_map", None
+ )
if (
schema_translate_map
@@ -1107,10 +1111,13 @@ class Connection(Connectable):
self, ddl, multiparams, params, execution_options
)
+ exec_opts = self._execution_options.merge_with(execution_options)
+ schema_translate_map = exec_opts.get("schema_translate_map", None)
+
dialect = self.dialect
compiled = ddl.compile(
- dialect=dialect, schema_translate_map=self._schema_translate_map
+ dialect=dialect, schema_translate_map=schema_translate_map
)
ret = self._execute_context(
dialect,
@@ -1147,9 +1154,9 @@ class Connection(Connectable):
dialect = self.dialect
- exec_opts = self._execution_options
- if execution_options:
- exec_opts = exec_opts.union(execution_options)
+ exec_opts = self._execution_options.merge_with(execution_options)
+
+ schema_translate_map = exec_opts.get("schema_translate_map", None)
if "compiled_cache" in exec_opts:
elem_cache_key = elem._generate_cache_key()
@@ -1162,7 +1169,7 @@ class Connection(Connectable):
dialect,
cache_key,
tuple(sorted(keys)),
- bool(self._schema_translate_map),
+ bool(schema_translate_map),
len(distilled_params) > 1,
)
cache = exec_opts["compiled_cache"]
@@ -1174,7 +1181,7 @@ class Connection(Connectable):
cache_key=elem_cache_key,
column_keys=keys,
inline=len(distilled_params) > 1,
- schema_translate_map=self._schema_translate_map,
+ schema_translate_map=schema_translate_map,
linting=self.dialect.compiler_linting
| compiler.WARN_LINTING,
)
@@ -1186,7 +1193,7 @@ class Connection(Connectable):
dialect=dialect,
column_keys=keys,
inline=len(distilled_params) > 1,
- schema_translate_map=self._schema_translate_map,
+ schema_translate_map=schema_translate_map,
linting=self.dialect.compiler_linting | compiler.WARN_LINTING,
)
@@ -1364,9 +1371,6 @@ class Connection(Connectable):
# the only feature that branching provides
self = self.__branch_from
- if execution_options:
- dialect.set_exec_execution_options(self, execution_options)
-
try:
conn = self._dbapi_connection
if conn is None:
diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py
index 094ab3d55..52651aa2d 100644
--- a/lib/sqlalchemy/engine/default.py
+++ b/lib/sqlalchemy/engine/default.py
@@ -499,34 +499,10 @@ class DefaultDialect(interfaces.Dialect):
if not branch:
self._set_connection_isolation(connection, isolation_level)
- if "schema_translate_map" in opts:
- engine._schema_translate_map = map_ = opts["schema_translate_map"]
-
- @event.listens_for(engine, "engine_connect")
- def set_schema_translate_map(connection, branch):
- connection._schema_translate_map = map_
-
def set_connection_execution_options(self, connection, opts):
if "isolation_level" in opts:
self._set_connection_isolation(connection, opts["isolation_level"])
- if "schema_translate_map" in opts:
- connection._schema_translate_map = opts["schema_translate_map"]
-
- def set_exec_execution_options(self, connection, opts):
- if "isolation_level" in opts:
- raise exc.InvalidRequestError(
- "The 'isolation_level' execution "
- "option is not supported at the per-statement level"
- )
- self._set_connection_isolation(connection, opts["isolation_level"])
-
- if "schema_translate_map" in opts:
- raise exc.InvalidRequestError(
- "The 'schema_translate_map' execution "
- "option is not supported at the per-statement level"
- )
-
def _set_connection_isolation(self, connection, level):
if connection.in_transaction():
if connection._is_future:
@@ -765,9 +741,13 @@ class DefaultExecutionContext(interfaces.ExecutionContext):
self.unicode_statement = util.text_type(compiled)
if compiled.schema_translate_map:
+ schema_translate_map = self.execution_options.get(
+ "schema_translate_map", {}
+ )
+
rst = compiled.preparer._render_schema_translates
self.unicode_statement = rst(
- self.unicode_statement, connection._schema_translate_map
+ self.unicode_statement, schema_translate_map
)
if not dialect.supports_unicode_statements:
@@ -894,9 +874,12 @@ class DefaultExecutionContext(interfaces.ExecutionContext):
positiontup = self.compiled.positiontup
if compiled.schema_translate_map:
+ schema_translate_map = self.execution_options.get(
+ "schema_translate_map", {}
+ )
rst = compiled.preparer._render_schema_translates
self.unicode_statement = rst(
- self.unicode_statement, connection._schema_translate_map
+ self.unicode_statement, schema_translate_map
)
# final self.unicode_statement is now assigned, encode if needed