Django

Code

Changeset 5974

Show
Ignore:
Timestamp:
08/19/07 21:20:33 (1 year ago)
Author:
adrian
Message:

Implemented BaseDatabaseFeatures? and changed all code to access it -- connection.features.foo instead of backend.foo

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/management/commands/syncdb.py

    r5920 r5974  
    3535        # so we know what needs to be added. 
    3636        table_list = table_list() 
    37         if backend.uses_case_insensitive_names: 
     37        if connection.features.uses_case_insensitive_names: 
    3838            table_name_converter = str.upper 
    3939        else: 
  • django/trunk/django/core/management/sql.py

    r5967 r5974  
    1616def installed_models(table_list): 
    1717    "Returns a set of all models that are installed, given a list of existing table names." 
    18     from django.db import backend, models 
     18    from django.db import connection, models 
    1919    all_models = [] 
    2020    for app in models.get_apps(): 
    2121        for model in models.get_models(app): 
    2222            all_models.append(model) 
    23     if backend.uses_case_insensitive_names: 
     23    if connection.features.uses_case_insensitive_names: 
    2424        converter = lambda x: x.upper() 
    2525    else: 
     
    111111    else: 
    112112        table_names = [] 
    113     if backend.uses_case_insensitive_names: 
     113    if connection.features.uses_case_insensitive_names: 
    114114        table_name_converter = str.upper 
    115115    else: 
     
    139139            output.append('%s %s;' % (style.SQL_KEYWORD('DROP TABLE'), 
    140140                style.SQL_TABLE(qn(model._meta.db_table)))) 
    141             if backend.supports_constraints and model in references_to_delete: 
     141            if connection.features.supports_constraints and model in references_to_delete: 
    142142                for rel_class, f in references_to_delete[model]: 
    143143                    table = rel_class._meta.db_table 
     
    233233            style.SQL_COLTYPE(col_type)] 
    234234        field_output.append(style.SQL_KEYWORD('%sNULL' % (not f.null and 'NOT ' or ''))) 
    235         if f.unique and (not f.primary_key or backend.allows_unique_and_pk): 
     235        if f.unique and (not f.primary_key or connection.features.allows_unique_and_pk): 
    236236            field_output.append(style.SQL_KEYWORD('UNIQUE')) 
    237237        if f.primary_key: 
    238238            field_output.append(style.SQL_KEYWORD('PRIMARY KEY')) 
    239         if tablespace and backend.supports_tablespaces and (f.unique or f.primary_key) and backend.autoindexes_primary_keys: 
     239        if tablespace and connection.features.supports_tablespaces and (f.unique or f.primary_key) and connection.features.autoindexes_primary_keys: 
    240240            # We must specify the index tablespace inline, because we 
    241241            # won't be generating a CREATE INDEX statement for this field. 
     
    265265        full_statement.append('    %s%s' % (line, i < len(table_output)-1 and ',' or '')) 
    266266    full_statement.append(')') 
    267     if opts.db_tablespace and backend.supports_tablespaces: 
     267    if opts.db_tablespace and connection.features.supports_tablespaces: 
    268268        full_statement.append(connection.ops.tablespace_sql(opts.db_tablespace)) 
    269269    full_statement.append(';') 
     
    288288    qn = connection.ops.quote_name 
    289289    final_output = [] 
    290     if backend.supports_constraints: 
     290    if connection.features.supports_constraints: 
    291291        opts = model._meta 
    292292        if model in pending_references: 
     
    317317        if not isinstance(f.rel, generic.GenericRel): 
    318318            tablespace = f.db_tablespace or opts.db_tablespace 
    319             if tablespace and backend.supports_tablespaces and backend.autoindexes_primary_keys: 
     319            if tablespace and connection.features.supports_tablespaces and connection.features.autoindexes_primary_keys: 
    320320                tablespace_sql = ' ' + connection.ops.tablespace_sql(tablespace, inline=True) 
    321321            else: 
     
    348348                tablespace_sql)) 
    349349            table_output.append(')') 
    350             if opts.db_tablespace and backend.supports_tablespaces: 
     350            if opts.db_tablespace and connection.features.supports_tablespaces: 
    351351                # f.db_tablespace is only for indices, so ignore its value here. 
    352352                table_output.append(connection.ops.tablespace_sql(opts.db_tablespace)) 
     
    396396    qn = connection.ops.quote_name 
    397397    for f in model._meta.fields: 
    398         if f.db_index and not ((f.primary_key or f.unique) and backend.autoindexes_primary_keys): 
     398        if f.db_index and not ((f.primary_key or f.unique) and connection.features.autoindexes_primary_keys): 
    399399            unique = f.unique and 'UNIQUE ' or '' 
    400400            tablespace = f.db_tablespace or model._meta.db_tablespace 
    401             if tablespace and backend.supports_tablespaces: 
     401            if tablespace and connection.features.supports_tablespaces: 
    402402                tablespace_sql = ' ' + connection.ops.tablespace_sql(tablespace) 
    403403            else: 
  • django/trunk/django/db/backends/ado_mssql/base.py

    r5970 r5974  
    55""" 
    66 
    7 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseOperations, util 
     7from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseOperations, util 
    88try: 
    99    import adodbapi as Database 
     
    4949Database.convertVariantToPython = variantToPython 
    5050 
     51class DatabaseFeatures(BaseDatabaseFeatures): 
     52    supports_tablespaces = True 
     53 
    5154class DatabaseOperations(BaseDatabaseOperations): 
    5255    def date_extract_sql(self, lookup_type, field_name): 
     
    8083 
    8184class DatabaseWrapper(BaseDatabaseWrapper): 
     85    features = DatabaseFeatures() 
    8286    ops = DatabaseOperations() 
    8387 
     
    9397            self.connection = Database.connect(conn_string) 
    9498        return self.connection.cursor() 
    95  
    96 allows_group_by_ordinal = True 
    97 allows_unique_and_pk = True 
    98 autoindexes_primary_keys = True 
    99 needs_datetime_string_cast = True 
    100 needs_upper_for_iops = False 
    101 supports_constraints = True 
    102 supports_tablespaces = True 
    103 uses_case_insensitive_names = False 
    10499 
    105100OPERATOR_MAPPING = { 
  • django/trunk/django/db/backends/dummy/base.py

    r5970 r5974  
    2222    pass 
    2323 
    24 class DatabaseOperations(object): 
     24class ComplainOnGetattr(object): 
    2525    def __getattr__(self, *args, **kwargs): 
    2626        complain() 
    2727 
    2828class DatabaseWrapper(object): 
    29     ops = DatabaseOperations() 
     29    ops = ComplainOnGetattr() 
     30    features = ComplainOnGetattr() 
    3031    cursor = complain 
    3132    _commit = complain 
     
    3839        pass # close() 
    3940 
    40 supports_constraints = False 
    41 supports_tablespaces = False 
    42  
    4341OPERATOR_MAPPING = {} 
  • django/trunk/django/db/backends/__init__.py

    r5971 r5974  
    3939        from django.db.backends import util 
    4040        return util.CursorDebugWrapper(cursor, self) 
     41 
     42class BaseDatabaseFeatures(object): 
     43    allows_group_by_ordinal = True 
     44    allows_unique_and_pk = True 
     45    autoindexes_primary_keys = True 
     46    needs_datetime_string_cast = True 
     47    needs_upper_for_iops = False 
     48    supports_constraints = True 
     49    supports_tablespaces = False 
     50    uses_case_insensitive_names = False 
    4151 
    4252class BaseDatabaseOperations(object): 
  • django/trunk/django/db/backends/mysql/base.py

    r5970 r5974  
    55""" 
    66 
    7 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseOperations, util 
     7from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseOperations, util 
    88try: 
    99    import MySQLdb as Database 
     
    5353# standard util.CursorDebugWrapper can be used. Also, using sql_mode 
    5454# TRADITIONAL will automatically cause most warnings to be treated as errors. 
     55 
     56class DatabaseFeatures(BaseDatabaseFeatures): 
     57    autoindexes_primary_keys = False 
    5558 
    5659class DatabaseOperations(BaseDatabaseOperations): 
     
    117120 
    118121class DatabaseWrapper(BaseDatabaseWrapper): 
     122    features = DatabaseFeatures() 
    119123    ops = DatabaseOperations() 
    120124 
     
    176180        return self.server_version 
    177181 
    178 allows_group_by_ordinal = True 
    179 allows_unique_and_pk = True 
    180 autoindexes_primary_keys = False 
    181 needs_datetime_string_cast = True     # MySQLdb requires a typecast for dates 
    182 needs_upper_for_iops = False 
    183 supports_constraints = True 
    184 supports_tablespaces = False 
    185 uses_case_insensitive_names = False 
    186  
    187182OPERATOR_MAPPING = { 
    188183    'exact': '= %s', 
  • django/trunk/django/db/backends/mysql_old/base.py

    r5970 r5974  
    55""" 
    66 
    7 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseOperations, util 
     7from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseOperations, util 
    88from django.utils.encoding import force_unicode 
    99try: 
     
    6464            return getattr(self.cursor, attr) 
    6565 
     66class DatabaseFeatures(BaseDatabaseFeatures): 
     67    autoindexes_primary_keys = False 
     68 
    6669class DatabaseOperations(BaseDatabaseOperations): 
    6770    def date_extract_sql(self, lookup_type, field_name): 
     
    127130 
    128131class DatabaseWrapper(BaseDatabaseWrapper): 
     132    features = DatabaseFeatures() 
    129133    ops = DatabaseOperations() 
    130134 
     
    195199        return self.server_version 
    196200 
    197 allows_group_by_ordinal = True 
    198 allows_unique_and_pk = True 
    199 autoindexes_primary_keys = False 
    200 needs_datetime_string_cast = True     # MySQLdb requires a typecast for dates 
    201 needs_upper_for_iops = False 
    202 supports_constraints = True 
    203 supports_tablespaces = False 
    204 uses_case_insensitive_names = False 
    205  
    206201OPERATOR_MAPPING = { 
    207202    'exact': '= %s', 
  • django/trunk/django/db/backends/oracle/base.py

    r5970 r5974  
    55""" 
    66 
    7 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseOperations, util 
     7from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseOperations, util 
    88from django.utils.datastructures import SortedDict 
    99from django.utils.encoding import smart_str, force_unicode 
     
    2121DatabaseError = Database.Error 
    2222IntegrityError = Database.IntegrityError 
     23 
     24class DatabaseFeatures(BaseDatabaseFeatures): 
     25    allows_group_by_ordinal = False 
     26    allows_unique_and_pk = False        # Suppress UNIQUE/PK for Oracle (ORA-02259) 
     27    needs_datetime_string_cast = False 
     28    needs_upper_for_iops = True 
     29    supports_tablespaces = True 
     30    uses_case_insensitive_names = True 
    2331 
    2432class DatabaseOperations(BaseDatabaseOperations): 
     
    129137 
    130138class DatabaseWrapper(BaseDatabaseWrapper): 
     139    features = DatabaseFeatures() 
    131140    ops = DatabaseOperations() 
    132141 
     
    151160        cursor.execute("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF'") 
    152161        return cursor 
    153  
    154 allows_group_by_ordinal = False 
    155 allows_unique_and_pk = False        # Suppress UNIQUE/PK for Oracle (ORA-02259) 
    156 autoindexes_primary_keys = True 
    157 needs_datetime_string_cast = False 
    158 needs_upper_for_iops = True 
    159 supports_constraints = True 
    160 supports_tablespaces = True 
    161 uses_case_insensitive_names = True 
    162162 
    163163class FormatStylePlaceholderCursor(Database.Cursor): 
  • django/trunk/django/db/backends/postgresql/base.py

    r5972 r5974  
    66 
    77from django.utils.encoding import smart_str, smart_unicode 
    8 from django.db.backends import BaseDatabaseWrapper, util 
     8from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, util 
    99from django.db.backends.postgresql.operations import DatabaseOperations 
    1010try: 
     
    5757            return getattr(self.cursor, attr) 
    5858 
     59class DatabaseFeatures(BaseDatabaseFeatures): 
     60    pass # This backend uses all the defaults. 
     61 
    5962class DatabaseWrapper(BaseDatabaseWrapper): 
     63    features = DatabaseFeatures() 
    6064    ops = DatabaseOperations() 
    6165 
     
    8791            self.ops.postgres_version = [int(val) for val in cursor.fetchone()[0].split()[1].split('.')] 
    8892        return cursor 
    89  
    90 allows_group_by_ordinal = True 
    91 allows_unique_and_pk = True 
    92 autoindexes_primary_keys = True 
    93 needs_datetime_string_cast = True 
    94 needs_upper_for_iops = False 
    95 supports_constraints = True 
    96 supports_tablespaces = False 
    97 uses_case_insensitive_names = False 
    9893 
    9994def typecast_string(s): 
  • django/trunk/django/db/backends/postgresql_psycopg2/base.py

    r5973 r5974  
    55""" 
    66 
    7 from django.db.backends import BaseDatabaseWrapper 
     7from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures 
    88from django.db.backends.postgresql.operations import DatabaseOperations 
    99try: 
     
    1919psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) 
    2020 
     21class DatabaseFeatures(BaseDatabaseFeatures): 
     22    needs_datetime_string_cast = False 
     23 
    2124class DatabaseWrapper(BaseDatabaseWrapper): 
     25    features = DatabaseFeatures() 
    2226    ops = DatabaseOperations() 
    2327 
     
    5054        return cursor 
    5155 
    52 allows_group_by_ordinal = True 
    53 allows_unique_and_pk = True 
    54 autoindexes_primary_keys = True 
    55 needs_datetime_string_cast = False 
    56 needs_upper_for_iops = False 
    57 supports_constraints = True 
    58 supports_tablespaces = False 
    59 uses_case_insensitive_names = False 
    60  
    6156OPERATOR_MAPPING = { 
    6257    'exact': '= %s', 
  • django/trunk/django/db/backends/sqlite3/base.py

    r5970 r5974  
    33""" 
    44 
    5 from django.db.backends import BaseDatabaseWrapper, BaseDatabaseOperations, util 
     5from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseOperations, util 
    66try: 
    77    try: 
     
    3434Database.register_converter("decimal", util.typecast_decimal) 
    3535Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal) 
     36 
     37class DatabaseFeatures(BaseDatabaseFeatures): 
     38    supports_constraints = False 
    3639 
    3740class DatabaseOperations(BaseDatabaseOperations): 
     
    7174 
    7275class DatabaseWrapper(BaseDatabaseWrapper): 
     76    features = DatabaseFeatures() 
    7377    ops = DatabaseOperations() 
    7478 
     
    111115    def convert_query(self, query, num_params): 
    112116        return query % tuple("?" * num_params) 
    113  
    114 allows_group_by_ordinal = True 
    115 allows_unique_and_pk = True 
    116 autoindexes_primary_keys = True 
    117 needs_datetime_string_cast = True 
    118 needs_upper_for_iops = False 
    119 supports_constraints = False 
    120 supports_tablespaces = False 
    121 uses_case_insensitive_names = False 
    122117 
    123118def _sqlite_extract(lookup_type, dt): 
  • django/trunk/django/db/models/query.py

    r5967 r5974  
    652652        field_name = qn(self._field.column) 
    653653 
    654         if backend.allows_group_by_ordinal: 
     654        if connection.features.allows_group_by_ordinal: 
    655655            group_by = '1' 
    656656        else: 
     
    664664 
    665665        has_resolve_columns = hasattr(self, 'resolve_columns') 
    666         needs_datetime_string_cast = backend.needs_datetime_string_cast 
     666        needs_datetime_string_cast = connection.features.needs_datetime_string_cast 
    667667        dates = [] 
    668668        # It would be better to use self._field here instead of DateTimeField(), 
     
    797797        field_cast_sql = '%s%s' 
    798798    field_sql = field_cast_sql % (table_prefix, field_name) 
    799     if lookup_type in ('iexact', 'icontains', 'istartswith', 'iendswith') and backend.needs_upper_for_iops: 
     799    if lookup_type in ('iexact', 'icontains', 'istartswith', 'iendswith') and connection.features.needs_upper_for_iops: 
    800800        format = 'UPPER(%s) %s' 
    801801    else: