diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-11-14 01:18:58 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-11-14 01:18:58 -0500 |
commit | 59f0685290b0847ecf83f5a9a64d99931d319bd6 (patch) | |
tree | 613ae288b5675ace2c3f7414647f537746161432 /lib/sqlalchemy/schema.py | |
parent | 504daf1bc09a9db475ed656c552d9bf7f993d20f (diff) | |
download | sqlalchemy-59f0685290b0847ecf83f5a9a64d99931d319bd6.tar.gz |
The :meth:`.Connection.connect` and :meth:`.Connection.contextual_connect`
methods now return a "branched" version so that the :meth:`.Connection.close`
method can be called on the returned connection without affecting the
original. Allows symmetry when using :class:`.Engine` and
:class:`.Connection` objects as context managers.
Diffstat (limited to 'lib/sqlalchemy/schema.py')
-rw-r--r-- | lib/sqlalchemy/schema.py | 28 |
1 files changed, 10 insertions, 18 deletions
diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index ac8be377c..9aa742177 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -27,6 +27,7 @@ Since these objects are part of the SQL expression language, they are usable as components in SQL expressions. """ +from __future__ import with_statement import re import inspect from . import exc, util, dialects, event, events, inspection @@ -2598,25 +2599,19 @@ class MetaData(SchemaItem): if bind is None: bind = _bind_or_error(self) - if bind.engine is not bind: - conn = bind - close = False - else: - conn = bind.contextual_connect() - close = True + with bind.connect() as conn: - reflect_opts = { - 'autoload': True, - 'autoload_with': bind - } + reflect_opts = { + 'autoload': True, + 'autoload_with': conn + } - if schema is None: - schema = self.schema + if schema is None: + schema = self.schema - if schema is not None: - reflect_opts['schema'] = schema + if schema is not None: + reflect_opts['schema'] = schema - try: available = util.OrderedSet(bind.engine.table_names(schema, connection=conn)) if views: @@ -2643,9 +2638,6 @@ class MetaData(SchemaItem): for name in load: Table(name, self, **reflect_opts) - finally: - if close: - conn.close() def append_ddl_listener(self, event_name, listener): """Append a DDL event listener to this ``MetaData``. |