class Database

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database
  2. 10 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database
  3. 9 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database
  4. 8.9.x core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database
  5. 7.x includes/database/database.inc \Database

Primary front-controller for the database system.

This class is un-extendable. It acts to encapsulate all control and shepherding of database connections into a single location without the use of globals.

@phpstan-type UnprocessedConnectionInfoArray array{ 'driver': string, 'autoload'?: string, 'namespace'?: string, 'database': string, 'username'?: string, 'password'?: string, 'host'?: string, 'port'?: string|int, 'prefix'?: string, 'collation'?: string, 'pdo'?: array<mixed>, 'isolation_level'?: int|string, 'init_commands'?: list<string>, 'dependencies'?: array<string,array{ 'autoload': string, 'namespace': string, }>, } @phpstan-type ConnectionInfoArray array{ 'driver': string, 'autoload': string, 'namespace': string, 'database': string, 'username'?: string, 'password'?: string, 'host'?: string, 'port'?: string|int, 'prefix': string, 'collation'?: string, 'pdo'?: array<mixed>, 'isolation_level'?: int|string, 'init_commands'?: list<string>, 'dependencies'?: array<string,array{ 'autoload': string, 'namespace': string, }>, } @phpstan-import-type DatabaseLogEntry from \Drupal\Core\Database\Log

@final

Hierarchy

Expanded class hierarchy of Database

263 files declare their use of Database
add-menu-block-with-zero-depth.php in core/modules/block/tests/fixtures/update/add-menu-block-with-zero-depth.php
Adds a menu block with a `depth` setting of 0.
add-text-with-summary-storage.php in core/modules/system/tests/fixtures/update/add-text-with-summary-storage.php
Adds a text_with_summary field storage to the fixture database.
BigPipeTest.php in core/modules/big_pipe/tests/src/Functional/BigPipeTest.php
BlockContentCreationTest.php in core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php
BootableCommandTraitTest.php in core/tests/Drupal/Tests/Core/Command/BootableCommandTraitTest.php

... See full list

68 string references to 'Database'
BackendCompilerPass::process in core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php
BackendCompilerPassTest::getDriverTestMysqlContainer in core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php
Creates a container with a DriverTestMysql database mock definition in it.
BackendCompilerPassTest::getSqliteContainer in core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php
Creates a container with a sqlite database service in it.
BlockWeightUpdateTest::testRunUpdates in core/modules/block/tests/src/Functional/BlockWeightUpdateTest.php
Tests update path for blocks' `weight` property.
CacheCollectorTest::register in core/tests/Drupal/KernelTests/Core/Cache/CacheCollectorTest.php
Registers test-specific services.

... See full list

File

core/lib/Drupal/Core/Database/Database.php, line 61

Namespace

Drupal\Core\Database
View source
abstract class Database {
  
  /**
   * A nested array of active connections, keyed by database key and target.
   *
   * @var array<string|int,array<string|int,\Drupal\Core\Database\Connection>>
   */
  protected static array $connections = [];
  
  /**
   * A processed copy of the database connection information from settings.php.
   *
   * @var array<string|int,array<string|int,ConnectionInfoArray>>
   */
  protected static array $databaseInfo = [];
  
  /**
   * A list of key/target credentials to simply ignore.
   *
   * @var array<string|int,array<string|int,true>>
   */
  protected static array $ignoreTargets = [];
  
  /**
   * The key of the currently active database connection.
   */
  protected static string|int $activeKey = 'default';
  
  /**
   * An array of active query log objects.
   *
   * Every connection has one and only one logger object for all targets and
   * logging keys.
   *
   * @var array<string|int,\Drupal\Core\Database\Log>
   */
  protected static array $logs = [];
  
