From 1653ffb57124164ea48b2a084a8423a35164939d Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 7 Jun 2009 23:56:42 +0000 Subject: [PATCH] [soc2009/multidb] Updated DateQuery to work correctly with multiple databases git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@10943 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- TODO.TXT | 1 - django/db/models/sql/datastructures.py | 14 ++++++-------- django/db/models/sql/query.py | 5 ++++- django/db/models/sql/subqueries.py | 3 +-- tests/regressiontests/multiple_database/tests.py | 6 ++++++ 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/TODO.TXT b/TODO.TXT index d2d7752b47..ef20dffb5c 100644 --- a/TODO.TXT +++ b/TODO.TXT @@ -48,7 +48,6 @@ that need to be done. I'm trying to be as granular as possible. phase. This involves changing the following methods: * ``Query.add_aggregate`` - * ``DateQuery.add_date_select`` * ``Field.get_db_prep_lookup`` * ``DateField.get_db_prep_value`` * ``DateTimeField.get_db_prep_value`` diff --git a/django/db/models/sql/datastructures.py b/django/db/models/sql/datastructures.py index 4d53999c79..5c9fd36456 100644 --- a/django/db/models/sql/datastructures.py +++ b/django/db/models/sql/datastructures.py @@ -29,22 +29,20 @@ class Date(object): """ Add a date selection column. """ - def __init__(self, col, lookup_type, date_sql_func): + as_sql_takes_connection = True + + def __init__(self, col, lookup_type): self.col = col self.lookup_type = lookup_type - self.date_sql_func = date_sql_func def relabel_aliases(self, change_map): c = self.col if isinstance(c, (list, tuple)): self.col = (change_map.get(c[0], c[0]), c[1]) - def as_sql(self, quote_func=None): - if not quote_func: - quote_func = lambda x: x + def as_sql(self, qn, connection): if isinstance(self.col, (list, tuple)): - col = '%s.%s' % tuple([quote_func(c) for c in self.col]) + col = '%s.%s' % tuple([qn(c) for c in self.col]) else: col = self.col - return self.date_sql_func(self.lookup_type, col) - + return connection.ops.date_trunc_sql(self.lookup_type, col) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 6acd649621..fb17db546a 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -729,7 +729,10 @@ class BaseQuery(object): aliases.add(r) col_aliases.add(col[1]) else: - result.append(col.as_sql(quote_func=qn)) + if getattr(col, 'as_sql_takes_connection', False): + result.append(col.as_sql(qn, self.connection)) + else: + result.append(col.as_sql(qn)) if hasattr(col, 'alias'): aliases.add(col.alias) diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py index 51f1acafbf..db514c8b75 100644 --- a/django/db/models/sql/subqueries.py +++ b/django/db/models/sql/subqueries.py @@ -408,8 +408,7 @@ class DateQuery(Query): result = self.setup_joins([field.name], self.get_meta(), self.get_initial_alias(), False) alias = result[3][-1] - select = Date((alias, field.column), lookup_type, - self.connection.ops.date_trunc_sql) + select = Date((alias, field.column), lookup_type) self.select = [select] self.select_fields = [None] self.select_related = False # See #7097. diff --git a/tests/regressiontests/multiple_database/tests.py b/tests/regressiontests/multiple_database/tests.py index 9578a26706..b7ca0e5e23 100644 --- a/tests/regressiontests/multiple_database/tests.py +++ b/tests/regressiontests/multiple_database/tests.py @@ -56,3 +56,9 @@ class QueryTestCase(TestCase): pro = Book.objects.using(db).get(published__year=2008) self.assertEqual(pro.title, "Pro Django") self.assertEqual(pro.published, datetime.date(2008, 12, 16)) + + years = Book.objects.using(db).dates('published', 'year') + self.assertEqual([o.year for o in years], [2008, 2009]) + + months = Book.objects.dates('published', 'month').using(db) + self.assertEqual(sorted(o.month for o in months), [5, 12])