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