Code

Ticket #15169: 15169.1.diff

File 15169.1.diff, 6.1 KB (added by jbronn, 3 years ago)
Line 
1diff -r fe89b6629482 django/contrib/gis/db/backends/mysql/compiler.py
2--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
3+++ b/django/contrib/gis/db/backends/mysql/compiler.py  Fri Jan 28 12:59:14 2011 -0600
4@@ -0,0 +1,22 @@
5+from django.contrib.gis.db.models.sql.compiler import GeoSQLCompiler as BaseGeoSQLCompiler
6+from django.db.backends.mysql import compiler
7+
8+SQLCompiler = compiler.SQLCompiler
9+
10+class GeoSQLCompiler(BaseGeoSQLCompiler, SQLCompiler):
11+    pass
12+
13+class SQLInsertCompiler(compiler.SQLInsertCompiler, GeoSQLCompiler):
14+    pass
15+
16+class SQLDeleteCompiler(compiler.SQLDeleteCompiler, GeoSQLCompiler):
17+    pass
18+
19+class SQLUpdateCompiler(compiler.SQLUpdateCompiler, GeoSQLCompiler):
20+    pass
21+
22+class SQLAggregateCompiler(compiler.SQLAggregateCompiler, GeoSQLCompiler):
23+    pass
24+
25+class SQLDateCompiler(compiler.SQLDateCompiler, GeoSQLCompiler):
26+    pass
27diff -r fe89b6629482 django/contrib/gis/db/backends/mysql/operations.py
28--- a/django/contrib/gis/db/backends/mysql/operations.py        Thu Jan 27 14:48:37 2011 +0000
29+++ b/django/contrib/gis/db/backends/mysql/operations.py        Fri Jan 28 12:59:14 2011 -0600
30@@ -5,7 +5,7 @@
31 
32 class MySQLOperations(DatabaseOperations, BaseSpatialOperations):
33 
34-    compiler_module = 'django.contrib.gis.db.models.sql.compiler'
35+    compiler_module = 'django.contrib.gis.db.backends.mysql.compiler'
36     mysql = True
37     name = 'mysql'
38     select = 'AsText(%s)'
39diff -r fe89b6629482 django/contrib/gis/db/models/sql/compiler.py
40--- a/django/contrib/gis/db/models/sql/compiler.py      Thu Jan 27 14:48:37 2011 +0000
41+++ b/django/contrib/gis/db/models/sql/compiler.py      Fri Jan 28 12:59:14 2011 -0600
42@@ -190,7 +190,8 @@
43                                self.query.extra_select_fields.get(a, None),
44                                self.connection)
45                   for v, a in izip(row[rn_offset:index_start], aliases)]
46-        if self.connection.ops.oracle or getattr(self.query, 'geo_values', False):
47+
48+        if self.connection.ops.oracle or self.connection.ops.mysql or getattr(self.query, 'geo_values', False):
49             # We resolve the rest of the columns if we're on Oracle or if
50             # the `geo_values` attribute is defined.
51             for value, field in map(None, row[index_start:], fields):
52diff -r fe89b6629482 django/contrib/gis/db/models/sql/query.py
53--- a/django/contrib/gis/db/models/sql/query.py Thu Jan 27 14:48:37 2011 +0000
54+++ b/django/contrib/gis/db/models/sql/query.py Fri Jan 28 12:59:14 2011 -0600
55@@ -56,8 +56,8 @@
56         extra selection objects into Geometry and Distance objects.
57         TODO: Make converted objects 'lazy' for less overhead.
58         """
59-        if connection.ops.oracle:
60-            # Running through Oracle's first.
61+        if connection.ops.oracle or connection.ops.mysql:
62+            # On MySQL and Oracle, call their version of `convert_values` first.
63             value = super(GeoQuery, self).convert_values(value, field or GeomField(), connection)
64 
65         if value is None:
66diff -r fe89b6629482 django/contrib/gis/tests/geoapp/models.py
67--- a/django/contrib/gis/tests/geoapp/models.py Thu Jan 27 14:48:37 2011 +0000
68+++ b/django/contrib/gis/tests/geoapp/models.py Fri Jan 28 12:59:14 2011 -0600
69@@ -33,6 +33,10 @@
70     objects = models.GeoManager()
71     def __unicode__(self): return self.name
72 
73+class Truth(models.Model):
74+    val = models.BooleanField()
75+    objects = models.GeoManager()
76+
77 if not spatialite:
78     class Feature(models.Model):
79         name = models.CharField(max_length=20)
80diff -r fe89b6629482 django/contrib/gis/tests/geoapp/test_regress.py
81--- a/django/contrib/gis/tests/geoapp/test_regress.py   Thu Jan 27 14:48:37 2011 +0000
82+++ b/django/contrib/gis/tests/geoapp/test_regress.py   Fri Jan 28 12:59:14 2011 -0600
83@@ -1,9 +1,9 @@
84-import os, unittest
85+from django.test import TestCase
86 from django.contrib.gis.tests.utils import no_mysql, no_oracle, no_postgis, no_spatialite
87 from django.contrib.gis.shortcuts import render_to_kmz
88-from models import City
89+from models import City, Truth
90 
91-class GeoRegressionTests(unittest.TestCase):
92+class GeoRegressionTests(TestCase):
93 
94     def test01_update(self):
95         "Testing GeoQuerySet.update(), see #10411."
96@@ -35,3 +35,11 @@
97         extent = City.objects.filter(name='Pueblo').extent()
98         for ref_val, val in zip(ref_ext, extent):
99             self.assertAlmostEqual(ref_val, val, 4)
100+
101+    def test04_boolean_conversion(self):
102+        "Testing Boolean value conversion with the spatial backend, see #15169."
103+        t1 = Truth.objects.create(val=True)
104+        t2 = Truth.objects.create(val=False)
105+
106+        self.assertTrue(Truth.objects.get(pk=1).val is True)
107+        self.assertTrue(Truth.objects.get(pk=2).val is False)
108diff -r fe89b6629482 django/db/backends/mysql/base.py
109--- a/django/db/backends/mysql/base.py  Thu Jan 27 14:48:37 2011 +0000
110+++ b/django/db/backends/mysql/base.py  Fri Jan 28 12:59:14 2011 -0600
111@@ -136,6 +136,12 @@
112 class DatabaseOperations(BaseDatabaseOperations):
113     compiler_module = "django.db.backends.mysql.compiler"
114 
115+    def convert_values(self, value, field):
116+        if (field and field.get_internal_type() in ("BooleanField", "NullBooleanField") and
117+            value in (0, 1)):
118+            value = bool(value)
119+        return value
120+
121     def date_extract_sql(self, lookup_type, field_name):
122         # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
123         if lookup_type == 'week_day':
124diff -r fe89b6629482 django/db/backends/mysql/compiler.py
125--- a/django/db/backends/mysql/compiler.py      Thu Jan 27 14:48:37 2011 +0000
126+++ b/django/db/backends/mysql/compiler.py      Fri Jan 28 12:59:14 2011 -0600
127@@ -5,10 +5,7 @@
128         values = []
129         index_extra_select = len(self.query.extra_select.keys())
130         for value, field in map(None, row[index_extra_select:], fields):
131-            if (field and field.get_internal_type() in ("BooleanField", "NullBooleanField") and
132-                value in (0, 1)):
133-                value = bool(value)
134-            values.append(value)
135+            values.append(self.query.convert_values(value, field, connection=self.connection))
136         return row[:index_extra_select] + tuple(values)
137 
138 class SQLInsertCompiler(compiler.SQLInsertCompiler, SQLCompiler):