Skip to content
Newer
Older
100644 652 lines (535 sloc) 25.6 KB
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
1 import cPickle as pickle
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
2 import datetime
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
3
77a4e19 @wrwrwr Parenthesis imports and XXXs.
wrwrwr authored Feb 21, 2012
4 from django.db.backends import (
5 BaseDatabaseFeatures,
6 BaseDatabaseOperations,
7 BaseDatabaseWrapper,
8 BaseDatabaseClient,
9 BaseDatabaseValidation,
10 BaseDatabaseIntrospection)
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
11 from django.db.utils import DatabaseError
12 from django.utils.functional import Promise
13 from django.utils.safestring import EscapeString, EscapeUnicode, SafeString, \
14 SafeUnicode
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
15
16 from .creation import NonrelDatabaseCreation
17
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
18
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
19 class NonrelDatabaseFeatures(BaseDatabaseFeatures):
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
20
21 # NoSQL databases usually return a key after saving a new object.
7dadbd4 updated to new Django trunk version
Waldemar Kornewald authored Oct 21, 2010
22 can_return_id_from_insert = True
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
23
24 # TODO: Doesn't seem necessary in general, move to back-ends.
25 # Mongo: see PyMongo's FAQ; GAE: see: http://timezones.appspot.com/.
7dadbd4 updated to new Django trunk version
Waldemar Kornewald authored Oct 21, 2010
26 supports_date_lookup_using_string = False
27 supports_timezones = False
fe89e14 added connection arguments to NonrelDatabaseFeatures and NonrelDataba…
Waldemar Kornewald authored May 3, 2010
28
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
29 # Features that are commonly not available on nonrel databases.
cd22166 set supports_joins to False
Waldemar Kornewald authored Dec 23, 2010
30 supports_joins = False
84d2015 disabled select_related by default and added unit test to make sure e…
Waldemar Kornewald authored Dec 5, 2010
31 supports_select_related = False
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
32 supports_deleting_related_objects = False
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
33
34 # Having to decide whether to use an INSERT or an UPDATE query is
35 # specific to SQL-based databases.
36 distinguishes_insert_from_update = False
37
178bb34 @wrwrwr Removed two Django features having the same values on the parent (in …
wrwrwr authored Feb 11, 2012
38 # Can primary_key be used on any field? Without encoding usually
39 # only a limited set of types is acceptable for keys. This is a set
40 # of all field kinds (internal_types) for which the primary_key
41 # argument may be used.
42 # TODO: Use during model validation.
43 # TODO: Move to core and use to skip unsuitable Django tests.
44 supports_primary_key_on = set(NonrelDatabaseCreation.data_types.keys()) - \
45 set(('ForeignKey', 'OneToOneField', 'ManyToManyField', 'RawField',
46 'AbstractIterableField', 'ListField', 'SetField', 'DictField',
47 'EmbeddedModelField', 'BlobField'))
48
7dadbd4 updated to new Django trunk version
Waldemar Kornewald authored Oct 21, 2010
49 def _supports_transactions(self):
50 return False
51
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
52
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
53 class NonrelDatabaseOperations(BaseDatabaseOperations):
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
54 """
55 Override all database conversions normally done by fields (through
d9a8df5 @wrwrwr Improved some comments and exceptions.
wrwrwr authored Feb 29, 2012
56 `get_db_prep_value/save/lookup`) to make it possible to pass Python
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
57 values directly to the database layer. On the other hand, provide a
58 framework for making type-based conversions -- drivers of NoSQL
59 database either can work with Python objects directly, sometimes
60 representing one type using a another or expect everything encoded
61 in some specific manner.
62
63 Django normally handles conversions for the database by providing
d9a8df5 @wrwrwr Improved some comments and exceptions.
wrwrwr authored Feb 29, 2012
64 `BaseDatabaseOperations.value_to_db_*` / `convert_values` methods,
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
65 but there are some problems with them:
66 -- some preparations need to be done for all values or for values
67 of a particular "kind" (e.g. lazy objects evaluation or casting
68 strings wrappers to standard types);
69 -- some conversions need more info about the field or model the
70 value comes from (e.g. key conversions, embedded deconversion);
71 -- there are no value_to_db_* methods for some value types (bools);
72 -- we need to handle collecion fields (list, set, dict): they
73 need to differentiate between deconverting from database and
74 deserializing (so single to_python is inconvenient) and need to
d9a8df5 @wrwrwr Improved some comments and exceptions.
wrwrwr authored Feb 29, 2012
75 do some recursion, so a single `value_for_db` is better than one
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
76 method for each field kind.
89e2e26 @wrwrwr Decimal-to-string conversion that preserves comparisons (moved from G…
wrwrwr authored Feb 28, 2012
77 Don't use these standard methods in nonrel, `value_for/from_db` are
78 more elastic and keeping all conversions in one place makes the
79 code easier to analyse.
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
80
81 Please note, that after changes to type conversions, data saved
82 using preexisting methods needs to be handled; and also that Django
83 does not expect any special database driver exceptions, so any such
84 exceptions should be reraised as django.db.utils.DatabaseError.
d9a8df5 @wrwrwr Improved some comments and exceptions.
wrwrwr authored Feb 29, 2012
85
86 TODO: Consider replacing all `value_to_db_*` and `convert_values`
87 with just `BaseDatabaseOperations.value_for/from_db` and also
88 moving there code from `Field.get_db_prep_lookup` (and maybe
89 `RelatedField.get_db_prep_lookup`).
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
90 """
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
91
fe89e14 added connection arguments to NonrelDatabaseFeatures and NonrelDataba…
Waldemar Kornewald authored May 3, 2010
92 def __init__(self, connection):
93 self.connection = connection
94 super(NonrelDatabaseOperations, self).__init__()
95
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
96 def pk_default_value(self):
97 """
98 Returns None, to be interpreted by back-ends as a request to
99 generate a new key for an "inserted" object.
100 """
101 return None
102
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
103 def quote_name(self, name):
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
104 """
105 Does not do any quoting, as it is not needed for most NoSQL
106 databases.
107 """
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
108 return name
109
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
110 def prep_for_like_query(self, value):
111 """
112 Does no conversion, parent string-cast is SQL specific.
113 """
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
114 return value
115
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
116 def prep_for_iexact_query(self, value):
117 """
118 Does no conversion, parent string-cast is SQL specific.
119 """
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
120 return value
e8776b9 @wrwrwr Back to the original AutoField solution.
wrwrwr authored Mar 21, 2012
121
122 def value_to_db_auto(self, value):
123 """
124 Assuming that the database has its own key type, leaves any
125 conversions to the back-end.
126
127 This method is added my nonrel to allow various types to be
128 used for automatic primary keys. `AutoField.get_db_prep_value`
129 calls it to prepare field's value for the database.
130
131 Note that Django can pass a string representation of the value
132 instead of the value itself (after receiving it as a query
133 parameter for example), so you'll likely need to limit
134 your `AutoFields` in a way that makes `str(value)` reversible.
135
136 TODO: This could become a part of `value_for_db` if it makes
137 to Django (with a `field_kind` condition).
138 """
139 return value
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
140
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
141 def value_to_db_date(self, value):
142 """
fa21b9c @wrwrwr Match parent arguments of value_to_db_decimal. Improved comments for …
wrwrwr authored Feb 24, 2012
143 Unlike with SQL database clients, it's better to assume that
144 a date can be stored directly.
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
145 """
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
146 return value
147
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
148 def value_to_db_datetime(self, value):
149 """
fa21b9c @wrwrwr Match parent arguments of value_to_db_decimal. Improved comments for …
wrwrwr authored Feb 24, 2012
150 We may pass a datetime object to a database driver without
151 casting it to a string.
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
152 """
8c99092 added prep_for_iexact query, so it doesn't escape its value
Waldemar Kornewald authored Jul 6, 2010
153 return value
154
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
155 def value_to_db_time(self, value):
156 """
fa21b9c @wrwrwr Match parent arguments of value_to_db_decimal. Improved comments for …
wrwrwr authored Feb 24, 2012
157 Unlike with SQL database clients, we may assume that a time can
158 be stored directly.
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
159 """
160 return value
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
161
fa21b9c @wrwrwr Match parent arguments of value_to_db_decimal. Improved comments for …
wrwrwr authored Feb 24, 2012
162 def value_to_db_decimal(self, value, max_digits, decimal_places):
b4315fc @wrwrwr Added overrides for value_to_db_decimal and convert_values; made fetc…
wrwrwr authored Feb 9, 2012
163 """
fa21b9c @wrwrwr Match parent arguments of value_to_db_decimal. Improved comments for …
wrwrwr authored Feb 24, 2012
164 We may assume that a decimal can be passed to a NoSQL database
165 driver directly.
b4315fc @wrwrwr Added overrides for value_to_db_decimal and convert_values; made fetc…
wrwrwr authored Feb 9, 2012
166 """
167 return value
168
121f540 added default implementation for check_aggregate_support() which make…
Waldemar Kornewald authored Oct 13, 2010
169 def year_lookup_bounds(self, value):
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
170 """
171 Converts year bounds to datetime bounds as these can likely be
172 used directly, also adds one to the upper bound as it should be
173 natural to use one strict inequality for BETWEEN-like filters
174 for most nonrel back-ends.
175 """
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
176 return [datetime.datetime(value, 1, 1, 0, 0, 0, 0),
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
177 datetime.datetime(value + 1, 1, 1, 0, 0, 0, 0)]
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
178
b4315fc @wrwrwr Added overrides for value_to_db_decimal and convert_values; made fetc…
wrwrwr authored Feb 9, 2012
179 def convert_values(self, value, field):
180 """
fa21b9c @wrwrwr Match parent arguments of value_to_db_decimal. Improved comments for …
wrwrwr authored Feb 24, 2012
181 We may assume that values returned by the database are standard
182 Python types suitable to be passed to fields.
b4315fc @wrwrwr Added overrides for value_to_db_decimal and convert_values; made fetc…
wrwrwr authored Feb 9, 2012
183 """
184 return value
185
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
186 def check_aggregate_support(self, aggregate):
9405aea added simple DatabaseFeatures flag for string-based AutoField
Waldemar Kornewald authored May 16, 2010
187 """
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
188 Nonrel back-ends are only expected to implement COUNT in
189 general.
9405aea added simple DatabaseFeatures flag for string-based AutoField
Waldemar Kornewald authored May 16, 2010
190 """
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
191 from django.db.models.sql.aggregates import Count
192 if not isinstance(aggregate, Count):
193 raise NotImplementedError("This database does not support %r "
194 "aggregates." % type(aggregate))
195
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
196 def value_for_db(self, value, field, lookup=None):
197 """
198 Does type-conversions needed before storing a value in the
199 the database or using it as a filter parameter.
200
201 This is a convience wrapper that only precomputes field's kind
202 and a db_type for the field (or the primary key of the related
203 model for ForeignKeys etc.) and knows that arguments to the
204 `isnull` lookup (`True` or `False`) should not be converted,
205 while some other lookups take a list of arguments.
206 In the end, it calls `_value_for_db` to do the real work; you
207 should typically extend that method, but only call this one.
208
209 :param value: A value to be passed to the database driver
210 :param field: A field the value comes from
211 :param lookup: None if the value is being prepared for storage;
212 lookup type name, when its going to be used as a
213 filter argument
214 """
200bf14 @wrwrwr Allow a nice exception to be raised for a DataField month/day lookup …
wrwrwr authored Mar 14, 2012
215 field, field_kind, db_type = self._convert_as(field, lookup)
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
216
217 # Argument to the "isnull" lookup is just a boolean, while some
218 # other lookups take a list of values.
219 if lookup == 'isnull':
220 return value
221 elif lookup in ('in', 'range', 'year'):
222 return [self._value_for_db(subvalue, field,
223 field_kind, db_type, lookup)
224 for subvalue in value]
225 else:
226 return self._value_for_db(value, field,
227 field_kind, db_type, lookup)
228
229 def value_from_db(self, value, field):
230 """
231 Performs deconversions defined by `_value_from_db`.
232
233 :param value: A value received from the database client
234 :param field: A field the value is meant for
235 """
9c3ef50 @wrwrwr Also use related model's primary key instead of a RelatedField for co…
wrwrwr authored Mar 8, 2012
236 return self._value_from_db(value, *self._convert_as(field))
237
200bf14 @wrwrwr Allow a nice exception to be raised for a DataField month/day lookup …
wrwrwr authored Mar 14, 2012
238 def _convert_as(self, field, lookup=None):
9c3ef50 @wrwrwr Also use related model's primary key instead of a RelatedField for co…
wrwrwr authored Mar 8, 2012
239 """
240 Computes parameters that should be used for preparing the field
241 for the database or deconverting a database value for it.
242 """
243 # We need to compute db_type using the original field to allow
244 # GAE to use different storage for primary and foreign keys.
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
245 db_type = self.connection.creation.db_type(field)
9c3ef50 @wrwrwr Also use related model's primary key instead of a RelatedField for co…
wrwrwr authored Mar 8, 2012
246
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
247 if field.rel is not None:
248 field = field.rel.get_related_field()
249 field_kind = field.get_internal_type()
200bf14 @wrwrwr Allow a nice exception to be raised for a DataField month/day lookup …
wrwrwr authored Mar 14, 2012
250
251 # Values for standard month / day queries are integers.
252 if (field_kind in ('DateField', 'DateTimeField') and
253 lookup in ('month', 'day')):
254 db_type = 'integer'
255
9c3ef50 @wrwrwr Also use related model's primary key instead of a RelatedField for co…
wrwrwr authored Mar 8, 2012
256 return field, field_kind, db_type
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
257
258 def _value_for_db(self, value, field, field_kind, db_type, lookup):
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
259 """
260 Converts a standard Python value to a type that can be stored
261 or processed by the database driver.
262
263 This implementation only converts elements of iterables passed
264 by collection fields, evaluates Django's lazy objects and
265 marked strings and handles embedded models.
266 Currently, we assume that dict keys and column, model, module
267 names (strings) of embedded models require no conversion.
268
269 We need to know the field for two reasons:
270 -- to allow back-ends having separate key spaces for different
271 tables to create keys refering to the right table (which can
272 be the field model's table or the table of the model of the
273 instance a ForeignKey or other relation field points to).
274 -- to know the field of values passed by typed collection
275 fields and to use the proper fields when deconverting values
276 stored for typed embedding field.
277 Avoid using the field in any other way than by inspecting its
278 properties, it may not hold any value or hold a value other
279 than the one you're asked to convert.
280
281 You may want to call this method before doing other back-end
282 specific conversions.
283
284 :param value: A value to be passed to the database driver
285 :param field: A field having the same properties as the field
4cdcc3e @wrwrwr Use related model primary key field and its properties instead of the…
wrwrwr authored Feb 28, 2012
286 the value comes from; instead of related fields
287 you'll get the related model primary key, as the
288 value usually needs to be converted using its
289 properties
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
290 :param field_kind: Equal to field.get_internal_type()
291 :param db_type: Same as creation.db_type(field)
4cdcc3e @wrwrwr Use related model primary key field and its properties instead of the…
wrwrwr authored Feb 28, 2012
292 :param lookup: None if the value is being prepared for storage;
293 lookup type name, when its going to be used as a
294 filter argument
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
295 """
296
297 # Back-ends may want to store empty lists or dicts as None.
298 if value is None:
299 return None
300
301 # Force evaluation of lazy objects (e.g. lazy translation
302 # strings).
303 # Some back-ends pass values directly to the database driver,
304 # which may fail if it relies on type inspection and gets a
305 # functional proxy.
306 # This code relies on unicode cast in django.utils.functional
307 # just evaluating the wrapped function and doing nothing more.
bf5016b @wrwrwr Mark the proxy evaluation for reconsideration with some future change…
wrwrwr authored Mar 14, 2012
308 # TODO: This has been partially fixed in vanilla with:
309 # https://code.djangoproject.com/changeset/17698, however
310 # still fails for proxies in lookups; reconsider in 1.4.
311 # Also research cases of database operations not done
312 # through the sql.Query.
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
313 if isinstance(value, Promise):
314 value = unicode(value)
315
316 # Django wraps strings marked as safe or needed escaping,
317 # convert them to just strings for type-inspecting back-ends.
318 if isinstance(value, (SafeString, EscapeString)):
319 value = str(value)
320 elif isinstance(value, (SafeUnicode, EscapeUnicode)):
321 value = unicode(value)
322
323 # Convert elements of collection fields.
324 if field_kind in ('ListField', 'SetField', 'DictField',):
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
325 value = self._value_for_db_collection(value, field,
326 field_kind, db_type, lookup)
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
327
328 # Store model instance fields' values.
329 elif field_kind == 'EmbeddedModelField':
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
330 value = self._value_for_db_model(value, field,
331 field_kind, db_type, lookup)
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
332
333 return value
334
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
335 def _value_from_db(self, value, field, field_kind, db_type):
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
336 """
337 Converts a database type to a type acceptable by the field.
338
339 If you encoded a value for storage in the database, reverse the
340 encoding here. This implementation only recursively deconverts
341 elements of collection fields and handles embedded models.
342
343 You may want to call this method after any back-end specific
344 deconversions.
345
346 :param value: A value to be passed to the database driver
347 :param field: A field having the same properties as the field
348 the value comes from
349 :param field_kind: Equal to field.get_internal_type()
350 :param db_type: Same as creation.db_type(field)
351
352 Note: lookup values never get deconverted.
353 """
354
355 # We did not convert Nones.
356 if value is None:
357 return None
358
359 # Deconvert items or values of a collection field.
360 if field_kind in ('ListField', 'SetField', 'DictField',):
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
361 value = self._value_from_db_collection(value, field,
362 field_kind, db_type)
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
363
364 # Reinstatiate a serialized model.
365 elif field_kind == 'EmbeddedModelField':
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
366 value = self._value_from_db_model(value, field,
367 field_kind, db_type)
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
368
369 return value
370
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
371 def _value_for_db_collection(self, value, field, field_kind, db_type,
372 lookup):
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
373 """
374 Recursively converts values from AbstractIterableFields.
375
376 Note that collection lookup values are plain values rather than
377 lists, sets or dicts, but they still should be converted as a
378 collection item (assuming all items or values are converted in
379 the same way).
380
381 We base the conversion on field class / kind and assume some
382 knowledge about field internals (e.g. that the field has an
383 "item_field" property that gives the right subfield for any of
384 its values), to avoid adding a framework for determination of
385 parameters for items' conversions; we do the conversion here
386 rather than inside get_db_prep_save/lookup for symmetry with
387 deconversion (which can't be in to_python because the method is
388 also used for data not coming from the database).
389
390 Returns a list, set, dict, string or bytes according to the
391 db_type given.
392 If the "list" db_type used for DictField, a list with keys and
393 values interleaved will be returned (list of pairs is not good,
394 because lists / tuples may need conversion themselves; the list
395 may still be nested for dicts containing collections).
396 The "string" and "bytes" db_types use serialization with pickle
397 protocol 0 or 2 respectively.
398 If an unknown db_type is specified, returns a generator
399 yielding converted elements / pairs with converted values.
400 """
200bf14 @wrwrwr Allow a nice exception to be raised for a DataField month/day lookup …
wrwrwr authored Mar 14, 2012
401 subfield, subkind, db_subtype = self._convert_as(field.item_field,
402 lookup)
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
403
404 # Do convert filter parameters.
405 if lookup:
65ba16b Allow for querying empty lists
Cezar authored Mar 26, 2012
406 # Special case where we are looking for an empty list
407 if lookup == 'exact' and db_type == 'list' and value == u'[]':
408 return []
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
409 value = self._value_for_db(value, subfield,
410 subkind, db_subtype, lookup)
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
411
412 # Convert list/set items or dict values.
413 else:
414 if field_kind == 'DictField':
415
416 # Generator yielding pairs with converted values.
417 value = (
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
418 (key, self._value_for_db(subvalue, subfield,
419 subkind, db_subtype, lookup))
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
420 for key, subvalue in value.iteritems())
421
422 # Return just a dict, a once-flattened list;
423 if db_type == 'dict':
424 return dict(value)
425 elif db_type == 'list':
426 return list(item for pair in value for item in pair)
427
428 else:
429
430 # Generator producing converted items.
431 value = (
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
432 self._value_for_db(subvalue, subfield,
433 subkind, db_subtype, lookup)
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
434 for subvalue in value)
435
436 # "list" may be used for SetField.
437 if db_type in 'list':
438 return list(value)
439 elif db_type == 'set':
440 # assert field_kind != 'ListField'
441 return set(value)
442
443 # Pickled formats may be used for all collection fields,
444 # the fields "natural" type is serialized (something
445 # concrete is needed, pickle can't handle generators :-)
446 if db_type == 'bytes':
447 return pickle.dumps(field._type(value), protocol=2)
448 elif db_type == 'string':
449 return pickle.dumps(field._type(value))
450
451 # If nothing matched, pass the generator to the back-end.
452 return value
453
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
454 def _value_from_db_collection(self, value, field, field_kind, db_type):
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
455 """
456 Recursively deconverts values for AbstractIterableFields.
457
458 Assumes that all values in a collection can be deconverted
459 using a single field (Field.item_field, possibly a RawField).
460
461 Returns a value in a format proper for the field kind (the
462 value will normally not go through to_python).
463 """
9c3ef50 @wrwrwr Also use related model's primary key instead of a RelatedField for co…
wrwrwr authored Mar 8, 2012
464 subfield, subkind, db_subtype = self._convert_as(field.item_field)
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
465
466 # Unpickle (a dict) if a serialized storage is used.
467 if db_type == 'bytes' or db_type == 'string':
468 value = pickle.loads(value)
469
470 if field_kind == 'DictField':
471
472 # Generator yielding pairs with deconverted values, the
473 # "list" db_type stores keys and values interleaved.
474 if db_type == 'list':
475 value = zip(value[::2], value[1::2])
476 else:
477 value = value.iteritems()
478
479 # DictField needs to hold a dict.
480 return dict(
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
481 (key, self._value_from_db(subvalue, subfield,
482 subkind, db_subtype))
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
483 for key, subvalue in value)
484 else:
485
486 # Generator yielding deconverted items.
487 value = (
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
488 self._value_from_db(subvalue, subfield,
489 subkind, db_subtype)
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
490 for subvalue in value)
491
492 # The value will be available from the field without any
493 # further processing and it has to have the right type.
494 if field_kind == 'ListField':
495 return list(value)
496 elif field_kind == 'SetField':
497 return set(value)
498
499 # A new field kind? Maybe it can take a generator.
500 return value
501
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
502 def _value_for_db_model(self, value, field, field_kind, db_type, lookup):
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
503 """
504 Converts a field => value mapping received from an
505 EmbeddedModelField the format chosen for the field storage.
506
507 The embedded instance fields' values are also converted /
508 deconverted using value_for/from_db, so any back-end
509 conversions will be applied.
510
511 Returns (field.column, value) pairs, possibly augmented with
512 model info (to be able to deconvert the embedded instance for
513 untyped fields) encoded according to the db_type chosen.
514 If "dict" db_type is given a Python dict is returned.
515 If "list db_type is chosen a list with columns and values
516 interleaved will be returned. Note that just a single level of
517 the list is flattened, so it still may be nested -- when the
518 embedded instance holds other embedded models or collections).
519 Using "bytes" or "string" pickles the mapping using pickle
520 protocol 0 or 2 respectively.
521 If an unknown db_type is used a generator yielding (column,
522 value) pairs with values converted will be returned.
523
524 TODO: How should EmbeddedModelField lookups work?
525 """
526 if lookup:
527 # raise NotImplementedError("Needs specification.")
528 return value
529
530 # Convert using proper instance field's info, change keys from
531 # fields to columns.
9c3ef50 @wrwrwr Also use related model's primary key instead of a RelatedField for co…
wrwrwr authored Mar 8, 2012
532 # TODO/XXX: Arguments order due to Python 2.5 compatibility.
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
533 value = (
9c3ef50 @wrwrwr Also use related model's primary key instead of a RelatedField for co…
wrwrwr authored Mar 8, 2012
534 (subfield.column, self._value_for_db(
200bf14 @wrwrwr Allow a nice exception to be raised for a DataField month/day lookup …
wrwrwr authored Mar 14, 2012
535 subvalue, lookup=lookup, *self._convert_as(subfield, lookup)))
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
536 for subfield, subvalue in value.iteritems())
537
538 # Cast to a dict, interleave columns with values on a list,
539 # serialize, or return a generator.
540 if db_type == 'dict':
541 value = dict(value)
542 elif db_type == 'list':
543 value = list(item for pair in value for item in pair)
544 elif db_type == 'bytes':
545 value = pickle.dumps(dict(value), protocol=2)
546 elif db_type == 'string':
547 value = pickle.dumps(dict(value))
548
549 return value
550
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
551 def _value_from_db_model(self, value, field, field_kind, db_type):
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
552 """
553 Deconverts values stored for EmbeddedModelFields.
554
555 Embedded instances are stored as a (column, value) pairs in a
556 dict, a single-flattened list or a serialized dict.
557
558 Returns a tuple with model class and field.attname => value
559 mapping.
560 """
561
562 # Separate keys from values and create a dict or unpickle one.
563 if db_type == 'list':
564 value = dict(zip(value[::2], value[1::2]))
565 elif db_type == 'bytes' or db_type == 'string':
566 value = pickle.loads(value)
567
568 # Let untyped fields determine the embedded instance's model.
fa21b9c @wrwrwr Match parent arguments of value_to_db_decimal. Improved comments for …
wrwrwr authored Feb 24, 2012
569 embedded_model = field.stored_model(value)
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
570
571 # Deconvert fields' values and prepare a dict that can be used
572 # to initialize a model (by changing keys from columns to
573 # attribute names).
574 return embedded_model, dict(
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
575 (subfield.attname, self._value_from_db(
9c3ef50 @wrwrwr Also use related model's primary key instead of a RelatedField for co…
wrwrwr authored Mar 8, 2012
576 value[subfield.column], *self._convert_as(subfield)))
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
577 for subfield in embedded_model._meta.fields
578 if subfield.column in value)
579
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
580 def _value_for_db_key(self, value, field_kind):
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
581 """
582 Converts value to be used as a key to an acceptable type.
583 On default we do no encoding, only allowing key values directly
584 acceptable by the database for its key type (if any).
585
586 The conversion has to be reversible given the field type,
587 encoding should preserve comparisons.
588
589 Use this to expand the set of fields that can be used as
590 primary keys, return value suitable for a key rather than
591 a key itself.
592 """
593 raise DatabaseError(
594 "%s may not be used as primary key field." % field_kind)
595
cf820c0 @wrwrwr Moved value_for/from_db wrappers to DatabaseOperations too, so things…
wrwrwr authored Mar 8, 2012
596 def _value_from_db_key(self, value, field_kind):
602c739 @wrwrwr Base implementation of database conversions (formerly convert_value_f…
wrwrwr authored Feb 13, 2012
597 """
598 Decodes a value previously encoded for a key.
599 """
600 return value
601
9405aea added simple DatabaseFeatures flag for string-based AutoField
Waldemar Kornewald authored May 16, 2010
602
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
603 class NonrelDatabaseClient(BaseDatabaseClient):
604 pass
605
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
606
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
607 class NonrelDatabaseValidation(BaseDatabaseValidation):
608 pass
609
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
610
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
611 class NonrelDatabaseIntrospection(BaseDatabaseIntrospection):
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
612
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
613 def table_names(self):
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
614 """
615 Returns a list of names of all tables that exist in the
616 database.
617 """
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
618 return self.django_table_names()
619
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
620
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
621 class FakeCursor(object):
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
622
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
623 def __getattribute__(self, name):
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
624 raise NotImplementedError("Cursors are not supported.")
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
625
626 def __setattr__(self, name, value):
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
627 raise NotImplementedError("Cursors are not supported.")
628
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
629
630 class NonrelDatabaseWrapper(BaseDatabaseWrapper):
214985e @wrwrwr Corrections suggested by the pep8 verifier with (some) exceptions for…
wrwrwr authored Feb 8, 2012
631
d02d111 fixed SQLQuery.as_sql() support
Waldemar Kornewald authored Oct 27, 2011
632 # These fake operators are required for SQLQuery.as_sql() support.
633 operators = {
634 'exact': '= %s',
635 'iexact': '= UPPER(%s)',
636 'contains': 'LIKE %s',
637 'icontains': 'LIKE UPPER(%s)',
638 'regex': '~ %s',
639 'iregex': '~* %s',
640 'gt': '> %s',
641 'gte': '>= %s',
642 'lt': '< %s',
643 'lte': '<= %s',
644 'startswith': 'LIKE %s',
645 'endswith': 'LIKE %s',
646 'istartswith': 'LIKE UPPER(%s)',
647 'iendswith': 'LIKE UPPER(%s)',
648 }
649
e4e5ae3 added more base classes for non-relational backends
Waldemar Kornewald authored Apr 2, 2010
650 def _cursor(self):
651 return FakeCursor()
Something went wrong with that request. Please try again.