ficapy
1/3/2012 - 8:21 PM

Reflecting in Pyramid/SqlAlchemy

Reflecting in Pyramid/SqlAlchemy

import logging
log = logging.getLogger(__name__)


from sqlalchemy import Table 
from sqlalchemy import MetaData 
from sqlalchemy.orm import mapper 
from sqlalchemy.orm import scoped_session 
from sqlalchemy.orm import sessionmaker 
from zope.sqlalchemy import ZopeTransactionExtension 

import app
import types


DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension())) 
DBMetadata = MetaData() 


class ReflectedTable(object): 
    """Base class for database objects that are mapped to tables by reflection.
       Have your various model classes inherit from this class.  If class.__tablename__ is defined, it will reflect

       Example:
		  class Useraccount(ReflectedTable):
			  __tablename__ = "useraccount"
    """ 
    __tablename__ = None


def map_tables( app_model ):
    """
    """
    to_reflect = []
    for content in dir( app_model ):
        module = getattr( app_model , content )
        if not isinstance( module , types.ModuleType ):
            continue
        for module_element in dir( module ):
            module_element = getattr( module, module_element )
            if not isinstance( module_element , types.TypeType ):
                continue
            if issubclass( module_element , ReflectedTable ):
                to_reflect.append( module_element )
    for _class in to_reflect: 
        table_name = _class.__tablename__
        if table_name:
            log.info("Reflecting : %s (table: %s)" % (_class , table_name) )
            table= Table( table_name, DBMetadata, autoload=True ) 
            mapper( _class , table) 


def initialize_sql(engine):
    """Call this once per engine from app.__init__.main 

		engine = sqlalchemy.engine_from_config(settings, prefix="sqlalchemy.")
		sqlahelper.add_engine(engine)
		models.initialize_sql(engine)
    
    """
    log.debug( "\ninitialize_sql" )
    DBSession.configure(bind=engine) 
    DBMetadata.bind = engine 
    map_tables( app.models )


## import various classes here.  
import models_1
import models_2
import models_3