iro/model/utils.py
branchdevel
changeset 122 b55754aa4f96
parent 117 351a02310dd8
child 259 5d9c24c2cb8d
child 260 4a03119a98c1
equal deleted inserted replaced
121:ca63b6776231 122:b55754aa4f96
     1 from sqlalchemy.orm import sessionmaker
     1 from sqlalchemy.orm import sessionmaker
       
     2 
       
     3 from .session import IroSession
     2 
     4 
     3 class WithSession(object):
     5 class WithSession(object):
     4     '''a with statement for a database session connection'''
     6     '''a with statement for a database session connection'''
     5     def __init__(self, engine, autocommit=False):
     7     def __init__(self, engine, autocommit=False):
     6         self.engine = engine
     8         self.engine = engine
     7         self.autocommit=autocommit
     9         self.autocommit=autocommit
     8     
    10     
     9     def __enter__(self):
    11     def __enter__(self):
    10         self.session = sessionmaker(bind=self.engine)()
    12         self.session = sessionmaker(bind=self.engine, class_ = IroSession)()
    11         return self.session
    13         return self.session
    12     
    14     
    13     def __exit__(self,exc_type, exc_value, traceback):
    15     def __exit__(self,exc_type, exc_value, traceback):
    14         if exc_type is None:
    16         if exc_type is None:
    15             if self.autocommit:
    17             if self.autocommit: