Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactored OPERATOR_MAPPING so that it exists as django.db.connection…

….operators instead of django.db.backend.OPERATOR_MAPPING. Refs #5106

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5982 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 14db37319bd2a73b796d4b1a078fa7f162fc25e8 1 parent b3912d3
Adrian Holovaty authored August 20, 2007
29  django/db/backends/ado_mssql/base.py
@@ -84,6 +84,20 @@ def tablespace_sql(self, tablespace, inline=False):
84 84
 class DatabaseWrapper(BaseDatabaseWrapper):
85 85
     features = DatabaseFeatures()
86 86
     ops = DatabaseOperations()
  87
+    operators = {
  88
+        'exact': '= %s',
  89
+        'iexact': 'LIKE %s',
  90
+        'contains': 'LIKE %s',
  91
+        'icontains': 'LIKE %s',
  92
+        'gt': '> %s',
  93
+        'gte': '>= %s',
  94
+        'lt': '< %s',
  95
+        'lte': '<= %s',
  96
+        'startswith': 'LIKE %s',
  97
+        'endswith': 'LIKE %s',
  98
+        'istartswith': 'LIKE %s',
  99
+        'iendswith': 'LIKE %s',
  100
+    }
87 101
 
88 102
     def _cursor(self, settings):
89 103
         if self.connection is None:
@@ -96,18 +110,3 @@ def _cursor(self, settings):
96 110
             conn_string = "PROVIDER=SQLOLEDB;DATA SOURCE=%s;UID=%s;PWD=%s;DATABASE=%s" % (settings.DATABASE_HOST, settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME)
97 111
             self.connection = Database.connect(conn_string)
98 112
         return self.connection.cursor()
99  
-
100  
-OPERATOR_MAPPING = {
101  
-    'exact': '= %s',
102  
-    'iexact': 'LIKE %s',
103  
-    'contains': 'LIKE %s',
104  
-    'icontains': 'LIKE %s',
105  
-    'gt': '> %s',
106  
-    'gte': '>= %s',
107  
-    'lt': '< %s',
108  
-    'lte': '<= %s',
109  
-    'startswith': 'LIKE %s',
110  
-    'endswith': 'LIKE %s',
111  
-    'istartswith': 'LIKE %s',
112  
-    'iendswith': 'LIKE %s',
113  
-}
5  django/db/backends/dummy/base.py
@@ -26,8 +26,9 @@ def __getattr__(self, *args, **kwargs):
26 26
         complain()
27 27
 
28 28
 class DatabaseWrapper(object):
29  
-    ops = ComplainOnGetattr()
30 29
     features = ComplainOnGetattr()
  30
+    ops = ComplainOnGetattr()
  31
+    operators = {}
31 32
     cursor = complain
32 33
     _commit = complain
33 34
     _rollback = ignore
@@ -37,5 +38,3 @@ def __init__(self, **kwargs):
37 38
 
38 39
     def close(self):
39 40
         pass # close()
40  
-
41  
-OPERATOR_MAPPING = {}
33  django/db/backends/mysql/base.py
@@ -121,6 +121,22 @@ def sql_flush(self, style, tables, sequences):
121 121
 class DatabaseWrapper(BaseDatabaseWrapper):
122 122
     features = DatabaseFeatures()
123 123
     ops = DatabaseOperations()
  124
+    operators = {
  125
+        'exact': '= %s',
  126
+        'iexact': 'LIKE %s',
  127
+        'contains': 'LIKE BINARY %s',
  128
+        'icontains': 'LIKE %s',
  129
+        'regex': 'REGEXP BINARY %s',
  130
+        'iregex': 'REGEXP %s',
  131
+        'gt': '> %s',
  132
+        'gte': '>= %s',
  133
+        'lt': '< %s',
  134
+        'lte': '<= %s',
  135
+        'startswith': 'LIKE BINARY %s',
  136
+        'endswith': 'LIKE BINARY %s',
  137
+        'istartswith': 'LIKE %s',
  138
+        'iendswith': 'LIKE %s',
  139
+    }
124 140
 
125 141
     def __init__(self, **kwargs):
126 142
         super(DatabaseWrapper, self).__init__(**kwargs)
@@ -178,20 +194,3 @@ def get_server_version(self):
178 194
                 raise Exception('Unable to determine MySQL version from version string %r' % self.connection.get_server_info())
179 195
             self.server_version = tuple([int(x) for x in m.groups()])
