summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2015-03-10 19:56:59 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2015-03-10 19:56:59 -0400
commit710021d22e8a5a053e1c4edc4a30612f6e10b83e (patch)
tree158a8e8ad555c255fdddadf891d981b40c6b87ec /lib/sqlalchemy/orm
parent201ba16fc88439fa100023369dfdfe22625253c9 (diff)
downloadsqlalchemy-710021d22e8a5a053e1c4edc4a30612f6e10b83e.tar.gz
- Added a new event suite :class:`.QueryEvents`. The
:meth:`.QueryEvents.before_compile` event allows the creation of functions which may place additional modifications to :class:`.Query` objects before the construction of the SELECT statement. It is hoped that this event be made much more useful via the advent of a new inspection system that will allow for detailed modifications to be made against :class:`.Query` objects in an automated fashion. fixes #3317
Diffstat (limited to 'lib/sqlalchemy/orm')
-rw-r--r--lib/sqlalchemy/orm/events.py55
-rw-r--r--lib/sqlalchemy/orm/query.py13
2 files changed, 63 insertions, 5 deletions
diff --git a/lib/sqlalchemy/orm/events.py b/lib/sqlalchemy/orm/events.py
index 7e23248b5..233cd66a6 100644
--- a/lib/sqlalchemy/orm/events.py
+++ b/lib/sqlalchemy/orm/events.py
@@ -17,7 +17,7 @@ from . import mapperlib, instrumentation
from .session import Session, sessionmaker
from .scoping import scoped_session
from .attributes import QueryableAttribute
-
+from .query import Query
class InstrumentationEvents(event.Events):
"""Events related to class instrumentation events.
@@ -1651,3 +1651,56 @@ class AttributeEvents(event.Events):
the :class:`.collection.linker` hook.
"""
+
+
+class QueryEvents(event.Events):
+ """Represent events within the construction of a :class:`.Query` object.
+
+ The events here are intended to be used with an as-yet-unreleased
+ inspection system for :class:`.Query`. Some very basic operations
+ are possible now, however the inspection system is intended to allow
+ complex query manipulations to be automated.
+
+ .. versionadded:: 1.0.0
+
+ """
+
+ _target_class_doc = "SomeQuery"
+ _dispatch_target = Query
+
+ def before_compile(self, query):
+ """Receive the :class:`.Query` object before it is composed into a
+ core :class:`.Select` object.
+
+ This event is intended to allow changes to the query given::
+
+ @event.listens_for(Query, "before_compile", retval=True)
+ def no_deleted(query):
+ for desc in query.column_descriptions:
+ if desc['type'] is User:
+ entity = desc['expr']
+ query = query.filter(entity.deleted == False)
+ return query
+
+ The event should normally be listened with the ``retval=True``
+ parameter set, so that the modified query may be returned.
+
+
+ """
+
+ @classmethod
+ def _listen(
+ cls, event_key, retval=False, **kw):
+ fn = event_key._listen_fn
+
+ if not retval:
+ def wrap(*arg, **kw):
+ if not retval:
+ query = arg[0]
+ fn(*arg, **kw)
+ return query
+ else:
+ return fn(*arg, **kw)
+ event_key = event_key.with_wrapper(wrap)
+
+ event_key.base_listen(**kw)
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py
index 9792b4e88..65c72e5e1 100644
--- a/lib/sqlalchemy/orm/query.py
+++ b/lib/sqlalchemy/orm/query.py
@@ -2934,6 +2934,12 @@ class Query(object):
return update_op.rowcount
def _compile_context(self, labels=True):
+ if self.dispatch.before_compile:
+ for fn in self.dispatch.before_compile:
+ new_query = fn(self)
+ if new_query is not None:
+ self = new_query
+
context = QueryContext(self)
if context.statement is not None:
@@ -2954,10 +2960,8 @@ class Query(object):
# "load from explicit FROMs" mode,
# i.e. when select_from() or join() is used
context.froms = list(context.from_clause)
- else:
- # "load from discrete FROMs" mode,
- # i.e. when each _MappedEntity has its own FROM
- context.froms = context.froms
+ # else "load from discrete FROMs" mode,
+ # i.e. when each _MappedEntity has its own FROM
if self._enable_single_crit:
self._adjust_for_single_inheritance(context)
@@ -2977,6 +2981,7 @@ class Query(object):
context.statement = self._compound_eager_statement(context)
else:
context.statement = self._simple_statement(context)
+
return context
def _compound_eager_statement(self, context):