Ticket #17760: 17760.diff

File 17760.diff, 6.0 KB (added by Claude Paroz, 13 years ago)

Replace confirm() by cached properties

  • django/contrib/gis/db/backends/spatialite/creation.py

    diff --git a/django/contrib/gis/db/backends/spatialite/creation.py b/django/contrib/gis/db/backends/spatialite/creation.py
    index 33b6f95..5c97113 100644
    a b class SpatiaLiteCreation(DatabaseCreation):  
    3131        self.connection.close()
    3232        self.connection.settings_dict["NAME"] = test_database_name
    3333
    34         # Confirm the feature set of the test database
    35         self.connection.features.confirm()
    36 
    3734        # Need to load the SpatiaLite initialization SQL before running `syncdb`.
    3835        self.load_spatialite_sql()
    3936
  • django/db/backends/__init__.py

    diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
    index 7674f5c..57a550b 100644
    a b class BaseDatabaseFeatures(object):  
    399399    # in the SQL standard.
    400400    supports_tablespaces = False
    401401
    402     # Features that need to be confirmed at runtime
    403     # Cache whether the confirmation has been performed.
    404     _confirmed = False
    405     supports_transactions = None
    406     supports_stddev = None
    407     can_introspect_foreign_keys = None
     402    # Confirm support for introspected foreign keys
     403    # Every database can do this reliably, except MySQL,
     404    # which can't do it for MyISAM tables
     405    can_introspect_foreign_keys = True
    408406
    409407    # Support for the DISTINCT ON clause
    410408    can_distinct_on_fields = False
    411409
    412410    def __init__(self, connection):
    413411        self.connection = connection
     412        # Cached properties
     413        self._supports_transactions = None
     414        self._supports_stddev = None
    414415
    415     def confirm(self):
    416         "Perform manual checks of any database features that might vary between installs"
    417         self._confirmed = True
    418         self.supports_transactions = self._supports_transactions()
    419         self.supports_stddev = self._supports_stddev()
    420         self.can_introspect_foreign_keys = self._can_introspect_foreign_keys()
    421 
    422     def _supports_transactions(self):
     416    @property
     417    def supports_transactions(self):
    423418        "Confirm support for transactions"
    424         cursor = self.connection.cursor()
    425         cursor.execute('CREATE TABLE ROLLBACK_TEST (X INT)')
    426         self.connection._commit()
    427         cursor.execute('INSERT INTO ROLLBACK_TEST (X) VALUES (8)')
    428         self.connection._rollback()
    429         cursor.execute('SELECT COUNT(X) FROM ROLLBACK_TEST')
    430         count, = cursor.fetchone()
    431         cursor.execute('DROP TABLE ROLLBACK_TEST')
    432         self.connection._commit()
    433         return count == 0
    434 
    435     def _supports_stddev(self):
     419        if self._supports_transactions is None:
     420            cursor = self.connection.cursor()
     421            cursor.execute('CREATE TABLE ROLLBACK_TEST (X INT)')
     422            self.connection._commit()
     423            cursor.execute('INSERT INTO ROLLBACK_TEST (X) VALUES (8)')
     424            self.connection._rollback()
     425            cursor.execute('SELECT COUNT(X) FROM ROLLBACK_TEST')
     426            count, = cursor.fetchone()
     427            cursor.execute('DROP TABLE ROLLBACK_TEST')
     428            self.connection._commit()
     429            self._supports_transactions = (count == 0)
     430        return self._supports_transactions
     431
     432    @property
     433    def supports_stddev(self):
    436434        "Confirm support for STDDEV and related stats functions"
    437         class StdDevPop(object):
    438             sql_function = 'STDDEV_POP'
    439 
    440         try:
    441             self.connection.ops.check_aggregate_support(StdDevPop())
    442         except NotImplementedError:
    443             self.supports_stddev = False
    444 
    445     def _can_introspect_foreign_keys(self):
    446         "Confirm support for introspected foreign keys"
    447         # Every database can do this reliably, except MySQL,
    448         # which can't do it for MyISAM tables
    449         return True
     435        if self._supports_stddev is None:
     436            class StdDevPop(object):
     437                sql_function = 'STDDEV_POP'
     438
     439            try:
     440                self.connection.ops.check_aggregate_support(StdDevPop())
     441                self._supports_stddev = True
     442            except NotImplementedError:
     443                self._supports_stddev = False
     444        return self._supports_stddev
    450445
    451446
    452447class BaseDatabaseOperations(object):
  • django/db/backends/creation.py

    diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
    index 2db0acc..a5ed311 100644
    a b class BaseDatabaseCreation(object):  
    258258        self.connection.close()
    259259        self.connection.settings_dict["NAME"] = test_database_name
    260260
    261         # Confirm the feature set of the test database
    262         self.connection.features.confirm()
    263 
    264261        # Report syncdb messages at one level lower than that requested.
    265262        # This ensures we don't get flooded with messages during testing
    266263        # (unless you really ask to be flooded)
  • django/db/backends/mysql/base.py

    diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
    index 830808b..ed5f68a 100644
    a b class DatabaseFeatures(BaseDatabaseFeatures):  
    153153    def __init__(self, connection):
    154154        super(DatabaseFeatures, self).__init__(connection)
    155155        self._storage_engine = None
     156        self._can_introspect_foreign_keys = None
    156157
    157158    def _mysql_storage_engine(self):
    158159        "Internal method used in Django tests. Don't rely on this from your code"
    class DatabaseFeatures(BaseDatabaseFeatures):  
    169170            self._storage_engine = result[1]
    170171        return self._storage_engine
    171172
    172     def _can_introspect_foreign_keys(self):
     173    @property
     174    def can_introspect_foreign_keys(self):
    173175        "Confirm support for introspected foreign keys"
    174         return self._mysql_storage_engine() != 'MyISAM'
     176        if self._can_introspect_foreign_keys is None:
     177            self._can_introspect_foreign_keys = (
     178                self._mysql_storage_engine() != 'MyISAM')
     179        return self._can_introspect_foreign_keys
    175180
    176181class DatabaseOperations(BaseDatabaseOperations):
    177182    compiler_module = "django.db.backends.mysql.compiler"
Back to Top