summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/deprecated_interfaces.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-11-14 15:54:37 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2010-11-14 15:54:37 -0500
commit80d1aaa66113ba3770cb9b2ec2c97fed28fa465c (patch)
tree6cb6f5c463974cb5b1c960af0897f89a4f637d63 /lib/sqlalchemy/orm/deprecated_interfaces.py
parent7513b46730d1b57a6b8addde8dfb4f83ee1e6cb4 (diff)
downloadsqlalchemy-80d1aaa66113ba3770cb9b2ec2c97fed28fa465c.tar.gz
- event documentation bonanza
Diffstat (limited to 'lib/sqlalchemy/orm/deprecated_interfaces.py')
-rw-r--r--lib/sqlalchemy/orm/deprecated_interfaces.py95
1 files changed, 81 insertions, 14 deletions
diff --git a/lib/sqlalchemy/orm/deprecated_interfaces.py b/lib/sqlalchemy/orm/deprecated_interfaces.py
index 76e023701..8aa57c687 100644
--- a/lib/sqlalchemy/orm/deprecated_interfaces.py
+++ b/lib/sqlalchemy/orm/deprecated_interfaces.py
@@ -3,15 +3,31 @@ from interfaces import EXT_CONTINUE
class MapperExtension(object):
- """Base implementation for customizing ``Mapper`` behavior.
+ """Base implementation for :class:`.Mapper` event hooks.
+
+ .. note:: :class:`.MapperExtension` is deprecated. Please
+ refer to :func:`.event.listen` as well as
+ :class:`.MapperEvents`.
- New extension classes subclass ``MapperExtension`` and are specified
+ New extension classes subclass :class:`.MapperExtension` and are specified
using the ``extension`` mapper() argument, which is a single
- ``MapperExtension`` or a list of such. A single mapper
- can maintain a chain of ``MapperExtension`` objects. When a
- particular mapping event occurs, the corresponding method
- on each ``MapperExtension`` is invoked serially, and each method
- has the ability to halt the chain from proceeding further.
+ :class:`.MapperExtension` or a list of such::
+
+ from sqlalchemy.orm.interfaces import MapperExtension
+
+ class MyExtension(MapperExtension):
+ def before_insert(self, mapper, connection, instance):
+ print "instance %s before insert !" % instance
+
+ m = mapper(User, users_table, extension=MyExtension())
+
+ A single mapper can maintain a chain of ``MapperExtension``
+ objects. When a particular mapping event occurs, the
+ corresponding method on each ``MapperExtension`` is invoked
+ serially, and each method has the ability to halt the chain
+ from proceeding further::
+
+ m = mapper(User, users_table, extension=[ext1, ext2, ext3])
Each ``MapperExtension`` method returns the symbol
EXT_CONTINUE by default. This symbol generally means "move
@@ -354,9 +370,28 @@ class MapperExtension(object):
class SessionExtension(object):
- """An extension hook object for Sessions. Subclasses may be
- installed into a Session (or sessionmaker) using the ``extension``
- keyword argument. """
+ """Base implementation for :class:`.Session` event hooks.
+
+ .. note:: :class:`.SessionExtension` is deprecated. Please
+ refer to :func:`.event.listen` as well as
+ :class:`.SessionEvents`.
+
+ Subclasses may be installed into a :class:`.Session` (or
+ :func:`.sessionmaker`) using the ``extension`` keyword
+ argument::
+
+ from sqlalchemy.orm.interfaces import SessionExtension
+
+ class MySessionExtension(SessionExtension):
+ def before_commit(self, session):
+ print "before commit!"
+
+ Session = sessionmaker(extension=MySessionExtension())
+
+ The same :class:`.SessionExtension` instance can be used
+ with any number of sessions.
+
+ """
def before_commit(self, session):
"""Execute right before commit is called.
@@ -432,11 +467,43 @@ class SessionExtension(object):
class AttributeExtension(object):
- """An event handler for individual attribute change events.
+ """Base implementation for :class:`.AttributeImpl` event hooks, events
+ that fire upon attribute mutations in user code.
+
+ .. note:: :class:`.AttributeExtension` is deprecated. Please
+ refer to :func:`.event.listen` as well as
+ :class:`.AttributeEvents`.
+
+ :class:`.AttributeExtension` is used to listen for set,
+ remove, and append events on individual mapped attributes.
+ It is established on an individual mapped attribute using
+ the `extension` argument, available on
+ :func:`.column_property`, :func:`.relationship`, and
+ others::
+
+ from sqlalchemy.orm.interfaces import AttributeExtension
+ from sqlalchemy.orm import mapper, relationship, column_property
- .. note:: :class:`AttributeExtension` is deprecated. Please
- refer to :func:`event.listen` as well as
- :attr:`AttributeImpl.events`.
+ class MyAttrExt(AttributeExtension):
+ def append(self, state, value, initiator):
+ print "append event !"
+ return value
+
+ def set(self, state, value, oldvalue, initiator):
+ print "set event !"
+ return value
+
+ mapper(SomeClass, sometable, properties={
+ 'foo':column_property(sometable.c.foo, extension=MyAttrExt()),
+ 'bar':relationship(Bar, extension=MyAttrExt())
+ })
+
+ Note that the :class:`AttributeExtension` methods
+ :meth:`~.AttributeExtension.append` and
+ :meth:`~.AttributeExtension.set` need to return the
+ ``value`` parameter. The returned value is used as the
+ effective value, and allows the extension to change what is
+ ultimately persisted.
AttributeExtension is assembled within the descriptors associated
with a mapped class.