diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2008-09-28 00:39:06 +0000 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2008-09-28 00:39:06 +0000 |
| commit | 5f75197e86059d7e4bbe20558c98011d89f9cfda (patch) | |
| tree | 5225252c7bb6a04ec656dcd8db283dc48a950e26 /lib/sqlalchemy | |
| parent | 15f1a5df20291f77a0a6e83ba9dd5527034237ad (diff) | |
| download | sqlalchemy-5f75197e86059d7e4bbe20558c98011d89f9cfda.tar.gz | |
- Fixed up slices on Query (i.e. query[x:y]) to work properly
for zero length slices, slices with None on either end.
[ticket:1177]
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/orm/query.py | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index c2d47afea..e25716316 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -927,16 +927,21 @@ class Query(object): def __getitem__(self, item): if isinstance(item, slice): start, stop, step = util.decode_slice(item) - # if we slice from the end we need to execute the query before - # slicing - if start < 0 or stop < 0: + + if isinstance(stop, int) and isinstance(start, int) and stop - start <= 0: + return [] + + # perhaps we should execute a count() here so that we + # can still use LIMIT/OFFSET ? + elif (isinstance(start, int) and start < 0) \ + or (isinstance(stop, int) and stop < 0): return list(self)[item] + + res = self.slice(start, stop) + if step is not None: + return list(res)[None:None:item.step] else: - res = self.slice(start, stop) - if step is not None: - return list(res)[None:None:item.step] - else: - return list(res) + return list(res) else: return list(self[item:item+1])[0] |