  /**
   * Starts logging a given logging key on the specified connection.
   *
   * @param string $logging_key
   *   The logging key to log.
   * @param string $key
   *   (optional) The database connection key for which we want to log. If not
   *   specified, the 'default' connection key will be logged.
   *
   * @return \Drupal\Core\Database\Log
   *   The query log object. Note that the log object does support richer
   *   methods than the few exposed through the Database class, so in some
   *   cases it may be desirable to access it directly.
   *
   * @see \Drupal\Core\Database\Log
   */
  final public static function startLog(string $logging_key, string|int $key = 'default') : Log {
    if (is_int($key)) {
      @trigger_error('Passing an integer value to the $key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if (empty(self::$logs[$key])) {
      self::$logs[$key] = new Log((string) $key);
      // Every target already active for this connection key needs to have the
      // logging object associated with it.
      if (!empty(self::$connections[$key])) {
        foreach (self::$connections[$key] as $connection) {
          $connection->enableEvents(StatementEvent::all());
          $connection->setLogger(self::$logs[$key]);
        }
      }
    }
    self::$logs[$key]->start($logging_key);
    return self::$logs[$key];
  }
  
  /**
   * Retrieves the queries logged on for given logging key.
   *
   * This method also ends logging for the specified key. To get the query log
   * to date without ending the logger request the logging object by starting
   * it again (which does nothing to an open log key) and call methods on it as
   * desired.
   *
   * @param string $logging_key
   *   The logging key to log.
   * @param string $key
   *   (optional) The database connection key for which we want to log. If not
   *   specified, the log for the 'default' connection key will be returned.
   *
   * @return list<DatabaseLogEntry>
   *   The query log for the specified logging key and connection.
   *
   * @see \Drupal\Core\Database\Log
   */
  final public static function getLog(string $logging_key, string|int $key = 'default') : array {
    if (is_int($key)) {
      @trigger_error('Passing an integer value to the $key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if (empty(self::$logs[$key])) {
      return [];
    }
    $queries = self::$logs[$key]->get($logging_key);
    self::$logs[$key]->end($logging_key);
    return $queries;
  }
  
  /**
   * Gets the connection object for the specified database key and target.
   *
   * @param string $target
   *   (optional) The database target name. If not specified, the 'default'
   *   target will be returned.
   * @param ?string $key
   *   (optional) The database connection key. Defaults to NULL which means the
   *   active key.
   *
   * @return \Drupal\Core\Database\Connection
   *   The corresponding connection object.
   */
  final public static function getConnection(string|int $target = 'default', string|int|null $key = NULL) : Connection {
    if (is_int($target)) {
      @trigger_error('Passing an integer value to the $target parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if ($key !== NULL && is_int($key)) {
      @trigger_error('Passing an integer value to the $key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if (!isset($key)) {
      // By default, we want the active connection, set in setActiveConnection.
      $key = self::$activeKey;
    }
    // If the requested target does not exist, or if it is ignored, we fall back
    // to the default target. The target is typically either "default" or
    // "replica", indicating to use a replica SQL server if one is available. If
    // it's not available, then the default/primary server is the correct server
    // to use.
    if (!empty(self::$ignoreTargets[$key][$target]) || !isset(self::$databaseInfo[$key][$target])) {
      $target = 'default';
    }
    if (!isset(self::$connections[$key][$target])) {
      // If necessary, a new connection is opened.
      self::$connections[$key][$target] = self::openConnection($key, $target);
    }
    return self::$connections[$key][$target];
  }
  
  /**
   * Determines if there is an active connection.
   *
   * Note that this method will return FALSE if no connection has been
   * established yet, even if one could be.
   *
   * @return bool
   *   TRUE if there is at least one database connection established, FALSE
   *   otherwise.
   */
  final public static function isActiveConnection() : bool {
    return !empty(self::$activeKey) && !empty(self::$connections) && !empty(self::$connections[self::$activeKey]);
  }
  
  /**
   * Sets the active connection to the specified key.
   *
   * @param string $key
   *   (optional) The database connection key. If not specified, the 'default'
   *   connection key will be activated.
   *
   * @return ?string
   *   The previous database connection key, or NULL if no connection was
   *   previously active.
   */
  final public static function setActiveConnection(string|int $key = 'default') : string|int|null {
    if (is_int($key)) {
      @trigger_error('Passing an integer value to the $key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if (!empty(self::$databaseInfo[$key])) {
      $old_key = self::$activeKey;
      self::$activeKey = $key;
      return $old_key;
    }
    return NULL;
  }
  
  /**
   * Process the configuration file for database information.
   *
   * @param UnprocessedConnectionInfoArray|list<UnprocessedConnectionInfoArray> $info
   *   The database connection information, as defined in settings.php. The
   *   structure of this array depends on the database driver it is connecting
   *   to.
   *
   * @return ConnectionInfoArray
   *   A validated connection info array.
   */
  final public static function parseConnectionInfo(array $info) : array {
    // If there is no "driver" property, then we assume it's an array of
    // possible connections for this target. Pick one at random. That allows
    // us to have, for example, multiple replica servers.
    if (empty($info['driver'])) {
      $info_pick = $info[mt_rand(0, count($info) - 1)];
    }
    /** @var UnprocessedConnectionInfoArray $connection_info */
    $connection_info = $info_pick ?? $info;
    // Prefix information, default to an empty prefix.
    $connection_info['prefix'] = $connection_info['prefix'] ?? '';
    // Backwards compatibility layer for Drupal 8 style database connection
    // arrays. Those have the wrong 'namespace' key set, or not set at all
    // for core supported database drivers.
    if (empty($connection_info['namespace']) || str_starts_with($connection_info['namespace'], 'Drupal\\Core\\Database\\Driver\\')) {
      switch (strtolower($connection_info['driver'])) {
        case 'mysql':
          $connection_info['namespace'] = 'Drupal\\mysql\\Driver\\Database\\mysql';
          break;

        case 'pgsql':
          $connection_info['namespace'] = 'Drupal\\pgsql\\Driver\\Database\\pgsql';
          break;

        case 'sqlite':
          $connection_info['namespace'] = 'Drupal\\sqlite\\Driver\\Database\\sqlite';
          break;

      }
    }
    // Backwards compatibility layer for Drupal 8 style database connection
    // arrays. Those do not have the 'autoload' key set for core database
    // drivers.
    if (empty($connection_info['autoload']) && isset($connection_info['namespace'])) {
      switch (trim($connection_info['namespace'], '\\')) {
        case "Drupal\\mysql\\Driver\\Database\\mysql":
          $connection_info['autoload'] = "core/modules/mysql/src/Driver/Database/mysql/";
          break;

        case "Drupal\\pgsql\\Driver\\Database\\pgsql":
          $connection_info['autoload'] = "core/modules/pgsql/src/Driver/Database/pgsql/";
          break;

        case "Drupal\\sqlite\\Driver\\Database\\sqlite":
          $connection_info['autoload'] = "core/modules/sqlite/src/Driver/Database/sqlite/";
          break;

      }
    }
    assert(isset($connection_info['namespace']));
    assert(isset($connection_info['autoload']));
    return $connection_info;
  }
  
  /**
   * Adds database connection information for a given key/target.
   *
   * This method allows to add new connections at runtime.
   *
   * Under normal circumstances the preferred way to specify database
   * credentials is via settings.php. However, this method allows them to be
   * added at arbitrary times, such as during unit tests, when connecting to
   * admin-defined third party databases, etc. Use
   * \Drupal\Core\Database\Database::setActiveConnection to select the
   * connection to use.
   *
   * If the given key/target pair already exists, this method will be ignored.
   *
   * @param string $key
   *   The database key.
   * @param string $target
   *   The database target name.
   * @param UnprocessedConnectionInfoArray $info
   *   The database connection information, as defined in settings.php. The
   *   structure of this array depends on the database driver it is connecting
   *   to.
   * @param \Composer\Autoload\ClassLoader|null $class_loader
   *   (optional) The class loader. Used for adding the database driver to the
   *   autoloader if $info['autoload'] is set.
   * @param string $app_root
   *   (optional) The app root.
   *
   * @see \Drupal\Core\Database\Database::setActiveConnection
   */
  final public static function addConnectionInfo(string|int $key, string|int $target, array $info, ?ClassLoader $class_loader = NULL, ?string $app_root = NULL) : void {
    if (is_int($key)) {
      @trigger_error('Passing an integer value to the $key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if (is_int($target)) {
      @trigger_error('Passing an integer value to the $target parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if (empty(self::$databaseInfo[$key][$target])) {
      $connection_info = self::parseConnectionInfo($info);
      self::$databaseInfo[$key][$target] = $connection_info;
      // If the database driver is provided by a module, then its code may need
      // to be instantiated prior to when the module's root namespace is added
      // to the autoloader, because that happens during service container
      // initialization but the container definition is likely in the database.
      // Therefore, allow the connection info to specify an autoload directory
      // for the driver.
      if ($class_loader && $app_root) {
        $class_loader->addPsr4($connection_info['namespace'] . '\\', $app_root . '/' . $connection_info['autoload']);
        // When the database driver is extending from other database drivers,
        // then add autoload directory for the parent database driver modules
        // as well.
        if (!empty($connection_info['dependencies'])) {
          foreach ($connection_info['dependencies'] as $dependency) {
            $class_loader->addPsr4($dependency['namespace'] . '\\', $app_root . '/' . $dependency['autoload']);
          }
        }
      }
    }
  }
  
  /**
   * Gets information on the specified database connection.
   *
   * @param string $key
   *   (optional) The connection key for which to return information. If not
   *   specified, the 'default' connection key will be returned.
   *
   * @return array<string|int,ConnectionInfoArray>
   *   An associative array of database information, keyed by target. Defaults
   *   to an empty array.
   */
  final public static function getConnectionInfo(string|int $key = 'default') : array {
    if (is_int($key)) {
      @trigger_error('Passing an integer value to the $key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if (!empty(self::$databaseInfo[$key])) {
      return self::$databaseInfo[$key];
    }
    return [];
  }
  
  /**
   * Gets connection information for all available databases.
   *
   * @return array<string|int,array<string|int,ConnectionInfoArray>>
   *   An associative array of database information for all available database,
   *   keyed by the database key and target. Defaults to an empty array.
   */
  final public static function getAllConnectionInfo() : array {
    return self::$databaseInfo;
  }
  
  /**
   * Sets connection information for multiple databases.
   *
   * @param array<string|int,array<string|int,UnprocessedConnectionInfoArray>> $databases
   *   A multi-dimensional array specifying database connection parameters, as
   *   defined in settings.php.
   * @param \Composer\Autoload\ClassLoader|null $class_loader
   *   (optional) The class loader. Used for adding the database driver(s) to
   *   the autoloader if $databases[$key][$target]['autoload'] is set.
   * @param string|null $app_root
   *   (optional) The app root.
   */
  final public static function setMultipleConnectionInfo(array $databases, ?ClassLoader $class_loader = NULL, ?string $app_root = NULL) : void {
    foreach ($databases as $key => $targets) {
      foreach ($targets as $target => $info) {
        self::addConnectionInfo($key, $target, $info, $class_loader, $app_root);
      }
    }
  }
  
  /**
   * Rename a connection and its corresponding connection information.
   *
   * @param string $old_key
   *   The old connection key.
   * @param string $new_key
   *   The new connection key.
   *
   * @return bool
   *   TRUE in case of success, FALSE otherwise.
   */
  final public static function renameConnection(string|int $old_key, string|int $new_key) : bool {
    if (is_int($old_key)) {
      @trigger_error('Passing an integer value to the $old_key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if (is_int($new_key)) {
      @trigger_error('Passing an integer value to the $new_key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if (!empty(self::$databaseInfo[$old_key]) && empty(self::$databaseInfo[$new_key])) {
      // Migrate the database connection information.
      self::$databaseInfo[$new_key] = self::$databaseInfo[$old_key];
      unset(self::$databaseInfo[$old_key]);
      // Migrate over the DatabaseConnection object if it exists.
      if (isset(self::$connections[$old_key])) {
        self::$connections[$new_key] = self::$connections[$old_key];
        unset(self::$connections[$old_key]);
      }
      return TRUE;
    }
    return FALSE;
  }
  
  /**
   * Remove a connection and its corresponding connection information.
   *
   * @param string $key
   *   The connection key.
   *
   * @return bool
   *   TRUE in case of success, FALSE otherwise.
   */
  final public static function removeConnection(string|int $key) : bool {
    if (is_int($key)) {
      @trigger_error('Passing an integer value to the $key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if (isset(self::$databaseInfo[$key])) {
      self::closeConnection(NULL, $key);
      unset(self::$databaseInfo[$key]);
      return TRUE;
    }
    return FALSE;
  }
  
  /**
   * Opens a connection to the server specified by the given key and target.
   *
   * @param string $key
   *   The database connection key, as specified in settings.php.
   * @param string $target
   *   The database target to open.
   *
   * @throws \Drupal\Core\Database\ConnectionNotDefinedException
   * @throws \Drupal\Core\Database\DriverNotSpecifiedException
   *
   * @return \Drupal\Core\Database\Connection
   *   The opened database connection.
   */
  final protected static function openConnection(string|int $key, string|int $target) : Connection {
    if (is_int($key)) {
      @trigger_error('Passing an integer value to the $key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if (is_int($target)) {
      @trigger_error('Passing an integer value to the $target parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    // If the requested database does not exist then it is an unrecoverable
    // error.
    if (!isset(self::$databaseInfo[$key])) {
      throw new ConnectionNotDefinedException('The specified database connection is not defined: ' . $key);
    }
    if (!self::$databaseInfo[$key][$target]['driver']) {
      throw new DriverNotSpecifiedException('Driver not specified for this database connection: ' . $key);
    }
    $driver_class = self::$databaseInfo[$key][$target]['namespace'] . '\\Connection';
    $client_connection = $driver_class::open(self::$databaseInfo[$key][$target]);
    $new_connection = new $driver_class($client_connection, self::$databaseInfo[$key][$target]);
    assert($new_connection instanceof Connection);
    $new_connection->setTarget((string) $target);
    $new_connection->setKey((string) $key);
    // If we have any active logging objects for this connection key, we need
    // to associate them with the connection we just opened.
    if (!empty(self::$logs[$key])) {
      $new_connection->enableEvents(StatementEvent::all());
      $new_connection->setLogger(self::$logs[$key]);
    }
    return $new_connection;
  }
  
  /**
   * Closes a connection to the server specified by the given key and target.
   *
   * @param ?string $target
   *   (optional) The database target name. Defaults to NULL meaning that all
   *   target connections will be closed.
   * @param ?string $key
   *   (optional) The database connection key. Defaults to NULL which means the
   *   active key.
   */
  public static function closeConnection(string|int|null $target = NULL, string|int|null $key = NULL) : void {
    if ($target !== NULL && is_int($target)) {
      @trigger_error('Passing an integer value to the $target parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if ($key !== NULL && is_int($key)) {
      @trigger_error('Passing an integer value to the $key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    // Gets the active connection by default.
    if (!isset($key)) {
      $key = self::$activeKey;
    }
    if (isset($target) && isset(self::$connections[$key][$target])) {
      // @phpstan-ignore method.deprecated
      self::$connections[$key][$target]->commitAll();
      unset(self::$connections[$key][$target]);
    }
    elseif (isset(self::$connections[$key])) {
      foreach (self::$connections[$key] as $connection) {
        // @phpstan-ignore method.deprecated
        $connection->commitAll();
      }
      unset(self::$connections[$key]);
    }
    // When last connection for $key is closed, we also stop any active
    // logging.
    if (empty(self::$connections[$key])) {
      unset(self::$logs[$key]);
    }
    // Force garbage collection to run. This ensures that client connection
    // objects and results in the connection being closed are destroyed.
    gc_collect_cycles();
  }
  
  /**
   * Instructs the system to temporarily ignore a given key/target.
   *
   * At times we need to temporarily disable replica queries. To do so, call
   * this method with the database key and the target to disable. That database
   * key will then always fall back to 'default' for that key, even if it's
   * defined.
   *
   * @param string $key
   *   The database connection key.
   * @param string $target
   *   The target of the specified key to ignore.
   */
  public static function ignoreTarget(string|int $key, string|int $target) : void {
    if (is_int($key)) {
      @trigger_error('Passing an integer value to the $key parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    if (is_int($target)) {
      @trigger_error('Passing an integer value to the $target parameter in ' . __METHOD__ . '() is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Pass only string values instead. See https://www.drupal.org/node/3577925', E_USER_DEPRECATED);
    }
    self::$ignoreTargets[$key][$target] = TRUE;
  }
  
  /**
   * Converts a URL to a database connection info array.
   *
   * @param string $url
   *   The URL.
   * @param bool|null $include_test_drivers
   *   (optional) Whether to include test extensions. If FALSE, all 'tests'
   *   directories are excluded in the search. When NULL will be determined by
   *   the extension_discovery_scan_tests setting.
   *
   * @return ConnectionInfoArray
   *   The database connection info.
   *
   * @throws \InvalidArgumentException
   *   Exception thrown when the provided URL does not meet the minimum
   *   requirements.
   * @throws \RuntimeException
   *   Exception thrown when a module provided database driver does not exist.
   */
  public static function convertDbUrlToConnectionInfo(string $url, ?bool $include_test_drivers = NULL) : array {
    // Check that the URL is well formed, starting with 'scheme://', where
    // 'scheme' is a database driver name.
    if (preg_match('/^(.*):\\/\\//', $url, $matches) !== 1) {
      throw new \InvalidArgumentException("Missing scheme in URL '{$url}'");
    }
    $driverName = $matches[1];
    // Determine if the database driver is provided by a module.
    // @todo https://www.drupal.org/project/drupal/issues/3250999. Refactor when
    // all database drivers are provided by modules.
    $url_components = parse_url($url);
    $url_component_query = $url_components['query'] ?? '';
    parse_str($url_component_query, $query);
    // Use the driver name as the module name when the module name is not
    // provided.
    $module = $query['module'] ?? $driverName;
    assert(is_string($module));
    $driverNamespace = "Drupal\\{$module}\\Driver\\Database\\{$driverName}";
    /** @var \Drupal\Core\Extension\DatabaseDriver $driver */
    $driver = self::getDriverList()->includeTestDrivers($include_test_drivers)
      ->get($driverNamespace);
    // Set up an additional autoloader. We don't use the main autoloader as
    // this method can be called before Drupal is installed and is never
    // called during regular runtime.
    $additional_class_loader = new ClassLoader();
    $additional_class_loader->addPsr4($driverNamespace . '\\', $driver->getPath());
    $additional_class_loader->register();
    $connection_class = $driverNamespace . '\\Connection';
    if (!class_exists($connection_class)) {
      throw new \InvalidArgumentException("Can not convert '{$url}' to a database connection, class '{$connection_class}' does not exist");
    }
    // When the database driver is extending another database driver, then
    // add autoload info for the parent database driver as well.
    $autoloadInfo = $driver->getAutoloadInfo();
    if (isset($autoloadInfo['dependencies'])) {
      foreach ($autoloadInfo['dependencies'] as $dependency) {
        $additional_class_loader->addPsr4($dependency['namespace'] . '\\', $dependency['autoload']);
      }
    }
    $additional_class_loader->register(TRUE);
    /** @var ConnectionInfoArray $options */
    $options = $connection_class::createConnectionOptionsFromUrl($url);
    // Add the necessary information to autoload code.
    // @see \Drupal\Core\Site\Settings::initialize()
    $options['autoload'] = $driver->getPath() . DIRECTORY_SEPARATOR;
    if (isset($autoloadInfo['dependencies'])) {
      $options['dependencies'] = $autoloadInfo['dependencies'];
    }
    return $options;
  }
  
  /**
   * Returns the list provider for available database drivers.
   *
   * @return \Drupal\Core\Extension\DatabaseDriverList
   *   The list provider for available database drivers.
   */
  public static function getDriverList() : DatabaseDriverList {
    if (\Drupal::hasContainer() && \Drupal::hasService('extension.list.database_driver')) {
      return \Drupal::service('extension.list.database_driver');
    }
    else {
      return new DatabaseDriverList(DRUPAL_ROOT, 'database_driver', new NullBackend('database_driver'));
    }
  }
  
  /**
   * Gets database connection info as a URL.
   *
   * @param string $key
   *   (Optional) The database connection key. If not specified, the 'default'
   *   connection key will be returned.
   *
   * @return string
   *   The connection info as a URL.
   *
   * @throws \RuntimeException
   *   When the database connection is not defined.
   */
  public static function getConnectionInfoAsUrl(string|int $key = 'default') : string {
    $db_info = static::getConnectionInfo($key);
    if (empty($db_info) || empty($db_info['default'])) {
      throw new \RuntimeException("Database connection {$key} not defined or missing the 'default' settings");
    }
    $namespace = $db_info['default']['namespace'];
    // Add the module name to the connection options to make it easy for the
    // connection class's createUrlFromConnectionOptions() method to add it to
    // the URL.
    $db_info['default']['module'] = explode('\\', $namespace)[1];
    $connection_class = $namespace . '\\Connection';
    $url = $connection_class::createUrlFromConnectionOptions($db_info['default']);
    assert(is_string($url));
    return $url;
  }
  
  /**
   * Calls commitAll() on all the open connections.
   *
   * If drupal_register_shutdown_function() exists the commit will occur during
   * shutdown so that it occurs at the latest possible moment.
   *
   * @param bool $shutdown
   *   Internal param to denote that the method is being called by
   *   _drupal_shutdown_function().
   *
   * @internal
   *   This method exists only to work around a bug caused by Drupal incorrectly
   *   relying on object destruction order to commit transactions. Xdebug 3.3.0
   *   changes the order of object destruction when the develop mode is enabled.
   *
   * @deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. There is no
   *   replacement.
   *
   * @see https://www.drupal.org/node/3524461
   */
  public static function commitAllOnShutdown(bool $shutdown = FALSE) : void {
    // Only soft deprecation (no @trigger_error) to avoid thousands of
    // occurrences per test run. PHPStan reports usage errors anyway.
    static $registered = FALSE;
    if ($shutdown) {
      foreach (self::$connections as $targets) {
        foreach ($targets as $connection) {
          $connection->commitAll();
        }
      }
      return;
    }
    if (!function_exists('drupal_register_shutdown_function')) {
      return;
    }
    if (!$registered) {
      $registered = TRUE;
      drupal_register_shutdown_function('\\Drupal\\Core\\Database\\Database::commitAllOnShutdown', TRUE);
    }
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary
Database::$activeKey protected static property The key of the currently active database connection.
Database::$connections protected static property A nested array of active connections, keyed by database key and target.
Database::$databaseInfo protected static property A processed copy of the database connection information from settings.php.
Database::$ignoreTargets protected static property A list of key/target credentials to simply ignore.
Database::$logs protected static property An array of active query log objects.
Database::addConnectionInfo final public static function Adds database connection information for a given key/target.
Database::closeConnection public static function Closes a connection to the server specified by the given key and target.
Database::commitAllOnShutdown Deprecated public static function Calls commitAll() on all the open connections.
Database::convertDbUrlToConnectionInfo public static function Converts a URL to a database connection info array.
Database::getAllConnectionInfo final public static function Gets connection information for all available databases.
Database::getConnection final public static function Gets the connection object for the specified database key and target.
Database::getConnectionInfo final public static function Gets information on the specified database connection.
Database::getConnectionInfoAsUrl public static function Gets database connection info as a URL.
Database::getDriverList public static function Returns the list provider for available database drivers.
Database::getLog final public static function Retrieves the queries logged on for given logging key.
Database::ignoreTarget public static function Instructs the system to temporarily ignore a given key/target.
Database::isActiveConnection final public static function Determines if there is an active connection.
Database::openConnection final protected static function Opens a connection to the server specified by the given key and target.
Database::parseConnectionInfo final public static function Process the configuration file for database information.
Database::removeConnection final public static function Remove a connection and its corresponding connection information.
Database::renameConnection final public static function Rename a connection and its corresponding connection information.
Database::setActiveConnection final public static function Sets the active connection to the specified key.
Database::setMultipleConnectionInfo final public static function Sets connection information for multiple databases.
Database::startLog final public static function Starts logging a given logging key on the specified connection.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.