mirror of
https://github.com/django/django.git
synced 2025-07-05 18:29:11 +00:00
[soc2010/query-refactor] Refactor, and implement a few more methods to get models saving under the mongodb backend.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/query-refactor@13332 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
4c45befaad
commit
1f9e422b8d
@ -3,6 +3,7 @@ from pymongo import Connection
|
|||||||
from django.db.backends import BaseDatabaseWrapper
|
from django.db.backends import BaseDatabaseWrapper
|
||||||
from django.db.backends.signals import connection_created
|
from django.db.backends.signals import connection_created
|
||||||
from django.contrib.mongodb.creation import DatabaseCreation
|
from django.contrib.mongodb.creation import DatabaseCreation
|
||||||
|
from django.utils.importlib import import_module
|
||||||
|
|
||||||
|
|
||||||
class DatabaseFeatures(object):
|
class DatabaseFeatures(object):
|
||||||
@ -10,12 +11,29 @@ class DatabaseFeatures(object):
|
|||||||
|
|
||||||
|
|
||||||
class DatabaseOperations(object):
|
class DatabaseOperations(object):
|
||||||
|
compiler_module = "django.contrib.mongodb.compiler"
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self._cache = {}
|
||||||
|
|
||||||
def max_name_length(self):
|
def max_name_length(self):
|
||||||
return 254
|
return 254
|
||||||
|
|
||||||
def value_to_db_datetime(self, value):
|
def value_to_db_datetime(self, value):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
# TODO: this is copy pasta, fix the abstractions in Ops
|
||||||
|
def compiler(self, compiler_name):
|
||||||
|
"""
|
||||||
|
Returns the SQLCompiler class corresponding to the given name,
|
||||||
|
in the namespace corresponding to the `compiler_module` attribute
|
||||||
|
on this backend.
|
||||||
|
"""
|
||||||
|
if compiler_name not in self._cache:
|
||||||
|
self._cache[compiler_name] = getattr(
|
||||||
|
import_module(self.compiler_module), compiler_name
|
||||||
|
)
|
||||||
|
return self._cache[compiler_name]
|
||||||
|
|
||||||
class DatabaseWrapper(BaseDatabaseWrapper):
|
class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
@ -33,6 +51,14 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
|||||||
connection_created.send(sender=self.__class__)
|
connection_created.send(sender=self.__class__)
|
||||||
return self._connection
|
return self._connection
|
||||||
|
|
||||||
|
@property
|
||||||
|
def db(self):
|
||||||
|
return self.connection[self.settings_dict["NAME"]]
|
||||||
|
|
||||||
def _rollback(self):
|
def _rollback(self):
|
||||||
# TODO: ???
|
# TODO: ???
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def _commit(self):
|
||||||
|
# TODO: ???
|
||||||
|
pass
|
||||||
|
15
django/contrib/mongodb/compiler.py
Normal file
15
django/contrib/mongodb/compiler.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# TODO: ...
|
||||||
|
class SQLCompiler(object):
|
||||||
|
def __init__(self, query, connection, using):
|
||||||
|
self.query = query
|
||||||
|
self.connection = connection
|
||||||
|
self.using = using
|
||||||
|
|
||||||
|
|
||||||
|
class SQLInsertCompiler(SQLCompiler):
|
||||||
|
def insert(self, return_id=False):
|
||||||
|
values = dict([
|
||||||
|
(c, v)
|
||||||
|
for c, v in zip(self.query.columns, self.query.params)
|
||||||
|
])
|
||||||
|
return self.connection.db[self.query.model._meta.db_table].insert(values)
|
@ -1476,4 +1476,4 @@ def insert_query(model, values, return_id=False, raw_values=False, using=None):
|
|||||||
"""
|
"""
|
||||||
query = sql.InsertQuery(model)
|
query = sql.InsertQuery(model)
|
||||||
query.insert_values(values, raw_values)
|
query.insert_values(values, raw_values)
|
||||||
return query.get_compiler(using=using).execute_sql(return_id)
|
return query.get_compiler(using=using).insert(return_id)
|
||||||
|
@ -788,6 +788,9 @@ class SQLInsertCompiler(SQLCompiler):
|
|||||||
return self.connection.ops.last_insert_id(cursor,
|
return self.connection.ops.last_insert_id(cursor,
|
||||||
self.query.model._meta.db_table, self.query.model._meta.pk.column)
|
self.query.model._meta.db_table, self.query.model._meta.pk.column)
|
||||||
|
|
||||||
|
def insert(self, *args, **kwargs):
|
||||||
|
return self.execute_sql(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class SQLDeleteCompiler(SQLCompiler):
|
class SQLDeleteCompiler(SQLCompiler):
|
||||||
def as_sql(self):
|
def as_sql(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user