| Versionen | |
|---|---|
| drupal7 | final protected static Database::openConnection($key, $target) |
Open a connection to the server specified by the given key and target.
$key The database connection key, as specified in settings.php. The default is "default".
$target The database target to open.
includes/
<?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