Here's the code I've come up with:
class GeometryDDL(object):
try:
from sqlalchemy import event
except ImportError:
# SQLAlchemy 0.6
use_event = False
columns_attribute = '_columns'
else:
# SQLALchemy 0.7
use_event = True
columns_attribute = 'columns'
def __init__(self, table):
if self.use_event:
event.listen(table, 'before_create', self.before_create)
event.listen(table, 'before_drop', self.before_drop)
event.listen(table, 'after_create', self.after_create)
event.listen(table, 'after_drop', self.after_drop)
else:
for e in ('before-create', 'after-create',
'before-drop', 'after-drop'):
table.ddl_listeners[e].append(self)
self._stack = []
def __call__(self, event, table, bind):
spatial_dialect = DialectManager.get_spatial_dialect(bind.dialect)
if event in ('before-create', 'before-drop'):
"""Remove geometry column from column list
(table._columns), so that it
does not show up in the create statement ("create table tab (..)").
Afterwards (on event 'after-create') restore the column
list from self._stack.
"""
regular_cols = [c for c in table.c if not
isinstance(c.type, Geometry)]
gis_cols = set(table.c).difference(regular_cols)
self._stack.append(table.c)
setattr(table, self.columns_attribute,
expression.ColumnCollection(*regular_cols))
if event == 'before-drop':
for c in gis_cols:
spatial_dialect.handle_ddl_before_drop(bind, table, c)
elif event == 'after-create':
setattr(table, self.columns_attribute, self._stack.pop())
for c in table.c:
if isinstance(c.type, Geometry):
spatial_dialect.handle_ddl_after_create(bind, table, c)
elif event == 'after-drop':
setattr(table, self.columns_attribute, self._stack.pop())
def before_create(self, target, connection, **kw):
self('before-create', target, connection)
def before_drop(self, target, connection, **kw):
self('before-drop', target, connection)
def after_create(self, target, connection, **kw):
self('after-create', target, connection)
def after_drop(self, target, connection, **kw):
self('after-drop', target, connection)
Thanks,
--
Eric Lemoine
Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex
Tel : 00 33 4 79 44 44 96
Mail : [email protected]
http://www.camptocamp.com
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.