openConnection

  1. drupal
    1. drupal7
Versionen
drupal7 final protected static Database::openConnection($key, $target)

Open a connection to the server specified by the given key and target.

Übergabeparameter

$key The database connection key, as specified in settings.php. The default is "default".

$target The database target to open.

Verwandte Themen

Code

includes/database/database.inc, line 1328

<?php
final protected static function openConnection($key, $target) {
  global $db_prefix;

  if (empty(self::$databaseInfo)) {
    self::parseConnectionInfo();
  }
  try {
    // If the requested database does not exist then it is an unrecoverable error.
    if (!isset(self::$databaseInfo[$key])) {
      throw new Exception('DB does not exist');
    }

    if (!$driver = self::$databaseInfo[$key][$target]['driver']) {
      throw new Exception('Drupal is not set up');
    }

    // We cannot rely on the registry yet, because the registry requires
    // an open database connection.
    $driver_class = 'DatabaseConnection_' . $driver;
    require_once DRUPAL_ROOT . '/includes/database/' . $driver . '/database.inc';
    $new_connection = new $driver_class(self::$databaseInfo[$key][$target]);
    $new_connection->setTarget($target);

    // 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->setLogger(self::$logs[$key]);
    }

    // We need to pass around the simpletest database prefix in the request
    // and we put that in the user_agent header.
    if (preg_match("/^simpletest\d+$/", $_SERVER['HTTP_USER_AGENT'])) {
      $db_prefix .= $_SERVER['HTTP_USER_AGENT'];
    }
    return $new_connection;
  }
  catch (Exception $e) {
    // It is extremely rare that an exception will be generated here other
    // than when installing. We therefore intercept it and try the installer,
    // passing on the exception otherwise.
    _db_check_install_needed();
    throw $e;
  }
}
?>

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen