Esempio n. 1
0
def init(connection_uri, echo=False, scopefunc=None):
    """Initialize the session.

    Args:
        connection_uri (str): The connection uri, such as
            "postgrseql://*****:*****@localhost/dbname"
        echo (bool): Echo SQL statements to stdout.  Useful for debugging.
        scopefunc (func): Optional function which defines
            the current scope.  See
            http://docs.sqlalchemy.org/en/latest/orm/contextual.html#sqlalchemy.orm.scoping.scoped_session.__init__
            for more information.  If this is not passed, the "thread-local"
            scope will be assumed.
    """
    global engine, session

    from frf.models import Model

    if 'postgres' in connection_uri:
        engine = create_engine(connection_uri,
                               echo=echo,
                               json_serializer=serialize,
                               json_deserializer=deserialize)
    else:
        engine = create_engine(connection_uri, echo=echo)

    if scopefunc is not None:
        session.registry = ScopedRegistry(session.session_factory,
                                          scopefunc=scopefunc)
    else:
        session.registry = ThreadLocalRegistry(session.session_factory)

    session.configure(bind=engine)
    Model.query = _QueryProperty(session)
Esempio n. 2
0
 def __init__(self, session_factory, scopefunc=None):
     self.session_factory = session_factory
     if scopefunc:
         self.registry = ScopedRegistry(session_factory, scopefunc)
     else:
         self.registry = ThreadLocalRegistry(session_factory)
     self.extension = _ScopedExt(self)
Esempio n. 3
0
    def __init__(self, session_factory, scopefunc=None):
        """Construct a new :class:`.scoped_session`.

        :param session_factory: a factory to create new :class:`.Session`
         instances. This is usually, but not necessarily, an instance
         of :class:`.sessionmaker`.
        :param scopefunc: optional function which defines
         the current scope.   If not passed, the :class:`.scoped_session`
         object assumes "thread-local" scope, and will use
         a Python ``threading.local()`` in order to maintain the current
         :class:`.Session`.  If passed, the function should return
         a hashable token; this token will be used as the key in a
         dictionary in order to store and retrieve the current
         :class:`.Session`.

        """
        self.session_factory = session_factory

        if scopefunc:
            self.registry = ScopedRegistry(session_factory, scopefunc)
        else:
            self.registry = ThreadLocalRegistry(session_factory)
Esempio n. 4
0
 def __init__(self, session_factory):
     self._session_factory = session_factory
     self._registry = ThreadLocalRegistry(session_factory)