Files
deb-python-sqlalchemy-utils/sqlalchemy_utils/query_chain.py
2014-05-06 14:28:51 +03:00

60 lines
1.6 KiB
Python

from copy import copy
class QueryChain(object):
"""
:param queries: A sequence of SQLAlchemy Query objects
:param limit: Similar to normal query limit this parameter can be used for
limiting the number of results for the whole query chain.
:param offset: Similar to normal query offset this parameter can be used
for offsetting the query chain as a whole.
::
chain = QueryChain([session.query(User), session.query(Article)])
for obj in chain[0:5]:
print obj
.. versionadded: 0.26.0
"""
def __init__(self, queries, limit=None, offset=None):
self.queries = queries
self.limit = limit
self.offset = offset
def __iter__(self):
consumed = 0
skipped = 0
for query in self.queries:
query_copy = copy(query)
if self.limit:
query = query.limit(self.limit - consumed)
if self.offset:
query = query.offset(self.offset - skipped)
obj_count = 0
for obj in query:
consumed += 1
obj_count += 1
yield obj
if not obj_count:
skipped += query_copy.count()
else:
skipped += obj_count
def __getitem__(self, key):
if isinstance(key, slice):
return self.__class__(
queries=self.queries,
limit=key.stop,
offset=key.start
)
else:
for obj in self[key:1]:
return obj
def __repr__(self):
return '<QueryChain at 0x%x>' % id(self)