View source
<?php
namespace Drupal\Driver\Database\sqlsrv;
use Drupal\Core\Site\Settings;
use PDO;
class DriverSettings {
private $_defaultIsolationLevel;
private $_defaultDirectQueries;
private $_defaultBypassQueryPreprocess;
private $_defaultStatementCaching;
private $_useNativeUpsert;
private $_useNativeMerge;
private $_statementCachingMode;
private $_appendStackComments;
private $_enableTransactions;
private $_monitorDriverStatus;
private static $default_driver_settings = array(
'default_isolation_level' => FALSE,
'default_direct_queries' => FALSE,
'default_statement_caching' => FALSE,
'use_native_upsert' => FALSE,
'use_native_merge' => FALSE,
'statement_caching_mode' => 'disabled',
'append_stack_comments' => FALSE,
'default_bypass_query_preprocess' => FALSE,
'enable_transactions' => TRUE,
'monitor_driver_status' => TRUE,
);
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 instanceFromSettings() {
$configuration = Settings::get('mssql_configuration', array());
$configuration = array_merge(static::$default_driver_settings, $configuration);
return new DriverSettings($configuration);
}
public static function instanceFromData($configuration) {
$configuration = array_merge(static::$default_driver_settings, $configuration);
return new DriverSettings($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->_defaultBypassQueryPreprocess = $this
->CheckValid('default_bypass_query_preprocess', $configuration['default_bypass_query_preprocess'], array(
TRUE,
FALSE,
));
$this->_useNativeUpsert = $this
->CheckValid('use_native_upsert', $configuration['use_native_upsert'], array(
TRUE,
FALSE,
));
$this->_useNativeMerge = $this
->CheckValid('use_native_merge', $configuration['use_native_merge'], 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->_enableTransactions = $this
->CheckValid('enable_transactions', $configuration['enable_transactions'], array(
TRUE,
FALSE,
));
$this->_monitorDriverStatus = $this
->CheckValid('monitor_driver_status', $configuration['monitor_driver_status'], array(
TRUE,
FALSE,
));
}
public function exportConfiguration() {
return array(
'default_isolation_level' => $this
->GetDefaultIsolationLevel(),
'default_direct_queries' => $this
->GetDefaultDirectQueries(),
'use_native_upsert' => $this
->GetUseNativeUpsert(),
'use_native_merge' => $this
->GetUseNativeMerge(),
'statement_caching_mode' => $this
->GetStatementCachingMode(),
'append_stack_comments' => $this
->GetAppendCallstackComment(),
'default_bypass_query_preprocess' => $this
->GetDeafultBypassQueryPreprocess(),
'default_statement_caching' => $this
->GetDeafultStatementCaching(),
'monitorDriverStatus' => $this
->GetMonitorDriverStatus(),
);
}
public function GetMonitorDriverStatus() {
return $this->_monitorDriverStatus;
}
public function GetEnableTransactions() {
return $this->_enableTransactions;
}
public function GetDefaultIsolationLevel() {
return $this->_defaultIsolationLevel;
}
public function GetDefaultTransactionIsolationLevelInStatement() {
return str_replace('_', ' ', $this
->GetDefaultIsolationLevel());
}
public function GetDeafultStatementCaching() {
return $this->_defaultStatementCaching;
}
public function GetDeafultBypassQueryPreprocess() {
return $this->_defaultBypassQueryPreprocess;
}
public function GetDefaultDirectQueries() {
return $this->_defaultDirectQueries;
}
public function GetUseNativeUpsert() {
return $this->_useNativeUpsert;
}
public function GetUseNativeMerge() {
return $this->_useNativeMerge;
}
public function GetAppendCallstackComment() {
return $this->_appendStackComments;
}
public function GetStatementCachingMode() {
return $this->_statementCachingMode;
}
}