Source

django-mongodb / django_mongodb / base.py

Full commit
#-*- coding:utf-8 -*-

# Copyright (c) 2009, Kirill Mavreshko (kimavr@gmail.com)
# All rights reserved.

# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are met:

#    * Redistributions of source code must retain the above copyright notice, 
#      this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice,
#      this list of conditions and the following disclaimer in the documentation
#      and/or other materials provided with the distribution.
#    * The name of the author may not be used to endorse or promote products
#      derived from this software without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

"""
django_mongodb.base -- Django to MongoDB (http://www.mongodb.org) database backend.
"""

from django.core.exceptions import ImproperlyConfigured
from django.db.backends import *
from django.db.backends.creation import BaseDatabaseCreation

def complain(*args, **kwargs):
    raise ImproperlyConfigured("django_mongodb is not supporting all Django ORM abilities.")

def ignore(*args, **kwargs):
    pass

class DatabaseError(Exception):
    pass

class IntegrityError(DatabaseError):
    pass

class MongoDatabaseOperations(BaseDatabaseOperations):
    quote_name = complain

    def query_class(self, DefaultQueryClass):
        from django_mongodb.query import MongoDbQuery
        return MongoDbQuery

    def quote_name(self, name):
        return name

    def value_to_db_datetime(self, value):
        return value

class DatabaseClient(BaseDatabaseClient):
    runshell = complain

class DatabaseIntrospection(BaseDatabaseIntrospection):
    get_table_list = complain
    get_table_description = complain
    get_relations = complain
    get_indexes = complain

class MongoDatabaseFeatures(BaseDatabaseFeatures):
    uses_custom_query_class = True

    allows_group_by_pk = False
    # True if django.db.backend.utils.typecast_timestamp is used on values
    # returned from dates() calls.
    needs_datetime_string_cast = True
    
    empty_fetchmany_value = []
    update_can_self_select = True
    interprets_empty_strings_as_nulls = False
    can_use_chunked_reads = False
    can_return_id_from_insert = True
    uses_autocommit = True
    uses_savepoints = False
    # If True, don't use integer foreign keys referring to, e.g., positive
    # integer primary keys.
    related_fields_match_type = False

class DatabaseWrapper(object):
    cursor = complain
    _commit = ignore
    _rollback = ignore
    _enter_transaction_management = ignore
    _leave_transaction_management = ignore
    _savepoint = ignore
    _savepoint_commit = ignore

    operators = {}

    def __init__(self, settings_dict):
        self.features = MongoDatabaseFeatures()
        self.ops = MongoDatabaseOperations()
        self.client = DatabaseClient(self)
        self.creation = BaseDatabaseCreation(self)
        self.introspection = DatabaseIntrospection(self)
        self.validation = BaseDatabaseValidation()

        self.connection = None
        self.queries = []
        self.settings_dict = settings_dict

    def close(self):
        if self.connection is not None:
            del self.connection
            self.connection = None

    def cursor(self):
        from pymongo.connection import Connection
        if self.connection is None:
            self.connection = Connection(self.settings_dict['DATABASE_HOST'], int(self.settings_dict['DATABASE_PORT']))
        return getattr(self.connection, self.settings_dict['DATABASE_NAME'])