180 196
         return self.server_version
181  
-
182  
-OPERATOR_MAPPING = {
183  
-    'exact': '= %s',
184  
-    'iexact': 'LIKE %s',
185  
-    'contains': 'LIKE BINARY %s',
186  
-    'icontains': 'LIKE %s',
187  
-    'regex': 'REGEXP BINARY %s',
188  
-    'iregex': 'REGEXP %s',
189  
-    'gt': '> %s',
190  
-    'gte': '>= %s',
191  
-    'lt': '< %s',
192  
-    'lte': '<= %s',
193  
-    'startswith': 'LIKE BINARY %s',
194  
-    'endswith': 'LIKE BINARY %s',
195  
-    'istartswith': 'LIKE %s',
196  
-    'iendswith': 'LIKE %s',
197  
-}
33  django/db/backends/mysql_old/base.py
@@ -131,6 +131,22 @@ def sql_flush(self, style, tables, sequences):
131 131
 class DatabaseWrapper(BaseDatabaseWrapper):
132 132
     features = DatabaseFeatures()
133 133
     ops = DatabaseOperations()
  134
+    operators = {
  135
+        'exact': '= %s',
  136
+        'iexact': 'LIKE %s',
  137
+        'contains': 'LIKE BINARY %s',
  138
+        'icontains': 'LIKE %s',
  139
+        'regex': 'REGEXP BINARY %s',
  140
+        'iregex': 'REGEXP %s',
  141
+        'gt': '> %s',
  142
+        'gte': '>= %s',
  143
+        'lt': '< %s',
  144
+        'lte': '<= %s',
  145
+        'startswith': 'LIKE BINARY %s',
  146
+        'endswith': 'LIKE BINARY %s',
  147
+        'istartswith': 'LIKE %s',
  148
+        'iendswith': 'LIKE %s',
  149
+    }
134 150
 
135 151
     def __init__(self, **kwargs):
136 152
         super(DatabaseWrapper, self).__init__(**kwargs)
@@ -197,20 +213,3 @@ def get_server_version(self):
197 213
                 raise Exception('Unable to determine MySQL version from version string %r' % self.connection.get_server_info())
198 214
             self.server_version = tuple([int(x) for x in m.groups()])
199 215
         return self.server_version
200  
-
201  
-OPERATOR_MAPPING = {
202  
-    'exact': '= %s',
203  
-    'iexact': 'LIKE %s',
204  
-    'contains': 'LIKE BINARY %s',
205  
-    'icontains': 'LIKE %s',
206  
-    'regex': 'REGEXP BINARY %s',
207  
-    'iregex': 'REGEXP %s',
208  
-    'gt': '> %s',
209  
-    'gte': '>= %s',
210  
-    'lt': '< %s',
211  
-    'lte': '<= %s',
212  
-    'startswith': 'LIKE BINARY %s',
213  
-    'endswith': 'LIKE BINARY %s',
214  
-    'istartswith': 'LIKE %s',
215  
-    'iendswith': 'LIKE %s',
216  
-}
29  django/db/backends/oracle/base.py
@@ -385,6 +385,20 @@ def tablespace_sql(self, tablespace, inline=False):
385 385
 class DatabaseWrapper(BaseDatabaseWrapper):
386 386
     features = DatabaseFeatures()
387 387
     ops = DatabaseOperations()
  388
+    operators = {
  389
+        'exact': '= %s',
  390
+        'iexact': '= UPPER(%s)',
  391
+        'contains': "LIKE %s ESCAPE '\\'",
  392
+        'icontains': "LIKE UPPER(%s) ESCAPE '\\'",
  393
+        'gt': '> %s',
  394
+        'gte': '>= %s',
  395
+        'lt': '< %s',
  396
+        'lte': '<= %s',
  397
+        'startswith': "LIKE %s ESCAPE '\\'",
  398
+        'endswith': "LIKE %s ESCAPE '\\'",
  399
+        'istartswith': "LIKE UPPER(%s) ESCAPE '\\'",
  400
+        'iendswith': "LIKE UPPER(%s) ESCAPE '\\'",
  401
+    }
388 402
 
389 403
     def _valid_connection(self):
390 404
         return self.connection is not None
@@ -498,18 +512,3 @@ def get_sequence_name(table):
498 512
 def get_trigger_name(table):
499 513
     name_length = DatabaseOperations().max_name_length() - 3
