Show
Ignore:
Timestamp:
08/31/07 13:19:19 (6 years ago)
Author:
ged
Message:
  • updated TODO file
  • (partially) changelogged what was done recently. Did I forget anything?
  • fixed single-table and multi-table inheritance which have relationships
    Concrete is still broken in this regard. I had to remove the "select_table"
    optimization of the multi-table polymorphic for that to work.
  • made each initialization step as a method on EntityDescriptor
  • reordered EntityDescriptor methods so that they are in the order they are
    called
  • added setup_entities method to be able to setup only some entities and not
    all of them together. This is not usable yet, as all entities are still
    appended to the _delayed_entities list whether you want it or not.
  • some misc cleanup
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/elixir/__init__.py

    r185 r196  
    4343           'using_options', 'using_table_options', 'using_mapper_options', 
    4444           'options_defaults', 'metadata', 'objectstore', 
    45            'create_all', 'drop_all', 'setup_all', 'cleanup_all'] + \ 
    46           sqlalchemy.types.__all__ 
     45           'create_all', 'drop_all', 
     46           'setup_all', 'cleanup_all'] + sqlalchemy.types.__all__ 
    4747 
    48 __pudge_all__ = ['create_all', 'drop_all', 'setup_all', 'cleanup_all', 
     48__pudge_all__ = ['create_all', 'drop_all', 
     49                 'setup_all', 'cleanup_all', 
    4950                 'metadata', 'objectstore'] 
    5051 
     
    7273        md.drop_all(bind=engine) 
    7374 
    74 _delayed_descriptors = list() 
     75_delayed_entities = list() 
     76 
     77def setup_entities(entities): 
     78    '''Setup all entities passed in entities''' 
     79 
     80    for entity in entities: 
     81        desc = entity._descriptor 
     82        entity._ready = False 
     83        del sqlalchemy.orm.mapper_registry[entity._class_key] 
     84 
     85        md = desc.metadata 
     86 
     87        # the fake table could have already been removed (namely in a  
     88        # single table inheritance scenario) 
     89        md.tables.pop(entity._table_key, None) 
     90 
     91        # restore original table iterator if not done already 
     92        if hasattr(md.table_iterator, '_non_elixir_patched_iterator'): 
     93            md.table_iterator = \ 
     94                md.table_iterator._non_elixir_patched_iterator 
     95 
     96    for method_name in ( 
     97            'setup_autoload_table', 'create_pk_cols', 'setup_relkeys', 
     98            'before_table', 'setup_table', 'setup_reltables', 'after_table', 
     99            'setup_events', 
     100            'before_mapper', 'setup_mapper', 'after_mapper', 
     101            'setup_properties', 
     102            'finalize'): 
     103        for entity in entities: 
     104            method = getattr(entity._descriptor, method_name) 
     105            method() 
    75106 
    76107def setup_all(create_tables=False): 
    77108    '''Setup the table and mapper for all entities which have been delayed. 
    78109 
    79     This is called automatically when your entity is first accessed, or ... 
    80     [TODO: complete this] 
     110    This is called automatically when any of your entities is first accessed, 
     111    instanciated (called) or the create_all method of a metadata containing 
     112    entities is called. 
    81113    ''' 
    82  
    83     if not _delayed_descriptors: 
     114    if not _delayed_entities: 
    84115        return 
    85116 
    86 #TODO: define all those operations as methods on the descriptor 
    87 #    for method_name in ('setup_table', 'setup_mapper', 'setup_relkeys', ...): 
    88 #        for desc in _delayed_descriptors: 
    89 #            method = getattr(desc, method_name) 
    90 #            method() 
    91117    try: 
    92         for desc in _delayed_descriptors: 
    93             entity = desc.entity 
    94             entity._ready = False 
    95             del sqlalchemy.orm.mapper_registry[entity._class_key] 
    96  
    97             md = desc.metadata 
    98             # the table could have already been removed (namely in a single  
    99             # table inheritance scenario) 
    100             md.tables.pop(entity._table_key, None) 
    101  
    102             # restore original table iterator if not done already 
    103             if hasattr(md.table_iterator, '_non_elixir_patched_iterator'): 
    104                 md.table_iterator = \ 
    105                     md.table_iterator._non_elixir_patched_iterator 
    106  
    107         # Make sure autoloaded tables are available so that we can setup  
    108         # foreign keys to their columns 
    109         for desc in _delayed_descriptors: 
    110             if desc.autoload: 
    111                 desc.setup_table() 
    112  
    113         for desc in _delayed_descriptors: 
    114             desc.create_pk_cols() 
    115  
    116         # Create other columns from belongs_to relationships. 
    117         for desc in _delayed_descriptors: 
    118             for rel in desc.relationships.itervalues(): 
    119                 rel.create_keys(False) 
    120  
    121         for desc in _delayed_descriptors: 
    122             if not desc.autoload: 
    123                 desc.setup_table() 
    124  
    125         for desc in _delayed_descriptors: 
    126             Statement.process(desc.entity, 'before_table') 
    127  
    128         for desc in _delayed_descriptors: 
    129             for rel in desc.relationships.itervalues(): 
    130                 rel.create_tables() 
    131  
    132         for desc in _delayed_descriptors: 
    133             Statement.process(desc.entity, 'after_table') 
    134  
    135         for desc in _delayed_descriptors: 
    136             desc.setup_events() 
    137          
    138         for desc in _delayed_descriptors: 
    139             Statement.process(desc.entity, 'before_mapper') 
    140  
    141         for desc in _delayed_descriptors: 
    142             desc.setup_mapper() 
    143  
    144         for desc in _delayed_descriptors: 
    145             Statement.process(desc.entity, 'after_mapper') 
    146  
    147         for desc in _delayed_descriptors: 
    148             for rel in desc.relationships.itervalues(): 
    149                 rel.create_properties() 
    150  
    151         for desc in _delayed_descriptors: 
    152             Statement.process(desc.entity, 'finalize') 
    153  
     118        setup_entities(_delayed_entities) 
    154119    finally: 
    155120        # make sure that even if we fail to initialize, we don't leave junk for 
    156121        # others 
    157         del _delayed_descriptors[:] 
     122        del _delayed_entities[:] 
    158123 
    159124    # issue the "CREATE" SQL statements 
     
    163128 
    164129def cleanup_all(drop_tables=False): 
    165     '''Drop table and clear mapper for all entities, and clear all metadatas. 
     130    '''Clear all mappers, clear the session, and clear all metadatas.  
     131    Optionally drops the tables. 
    166132    ''' 
    167133    if drop_tables: