Django

Code

Changeset 5950

Show
Ignore:
Timestamp:
08/19/07 17:29:57 (2 years ago)
Author:
adrian
Message:

Began implementing BaseDatabaseOperations? class for every database backend. This class will be used to hold the database-specific methods that currently live at the module level in each backend. Only autoinc_sql() has been implemented so far.

Files:

Legend:

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

    r5898 r5950  
    214214        (list_of_sql, pending_references_dict) 
    215215    """ 
    216     from django.db import backend, models 
     216    from django.db import backend, connection, models 
    217217 
    218218    opts = model._meta 
     
    268268    final_output.append('\n'.join(full_statement)) 
    269269 
    270     if opts.has_auto_field and hasattr(backend, 'get_autoinc_sql')
    271         # Add any extra SQL needed to support auto-incrementing primary keys 
    272         autoinc_sql = backend.get_autoinc_sql(opts.db_table) 
     270    if opts.has_auto_field
     271        # Add any extra SQL needed to support auto-incrementing primary keys. 
     272        autoinc_sql = connection.ops.autoinc_sql(opts.db_table) 
    273273        if autoinc_sql: 
    274274            for stmt in autoinc_sql: 
  • django/trunk/django/db/backends/ado_mssql/base.py

    r5949 r5950  
    55""" 
    66 
    7 from django.db.backends import BaseDatabaseWrapper, util 
     7from django.db.backends import BaseDatabaseWrapper, BaseDatabaseOperations, util 
    88try: 
    99    import adodbapi as Database 
     
    4949Database.convertVariantToPython = variantToPython 
    5050 
     51class DatabaseOperations(BaseDatabaseOperations): 
     52    pass 
     53 
    5154class DatabaseWrapper(BaseDatabaseWrapper): 
     55    ops = DatabaseOperations() 
     56 
    5257    def _cursor(self, settings): 
    5358        if self.connection is None: 
     
    131136    return "ON %s" % quote_name(tablespace) 
    132137 
    133 def get_autoinc_sql(table): 
    134     return None 
    135  
    136138def get_sql_flush(style, tables, sequences): 
    137139    """Return a list of SQL statements required to remove all data from 
  • django/trunk/django/db/backends/dummy/base.py

    r5557 r5950  
    2222    pass 
    2323 
    24 class DatabaseWrapper: 
     24class DatabaseOperations(object): 
     25    def __getattr__(self, *args, **kwargs): 
     26        complain() 
     27 
     28class DatabaseWrapper(object): 
     29    ops = DatabaseOperations() 
    2530    cursor = complain 
    2631    _commit = complain 
     
    5156get_max_name_length = ignore 
    5257get_start_transaction_sql = complain 
    53 get_autoinc_sql = complain 
    5458get_sql_flush = complain 
    5559get_sql_sequence_reset = complain 
  • django/trunk/django/db/backends/__init__.py

    r5949 r5950  
    77 
    88class BaseDatabaseWrapper(local): 
     9    """ 
     10    Represents a database connection. 
     11    """ 
     12    ops = None 
    913    def __init__(self, **kwargs): 
    1014        self.connection = None 
     
    3539        from django.db.backends import util 
    3640        return util.CursorDebugWrapper(cursor, self) 
     41 
     42class BaseDatabaseOperations(object): 
     43    """ 
     44    This class encapsulates all backend-specific differences, such as the way 
     45    a backend performs ordering or calculates the ID of a recently-inserted 
     46    row. 
     47    """ 
     48    def autoinc_sql(self, table): 
     49        """ 
     50        Returns any SQL needed to support auto-incrementing primary keys, or 
     51        None if no SQL is necessary. 
     52 
     53        This SQL is executed when a table is created. 
     54        """ 
     55        return None 
  • django/trunk/django/db/backends/mysql/base.py

    r5949 r5950  
    55""" 
    66 
    7 from django.db.backends import BaseDatabaseWrapper, util 
     7from django.db.backends import BaseDatabaseWrapper, BaseDatabaseOperations, util 
    88try: 
    99    import MySQLdb as Database 
     
    5454# TRADITIONAL will automatically cause most warnings to be treated as errors. 
    5555 
     56class DatabaseOperations(BaseDatabaseOperations): 
     57    pass 
     58 
    5659class DatabaseWrapper(BaseDatabaseWrapper): 
     60    ops = DatabaseOperations() 
     61 
    5762    def __init__(self, **kwargs): 
    5863        super(DatabaseWrapper, self).__init__(**kwargs) 
     
    182187    return "BEGIN;" 
    183188 
    184 def get_autoinc_sql(table): 
    185     return None 
    186  
    187189def get_sql_flush(style, tables, sequences): 
    188190    """Return a list of SQL statements required to remove all data from 
  • django/trunk/django/db/backends/mysql_old/base.py

    r5949 r5950  
    55""" 
    66 
    7 from django.db.backends import BaseDatabaseWrapper, util 
     7from django.db.backends import BaseDatabaseWrapper, BaseDatabaseOperations, util 
    88from django.utils.encoding import force_unicode 
    99try: 
     
    6464            return getattr(self.cursor, attr) 
    6565 
     66class DatabaseOperations(BaseDatabaseOperations): 
     67    pass 
     68 
    6669class DatabaseWrapper(BaseDatabaseWrapper): 
     70    ops = DatabaseOperations() 
     71 
    6772    def __init__(self, **kwargs): 
    6873        super(DatabaseWrapper, self).__init__(**kwargs) 
     
    201206    return "BEGIN;" 
    202207 
    203 def get_autoinc_sql(table): 
    204     return None 
    205  
    206208def get_sql_flush(style, tables, sequences): 
    207209    """Return a list of SQL statements required to remove all data from 
  • django/trunk/django/db/backends/oracle/base.py

    r5949 r5950  
    55""" 
    66 
    7 from django.db.backends import BaseDatabaseWrapper, util 
     7from django.db.backends import BaseDatabaseWrapper, BaseDatabaseOperations, util 
    88from django.utils.datastructures import SortedDict 
    99from django.utils.encoding import smart_str, force_unicode 
     
    2222IntegrityError = Database.IntegrityError 
    2323 
     24class DatabaseOperations(BaseDatabaseOperations): 
     25    def autoinc_sql(self, table): 
     26        # To simulate auto-incrementing primary keys in Oracle, we have to 
     27        # create a sequence and a trigger. 
     28        sq_name = get_sequence_name(table) 
     29        tr_name = get_trigger_name(table) 
     30        sequence_sql = 'CREATE SEQUENCE %s;' % sq_name 
     31        trigger_sql = """ 
     32            CREATE OR REPLACE TRIGGER %s 
     33            BEFORE INSERT ON %s 
     34            FOR EACH ROW 
     35            WHEN (new.id IS NULL) 
     36                BEGIN 
     37                    SELECT %s.nextval INTO :new.id FROM dual; 
     38                END;/""" % (tr_name, quote_name(table), sq_name) 
     39        return sequence_sql, trigger_sql 
     40 
    2441class DatabaseWrapper(BaseDatabaseWrapper): 
     42    ops = DatabaseOperations() 
     43 
    2544    def _valid_connection(self): 
    2645        return self.connection is not None 
     
    187206def get_tablespace_sql(tablespace, inline=False): 
    188207    return "%sTABLESPACE %s" % ((inline and "USING INDEX " or ""), quote_name(tablespace)) 
    189  
    190 def get_autoinc_sql(table): 
    191     # To simulate auto-incrementing primary keys in Oracle, we have to 
    192     # create a sequence and a trigger. 
    193     sq_name = get_sequence_name(table) 
    194     tr_name = get_trigger_name(table) 
    195     sequence_sql = 'CREATE SEQUENCE %s;' % sq_name 
    196     trigger_sql = """CREATE OR REPLACE TRIGGER %s 
    197   BEFORE INSERT ON %s 
    198   FOR EACH ROW 
    199   WHEN (new.id IS NULL) 
    200     BEGIN 
    201       SELECT %s.nextval INTO :new.id FROM dual; 
    202     END; 
    203     /""" % (tr_name, quote_name(table), sq_name) 
    204     return sequence_sql, trigger_sql 
    205208 
    206209def get_drop_sequence(table): 
  • django/trunk/django/db/backends/postgresql/base.py

    r5949 r5950  
    66 
    77from django.utils.encoding import smart_str, smart_unicode 
    8 from django.db.backends import BaseDatabaseWrapper, util 
     8from django.db.backends import BaseDatabaseWrapper, BaseDatabaseOperations, util 
    99try: 
    1010    import psycopg as Database 
     
    5858postgres_version = None 
    5959 
     60class DatabaseOperations(BaseDatabaseOperations): 
     61    pass 
     62 
    6063class DatabaseWrapper(BaseDatabaseWrapper): 
     64    ops = DatabaseOperations() 
     65 
    6166    def _cursor(self, settings): 
    6267        set_tz = False 
     
    157162def get_start_transaction_sql(): 
    158163    return "BEGIN;" 
    159  
    160 def get_autoinc_sql(table): 
    161     return None 
    162164 
    163165def get_sql_flush(style, tables, sequences): 
  • django/trunk/django/db/backends/postgresql_psycopg2/base.py

    r5949 r5950  
    55""" 
    66 
    7 from django.db.backends import BaseDatabaseWrapper, util 
     7from django.db.backends import BaseDatabaseWrapper, BaseDatabaseOperations, util 
    88try: 
    99    import psycopg2 as Database 
     
    2020postgres_version = None 
    2121 
     22class DatabaseOperations(BaseDatabaseOperations): 
     23    pass 
     24 
    2225class DatabaseWrapper(BaseDatabaseWrapper): 
     26    ops = DatabaseOperations() 
     27 
    2328    def _cursor(self, settings): 
    2429        set_tz = False 
     
    111116def get_start_transaction_sql(): 
    112117    return "BEGIN;" 
    113  
    114 def get_autoinc_sql(table): 
    115     return None 
    116118 
    117119def get_sql_flush(style, tables, sequences): 
  • django/trunk/django/db/backends/sqlite3/base.py

    r5949 r5950  
    33""" 
    44 
    5 from django.db.backends import BaseDatabaseWrapper, util 
     5from django.db.backends import BaseDatabaseWrapper, BaseDatabaseOperations, util 
    66try: 
    77    try: 
     
    3535Database.register_adapter(decimal.Decimal, util.rev_typecast_decimal) 
    3636 
     37class DatabaseOperations(BaseDatabaseOperations): 
     38    pass 
     39 
    3740class DatabaseWrapper(BaseDatabaseWrapper): 
     41    ops = DatabaseOperations() 
     42 
    3843    def _cursor(self, settings): 
    3944        if self.connection is None: 
     
    143148def get_start_transaction_sql(): 
    144149    return "BEGIN;" 
    145  
    146 def get_autoinc_sql(table): 
    147     return None 
    148150 
    149151def get_sql_flush(style, tables, sequences):