Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #12818. SQLite now properly quotes strings for date ext…

…raction and truncation. Backport of r12573 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12575 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 692d122ce20c2136e796a72532e7a5f753785769 1 parent 770b31e
Joseph Kocherhans authored February 24, 2010
12  django/db/backends/sqlite3/base.py
@@ -64,13 +64,17 @@ class DatabaseFeatures(BaseDatabaseFeatures):
64 64
 class DatabaseOperations(BaseDatabaseOperations):
65 65
     def date_extract_sql(self, lookup_type, field_name):
66 66
         # sqlite doesn't support extract, so we fake it with the user-defined
67  
-        # function django_extract that's registered in connect().
68  
-        return 'django_extract("%s", %s)' % (lookup_type.lower(), field_name)
  67
+        # function django_extract that's registered in connect(). Note that
  68
+        # single quotes are used because this is a string (and could otherwise
  69
+        # cause a collision with a field name).
  70
+        return "django_extract('%s', %s)" % (lookup_type.lower(), field_name)
69 71
 
70 72
     def date_trunc_sql(self, lookup_type, field_name):
71 73
         # sqlite doesn't support DATE_TRUNC, so we fake it with a user-defined
72  
-        # function django_date_trunc that's registered in connect().
73  
-        return 'django_date_trunc("%s", %s)' % (lookup_type.lower(), field_name)
  74
+        # function django_date_trunc that's registered in connect(). Note that
  75
+        # single quotes are used because this is a string (and could otherwise
  76
+        # cause a collision with a field name).
  77
+        return "django_date_trunc('%s', %s)" % (lookup_type.lower(), field_name)
74 78
 
75 79
     def drop_foreignkey_sql(self):
76 80
         return ""
5  tests/regressiontests/backends/models.py
@@ -15,6 +15,11 @@ class Person(models.Model):
15 15
     def __unicode__(self):
16 16
         return u'%s %s' % (self.first_name, self.last_name)
17 17
 
  18
+class SchoolClass(models.Model):
  19
+    year = models.PositiveIntegerField()
  20
+    day = models.CharField(max_length=9, blank=True)
  21
+    last_updated = models.DateTimeField()
  22
+
18 23
 qn = connection.ops.quote_name
19 24
 
20 25
 __test__ = {'API_TESTS': """
32  tests/regressiontests/backends/tests.py
... ...
@@ -1,9 +1,12 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 # Unit and doctests for specific database backends.
  3
+import datetime
  4
+import models
3 5
 import unittest
4 6
 from django.db import backend, connection
5 7
 from django.db.backends.signals import connection_created
6 8
 from django.conf import settings
  9
+from django.test import TestCase
7 10
 
8 11
 class Callproc(unittest.TestCase):
9 12
 
@@ -34,6 +37,35 @@ def test_long_string(self):
34 37
             c.execute('DROP TABLE ltext')
35 38
             self.assertEquals(long_str, row[0].read())
36 39
 
  40
+class DateQuotingTest(TestCase):
  41
+
  42
+    def test_django_date_trunc(self):
  43
+        """
  44
+        Test the custom ``django_date_trunc method``, in particular against
  45
+        fields which clash with strings passed to it (e.g. 'year') - see
  46
+        #12818__.
  47
+
  48
+        __: http://code.djangoproject.com/ticket/12818
  49
+
  50
+        """
  51
+        updated = datetime.datetime(2010, 2, 20)
  52
+        models.SchoolClass.objects.create(year=2009, last_updated=updated)
  53
+        years = models.SchoolClass.objects.dates('last_updated', 'year')
  54
+        self.assertEqual(list(years), [datetime.datetime(2010, 1, 1, 0, 0)])
  55
+
  56
+    def test_django_extract(self):
  57
+        """
  58
+        Test the custom ``django_extract method``, in particular against fields
  59
+        which clash with strings passed to it (e.g. 'day') - see #12818__.
  60
+
  61
+        __: http://code.djangoproject.com/ticket/12818
  62
+
  63
+        """
  64
+        updated = datetime.datetime(2010, 2, 20)
  65
+        models.SchoolClass.objects.create(year=2009, last_updated=updated)
  66
+        classes = models.SchoolClass.objects.filter(last_updated__day=20)
  67
+        self.assertEqual(len(classes), 1)
  68
+
37 69
 def connection_created_test(sender, **kwargs):
38 70
     print 'connection_created signal'
39 71
 

0 notes on commit 692d122

Please sign in to comment.
Something went wrong with that request. Please try again.