Unverified Commit 61c5e4e3 authored by Sergei Morozov's avatar Sergei Morozov Committed by GitHub

Merge pull request #3020 from stlrnz/mssql-schema

Improve handling of schemas in SQL Server >= 2008
parents 179ff5d9 3f0fd621
......@@ -27,6 +27,16 @@ namespace Doctrine\DBAL\Platforms;
*/
class SQLServer2008Platform extends SQLServer2005Platform
{
/**
* {@inheritDoc}
*/
public function getListTablesSQL()
{
// "sysdiagrams" table must be ignored as it's internal SQL Server table for Database Diagrams
// Category 2 must be ignored as it is "MS SQL Server 'pseudo-system' object[s]" for replication
return "SELECT name, SCHEMA_NAME (uid) AS schema_name FROM sysobjects WHERE type = 'U' AND name != 'sysdiagrams' AND category != 2 ORDER BY name";
}
/**
* {@inheritDoc}
*/
......
......@@ -28,8 +28,10 @@ use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types;
use function explode;
use function implode;
use function sprintf;
use function strpos;
/**
* The SQLServerPlatform provides the behavior, features and SQL dialect of the
......@@ -330,13 +332,22 @@ class SQLServerPlatform extends AbstractPlatform
*/
protected function getCreateColumnCommentSQL($tableName, $columnName, $comment)
{
if (strpos($tableName, '.') !== false) {
[$schemaSQL, $tableSQL] = explode('.', $tableName);
$schemaSQL = $this->quoteStringLiteral($schemaSQL);
$tableSQL = $this->quoteStringLiteral($tableSQL);
} else {
$schemaSQL = "'dbo'";
$tableSQL = $this->quoteStringLiteral($tableName);
}
return $this->getAddExtendedPropertySQL(
'MS_Description',
$comment,
'SCHEMA',
'dbo',
$schemaSQL,
'TABLE',
$tableName,
$tableSQL,
'COLUMN',
$columnName
);
......@@ -678,13 +689,22 @@ class SQLServerPlatform extends AbstractPlatform
*/
protected function getAlterColumnCommentSQL($tableName, $columnName, $comment)
{
if (strpos($tableName, '.') !== false) {
[$schemaSQL, $tableSQL] = explode('.', $tableName);
$schemaSQL = $this->quoteStringLiteral($schemaSQL);
$tableSQL = $this->quoteStringLiteral($tableSQL);
} else {
$schemaSQL = "'dbo'";
$tableSQL = $this->quoteStringLiteral($tableName);
}
return $this->getUpdateExtendedPropertySQL(
'MS_Description',
$comment,
'SCHEMA',
'dbo',
$schemaSQL,
'TABLE',
$tableName,
$tableSQL,
'COLUMN',
$columnName
);
......@@ -708,12 +728,21 @@ class SQLServerPlatform extends AbstractPlatform
*/
protected function getDropColumnCommentSQL($tableName, $columnName)
{
if (strpos($tableName, '.') !== false) {
[$schemaSQL, $tableSQL] = explode('.', $tableName);
$schemaSQL = $this->quoteStringLiteral($schemaSQL);
$tableSQL = $this->quoteStringLiteral($tableSQL);
} else {
$schemaSQL = "'dbo'";
$tableSQL = $this->quoteStringLiteral($tableName);
}
return $this->getDropExtendedPropertySQL(
'MS_Description',
'SCHEMA',
'dbo',
$schemaSQL,
'TABLE',
$tableName,
$tableSQL,
'COLUMN',
$columnName
);
......
......@@ -198,6 +198,10 @@ class SQLServerSchemaManager extends AbstractSchemaManager
*/
protected function _getPortableTableDefinition($table)
{
if (isset($table['schema_name']) && $table['schema_name'] !== 'dbo') {
return $table['schema_name'] . '.' . $table['name'];
}
return $table['name'];
}
......
......@@ -3,11 +3,13 @@
namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\SchemaDiff;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
......@@ -42,6 +44,23 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->_sm = $this->_conn->getSchemaManager();
}
protected function tearDown()
{
parent::tearDown();
$this->_sm->tryMethod('dropTable', 'testschema.my_table_in_namespace');
//TODO: SchemaDiff does not drop removed namespaces?
try {
//sql server versions below 2016 do not support 'IF EXISTS' so we have to catch the exception here
$this->_conn->exec('DROP SCHEMA testschema');
} catch (DBALException $e) {
return;
}
}
/**
* @group DBAL-1220
*/
......@@ -570,6 +589,31 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
}
}
public function testTableInNamespace()
{
if (! $this->_sm->getDatabasePlatform()->supportsSchemas()) {
$this->markTestSkipped('Schema definition is not supported by this platform.');
}
//create schema
$diff = new SchemaDiff();
$diff->newNamespaces[] = 'testschema';
foreach ($diff->toSql($this->_sm->getDatabasePlatform()) as $sql) {
$this->_conn->exec($sql);
}
//test if table is create in namespace
$this->createTestTable('testschema.my_table_in_namespace');
self::assertContains('testschema.my_table_in_namespace', $this->_sm->listTableNames());
//tables without namespace should be created in default namespace
//default namespaces are ignored in table listings
$this->createTestTable('my_table_not_in_namespace');
self::assertContains('my_table_not_in_namespace', $this->_sm->listTableNames());
}
public function testCreateAndListViews()
{
if (!$this->_sm->getDatabasePlatform()->supportsViews()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment