Skip to content

Commit

Permalink
emit events before and after SA session flush
Browse files Browse the repository at this point in the history
  • Loading branch information
gotcha committed Sep 24, 2007
1 parent 45be0e9 commit ef54fe8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/z3c/sqlalchemy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import sqlalchemy
from sqlalchemy.engine.url import make_url

from zope.event import notify
from zope.interface import implements
from zope.component import getUtility
from zope.component.interfaces import ComponentLookupError

from z3c.sqlalchemy.interfaces import ISQLAlchemyWrapper, IModelProvider
from z3c.sqlalchemy.interfaces import ISessionFlushedEvent, IBeforeSessionFlushEvent
from z3c.sqlalchemy.model import Model
from z3c.sqlalchemy.mapper import LazyMapperCollection

Expand Down Expand Up @@ -165,14 +167,16 @@ def abort(self, trans):
def _flush(self):
# check if the session contains something flushable
if self.session.new or self.session.deleted or self.session.dirty:

# Check if a session-bound transaction has been created so far.
# If not, create a new transaction
if self.transaction is None:
self.transaction = self.session.create_transaction()

# Flush
notify(BeforeSessionFlushEvent(self.session))
self.session.flush()
notify(SessionFlushedEvent(self.session))

def commit(self, trans):
self._flush()
Expand Down Expand Up @@ -293,3 +297,13 @@ def connection(self):
# return the connection
return connection

class SessionEvent(object):
def __init__(self, session):
self.session = session

class SessionFlushedEvent(SessionEvent):
implements(ISessionFlushedEvent)

class BeforeSessionFlushEvent(SessionEvent):
implements(IBeforeSessionFlushEvent)

6 changes: 6 additions & 0 deletions src/z3c/sqlalchemy/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,9 @@ def add(name, table=None, mapper_class=None, relations=None, autodetect_relation
def items():
""" return items in insertion order """


class ISessionFlushedEvent(Interface):
""" Event triggered after session has been flushed """

class IBeforeSessionFlushEvent(Interface):
""" Event triggered before session will be flushed """

0 comments on commit ef54fe8

Please sign in to comment.