View source
<?php
namespace Drupal\Driver\Database\sqlsrv;
use PDO;
class ConnectionSettings {
private $_defaultIsolationLevel;
private $_defaultDirectQueries;
private $_defaultStatementCaching;
private $_statementCachingMode;
private $_appendStackComments;
private $_additionalDSN;
private static $default_driver_settings = [
'default_isolation_level' => false,
'default_direct_queries' => true,
'default_statement_caching' => false,
'statement_caching_mode' => 'disabled',
'append_stack_comments' => false,
'additional_dsn' => [],
];
private function CheckValid($name, $value, array $allowed) {
if (!in_array($value, $allowed)) {
throw new \Exception("Invalid driver setting for {$name}");
}
return $value;
}
public static function instanceFromData($configuration = array()) {
$configuration = array_merge(static::$default_driver_settings, $configuration);
return new ConnectionSettings($configuration);
}
private function __construct($configuration) {
$this->_defaultIsolationLevel = $this
->CheckValid('default_isolation_level', $configuration['default_isolation_level'], array(
false,
PDO::SQLSRV_TXN_READ_UNCOMMITTED,
PDO::SQLSRV_TXN_READ_COMMITTED,
PDO::SQLSRV_TXN_REPEATABLE_READ,
PDO::SQLSRV_TXN_SNAPSHOT,
PDO::SQLSRV_TXN_SERIALIZABLE,
));
$this->_defaultDirectQueries = $this
->CheckValid('default_direct_queries', $configuration['default_direct_queries'], array(
true,
false,
));
$this->_defaultStatementCaching = $this
->CheckValid('default_statement_caching', $configuration['default_statement_caching'], array(
true,
false,
));
$this->_statementCachingMode = $this
->CheckValid('statement_caching_mode', $configuration['statement_caching_mode'], array(
'disabled',
'on-demand',
'always',
));
$this->_appendStackComments = $this
->CheckValid('append_stack_comments', $configuration['append_stack_comments'], array(
true,
false,
));
$this->_additionalDSN = $configuration['additional_dsn'];
}
public function exportConfiguration() {
return array(
'default_isolation_level' => $this
->GetDefaultIsolationLevel(),
'default_direct_queries' => $this
->GetDefaultDirectQueries(),
'statement_caching_mode' => $this
->GetStatementCachingMode(),
'append_stack_comments' => $this
->GetAppendCallstackComment(),
'default_statement_caching' => $this
->GetDeafultStatementCaching(),
'additional_dsn' => $this
->GetAdditionalDSN(),
);
}
public function GetAdditionalDSN() {
return $this->_additionalDSN;
}
public function GetDefaultIsolationLevel() {
return $this->_defaultIsolationLevel;
}
public function GetDefaultTransactionIsolationLevelInStatement() {
return str_replace('_', ' ', $this
->GetDefaultIsolationLevel());
}
public function GetDeafultStatementCaching() {
return $this->_defaultStatementCaching;
}
public function GetDefaultDirectQueries() {
return $this->_defaultDirectQueries;
}
public function GetAppendCallstackComment() {
return $this->_appendStackComments;
}
public function GetStatementCachingMode() {
return $this->_statementCachingMode;
}
public function buildDSN(array $options) {
$options = $options + $this
->GetAdditionalDSN();
$dsn = 'sqlsrv:';
foreach ($options as $key => $value) {
$dsn .= (empty($key) ? '' : "{$key}=") . $value . ';';
}
return $dsn;
}
}