diff options
Diffstat (limited to 'lib/sqlalchemy/ext/activemapper.py')
-rw-r--r-- | lib/sqlalchemy/ext/activemapper.py | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/lib/sqlalchemy/ext/activemapper.py b/lib/sqlalchemy/ext/activemapper.py index 8b3387eae..e5b3e4929 100644 --- a/lib/sqlalchemy/ext/activemapper.py +++ b/lib/sqlalchemy/ext/activemapper.py @@ -1,10 +1,10 @@ from sqlalchemy import objectstore, create_engine, assign_mapper, relation, mapper from sqlalchemy import and_, or_ -from sqlalchemy import Table, Column +from sqlalchemy import Table, Column, ForeignKey from sqlalchemy.ext.proxy import ProxyEngine import inspect - +import sys # # the "proxy" to the database engine... this can be swapped out at runtime @@ -17,13 +17,18 @@ engine = ProxyEngine() # declarative column declaration - this is so that we can infer the colname # class column(object): - def __init__(self, coltype, colname=None, foreign_key=None, primary_key=False): + def __init__(self, coltype, colname=None, foreign_key=None, + primary_key=False, *args, **kwargs): + if isinstance( foreign_key, basestring ): + foreign_key= ForeignKey( foreign_key ) self.coltype = coltype self.colname = colname self.foreign_key = foreign_key self.primary_key = primary_key - - + self.unique = kwargs.pop( 'unique', False ) + self.indexed = kwargs.pop( 'indexed', self.unique ) + self.kwargs = kwargs + self.args = args # # declarative relationship declaration @@ -89,6 +94,7 @@ class ActiveMapperMeta(type): table_name = clsname.lower() columns = [] relations = {} + _engine = getattr( sys.modules[cls.__module__], "__engine__", engine ) if 'mapping' in dict: members = inspect.getmembers(dict.get('mapping')) @@ -97,6 +103,10 @@ class ActiveMapperMeta(type): table_name = value continue + if '__engine__' == name: + _engine= value + continue + if name.startswith('__'): continue if isinstance(value, column): @@ -104,18 +114,24 @@ class ActiveMapperMeta(type): col = Column(value.colname or name, value.coltype, value.foreign_key, - primary_key=value.primary_key) + primary_key=value.primary_key, + *value.args, **value.kwargs) else: col = Column(value.colname or name, value.coltype, - primary_key=value.primary_key) + primary_key=value.primary_key, + *value.args, **value.kwargs) columns.append(col) + if value.indexed: + # create a Index object for the column + index= Index( "%s_idx" % (value.colname or name), + col, unique= value.unique ) continue if isinstance(value, relationship): relations[name] = value - - cls.table = Table(table_name, engine, *columns) + assert _engine is not None, "No engine specified" + cls.table = Table(table_name, _engine, *columns) assign_mapper(cls, cls.table) cls.relations = relations ActiveMapperMeta.classes[clsname] = cls |