1
0
mirror of https://github.com/django/django.git synced 2025-07-05 10:19:20 +00:00

[soc2009/multidb] Fixed the usage of the connection during Query construction, and defer it until actual SQL construction. In practice this means the GROUP BY optimization we do will be correctly applied based on the connection the Query is executed against, as oppossed to the on the QuerySet is targeting at the time the GROUP BY items are added

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@10934 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2009-06-06 19:38:29 +00:00
parent 6aca03c24d
commit 4d6c6d0596
4 changed files with 11 additions and 6 deletions

View File

@ -49,7 +49,6 @@ that need to be done. I'm trying to be as granular as possible.
phase. This involves changing the following methods: phase. This involves changing the following methods:
* ``Query.add_aggregate`` * ``Query.add_aggregate``
* ``Query.set_group_by``
* ``DateQuery.add_date_select`` * ``DateQuery.add_date_select``
* ``Field.get_db_prep_lookup`` * ``Field.get_db_prep_lookup``
* ``DateField.get_db_prep_value`` * ``DateField.get_db_prep_value``

View File

@ -204,6 +204,7 @@ class Field(object):
sql, params = value._as_sql() sql, params = value._as_sql()
return QueryWrapper(('(%s)' % sql), params) return QueryWrapper(('(%s)' % sql), params)
if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'search'): if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'search'):
return [value] return [value]
elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'): elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'):

View File

@ -878,6 +878,9 @@ class BaseQuery(object):
qn = self.quote_name_unless_alias qn = self.quote_name_unless_alias
result, params = [], [] result, params = [], []
if self.group_by is not None: if self.group_by is not None:
if len(self.model._meta.fields) == len(self.group_by) and \
self.connection.features.allows_group_by_pk:
self.group_by = [(self.model._meta.db_table, self.model._meta.pk.column)]
group_by = self.group_by or [] group_by = self.group_by or []
extra_selects = [] extra_selects = []
@ -2099,11 +2102,6 @@ class BaseQuery(object):
will be made automatically. will be made automatically.
""" """
self.group_by = [] self.group_by = []
if self.connection.features.allows_group_by_pk:
if len(self.select) == len(self.model._meta.fields):
self.group_by.append((self.model._meta.db_table,
self.model._meta.pk.column))
return
for sel in self.select: for sel in self.select:
self.group_by.append(sel) self.group_by.append(sel)

View File

@ -49,3 +49,10 @@ class QueryTestCase(TestCase):
dive = Book.objects.using(db).get(title__icontains="dive") dive = Book.objects.using(db).get(title__icontains="dive")
self.assertEqual(dive.title, "Dive into Python") self.assertEqual(dive.title, "Dive into Python")
dive = Book.objects.using(db).get(title__iexact="dive INTO python")
self.assertEqual(dive.title, "Dive into Python")
pro = Book.objects.using(db).get(published__year=2008)
self.assertEqual(pro.title, "Pro Django")
self.assertEqual(pro.published, datetime.date(2008, 12, 16))