class ODMCursor(object): """Represents the results of query. The cursors can be iterated over to retrieve the results one by one. """ def __bool__(self): raise MingException('Cannot evaluate ODMCursor to a boolean') __nonzero__ = __bool__ # python 2 def __init__(self, session, cls, ming_cursor, refresh=False, decorate=None, fields=None): self.session = session self.cls = cls self.mapper = mapper(cls) self.ming_cursor = ming_cursor self._options = Object(refresh=refresh, decorate=decorate, fields=fields, instrument=True) def __iter__(self): return self @property def extensions(self): return self.session.extensions def count(self): """Get the number of objects retrieved by the query""" return self.ming_cursor.count() def distinct(self, *args, **kwargs): return self.ming_cursor.distinct(*args, **kwargs) def _next_impl(self): doc = next(self.ming_cursor) obj = self.session.imap.get(self.cls, doc['_id']) if obj is None: obj = self.mapper.create(doc, self._options, remake=False) state(obj).status = ObjectState.clean self.session.save(obj) elif self._options.refresh: # Refresh object state(obj).update(doc) state(obj).status = ObjectState.clean else: # Never refresh objects from the DB unless explicitly requested pass other_session = session(obj) if other_session is not None and other_session != self: other_session.expunge(obj) self.session.save(obj) if self._options.decorate is not None: return self._options.decorate(obj) else: return obj def next(self): _call_hook(self, 'before_cursor_next', self) try: return self._next_impl() finally: _call_hook(self, 'after_cursor_next', self) __next__ = next def options(self, **kwargs): odm_cursor = ODMCursor(self.session, self.cls, self.ming_cursor) odm_cursor._options = Object(self._options, **kwargs) _call_hook(self, 'cursor_created', odm_cursor, 'options', self, **kwargs) return odm_cursor def limit(self, limit): """Limit the number of entries retrieved by the query""" odm_cursor = ODMCursor(self.session, self.cls, self.ming_cursor.limit(limit)) _call_hook(self, 'cursor_created', odm_cursor, 'limit', self, limit) return odm_cursor def skip(self, skip): """Skip the first ``skip`` entries retrieved by the query""" odm_cursor = ODMCursor(self.session, self.cls, self.ming_cursor.skip(skip)) _call_hook(self, 'cursor_created', odm_cursor, 'skip', self, skip) return odm_cursor def hint(self, index_or_name): odm_cursor = ODMCursor(self.session, self.cls, self.ming_cursor.hint(index_or_name)) _call_hook(self, 'cursor_created', odm_cursor, 'hint', self, index_or_name) return odm_cursor def sort(self, *args, **kwargs): """Sort results of the query. See :meth:`pymongo.cursor.Cursor.sort` for details on the available arguments. """ odm_cursor = ODMCursor(self.session, self.cls, self.ming_cursor.sort(*args, **kwargs)) _call_hook(self, 'cursor_created', odm_cursor, 'sort', self, *args, **kwargs) return odm_cursor def one(self): """Gets one result and exaclty one. Raises ``ValueError`` exception if less or more than one result is returned by the query. """ try: result = self.next() except StopIteration: raise ValueError('Less than one result from .one()') try: self.next() except StopIteration: return result raise ValueError('More than one result from .one()') def first(self): """Gets the first result of the query""" try: return self.next() except StopIteration: return None def all(self): """Retrieve all the results of the query""" return list(self) def rewind(self): """Rewind this cursor to its unevaluated state. Reset this cursor if it has been partially or completely evaluated. Any options that are present on the cursor will remain in effect. Future iterating performed on this cursor will cause new queries to be sent to the server, even if the resultant data has already been retrieved by this cursor. """ return self.ming_cursor.rewind()
class ODMCursor(object): """Represents the results of query. The cursors can be iterated over to retrieve the results one by one. """ def __bool__(self): raise MingException('Cannot evaluate ODMCursor to a boolean') __nonzero__ = __bool__ # python 2 def __init__(self, session, cls, ming_cursor, refresh=False, decorate=None, fields=None): self.session = session self.cls = cls self.mapper = mapper(cls) self.ming_cursor = ming_cursor self._options = Object( refresh=refresh, decorate=decorate, fields=fields, instrument=True) def __iter__(self): return self @property def extensions(self): return self.session.extensions def count(self): """Get the number of objects retrieved by the query""" return self.ming_cursor.count() def _next_impl(self): doc = next(self.ming_cursor) obj = self.session.imap.get(self.cls, doc['_id']) if obj is None: obj = self.mapper.create(doc, self._options, remake=False) state(obj).status = ObjectState.clean self.session.save(obj) elif self._options.refresh: # Refresh object state(obj).update(doc) state(obj).status = ObjectState.clean else: # Never refresh objects from the DB unless explicitly requested pass other_session = session(obj) if other_session is not None and other_session != self: other_session.expunge(obj) self.session.save(obj) if self._options.decorate is not None: return self._options.decorate(obj) else: return obj def next(self): _call_hook(self, 'before_cursor_next', self) try: return self._next_impl() finally: _call_hook(self, 'after_cursor_next', self) __next__ = next def options(self, **kwargs): odm_cursor = ODMCursor(self.session, self.cls,self.ming_cursor) odm_cursor._options = Object(self._options, **kwargs) _call_hook(self, 'cursor_created', odm_cursor, 'options', self, **kwargs) return odm_cursor def limit(self, limit): """Limit the number of entries retrieved by the query""" odm_cursor = ODMCursor(self.session, self.cls, self.ming_cursor.limit(limit)) _call_hook(self, 'cursor_created', odm_cursor, 'limit', self, limit) return odm_cursor def skip(self, skip): """Skip the first ``skip`` entries retrieved by the query""" odm_cursor = ODMCursor(self.session, self.cls, self.ming_cursor.skip(skip)) _call_hook(self, 'cursor_created', odm_cursor, 'skip', self, skip) return odm_cursor def hint(self, index_or_name): odm_cursor = ODMCursor(self.session, self.cls, self.ming_cursor.hint(index_or_name)) _call_hook(self, 'cursor_created', odm_cursor, 'hint', self, index_or_name) return odm_cursor def sort(self, *args, **kwargs): """Sort results of the query. See :meth:`pymongo.cursor.Cursor.sort` for details on the available arguments. """ odm_cursor = ODMCursor(self.session, self.cls, self.ming_cursor.sort(*args, **kwargs)) _call_hook(self, 'cursor_created', odm_cursor, 'sort', self, *args, **kwargs) return odm_cursor def one(self): """Gets one result and exaclty one. Raises ``ValueError`` exception if less or more than one result is returned by the query. """ try: result = self.next() except StopIteration: raise ValueError('Less than one result from .one()') try: self.next() except StopIteration: return result raise ValueError('More than one result from .one()') def first(self): """Gets the first result of the query""" try: return self.next() except StopIteration: return None def all(self): """Retrieve all the results of the query""" return list(self)
class ODMCursor(object): def __init__(self, session, cls, ming_cursor, refresh=False, decorate=None): self.session = session self.cls = cls self.mapper = mapper(cls) self.ming_cursor = ming_cursor self._options = Object( refresh=refresh, decorate=decorate, instrument=True) def __iter__(self): return self def __len__(self): return self.count() @property def extensions(self): return self.session.extensions def count(self): return self.ming_cursor.count() def _next_impl(self): doc = self.ming_cursor.next() obj = self.session.imap.get(self.cls, doc['_id']) if obj is None: obj = self.mapper.create(doc, self._options) state(obj).status = ObjectState.clean self.session.save(obj) elif self._options.refresh: # Refresh object state(obj).update(doc) state(obj).status = ObjectState.clean else: # Never refresh objects from the DB unless explicitly requested pass other_session = session(obj) if other_session is not None and other_session != self: other_session.expunge(obj) self.session.save(obj) if self._options.decorate is not None: return self._options.decorate(obj) else: return obj def next(self): call_hook(self, 'before_cursor_next', self) try: return self._next_impl() finally: call_hook(self, 'after_cursor_next', self) def options(self, **kwargs): odm_cursor = ODMCursor(self.session, self.cls,self.ming_cursor) odm_cursor._options = Object(self._options, **kwargs) call_hook(self, 'cursor_created', odm_cursor, 'options', self, **kwargs) return odm_cursor def limit(self, limit): odm_cursor = ODMCursor(self.session, self.cls, self.ming_cursor.limit(limit)) call_hook(self, 'cursor_created', odm_cursor, 'limit', self, limit) return odm_cursor def skip(self, skip): odm_cursor = ODMCursor(self.session, self.cls, self.ming_cursor.skip(skip)) call_hook(self, 'cursor_created', odm_cursor, 'skip', self, skip) return odm_cursor def hint(self, index_or_name): odm_cursor = ODMCursor(self.session, self.cls, self.ming_cursor.hint(index_or_name)) call_hook(self, 'cursor_created', odm_cursor, 'hint', self, index_or_name) return odm_cursor def sort(self, *args, **kwargs): odm_cursor = ODMCursor(self.session, self.cls, self.ming_cursor.sort(*args, **kwargs)) call_hook(self, 'cursor_created', odm_cursor, 'sort', self, *args, **kwargs) return odm_cursor def one(self): try: result = self.next() except StopIteration: raise ValueError, 'Less than one result from .one()' try: self.next() except StopIteration: return result raise ValueError, 'More than one result from .one()' def first(self): try: return self.next() except StopIteration: return None def all(self): return list(self)