From: Gaƫtan de Menten Date: Mon, 30 Apr 2007 09:36:12 +0000 (+0000) Subject: - docstring improvements in query X-Git-Tag: rel_0_3_8~70 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=39947e1f5d131775e7beef7773d9905706dced64;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - docstring improvements in query - added support for __getitem__ on OrderedSet --- diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 355ba68f79..be299d2005 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -737,7 +737,8 @@ class Query(object): return q def select_from(self, from_obj): - """Set the `from_obj` parameter of the query. + """Set the `from_obj` parameter of the query and return the newly + resulting ``Query``. `from_obj` is a list of one or more tables. """ @@ -764,14 +765,15 @@ class Query(object): if isinstance(item, slice): start = item.start stop = item.stop + # if we slice from the end we need to execute the query if (isinstance(start, int) and start < 0) or \ (isinstance(stop, int) and stop < 0): return list(self)[item] else: res = self._clone() if start is not None and stop is not None: - res._offset = (self._offset or 0)+ start - res._limit = stop-start + res._offset = (self._offset or 0) + start + res._limit = stop - start elif start is None and stop is not None: res._limit = stop elif start is not None and stop is None: @@ -784,17 +786,23 @@ class Query(object): return list(self[item:item+1])[0] def limit(self, limit): - """Apply a ``LIMIT`` to the query.""" + """Apply a ``LIMIT`` to the query and return the newly resulting + ``Query``. + """ return self[:limit] def offset(self, offset): - """Apply an ``OFFSET`` to the query.""" + """Apply an ``OFFSET`` to the query and return the newly resulting + ``Query``. + """ return self[offset:] def distinct(self): - """Apply a ``DISTINCT`` to the query.""" + """Apply a ``DISTINCT`` to the query and return the newly resulting + ``Query``. + """ new = self._clone() new._distinct = True @@ -809,6 +817,10 @@ class Query(object): return list(self) def scalar(self): + """Return the first result of this ``Query``. + + This results in an execution of the underlying query. + """ if self._col is None or self._func is None: return self[0] else: diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index d4627974b5..e4b0efad4b 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -331,6 +331,9 @@ class OrderedSet(Set): super(OrderedSet, self).clear() self._list=[] + def __getitem__(self, key): + return self._list[key] + def __iter__(self): return iter(self._list)