summaryrefslogtreecommitdiff
path: root/django/db/models/sql
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2013-07-08 11:05:08 -0400
committerTim Graham <timograham@gmail.com>2013-07-09 06:41:52 -0400
commit95eb68c98fe2dc95277d4e26a0316d856dc868dd (patch)
treec8e31f3c7c66f062d56e54b1f5124d2b023eb1aa /django/db/models/sql
parentc0a4894dca4bda0053a56cd677c80340a68c4c95 (diff)
Fixed #17339 -- Factor out has_result into database backend.
Thanks jonash.
Diffstat (limited to 'django/db/models/sql')
-rw-r--r--django/db/models/sql/compiler.py10
-rw-r--r--django/db/models/sql/query.py4
2 files changed, 11 insertions, 3 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 0b12bd1552..e17cb3f616 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -750,6 +750,16 @@ class SQLCompiler(object):
yield row
+ def has_results(self):
+ """
+ Backends (e.g. NoSQL) can override this in order to use optimized
+ versions of "query has any results."
+ """
+ # This is always executed on a query clone, so we can modify self.query
+ self.query.add_extra({'a': 1}, None, None, None, None, None)
+ self.query.set_extra_mask(['a'])
+ return bool(self.execute_sql(SINGLE))
+
def execute_sql(self, result_type=MULTI):
"""
Run the query against the database and returns the result(s). The
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 110925114c..2a9b8ef826 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -401,12 +401,10 @@ class Query(object):
def has_results(self, using):
q = self.clone()
q.clear_select_clause()
- q.add_extra({'a': 1}, None, None, None, None, None)
- q.set_extra_mask(['a'])
q.clear_ordering(True)
q.set_limits(high=1)
compiler = q.get_compiler(using=using)
- return bool(compiler.execute_sql(SINGLE))
+ return compiler.has_results()
def combine(self, rhs, connector):
"""