1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

[soc2010/query-refactor] MongoDB backend can now update saved objects.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/query-refactor@13340 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2010-06-09 16:23:26 +00:00
parent 9142ba5c4c
commit fe2bd63e37
4 changed files with 60 additions and 0 deletions

View File

@ -9,6 +9,7 @@ from django.utils.importlib import import_module
class DatabaseFeatures(object): class DatabaseFeatures(object):
interprets_empty_strings_as_nulls = False interprets_empty_strings_as_nulls = False
typed_columns = False
class DatabaseOperations(object): class DatabaseOperations(object):

View File

@ -5,6 +5,54 @@ class SQLCompiler(object):
self.connection = connection self.connection = connection
self.using = using self.using = using
def get_filters(self, where):
assert where.connector == "AND"
assert not where.negated
filters = {}
for child in where.children:
if isinstance(child, self.query.where_class):
# TODO: probably needs to check for dupe keys
filters.update(self.get_filters(child))
else:
field, val = self.make_atom(*child)
filters[field] = val
return filters
def make_atom(self, lhs, lookup_type, value_annotation, params_or_value):
assert lookup_type == "exact"
if hasattr(lhs, "process"):
lhs, params = lhs.process(lookup_type, params_or_value, self.connection)
else:
params = Field().get_db_prep_lookup(lookup_type, params_or_value,
connection=self.connection, prepared=True)
assert isinstance(lhs, (list, tuple))
table, column, _ = lhs
assert table == self.query.model._meta.db_table
if column == self.query.model._meta.pk.column:
column = "_id"
return column, params[0]
def build_query(self):
assert not self.query.aggregates
assert len(self.query.alias_map) == 1
assert self.query.default_cols
assert not self.query.distinct
assert not self.query.extra
assert not self.query.having
assert self.query.high_mark is None
assert not self.query.order_by
filters = self.get_filters(self.query.where)
return self.connection.db[self.query.model._meta.db_table].find(filters)
def has_results(self):
try:
self.build_query()[0]
except IndexError:
return False
else:
return True
class SQLInsertCompiler(SQLCompiler): class SQLInsertCompiler(SQLCompiler):
def insert(self, return_id=False): def insert(self, return_id=False):

View File

@ -215,6 +215,8 @@ class Field(object):
# mapped to one of the built-in Django field types. In this case, you # mapped to one of the built-in Django field types. In this case, you
# can implement db_type() instead of get_internal_type() to specify # can implement db_type() instead of get_internal_type() to specify
# exactly which wacky database column type you want to use. # exactly which wacky database column type you want to use.
if not getattr(connection.features, "typed_columns", True):
return None
data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_")
try: try:
return connection.creation.data_types[self.get_internal_type()] % data return connection.creation.data_types[self.get_internal_type()] % data

View File

@ -9,3 +9,12 @@ class MongoTestCase(TestCase):
self.assertTrue(b.pk is not None) self.assertTrue(b.pk is not None)
self.assertEqual(b.name, "Bruce Springsteen") self.assertEqual(b.name, "Bruce Springsteen")
self.assertTrue(b.good) self.assertTrue(b.good)
def test_update(self):
l = Artist.objects.create(name="Lady Gaga", good=True)
self.assertTrue(l.pk is not None)
pk = l.pk
# Whoops, we screwed up.
l.good = False
l.save()
self.assertEqual(l.pk, pk)