1.20.x database.inc | final protected static Database::openConnection($key, $target) |
Opens a connection to the server specified by the given key and target.
Parameters
$key: The database connection key, as specified in settings.php. The default is "default".
$target: The database target to open.
Return value
DatabaseConnection: The created database connection object.
Throws
DatabaseConnectionNotDefinedException
DatabaseDriverNotSpecifiedException
File
- includes/
database/ database.inc, line 1862 - Core systems for the database layer.
Class
- Database
- Primary front-controller for the database system.
Code
final protected static function openConnection($key, $target) {
if (empty(self::$databaseInfo)) {
self::parseConnectionInfo();
}
// If the requested database does not exist then it is an unrecoverable
// error.
if (!isset(self::$databaseInfo[$key])) {
throw new DatabaseConnectionNotDefinedException('The specified database connection is not defined: ' . $key);
}
if (!$driver = self::$databaseInfo[$key][$target]['driver']) {
throw new DatabaseDriverNotSpecifiedException('Driver not specified for this database connection: ' . $key);
}
$backends = silkscreen_find_backends('database');
$driver_class = 'DatabaseConnection_' . $driver;
if (array_key_exists($driver_class, $backends)) {
require_once $backends[$driver_class];
}
/* @var DatabaseConnection $new_connection */
$new_connection = new $driver_class(self::$databaseInfo[$key][$target]);
$new_connection->setTarget($target);
$new_connection->setKey($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->setLogger(self::$logs[$key]);
}
return $new_connection;
}