500 514
     return '%s_TR' % util.truncate_name(table, name_length).upper()
501  
-
502  
-OPERATOR_MAPPING = {
503  
-    'exact': '= %s',
504  
-    'iexact': '= UPPER(%s)',
505  
-    'contains': "LIKE %s ESCAPE '\\'",
506  
-    'icontains': "LIKE UPPER(%s) ESCAPE '\\'",
507  
-    'gt': '> %s',
508  
-    'gte': '>= %s',
509  
-    'lt': '< %s',
510  
-    'lte': '<= %s',
511  
-    'startswith': "LIKE %s ESCAPE '\\'",
512  
-    'endswith': "LIKE %s ESCAPE '\\'",
513  
-    'istartswith': "LIKE UPPER(%s) ESCAPE '\\'",
514  
-    'iendswith': "LIKE UPPER(%s) ESCAPE '\\'",
515  
-}
33  django/db/backends/postgresql/base.py
@@ -62,6 +62,22 @@ class DatabaseFeatures(BaseDatabaseFeatures):
62 62
 class DatabaseWrapper(BaseDatabaseWrapper):
63 63
     features = DatabaseFeatures()
64 64
     ops = DatabaseOperations()
  65
+    operators = {
  66
+        'exact': '= %s',
  67
+        'iexact': 'ILIKE %s',
  68
+        'contains': 'LIKE %s',
  69
+        'icontains': 'ILIKE %s',
  70
+        'regex': '~ %s',
  71
+        'iregex': '~* %s',
  72
+        'gt': '> %s',
  73
+        'gte': '>= %s',
  74
+        'lt': '< %s',
  75
+        'lte': '<= %s',
  76
+        'startswith': 'LIKE %s',
  77
+        'endswith': 'LIKE %s',
  78
+        'istartswith': 'ILIKE %s',
  79
+        'iendswith': 'ILIKE %s',
  80
+    }
65 81
 
66 82
     def _cursor(self, settings):
67 83
         set_tz = False
@@ -111,20 +127,3 @@ def typecast_string(s):
111 127
 Database.register_type(Database.new_type((16,), "BOOLEAN", util.typecast_boolean))
112 128
 Database.register_type(Database.new_type((1700,), "NUMERIC", util.typecast_decimal))
113 129
 Database.register_type(Database.new_type(Database.types[1043].values, 'STRING', typecast_string))
114  
-
115  
-OPERATOR_MAPPING = {
116  
-    'exact': '= %s',
117  
-    'iexact': 'ILIKE %s',
118  
-    'contains': 'LIKE %s',
119  
-    'icontains': 'ILIKE %s',
120  
-    'regex': '~ %s',
121  
-    'iregex': '~* %s',
122  
-    'gt': '> %s',
123  
-    'gte': '>= %s',
124  
-    'lt': '< %s',
125  
-    'lte': '<= %s',
126  
-    'startswith': 'LIKE %s',
127  
-    'endswith': 'LIKE %s',
128  
-    'istartswith': 'ILIKE %s',
129  
-    'iendswith': 'ILIKE %s',
130  
-}
33  django/db/backends/postgresql_psycopg2/base.py
@@ -24,6 +24,22 @@ class DatabaseFeatures(BaseDatabaseFeatures):
24 24
 class DatabaseWrapper(BaseDatabaseWrapper):
25 25
     features = DatabaseFeatures()
26 26
     ops = DatabaseOperations()
  27
+    operators = {
  28
+        'exact': '= %s',
  29
+        'iexact': 'ILIKE %s',
  30
+        'contains': 'LIKE %s',
  31
+        'icontains': 'ILIKE %s',
  32
+        'regex': '~ %s',
  33
+        'iregex': '~* %s',
  34
+        'gt': '> %s',
  35
+        'gte': '>= %s',
  36
+        'lt': '< %s',
  37
+        'lte': '<= %s',
  38
+        'startswith': 'LIKE %s',
  39
+        'endswith': 'LIKE %s',
  40
+        'istartswith': 'ILIKE %s',
  41
+        'iendswith': 'ILIKE %s',
  42
+    }
27 43
 
28 44
     def _cursor(self, settings):
29 45
         set_tz = False
@@ -52,20 +68,3 @@ def _cursor(self, settings):
52 68
             cursor.execute("SELECT version()")
53 69
             self.ops.postgres_version = [int(val) for val in cursor.fetchone()[0].split()[1].split('.')]
