dibbler/sqlalchemy/orm/scoping.py

185 lines
6.3 KiB
Python
Raw Normal View History

2017-04-15 18:27:12 +02:00
# orm/scoping.py
# Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
# <see AUTHORS file>
2010-05-07 19:33:49 +02:00
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
2017-04-15 18:27:12 +02:00
from .. import exc as sa_exc
from ..util import ScopedRegistry, ThreadLocalRegistry, warn
from . import class_mapper, exc as orm_exc
from .session import Session
2010-05-07 19:33:49 +02:00
2017-04-15 18:27:12 +02:00
__all__ = ['scoped_session']
2010-05-07 19:33:49 +02:00
2017-04-15 18:27:12 +02:00
class scoped_session(object):
"""Provides scoped management of :class:`.Session` objects.
2010-05-07 19:33:49 +02:00
2017-04-15 18:27:12 +02:00
See :ref:`unitofwork_contextual` for a tutorial.
2010-05-07 19:33:49 +02:00
"""
2017-04-15 18:27:12 +02:00
session_factory = None
"""The `session_factory` provided to `__init__` is stored in this
attribute and may be accessed at a later time. This can be useful when
a new non-scoped :class:`.Session` or :class:`.Connection` to the
database is needed."""
2010-05-07 19:33:49 +02:00
def __init__(self, session_factory, scopefunc=None):
2017-04-15 18:27:12 +02:00
"""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`.
"""
2010-05-07 19:33:49 +02:00
self.session_factory = session_factory
2017-04-15 18:27:12 +02:00
2010-05-07 19:33:49 +02:00
if scopefunc:
self.registry = ScopedRegistry(session_factory, scopefunc)
else:
self.registry = ThreadLocalRegistry(session_factory)
2017-04-15 18:27:12 +02:00
def __call__(self, **kw):
r"""Return the current :class:`.Session`, creating it
using the :attr:`.scoped_session.session_factory` if not present.
:param \**kw: Keyword arguments will be passed to the
:attr:`.scoped_session.session_factory` callable, if an existing
:class:`.Session` is not present. If the :class:`.Session` is present
and keyword arguments have been passed,
:exc:`~sqlalchemy.exc.InvalidRequestError` is raised.
"""
if kw:
scope = kw.pop('scope', False)
2010-05-07 19:33:49 +02:00
if scope is not None:
if self.registry.has():
2017-04-15 18:27:12 +02:00
raise sa_exc.InvalidRequestError(
"Scoped session is already present; "
"no new arguments may be specified.")
2010-05-07 19:33:49 +02:00
else:
2017-04-15 18:27:12 +02:00
sess = self.session_factory(**kw)
2010-05-07 19:33:49 +02:00
self.registry.set(sess)
return sess
else:
2017-04-15 18:27:12 +02:00
return self.session_factory(**kw)
2010-05-07 19:33:49 +02:00
else:
return self.registry()
def remove(self):
2017-04-15 18:27:12 +02:00
"""Dispose of the current :class:`.Session`, if present.
This will first call :meth:`.Session.close` method
on the current :class:`.Session`, which releases any existing
transactional/connection resources still being held; transactions
specifically are rolled back. The :class:`.Session` is then
discarded. Upon next usage within the same scope,
the :class:`.scoped_session` will produce a new
:class:`.Session` object.
"""
2010-05-07 19:33:49 +02:00
if self.registry.has():
self.registry().close()
self.registry.clear()
2017-04-15 18:27:12 +02:00
def configure(self, **kwargs):
"""reconfigure the :class:`.sessionmaker` used by this
:class:`.scoped_session`.
2010-05-07 19:33:49 +02:00
2017-04-15 18:27:12 +02:00
See :meth:`.sessionmaker.configure`.
2010-05-07 19:33:49 +02:00
"""
2017-04-15 18:27:12 +02:00
if self.registry.has():
warn('At least one scoped session is already present. '
' configure() can not affect sessions that have '
'already been created.')
2010-05-07 19:33:49 +02:00
self.session_factory.configure(**kwargs)
def query_property(self, query_cls=None):
2017-04-15 18:27:12 +02:00
"""return a class property which produces a :class:`.Query` object
against the class and the current :class:`.Session` when called.
2010-05-07 19:33:49 +02:00
e.g.::
2017-04-15 18:27:12 +02:00
2010-05-07 19:33:49 +02:00
Session = scoped_session(sessionmaker())
class MyClass(object):
query = Session.query_property()
# after mappers are defined
result = MyClass.query.filter(MyClass.name=='foo').all()
Produces instances of the session's configured query class by
default. To override and use a custom implementation, provide
a ``query_cls`` callable. The callable will be invoked with
the class's mapper as a positional argument and a session
keyword argument.
There is no limit to the number of query properties placed on
a class.
"""
class query(object):
def __get__(s, instance, owner):
try:
mapper = class_mapper(owner)
if mapper:
if query_cls:
# custom query class
return query_cls(mapper, session=self.registry())
else:
# session's configured query class
return self.registry().query(mapper)
except orm_exc.UnmappedClassError:
return None
return query()
2017-04-15 18:27:12 +02:00
ScopedSession = scoped_session
"""Old name for backwards compatibility."""
2010-05-07 19:33:49 +02:00
def instrument(name):
def do(self, *args, **kwargs):
return getattr(self.registry(), name)(*args, **kwargs)
return do
2017-04-15 18:27:12 +02:00
2010-05-07 19:33:49 +02:00
for meth in Session.public_methods:
2017-04-15 18:27:12 +02:00
setattr(scoped_session, meth, instrument(meth))
2010-05-07 19:33:49 +02:00
def makeprop(name):
def set(self, attr):
setattr(self.registry(), name, attr)
2017-04-15 18:27:12 +02:00
2010-05-07 19:33:49 +02:00
def get(self):
return getattr(self.registry(), name)
2017-04-15 18:27:12 +02:00
2010-05-07 19:33:49 +02:00
return property(get, set)
2017-04-15 18:27:12 +02:00
for prop in ('bind', 'dirty', 'deleted', 'new', 'identity_map',
'is_active', 'autoflush', 'no_autoflush', 'info'):
setattr(scoped_session, prop, makeprop(prop))
2010-05-07 19:33:49 +02:00
def clslevel(name):
def do(cls, *args, **kwargs):
return getattr(Session, name)(*args, **kwargs)
return classmethod(do)
2017-04-15 18:27:12 +02:00
for prop in ('close_all', 'object_session', 'identity_key'):
setattr(scoped_session, prop, clslevel(prop))