Skip to content

Commit

Permalink
Fixed #12192 -- Don't execute any DB query when the QS slicing being …
Browse files Browse the repository at this point in the history
…performed

will result in use of LIMIT 0. Thanks Suor for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14204 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
ramiro committed Oct 13, 2010
1 parent 5f5a61e commit 08d1492
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions django/db/backends/oracle/compiler.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def as_sql(self, with_limits=True, with_col_aliases=False):
If 'with_limits' is False, any limit/offset information is not If 'with_limits' is False, any limit/offset information is not
included in the query. included in the query.
""" """
if with_limits and self.query.low_mark == self.query.high_mark:
return '', ()


# The `do_offset` flag indicates whether we need to construct # The `do_offset` flag indicates whether we need to construct
# the SQL needed to use limit/offset with Oracle. # the SQL needed to use limit/offset with Oracle.
Expand Down
3 changes: 3 additions & 0 deletions django/db/models/sql/compiler.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def as_sql(self, with_limits=True, with_col_aliases=False):
If 'with_limits' is False, any limit/offset information is not included If 'with_limits' is False, any limit/offset information is not included
in the query. in the query.
""" """
if with_limits and self.query.low_mark == self.query.high_mark:
return '', ()

self.pre_sql_setup() self.pre_sql_setup()
out_cols = self.get_columns(with_col_aliases) out_cols = self.get_columns(with_col_aliases)
ordering, ordering_group_by = self.get_ordering() ordering, ordering_group_by = self.get_ordering()
Expand Down
12 changes: 11 additions & 1 deletion tests/regressiontests/queries/tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -86,8 +86,18 @@ class EmptyQuerySetTests(TestCase):
def test_emptyqueryset_values(self): def test_emptyqueryset_values(self):
"#14366 -- calling .values() on an EmptyQuerySet and then cloning that should not cause an error" "#14366 -- calling .values() on an EmptyQuerySet and then cloning that should not cause an error"
self.assertEqual(list(Number.objects.none().values('num').order_by('num')), []) self.assertEqual(list(Number.objects.none().values('num').order_by('num')), [])

def test_values_subquery(self): def test_values_subquery(self):
self.assertQuerysetEqual( self.assertQuerysetEqual(
Number.objects.filter(pk__in=Number.objects.none().values("pk")), [] Number.objects.filter(pk__in=Number.objects.none().values("pk")), []
) )


class WeirdQuerySetSlicing(TestCase):
def setUp(self):
Number.objects.create(num=1)
Number.objects.create(num=2)

def test_empty_resultset_sql(self):
# ticket #12192
self.assertNumQueries(0, lambda: list(Number.objects.all()[1:1]))

0 comments on commit 08d1492

Please sign in to comment.