54 70
         return cursor
55  
-
56  
-OPERATOR_MAPPING = {
57  
-    'exact': '= %s',
58  
-    'iexact': 'ILIKE %s',
59  
-    'contains': 'LIKE %s',
60  
-    'icontains': 'ILIKE %s',
61  
-    'regex': '~ %s',
62  
-    'iregex': '~* %s',
63  
-    'gt': '> %s',
64  
-    'gte': '>= %s',
65  
-    'lt': '< %s',
66  
-    'lte': '<= %s',
67  
-    'startswith': 'LIKE %s',
68  
-    'endswith': 'LIKE %s',
69  
-    'istartswith': 'ILIKE %s',
70  
-    'iendswith': 'ILIKE %s',
71  
-}
41  django/db/backends/sqlite3/base.py
@@ -76,6 +76,26 @@ class DatabaseWrapper(BaseDatabaseWrapper):
76 76
     features = DatabaseFeatures()
77 77
     ops = DatabaseOperations()
78 78
 
  79
+    # SQLite requires LIKE statements to include an ESCAPE clause if the value
  80
+    # being escaped has a percent or underscore in it.
  81
+    # See http://www.sqlite.org/lang_expr.html for an explanation.
  82
+    operators = {
  83
+        'exact': '= %s',
  84
+        'iexact': "LIKE %s ESCAPE '\\'",
  85
+        'contains': "LIKE %s ESCAPE '\\'",
  86
+        'icontains': "LIKE %s ESCAPE '\\'",
  87
+        'regex': 'REGEXP %s',
  88
+        'iregex': "REGEXP '(?i)' || %s",
  89
+        'gt': '> %s',
  90
+        'gte': '>= %s',
  91
+        'lt': '< %s',
  92
+        'lte': '<= %s',
  93
+        'startswith': "LIKE %s ESCAPE '\\'",
  94
+        'endswith': "LIKE %s ESCAPE '\\'",
  95
+        'istartswith': "LIKE %s ESCAPE '\\'",
  96
+        'iendswith': "LIKE %s ESCAPE '\\'",
  97
+    }
  98
+
79 99
     def _cursor(self, settings):
80 100
         if self.connection is None:
81 101
             kwargs = {
@@ -140,24 +160,3 @@ def _sqlite_regexp(re_pattern, re_string):
140 160
         return bool(re.search(re_pattern, re_string))
141 161
     except:
142 162
         return False
143  
-
144  
-# SQLite requires LIKE statements to include an ESCAPE clause if the value
145  
-# being escaped has a percent or underscore in it.
146  
-# See http://www.sqlite.org/lang_expr.html for an explanation.
147  
-OPERATOR_MAPPING = {
148  
-    'exact': '= %s',
149  
-    'iexact': "LIKE %s ESCAPE '\\'",
150  
-    'contains': "LIKE %s ESCAPE '\\'",
151  
-    'icontains': "LIKE %s ESCAPE '\\'",
152  
-    'regex': 'REGEXP %s',
153  
-    'iregex': "REGEXP '(?i)' || %s",
154  
-    'gt': '> %s',
155  
-    'gte': '>= %s',
156  
-    'lt': '< %s',
157  
-    'lte': '<= %s',
158  
-    'startswith': "LIKE %s ESCAPE '\\'",
159  
-    'endswith': "LIKE %s ESCAPE '\\'",
160  
-    'istartswith': "LIKE %s ESCAPE '\\'",
161  
-    'iendswith': "LIKE %s ESCAPE '\\'",
162  
-}
163  
-
4  django/db/models/query.py
... ...
@@ -1,5 +1,5 @@
1 1
 from django.conf import settings
2  
-from django.db import backend, connection, transaction
  2
+from django.db import connection, transaction
3 3
 from django.db.models.fields import DateField, FieldDoesNotExist
4 4
 from django.db.models import signals, loading
5 5
 from django.dispatch import dispatcher
@@ -797,7 +797,7 @@ def get_where_clause(lookup_type, table_prefix, field_name, value, db_type):
797 797
     else:
798 798
         format = '%s %s'
799 799
     try:
800  
-        return format % (field_sql, backend.OPERATOR_MAPPING[lookup_type] % cast_sql)
  800
+        return format % (field_sql, connection.operators[lookup_type] % cast_sql)
801 801
     except KeyError:
802 802
         pass
803 803
     if lookup_type == 'in':

0 notes on commit 14db373

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