You are here

public function DatabaseConnection_sqlsrv::__construct in Drupal driver for SQL Server and SQL Azure 7.2

Same name and namespace in other branches
  1. 7.3 sqlsrv/database.inc \DatabaseConnection_sqlsrv::__construct()
  2. 7 sqlsrv/database.inc \DatabaseConnection_sqlsrv::__construct()

Override of DatabaseConnection::databaseType().

@status complete

Overrides DatabaseConnection::__construct

File

sqlsrv/database.inc, line 78
Database interface code for Microsoft SQL Server.

Class

DatabaseConnection_sqlsrv
Summary of DatabaseConnection_sqlsrv

Code

public function __construct(array $connection_options = array()) {
  global $conf;

  // Store connection options for future reference.
  $this->connectionOptions =& $connection_options;

  // Set our custom statement class.
  $this->statementClass = 'DatabaseStatement_sqlsrv';

  // This driver defaults to transaction support, except if explicitly passed FALSE.
  $this->transactionSupport = !isset($connection_options['transactions']) || $connection_options['transactions'] !== FALSE;
  $this->transactionalDDLSupport = $this->transactionSupport;

  // Build the DSN.
  $options = array();
  $options['Server'] = $connection_options['host'];

  // If a port is specified and this is NOT a named instance
  if (!empty($connection_options['port']) && stripos($connection_options['host'], '\\') === false) {
    $options['Server'] .= ',' . $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'];
  }

  // Set isolation level if specified.
  if ($level = DatabaseUtils::GetConfigConstant('MSSQL_DEFAULT_ISOLATION_LEVEL', FALSE)) {
    $options['TransactionIsolation'] = $level;
  }
  $options['MultipleActiveResultSets'] = 'false';

  // Set default direct query behaviour
  $this->directQuery = DatabaseUtils::GetConfigBoolean('MSSQL_DEFAULT_DIRECTQUERY');
  $this->statementCaching = DatabaseUtils::GetConfigBoolean('MSSQL_STATEMENT_CACHING');

  // Build the DSN
  $dsn = 'sqlsrv:';
  foreach ($options as $key => $value) {
    $dsn .= (empty($key) ? '' : "{$key}=") . $value . ';';
  }

  // PDO Options are set at a connection level.
  // and apply to all statements.
  // Allow PDO options to be overridden.
  $connection_options += array(
    'pdo' => array(),
  );
  $connection_options['pdo'] += array(
    PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => TRUE,
    // Do not convert numeric values to strings when fetching.
    // This is set for legacy reasons. Core tests prefer it to be TRUE.
    PDO::ATTR_STRINGIFY_FETCHES => FALSE,
    // Set proper error mode for all statements
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  );

  // Set a Statement class, unless the driver opted out.
  if (!empty($this->statementClass)) {
    $connection_options['pdo'][PDO::ATTR_STATEMENT_CLASS] = array(
      $this->statementClass,
      array(
        $this,
      ),
    );
  }

  // Initialize and prepare the connection prefix.
  $this
    ->setPrefix(isset($this->connectionOptions['prefix']) ? $this->connectionOptions['prefix'] : '');

  // Set the connection.
  $this->connection = new PDO($dsn, $connection_options['username'], $connection_options['password'], $connection_options['pdo']);
}