public static function Connection::open in Drupal driver for SQL Server and SQL Azure 3.1.x
Same name and namespace in other branches
- 4.2.x src/Driver/Database/sqlsrv/Connection.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Connection::open()
- 4.0.x src/Driver/Database/sqlsrv/Connection.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Connection::open()
- 4.1.x src/Driver/Database/sqlsrv/Connection.php \Drupal\sqlsrv\Driver\Database\sqlsrv\Connection::open()
Uses SQL Server format.
Overrides Connection::open
File
- src/
Driver/ Database/ sqlsrv/ Connection.php, line 391
Class
- Connection
- Sqlsvr implementation of \Drupal\Core\Database\Connection.
Namespace
Drupal\sqlsrv\Driver\Database\sqlsrvCode
public static function open(array &$connection_options = []) {
// Build the DSN.
$options = [];
$options['Server'] = $connection_options['host'] . (!empty($connection_options['port']) ? ',' . $connection_options['port'] : '');
// We might not have a database in the
// connection options, for example, during
// database creation in Install.
if (!empty($connection_options['database'])) {
$options['Database'] = $connection_options['database'];
}
// Build the DSN.
$dsn = 'sqlsrv:';
foreach ($options as $key => $value) {
$dsn .= (empty($key) ? '' : "{$key}=") . $value . ';';
}
// Allow PDO options to be overridden.
$connection_options += [
'pdo' => [],
];
$connection_options['pdo'] += [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
];
// Set a Statement class, unless the driver opted out.
// $connection_options['pdo'][PDO::ATTR_STATEMENT_CLASS] =
// array(Statement::class, array(Statement::class));.
// Actually instantiate the PDO.
try {
$pdo = new \PDO($dsn, $connection_options['username'], $connection_options['password'], $connection_options['pdo']);
} catch (\PDOException $e) {
if ($e
->getCode() == static::DATABASE_NOT_FOUND) {
throw new DatabaseNotFoundException($e
->getMessage(), $e
->getCode(), $e);
}
if ($e
->getCode() == static::ACCESS_DENIED) {
throw new DatabaseAccessDeniedException($e
->getMessage(), $e
->getCode(), $e);
}
throw $e;
}
return $pdo;
}