summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/session.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2007-08-02 05:42:49 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2007-08-02 05:42:49 +0000
commitb8b51fe4379936fe142c875ea0f17da14a12c27d (patch)
tree987fc4033cad747f0decfa80e38708a13d812d4c /lib/sqlalchemy/orm/session.py
parent9f23ec7423e98305f43a0b7a7ef894da74325329 (diff)
downloadsqlalchemy-b8b51fe4379936fe142c875ea0f17da14a12c27d.tar.gz
- sessionmaker module is out, replaced with simple function in session.py
- scoping/class instrumenting behavior of sessionmaker moved into new scoping module which implements scoped_session() (subject to potential name change) - SessionContext / assignmapper are deprecated, replaced with scoped_session()
Diffstat (limited to 'lib/sqlalchemy/orm/session.py')
-rw-r--r--lib/sqlalchemy/orm/session.py92
1 files changed, 15 insertions, 77 deletions
diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py
index 996c7d8a0..f982da536 100644
--- a/lib/sqlalchemy/orm/session.py
+++ b/lib/sqlalchemy/orm/session.py
@@ -14,93 +14,31 @@ from sqlalchemy.orm.mapper import global_extensions
__all__ = ['Session', 'SessionTransaction']
-def sessionmaker(autoflush, transactional, bind=None, scope=None, enhance_classes=False, **kwargs):
+def sessionmaker(autoflush=True, transactional=True, bind=None, **kwargs):
"""Generate a Session configuration."""
+
+ kwargs['bind'] = bind
+ kwargs['autoflush'] = autoflush
+ kwargs['transactional'] = transactional
- if enhance_classes and scope is None:
- raise exceptions.InvalidRequestError("enhance_classes requires a non-None 'scope' argument, so that mappers can automatically locate a Session already in progress.")
-
class Sess(Session):
def __init__(self, **local_kwargs):
- local_kwargs.setdefault('bind', bind)
- local_kwargs.setdefault('autoflush', autoflush)
- local_kwargs.setdefault('transactional', transactional)
for k in kwargs:
local_kwargs.setdefault(k, kwargs[k])
super(Sess, self).__init__(**local_kwargs)
- if scope=="thread":
- registry = util.ScopedRegistry(Sess, scopefunc=None)
-
- if enhance_classes:
- class SessionContextExt(MapperExtension):
- def get_session(self):
- return registry()
-
- def instrument_class(self, mapper, class_):
- class query(object):
- def __getattr__(self, key):
- return getattr(registry().query(class_), key)
- def __call__(self):
- return registry().query(class_)
-
- if not hasattr(class_, 'query'):
- class_.query = query()
-
- def init_instance(self, mapper, class_, oldinit, instance, args, kwargs):
- session = kwargs.pop('_sa_session', registry())
- if not isinstance(oldinit, types.MethodType):
- for key, value in kwargs.items():
- #if validate:
- # if not self.mapper.get_property(key, resolve_synonyms=False, raiseerr=False):
- # raise exceptions.ArgumentError("Invalid __init__ argument: '%s'" % key)
- setattr(instance, key, value)
- session._save_impl(instance, entity_name=kwargs.pop('_sa_entity_name', None))
- return EXT_CONTINUE
-
- def init_failed(self, mapper, class_, oldinit, instance, args, kwargs):
- object_session(instance).expunge(instance)
- return EXT_CONTINUE
-
- def dispose_class(self, mapper, class_):
- if hasattr(class_, '__init__') and hasattr(class_.__init__, '_oldinit'):
- if class_.__init__._oldinit is not None:
- class_.__init__ = class_.__init__._oldinit
- else:
- delattr(class_, '__init__')
- if hasattr(class_, 'query'):
- delattr(class_, 'query')
-
- global_extensions.append(SessionContextExt())
+ def configure(self, **new_kwargs):
+ """(re)configure the arguments for this sessionmaker.
- default_scope=scope
- class ScopedSess(Sess):
- def __new__(cls, **kwargs):
- if len(kwargs):
- scope = kwargs.pop('scope', default_scope)
- if scope is not None:
- if registry.has():
- raise exceptions.InvalidRequestError("Scoped session is already present; no new arguments may be specified.")
- else:
- sess = Sess(**kwargs)
- registry.set(sess)
- return sess
- else:
- return Sess(**kwargs)
- else:
- return registry()
- def instrument(name):
- def do(cls, *args, **kwargs):
- return getattr(registry(), name)(*args, **kwargs)
- return classmethod(do)
- for meth in ('get', 'close', 'save', 'commit', 'update', 'flush', 'query', 'delete'):
- setattr(ScopedSess, meth, instrument(meth))
+ e.g.
+ Session = sessionmaker()
+ Session.configure(bind=create_engine('sqlite://'))
+ """
- return ScopedSess
- elif scope is not None:
- raise exceptions.ArgumentError("Unknown scope '%s'" % scope)
- else:
- return session
+ kwargs.update(new_kwargs)
+ configure = classmethod(configure)
+
+ return Sess
class SessionTransaction(object):
"""Represents a Session-level Transaction.