You are here

public static function Connection::open in Drupal driver for SQL Server and SQL Azure 3.0.x

Same name and namespace in other branches
  1. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php \Drupal\Driver\Database\sqlsrv\Connection::open()
  2. 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php \Drupal\Driver\Database\sqlsrv\Connection::open()

Uses SQL Server format.

Overrides Connection::open

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php, line 392

Class

Connection
Sqlsvr implementation of \Drupal\Core\Database\Connection.

Namespace

Drupal\Driver\Database\sqlsrv

Code

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 (\Exception $e) {
    if ($e
      ->getCode() == static::DATABASE_NOT_FOUND) {
      throw new DatabaseNotFoundException($e
        ->getMessage(), $e
        ->getCode(), $e);
    }
    throw new $e();
  }
  return $pdo;
}