web/lib/django/db/backends/__init__.py
changeset 38 77b6da96e6f1
equal deleted inserted replaced
37:8d941af65caf 38:77b6da96e6f1
       
     1 import decimal
       
     2 from threading import local
       
     3 
       
     4 from django.db import DEFAULT_DB_ALIAS
       
     5 from django.db.backends import util
       
     6 from django.utils import datetime_safe
       
     7 from django.utils.importlib import import_module
       
     8 
       
     9 class BaseDatabaseWrapper(local):
       
    10     """
       
    11     Represents a database connection.
       
    12     """
       
    13     ops = None
       
    14 
       
    15     def __init__(self, settings_dict, alias=DEFAULT_DB_ALIAS):
       
    16         # `settings_dict` should be a dictionary containing keys such as
       
    17         # NAME, USER, etc. It's called `settings_dict` instead of `settings`
       
    18         # to disambiguate it from Django settings modules.
       
    19         self.connection = None
       
    20         self.queries = []
       
    21         self.settings_dict = settings_dict
       
    22         self.alias = alias
       
    23 
       
    24     def __eq__(self, other):
       
    25         return self.settings_dict == other.settings_dict
       
    26 
       
    27     def __ne__(self, other):
       
    28         return not self == other
       
    29 
       
    30     def _commit(self):
       
    31         if self.connection is not None:
       
    32             return self.connection.commit()
       
    33 
       
    34     def _rollback(self):
       
    35         if self.connection is not None:
       
    36             return self.connection.rollback()
       
    37 
       
    38     def _enter_transaction_management(self, managed):
       
    39         """
       
    40         A hook for backend-specific changes required when entering manual
       
    41         transaction handling.
       
    42         """
       
    43         pass
       
    44 
       
    45     def _leave_transaction_management(self, managed):
       
    46         """
       
    47         A hook for backend-specific changes required when leaving manual
       
    48         transaction handling. Will usually be implemented only when
       
    49         _enter_transaction_management() is also required.
       
    50         """
       
    51         pass
       
    52 
       
    53     def _savepoint(self, sid):
       
    54         if not self.features.uses_savepoints:
       
    55             return
       
    56         self.cursor().execute(self.ops.savepoint_create_sql(sid))
       
    57 
       
    58     def _savepoint_rollback(self, sid):
       
    59         if not self.features.uses_savepoints:
       
    60             return
       
    61         self.cursor().execute(self.ops.savepoint_rollback_sql(sid))
       
    62 
       
    63     def _savepoint_commit(self, sid):
       
    64         if not self.features.uses_savepoints:
       
    65             return
       
    66         self.cursor().execute(self.ops.savepoint_commit_sql(sid))
       
    67 
       
    68     def close(self):
       
    69         if self.connection is not None:
       
    70             self.connection.close()
       
    71             self.connection = None
       
    72 
       
    73     def cursor(self):
       
    74         from django.conf import settings
       
    75         cursor = self._cursor()
       
    76         if settings.DEBUG:
       
    77             return self.make_debug_cursor(cursor)
       
    78         return cursor
       
    79 
       
    80     def make_debug_cursor(self, cursor):
       
    81         return util.CursorDebugWrapper(cursor, self)
       
    82 
       
    83 class BaseDatabaseFeatures(object):
       
    84     allows_group_by_pk = False
       
    85     # True if django.db.backend.utils.typecast_timestamp is used on values
       
    86     # returned from dates() calls.
       
    87     needs_datetime_string_cast = True
       
    88     empty_fetchmany_value = []
       
    89     update_can_self_select = True
       
    90     interprets_empty_strings_as_nulls = False
       
    91     can_use_chunked_reads = True
       
    92     can_return_id_from_insert = False
       
    93     uses_autocommit = False
       
    94     uses_savepoints = False
       
    95     # If True, don't use integer foreign keys referring to, e.g., positive
       
    96     # integer primary keys.
       
    97     related_fields_match_type = False
       
    98     allow_sliced_subqueries = True
       
    99 
       
   100 class BaseDatabaseOperations(object):
       
   101     """
       
   102     This class encapsulates all backend-specific differences, such as the way
       
   103     a backend performs ordering or calculates the ID of a recently-inserted
       
   104     row.
       
   105     """
       
   106     compiler_module = "django.db.models.sql.compiler"
       
   107 
       
   108     def __init__(self):
       
   109         self._cache = {}
       
   110 
       
   111     def autoinc_sql(self, table, column):
       
   112         """
       
   113         Returns any SQL needed to support auto-incrementing primary keys, or
       
   114         None if no SQL is necessary.
       
   115 
       
   116         This SQL is executed when a table is created.
       
   117         """
       
   118         return None
       
   119 
       
   120     def date_extract_sql(self, lookup_type, field_name):
       
   121         """
       
   122         Given a lookup_type of 'year', 'month' or 'day', returns the SQL that
       
   123         extracts a value from the given date field field_name.
       
   124         """
       
   125         raise NotImplementedError()
       
   126 
       
   127     def date_trunc_sql(self, lookup_type, field_name):
       
   128         """
       
   129         Given a lookup_type of 'year', 'month' or 'day', returns the SQL that
       
   130         truncates the given date field field_name to a DATE object with only
       
   131         the given specificity.
       
   132         """
       
   133         raise NotImplementedError()
       
   134 
       
   135     def datetime_cast_sql(self):
       
   136         """
       
   137         Returns the SQL necessary to cast a datetime value so that it will be
       
   138         retrieved as a Python datetime object instead of a string.
       
   139 
       
   140         This SQL should include a '%s' in place of the field's name.
       
   141         """
       
   142         return "%s"
       
   143 
       
   144     def deferrable_sql(self):
       
   145         """
       
   146         Returns the SQL necessary to make a constraint "initially deferred"
       
   147         during a CREATE TABLE statement.
       
   148         """
       
   149         return ''
       
   150 
       
   151     def drop_foreignkey_sql(self):
       
   152         """
       
   153         Returns the SQL command that drops a foreign key.
       
   154         """
       
   155         return "DROP CONSTRAINT"
       
   156 
       
   157     def drop_sequence_sql(self, table):
       
   158         """
       
   159         Returns any SQL necessary to drop the sequence for the given table.
       
   160         Returns None if no SQL is necessary.
       
   161         """
       
   162         return None
       
   163 
       
   164     def fetch_returned_insert_id(self, cursor):
       
   165         """
       
   166         Given a cursor object that has just performed an INSERT...RETURNING
       
   167         statement into a table that has an auto-incrementing ID, returns the
       
   168         newly created ID.
       
   169         """
       
   170         return cursor.fetchone()[0]
       
   171 
       
   172     def field_cast_sql(self, db_type):
       
   173         """
       
   174         Given a column type (e.g. 'BLOB', 'VARCHAR'), returns the SQL necessary
       
   175         to cast it before using it in a WHERE statement. Note that the
       
   176         resulting string should contain a '%s' placeholder for the column being
       
   177         searched against.
       
   178         """
       
   179         return '%s'
       
   180 
       
   181     def force_no_ordering(self):
       
   182         """
       
   183         Returns a list used in the "ORDER BY" clause to force no ordering at
       
   184         all. Returning an empty list means that nothing will be included in the
       
   185         ordering.
       
   186         """
       
   187         return []
       
   188 
       
   189     def fulltext_search_sql(self, field_name):
       
   190         """
       
   191         Returns the SQL WHERE clause to use in order to perform a full-text
       
   192         search of the given field_name. Note that the resulting string should
       
   193         contain a '%s' placeholder for the value being searched against.
       
   194         """
       
   195         raise NotImplementedError('Full-text search is not implemented for this database backend')
       
   196 
       
   197     def last_executed_query(self, cursor, sql, params):
       
   198         """
       
   199         Returns a string of the query last executed by the given cursor, with
       
   200         placeholders replaced with actual values.
       
   201 
       
   202         `sql` is the raw query containing placeholders, and `params` is the
       
   203         sequence of parameters. These are used by default, but this method
       
   204         exists for database backends to provide a better implementation
       
   205         according to their own quoting schemes.
       
   206         """
       
   207         from django.utils.encoding import smart_unicode, force_unicode
       
   208 
       
   209         # Convert params to contain Unicode values.
       
   210         to_unicode = lambda s: force_unicode(s, strings_only=True, errors='replace')
       
   211         if isinstance(params, (list, tuple)):
       
   212             u_params = tuple([to_unicode(val) for val in params])
       
   213         else:
       
   214             u_params = dict([(to_unicode(k), to_unicode(v)) for k, v in params.items()])
       
   215 
       
   216         return smart_unicode(sql) % u_params
       
   217 
       
   218     def last_insert_id(self, cursor, table_name, pk_name):
       
   219         """
       
   220         Given a cursor object that has just performed an INSERT statement into
       
   221         a table that has an auto-incrementing ID, returns the newly created ID.
       
   222 
       
   223         This method also receives the table name and the name of the primary-key
       
   224         column.
       
   225         """
       
   226         return cursor.lastrowid
       
   227 
       
   228     def lookup_cast(self, lookup_type):
       
   229         """
       
   230         Returns the string to use in a query when performing lookups
       
   231         ("contains", "like", etc). The resulting string should contain a '%s'
       
   232         placeholder for the column being searched against.
       
   233         """
       
   234         return "%s"
       
   235 
       
   236     def max_name_length(self):
       
   237         """
       
   238         Returns the maximum length of table and column names, or None if there
       
   239         is no limit.
       
   240         """
       
   241         return None
       
   242 
       
   243     def no_limit_value(self):
       
   244         """
       
   245         Returns the value to use for the LIMIT when we are wanting "LIMIT
       
   246         infinity". Returns None if the limit clause can be omitted in this case.
       
   247         """
       
   248         raise NotImplementedError
       
   249 
       
   250     def pk_default_value(self):
       
   251         """
       
   252         Returns the value to use during an INSERT statement to specify that
       
   253         the field should use its default value.
       
   254         """
       
   255         return 'DEFAULT'
       
   256 
       
   257     def process_clob(self, value):
       
   258         """
       
   259         Returns the value of a CLOB column, for backends that return a locator
       
   260         object that requires additional processing.
       
   261         """
       
   262         return value
       
   263 
       
   264     def return_insert_id(self):
       
   265         """
       
   266         For backends that support returning the last insert ID as part
       
   267         of an insert query, this method returns the SQL and params to
       
   268         append to the INSERT query. The returned fragment should
       
   269         contain a format string to hold the appropriate column.
       
   270         """
       
   271         pass
       
   272 
       
   273     def compiler(self, compiler_name):
       
   274         """
       
   275         Returns the SQLCompiler class corresponding to the given name,
       
   276         in the namespace corresponding to the `compiler_module` attribute
       
   277         on this backend.
       
   278         """
       
   279         if compiler_name not in self._cache:
       
   280             self._cache[compiler_name] = getattr(
       
   281                 import_module(self.compiler_module), compiler_name
       
   282             )
       
   283         return self._cache[compiler_name]
       
   284 
       
   285     def quote_name(self, name):
       
   286         """
       
   287         Returns a quoted version of the given table, index or column name. Does
       
   288         not quote the given name if it's already been quoted.
       
   289         """
       
   290         raise NotImplementedError()
       
   291 
       
   292     def random_function_sql(self):
       
   293         """
       
   294         Returns a SQL expression that returns a random value.
       
   295         """
       
   296         return 'RANDOM()'
       
   297 
       
   298     def regex_lookup(self, lookup_type):
       
   299         """
       
   300         Returns the string to use in a query when performing regular expression
       
   301         lookups (using "regex" or "iregex"). The resulting string should
       
   302         contain a '%s' placeholder for the column being searched against.
       
   303 
       
   304         If the feature is not supported (or part of it is not supported), a
       
   305         NotImplementedError exception can be raised.
       
   306         """
       
   307         raise NotImplementedError
       
   308 
       
   309     def savepoint_create_sql(self, sid):
       
   310         """
       
   311         Returns the SQL for starting a new savepoint. Only required if the
       
   312         "uses_savepoints" feature is True. The "sid" parameter is a string
       
   313         for the savepoint id.
       
   314         """
       
   315         raise NotImplementedError
       
   316 
       
   317     def savepoint_commit_sql(self, sid):
       
   318         """
       
   319         Returns the SQL for committing the given savepoint.
       
   320         """
       
   321         raise NotImplementedError
       
   322 
       
   323     def savepoint_rollback_sql(self, sid):
       
   324         """
       
   325         Returns the SQL for rolling back the given savepoint.
       
   326         """
       
   327         raise NotImplementedError
       
   328 
       
   329     def sql_flush(self, style, tables, sequences):
       
   330         """
       
   331         Returns a list of SQL statements required to remove all data from
       
   332         the given database tables (without actually removing the tables
       
   333         themselves).
       
   334 
       
   335         The `style` argument is a Style object as returned by either
       
   336         color_style() or no_style() in django.core.management.color.
       
   337         """
       
   338         raise NotImplementedError()
       
   339 
       
   340     def sequence_reset_sql(self, style, model_list):
       
   341         """
       
   342         Returns a list of the SQL statements required to reset sequences for
       
   343         the given models.
       
   344 
       
   345         The `style` argument is a Style object as returned by either
       
   346         color_style() or no_style() in django.core.management.color.
       
   347         """
       
   348         return [] # No sequence reset required by default.
       
   349 
       
   350     def start_transaction_sql(self):
       
   351         """
       
   352         Returns the SQL statement required to start a transaction.
       
   353         """
       
   354         return "BEGIN;"
       
   355 
       
   356     def tablespace_sql(self, tablespace, inline=False):
       
   357         """
       
   358         Returns the SQL that will be appended to tables or rows to define
       
   359         a tablespace. Returns '' if the backend doesn't use tablespaces.
       
   360         """
       
   361         return ''
       
   362 
       
   363     def prep_for_like_query(self, x):
       
   364         """Prepares a value for use in a LIKE query."""
       
   365         from django.utils.encoding import smart_unicode
       
   366         return smart_unicode(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_")
       
   367 
       
   368     # Same as prep_for_like_query(), but called for "iexact" matches, which
       
   369     # need not necessarily be implemented using "LIKE" in the backend.
       
   370     prep_for_iexact_query = prep_for_like_query
       
   371 
       
   372     def value_to_db_date(self, value):
       
   373         """
       
   374         Transform a date value to an object compatible with what is expected
       
   375         by the backend driver for date columns.
       
   376         """
       
   377         if value is None:
       
   378             return None
       
   379         return datetime_safe.new_date(value).strftime('%Y-%m-%d')
       
   380 
       
   381     def value_to_db_datetime(self, value):
       
   382         """
       
   383         Transform a datetime value to an object compatible with what is expected
       
   384         by the backend driver for datetime columns.
       
   385         """
       
   386         if value is None:
       
   387             return None
       
   388         return unicode(value)
       
   389 
       
   390     def value_to_db_time(self, value):
       
   391         """
       
   392         Transform a datetime value to an object compatible with what is expected
       
   393         by the backend driver for time columns.
       
   394         """
       
   395         if value is None:
       
   396             return None
       
   397         return unicode(value)
       
   398 
       
   399     def value_to_db_decimal(self, value, max_digits, decimal_places):
       
   400         """
       
   401         Transform a decimal.Decimal value to an object compatible with what is
       
   402         expected by the backend driver for decimal (numeric) columns.
       
   403         """
       
   404         if value is None:
       
   405             return None
       
   406         return util.format_number(value, max_digits, decimal_places)
       
   407 
       
   408     def year_lookup_bounds(self, value):
       
   409         """
       
   410         Returns a two-elements list with the lower and upper bound to be used
       
   411         with a BETWEEN operator to query a field value using a year lookup
       
   412 
       
   413         `value` is an int, containing the looked-up year.
       
   414         """
       
   415         first = '%s-01-01 00:00:00'
       
   416         second = '%s-12-31 23:59:59.999999'
       
   417         return [first % value, second % value]
       
   418 
       
   419     def year_lookup_bounds_for_date_field(self, value):
       
   420         """
       
   421         Returns a two-elements list with the lower and upper bound to be used
       
   422         with a BETWEEN operator to query a DateField value using a year lookup
       
   423 
       
   424         `value` is an int, containing the looked-up year.
       
   425 
       
   426         By default, it just calls `self.year_lookup_bounds`. Some backends need
       
   427         this hook because on their DB date fields can't be compared to values
       
   428         which include a time part.
       
   429         """
       
   430         return self.year_lookup_bounds(value)
       
   431 
       
   432     def convert_values(self, value, field):
       
   433         """Coerce the value returned by the database backend into a consistent type that
       
   434         is compatible with the field type.
       
   435         """
       
   436         internal_type = field.get_internal_type()
       
   437         if internal_type == 'DecimalField':
       
   438             return value
       
   439         elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField':
       
   440             return int(value)
       
   441         elif internal_type in ('DateField', 'DateTimeField', 'TimeField'):
       
   442             return value
       
   443         # No field, or the field isn't known to be a decimal or integer
       
   444         # Default to a float
       
   445         return float(value)
       
   446 
       
   447     def check_aggregate_support(self, aggregate_func):
       
   448         """Check that the backend supports the provided aggregate
       
   449 
       
   450         This is used on specific backends to rule out known aggregates
       
   451         that are known to have faulty implementations. If the named
       
   452         aggregate function has a known problem, the backend should
       
   453         raise NotImplemented.
       
   454         """
       
   455         pass
       
   456 
       
   457     def combine_expression(self, connector, sub_expressions):
       
   458         """Combine a list of subexpressions into a single expression, using
       
   459         the provided connecting operator. This is required because operators
       
   460         can vary between backends (e.g., Oracle with %% and &) and between
       
   461         subexpression types (e.g., date expressions)
       
   462         """
       
   463         conn = ' %s ' % connector
       
   464         return conn.join(sub_expressions)
       
   465 
       
   466 class BaseDatabaseIntrospection(object):
       
   467     """
       
   468     This class encapsulates all backend-specific introspection utilities
       
   469     """
       
   470     data_types_reverse = {}
       
   471 
       
   472     def __init__(self, connection):
       
   473         self.connection = connection
       
   474 
       
   475     def get_field_type(self, data_type, description):
       
   476         """Hook for a database backend to use the cursor description to
       
   477         match a Django field type to a database column.
       
   478 
       
   479         For Oracle, the column data_type on its own is insufficient to
       
   480         distinguish between a FloatField and IntegerField, for example."""
       
   481         return self.data_types_reverse[data_type]
       
   482 
       
   483     def table_name_converter(self, name):
       
   484         """Apply a conversion to the name for the purposes of comparison.
       
   485 
       
   486         The default table name converter is for case sensitive comparison.
       
   487         """
       
   488         return name
       
   489 
       
   490     def table_names(self):
       
   491         "Returns a list of names of all tables that exist in the database."
       
   492         cursor = self.connection.cursor()
       
   493         return self.get_table_list(cursor)
       
   494 
       
   495     def django_table_names(self, only_existing=False):
       
   496         """
       
   497         Returns a list of all table names that have associated Django models and
       
   498         are in INSTALLED_APPS.
       
   499 
       
   500         If only_existing is True, the resulting list will only include the tables
       
   501         that actually exist in the database.
       
   502         """
       
   503         from django.db import models, router
       
   504         tables = set()
       
   505         for app in models.get_apps():
       
   506             for model in models.get_models(app):
       
   507                 if not model._meta.managed:
       
   508                     continue
       
   509                 if not router.allow_syncdb(self.connection.alias, model):
       
   510                     continue
       
   511                 tables.add(model._meta.db_table)
       
   512                 tables.update([f.m2m_db_table() for f in model._meta.local_many_to_many])
       
   513         if only_existing:
       
   514             tables = [t for t in tables if self.table_name_converter(t) in self.table_names()]
       
   515         return tables
       
   516 
       
   517     def installed_models(self, tables):
       
   518         "Returns a set of all models represented by the provided list of table names."
       
   519         from django.db import models, router
       
   520         all_models = []
       
   521         for app in models.get_apps():
       
   522             for model in models.get_models(app):
       
   523                 if router.allow_syncdb(self.connection.alias, model):
       
   524                     all_models.append(model)
       
   525         return set([m for m in all_models
       
   526             if self.table_name_converter(m._meta.db_table) in map(self.table_name_converter, tables)
       
   527         ])
       
   528 
       
   529     def sequence_list(self):
       
   530         "Returns a list of information about all DB sequences for all models in all apps."
       
   531         from django.db import models, router
       
   532 
       
   533         apps = models.get_apps()
       
   534         sequence_list = []
       
   535 
       
   536         for app in apps:
       
   537             for model in models.get_models(app):
       
   538                 if not model._meta.managed:
       
   539                     continue
       
   540                 if not router.allow_syncdb(self.connection.alias, model):
       
   541                     continue
       
   542                 for f in model._meta.local_fields:
       
   543                     if isinstance(f, models.AutoField):
       
   544                         sequence_list.append({'table': model._meta.db_table, 'column': f.column})
       
   545                         break # Only one AutoField is allowed per model, so don't bother continuing.
       
   546 
       
   547                 for f in model._meta.local_many_to_many:
       
   548                     # If this is an m2m using an intermediate table,
       
   549                     # we don't need to reset the sequence.
       
   550                     if f.rel.through is None:
       
   551                         sequence_list.append({'table': f.m2m_db_table(), 'column': None})
       
   552 
       
   553         return sequence_list
       
   554 
       
   555 class BaseDatabaseClient(object):
       
   556     """
       
   557     This class encapsulates all backend-specific methods for opening a
       
   558     client shell.
       
   559     """
       
   560     # This should be a string representing the name of the executable
       
   561     # (e.g., "psql"). Subclasses must override this.
       
   562     executable_name = None
       
   563 
       
   564     def __init__(self, connection):
       
   565         # connection is an instance of BaseDatabaseWrapper.
       
   566         self.connection = connection
       
   567 
       
   568     def runshell(self):
       
   569         raise NotImplementedError()
       
   570 
       
   571 class BaseDatabaseValidation(object):
       
   572     """
       
   573     This class encapsualtes all backend-specific model validation.
       
   574     """
       
   575     def __init__(self, connection):
       
   576         self.connection = connection
       
   577 
       
   578     def validate_field(self, errors, opts, f):
       
   579         "By default, there is no backend-specific validation"
       
   580         pass