diff options
| author | Jason Kirtland <jek@discorporate.us> | 2008-03-04 22:47:35 +0000 |
|---|---|---|
| committer | Jason Kirtland <jek@discorporate.us> | 2008-03-04 22:47:35 +0000 |
| commit | 38606681e7d56b8dd1d2091c6b2d2cf387e04830 (patch) | |
| tree | 0b114021d6050e16895f1a0e0d9e1b05fe41e2da /lib/sqlalchemy/schema.py | |
| parent | 5413a207f0549677be3925b54884b0e065d79f22 (diff) | |
| download | sqlalchemy-38606681e7d56b8dd1d2091c6b2d2cf387e04830.tar.gz | |
- Gave DDL() statements the same .bind treatment as the DML ones in r4220
Diffstat (limited to 'lib/sqlalchemy/schema.py')
| -rw-r--r-- | lib/sqlalchemy/schema.py | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 0ca5c68a9..6d43afe45 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -1540,7 +1540,7 @@ class DDL(object): connection.execute(drop_spow) """ - def __init__(self, statement, on=None, context=None): + def __init__(self, statement, on=None, context=None, bind=None): """Create a DDL statement. statement @@ -1593,16 +1593,19 @@ class DDL(object): self.statement = statement self.on = on self.context = context or {} + self._bind = bind - def execute(self, bind, schema_item=None): + def execute(self, bind=None, schema_item=None): """Execute this DDL immediately. Executes the DDL statement in isolation using the supplied - ``Connectable``. If the DDL has a conditional ``on`` criteria, it + ``Connectable`` or ``Connectable`` assigned to the ``.bind`` property, + if not supplied. If the DDL has a conditional ``on`` criteria, it will be invoked with None as the event. bind - An Engine or Connection + Optional, an ``Engine`` or ``Connection``. If not supplied, a + valid ``Connectable`` must be present in the ``.bind`` property. schema_item Optional, defaults to None. Will be passed to the ``on`` callable @@ -1610,7 +1613,9 @@ class DDL(object): statement. See ``execute_at`` for more information. """ - # no bind params are supported + if bind is None: + bind = _bind_or_error(self) + # no SQL bind params are supported if self._should_execute(None, schema_item, bind): executable = expression.text(self._expand(schema_item, bind)) return bind.execute(executable) @@ -1663,6 +1668,29 @@ class DDL(object): schema_item.ddl_listeners[event].append(self) return self + def bind(self): + """An Engine or Connection to which this DDL is bound. + + This property may be assigned an ``Engine`` or ``Connection``, or + assigned a string or URL to automatically create a basic ``Engine`` + for this bind with ``create_engine()``. + """ + return self._bind + + def _bind_to(self, bind): + """Bind this MetaData to an Engine, Connection, string or URL.""" + + global URL + if URL is None: + from sqlalchemy.engine.url import URL + + if isinstance(bind, (basestring, URL)): + from sqlalchemy import create_engine + self._bind = create_engine(bind) + else: + self._bind = bind + bind = property(bind, _bind_to) + def __call__(self, event, schema_item, bind): """Execute the DDL as a ddl_listener.""" |
