public function DatabaseConnection_sqlite::__destruct

public DatabaseConnection_sqlite::__destruct()

Destructor for the SQLite connection.

We prune empty databases on destruct, but only if tables have been dropped. This is especially needed when running the test suite, which creates and destroy databases several times in a row.

File

includes/database/sqlite/database.inc, line 123
Database interface code for SQLite embedded database engine.

Class

DatabaseConnection_sqlite
Specific SQLite implementation of DatabaseConnection.

Code

public function __destruct() {
  if ($this->tableDropped && !empty($this->attachedDatabases)) {
    foreach ($this->attachedDatabases as $prefix) {
      // Check if the database is now empty, ignore the internal SQLite tables.
      try {
        $count = $this->query('SELECT COUNT(*) FROM ' . $prefix . '.sqlite_master WHERE type = :type AND name NOT LIKE :pattern', array(':type' => 'table', ':pattern' => 'sqlite_%'))->fetchField();

        // We can prune the database file if it doesn't have any tables.
        if ($count == 0) {
          // Detach the database.
          $this->query('DETACH DATABASE :schema', array(':schema' => $prefix));
          // Destroy the database file.
          unlink($this->connectionOptions['database'] . '-' . $prefix);
        }
      }
      catch (Exception $e) {
        // Ignore the exception and continue. There is nothing we can do here
        // to report the error or fail safe.
      }
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/includes!database!sqlite!database.inc/function/DatabaseConnection_sqlite::__destruct/7.x