View source
<?php
namespace Drupal\Driver\Database\sqlsrv\Install;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Install\Tasks as InstallTasks;
use Drupal\Core\Database\DatabaseNotFoundException;
use Drupal\Driver\Database\sqlsrv\Connection;
use Drupal\Driver\Database\sqlsrv\Utils;
class Tasks extends InstallTasks {
protected $pdoDriver = 'sqlsrv';
public function __construct() {
$this->tasks[] = [
'function' => 'checkEncoding',
'arguments' => [],
];
$this->tasks[] = [
'function' => 'initializeDatabase',
'arguments' => [],
];
$this->tasks[] = [
'function' => 'enableModule',
'arguments' => [],
];
}
public function name() {
return t('SQLServer');
}
public function minimumVersion() {
return '13.0';
}
protected function connect() {
try {
Database::setActiveConnection();
Database::getConnection();
$this
->pass('Drupal can CONNECT to the database ok.');
} catch (\Exception $e) {
if ($e
->getCode() == Connection::DATABASE_NOT_FOUND) {
$connection_info = Database::getConnectionInfo();
$database = $connection_info['default']['database'];
unset($connection_info['default']['database']);
Database::removeConnection('default');
Database::addConnectionInfo('default', 'default', $connection_info['default']);
try {
Database::getConnection()
->createDatabase($database);
Database::closeConnection();
Database::removeConnection('default');
$connection_info['default']['database'] = $database;
Database::addConnectionInfo('default', 'default', $connection_info['default']);
Database::getConnection();
$this
->pass('Drupal can CONNECT to the database ok.');
} catch (DatabaseNotFoundException $e) {
$this
->fail(t('Database %database not found. The server reports the following message when attempting to create the database: %error.', [
'%database' => $database,
'%error' => $e
->getMessage(),
]));
return FALSE;
} catch (\PDOException $e) {
$this
->fail(t('Database %database not found. The server reports the following message when attempting to create the database: %error.', [
'%database' => $database,
'%error' => $e
->getMessage(),
]));
return FALSE;
}
}
else {
$this
->fail(t('Failed to connect to your database server. The server reports the following message: %error.<ul><li>Is the database server running?</li><li>Does the database exist, and have you entered the correct database name?</li><li>Have you entered the correct username and password?</li><li>Have you entered the correct database hostname?</li></ul>', [
'%error' => $e
->getMessage(),
]));
return FALSE;
}
}
return TRUE;
}
protected function checkEncoding() {
try {
$database = Database::getConnection();
$schema = $database
->schema();
$collation = $schema
->getCollation();
if (stristr($collation, '_CI_') !== FALSE) {
$this
->pass(t('Database is encoded in case insensitive collation: $collation'));
}
else {
$this
->fail(t('The %driver database is using %current collation, but must use case insensitive collation to work with Drupal. Recreate the database with this collation. See !link for more details.', [
'%current' => $collation,
'%driver' => $this
->name(),
':link' => '<a href="INSTALL.sqlsrv.txt">INSTALL.sqlsrv.txt</a>',
]));
}
} catch (\Exception $e) {
$this
->fail(t('Drupal could not determine the encoding of the database was set to UTF-8'));
}
}
public function initializeDatabase() {
try {
$connection = Database::getConnection();
Utils::DeployCustomFunctions($connection);
$this
->pass(t('SQLServer has initialized itself.'));
} catch (\Exception $e) {
$this
->fail(t('Drupal could not be correctly setup with the existing database. Revise any errors.'));
}
}
public function enableModule() {
}
public function getFormOptions(array $database) {
$form = parent::getFormOptions($database);
if (empty($form['advanced_options']['port']['#default_value'])) {
$form['advanced_options']['port']['#default_value'] = '1433';
}
$form['advanced_options']['schema'] = [
'#type' => 'textfield',
'#title' => t('Schema'),
'#default_value' => empty($database['schema']) ? 'dbo' : $database['schema'],
'#size' => 10,
'#required' => FALSE,
];
$form['advanced_options']['cache_schema'] = [
'#type' => 'checkbox',
'#title' => t('Cache Schema Definitions'),
'#description' => t('Allow the table schema to be cached. This will significantly speed up the site, but the schema must be stable.'),
'#return_value' => 'true',
];
$form['username']['#required'] = FALSE;
$form['username']['#description'] = t('Leave username (and password) blank to use Windows authentication.');
return $form;
}
}