diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-08-29 11:22:46 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-08-29 11:22:46 -0400 |
| commit | 1149197a36f01dae1f8da82b7cfb73a7777e7a4a (patch) | |
| tree | 3b2cacd6c9fc75c51862eb886420036b7cd08c47 /lib/sqlalchemy | |
| parent | a635750213c346a895e417ae8e629ce924d557e8 (diff) | |
| download | sqlalchemy-1149197a36f01dae1f8da82b7cfb73a7777e7a4a.tar.gz | |
- moved out to on_before_execute, on_after_execute. not much option here,
need both forms, the wrapping thing is just silly
- fixed the listen() to not re-wrap continuously.
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/engine/base.py | 93 | ||||
| -rw-r--r-- | lib/sqlalchemy/engine/threadlocal.py | 4 | ||||
| -rw-r--r-- | lib/sqlalchemy/event.py | 19 | ||||
| -rw-r--r-- | lib/sqlalchemy/interfaces.py | 70 | ||||
| -rw-r--r-- | lib/sqlalchemy/test/assertsql.py | 6 | ||||
| -rw-r--r-- | lib/sqlalchemy/test/engines.py | 4 |
6 files changed, 119 insertions, 77 deletions
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 1a7b2faaf..70ad01914 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -1551,16 +1551,23 @@ class EngineEvents(event.Events): @classmethod def listen(cls, fn, identifier, target): - if issubclass(target.Connection, Connection): - target.Connection = _proxy_connection_cls( + if target.Connection is Connection: + target.Connection = _listener_connection_cls( Connection, target.dispatch) event.Events.listen(fn, identifier, target) - def on_execute(self, conn, clauseelement, *multiparams, **params): + def on_before_execute(self, conn, clauseelement, multiparams, params): + """Intercept high level execute() events.""" + + def on_after_execute(self, conn, clauseelement, multiparams, params, result): """Intercept high level execute() events.""" - def on_cursor_execute(self, conn, cursor, statement, + def on_before_cursor_execute(self, conn, cursor, statement, + parameters, context, executemany): + """Intercept low-level cursor execute() events.""" + + def on_after_cursor_execute(self, conn, cursor, statement, parameters, context, executemany): """Intercept low-level cursor execute() events.""" @@ -1845,100 +1852,126 @@ class Engine(Connectable, log.Identified): return self.pool.unique_connection() -def _proxy_connection_cls(cls, dispatch): - class ProxyConnection(cls): +def _listener_connection_cls(cls, dispatch): + """Produce a wrapper for :class:`.Connection` which will apply event + dispatch to each method. + + :class:`.Connection` does not provide event dispatch built in so that + method call overhead is avoided in the absense of any listeners. + + """ + class EventListenerConnection(cls): def execute(self, clauseelement, *multiparams, **params): - for fn in dispatch.on_execute: - result = fn(self, clauseelement, *multiparams, **params) - if result: - clauseelement, multiparams, params = result + if dispatch.on_before_execute: + for fn in dispatch.on_before_execute: + result = fn(self, clauseelement, multiparams, params) + if result: + clauseelement, multiparams, params = result + + ret = super(EventListenerConnection, self).execute(clauseelement, *multiparams, **params) + + if dispatch.on_after_execute: + for fn in dispatch.on_after_execute: + fn(self, clauseelement, multiparams, params, ret) - return super(ProxyConnection, self).execute(clauseelement, *multiparams, **params) + return ret def _execute_clauseelement(self, clauseelement, multiparams=None, params=None): return self.execute(clauseelement, *(multiparams or []), **(params or {})) def _cursor_execute(self, cursor, statement, parameters, context=None): - for fn in dispatch.on_cursor_execute: - result = fn(self, cursor, statement, parameters, context, False) - if result: - statement, parameters = result + if dispatch.on_before_cursor_execute: + for fn in dispatch.on_before_cursor_execute: + result = fn(self, cursor, statement, parameters, context, False) + if result: + statement, parameters = result - return super(ProxyConnection, self).\ + ret = super(EventListenerConnection, self).\ _cursor_execute(cursor, statement, parameters, context) + if dispatch.on_after_cursor_execute: + for fn in dispatch.on_after_cursor_execute: + fn(self, cursor, statement, parameters, context, False) + + return ret + def _cursor_executemany(self, cursor, statement, parameters, context=None): - for fn in dispatch.on_cursor_execute: + for fn in dispatch.on_before_cursor_execute: result = fn(self, cursor, statement, parameters, context, True) if result: statement, parameters = result - return super(ProxyConnection, self).\ + ret = super(EventListenerConnection, self).\ _cursor_executemany(cursor, statement, parameters, context) - + + for fn in dispatch.on_after_cursor_execute: + fn(self, cursor, statement, parameters, context, True) + + return ret + def _begin_impl(self): for fn in dispatch.on_begin: fn(self) - return super(ProxyConnection, self).\ + return super(EventListenerConnection, self).\ _begin_impl() def _rollback_impl(self): for fn in dispatch.on_rollback: fn(self) - return super(ProxyConnection, self).\ + return super(EventListenerConnection, self).\ _rollback_impl() def _commit_impl(self): for fn in dispatch.on_commit: fn(self) - return super(ProxyConnection, self).\ + return super(EventListenerConnection, self).\ _commit_impl() def _savepoint_impl(self, name=None): for fn in dispatch.on_savepoint: fn(self, name) - return super(ProxyConnection, self).\ + return super(EventListenerConnection, self).\ _savepoint_impl(name=name) def _rollback_to_savepoint_impl(self, name, context): for fn in dispatch.on_rollback_to_savepoint: fn(self, name, context) - return super(ProxyConnection, self).\ + return super(EventListenerConnection, self).\ _rollback_to_savepoint_impl(name, context) def _release_savepoint_impl(self, name, context): for fn in dispatch.on_release_savepoint: fn(self, name, context) - return super(ProxyConnection, self).\ + return super(EventListenerConnection, self).\ _release_savepoint_impl(name, context) def _begin_twophase_impl(self, xid): for fn in dispatch.on_begin_twophase: fn(self, xid) - return super(ProxyConnection, self).\ + return super(EventListenerConnection, self).\ _begin_twophase_impl(xid) def _prepare_twophase_impl(self, xid): for fn in dispatch.on_prepare_twophase: fn(self, xid) - return super(ProxyConnection, self).\ + return super(EventListenerConnection, self).\ _prepare_twophase_impl(xid) def _rollback_twophase_impl(self, xid, is_prepared): for fn in dispatch.on_rollback_twophase: fn(self, xid) - return super(ProxyConnection, self).\ + return super(EventListenerConnection, self).\ _rollback_twophase_impl(xid) def _commit_twophase_impl(self, xid, is_prepared): for fn in dispatch.on_commit_twophase: fn(self, xid) - return super(ProxyConnection, self).\ + return super(EventListenerConnection, self).\ _commit_twophase_impl(xid) - return ProxyConnection + return EventListenerConnection # This reconstructor is necessary so that pickles with the C extension or # without use the same Binary format. diff --git a/lib/sqlalchemy/engine/threadlocal.py b/lib/sqlalchemy/engine/threadlocal.py index b6e687b7c..c982afd63 100644 --- a/lib/sqlalchemy/engine/threadlocal.py +++ b/lib/sqlalchemy/engine/threadlocal.py @@ -30,8 +30,8 @@ class TLConnection(base.Connection): class TLEvents(base.EngineEvents): @classmethod def listen(cls, fn, identifier, target): - if issubclass(target.TLConnection, TLConnection): - target.TLConnection = base._proxy_connection_cls( + if target.TLConnection is TLConnection: + target.TLConnection = base._listener_connection_cls( TLConnection, target.dispatch) base.EngineEvents.listen(fn, identifier, target) diff --git a/lib/sqlalchemy/event.py b/lib/sqlalchemy/event.py index 28ed7f563..5448503b2 100644 --- a/lib/sqlalchemy/event.py +++ b/lib/sqlalchemy/event.py @@ -143,26 +143,11 @@ class _ListenerCollection(object): self(*args, **kw) self._exec_once = True - def exec_until_return(self, *args, **kw): - """Execute listeners for this event until - one returns a non-None value. - - Returns the value, or None. - """ - - if self: - for fn in self: - r = fn(*args, **kw) - if r is not None: - return r - return None - def __call__(self, *args, **kw): """Execute this event.""" - if self: - for fn in self: - fn(*args, **kw) + for fn in self: + fn(*args, **kw) # I'm not entirely thrilled about the overhead here, # but this allows class-level listeners to be added diff --git a/lib/sqlalchemy/interfaces.py b/lib/sqlalchemy/interfaces.py index 1cceff0b4..2c16935ce 100644 --- a/lib/sqlalchemy/interfaces.py +++ b/lib/sqlalchemy/interfaces.py @@ -174,41 +174,69 @@ class ConnectionProxy(object): @classmethod def _adapt_listener(cls, self, listener): - - def adapt_execute(conn, clauseelement, *multiparams, **params): + + def adapt_execute(conn, clauseelement, multiparams, params): + def execute_wrapper(clauseelement, *multiparams, **params): return clauseelement, multiparams, params - return listener.execute(conn, execute_wrapper, clauseelement, *multiparams, **params) - - event.listen(adapt_execute, 'on_execute', self) + + return listener.execute(conn, execute_wrapper, + clauseelement, *multiparams, + **params) + + event.listen(adapt_execute, 'on_before_execute', self) def adapt_cursor_execute(conn, cursor, statement, - parameters, context, executemany): - def execute_wrapper(cursor, statement, parameters, context): + parameters,context, executemany, ): + + def execute_wrapper( + cursor, + statement, + parameters, + context, + ): return statement, parameters - return listener.cursor_execute(execute_wrapper, cursor, statement, - parameters, context, executemany) - - event.listen(adapt_cursor_execute, 'on_cursor_execute', self) + + return listener.cursor_execute( + execute_wrapper, + cursor, + statement, + parameters, + context, + executemany, + ) + + event.listen(adapt_cursor_execute, 'on_before_cursor_execute', + self) def do_nothing_callback(*arg, **kw): pass - + def adapt_listener(fn): + def go(conn, *arg, **kw): fn(conn, do_nothing_callback, *arg, **kw) + return util.update_wrapper(go, fn) - + event.listen(adapt_listener(listener.begin), 'on_begin', self) - event.listen(adapt_listener(listener.rollback), 'on_rollback', self) + event.listen(adapt_listener(listener.rollback), 'on_rollback', + self) event.listen(adapt_listener(listener.commit), 'on_commit', self) - event.listen(adapt_listener(listener.savepoint), 'on_savepoint', self) - event.listen(adapt_listener(listener.rollback_savepoint), 'on_rollback_savepoint', self) - event.listen(adapt_listener(listener.release_savepoint), 'on_release_savepoint', self) - event.listen(adapt_listener(listener.begin_twophase), 'on_begin_twophase', self) - event.listen(adapt_listener(listener.prepare_twophase), 'on_prepare_twophase', self) - event.listen(adapt_listener(listener.rollback_twophase), 'on_rollback_twophase', self) - event.listen(adapt_listener(listener.commit_twophase), 'on_commit_twophase', self) + event.listen(adapt_listener(listener.savepoint), 'on_savepoint' + , self) + event.listen(adapt_listener(listener.rollback_savepoint), + 'on_rollback_savepoint', self) + event.listen(adapt_listener(listener.release_savepoint), + 'on_release_savepoint', self) + event.listen(adapt_listener(listener.begin_twophase), + 'on_begin_twophase', self) + event.listen(adapt_listener(listener.prepare_twophase), + 'on_prepare_twophase', self) + event.listen(adapt_listener(listener.rollback_twophase), + 'on_rollback_twophase', self) + event.listen(adapt_listener(listener.commit_twophase), + 'on_commit_twophase', self) def execute(self, conn, execute, clauseelement, *multiparams, **params): diff --git a/lib/sqlalchemy/test/assertsql.py b/lib/sqlalchemy/test/assertsql.py index 11ad20e77..dee63a876 100644 --- a/lib/sqlalchemy/test/assertsql.py +++ b/lib/sqlalchemy/test/assertsql.py @@ -273,10 +273,7 @@ class SQLAssert(object): def clear_rules(self): del self.rules - def execute(self, conn, clauseelement, *multiparams, **params): - # TODO: this doesn't work. we need to execute before so that we know - # what's happened with the parameters. - + def execute(self, conn, clauseelement, multiparams, params, result): if self.rules is not None: if not self.rules: assert False, "All rules have been exhausted, but further statements remain" @@ -287,7 +284,6 @@ class SQLAssert(object): def cursor_execute(self, conn, cursor, statement, parameters, context, executemany): - print "RECEIVE !", statement, parameters if self.rules: rule = self.rules[0] rule.process_cursor_execute(statement, parameters, context, executemany) diff --git a/lib/sqlalchemy/test/engines.py b/lib/sqlalchemy/test/engines.py index 779f87264..8b930175f 100644 --- a/lib/sqlalchemy/test/engines.py +++ b/lib/sqlalchemy/test/engines.py @@ -135,8 +135,8 @@ def testing_engine(url=None, options=None): options = options or config.db_opts engine = create_engine(url, **options) - event.listen(asserter.execute, 'on_execute', engine) - event.listen(asserter.cursor_execute, 'on_cursor_execute', engine) + event.listen(asserter.execute, 'on_after_execute', engine) + event.listen(asserter.cursor_execute, 'on_after_cursor_execute', engine) event.listen(testing_reaper.checkout, 'on_checkout', engine.pool) # may want to call this, results |
