Unverified Commit 3f0fd621 authored by Stefan Lorenz's avatar Stefan Lorenz Committed by Michael Moravec

Improve handling of schemas in SQL Server >= 2008

parent 684122ca
...@@ -27,6 +27,16 @@ namespace Doctrine\DBAL\Platforms; ...@@ -27,6 +27,16 @@ namespace Doctrine\DBAL\Platforms;
*/ */
class SQLServer2008Platform extends SQLServer2005Platform 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} * {@inheritDoc}
*/ */
......
...@@ -28,8 +28,10 @@ use Doctrine\DBAL\Schema\Index; ...@@ -28,8 +28,10 @@ use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types; use Doctrine\DBAL\Types;
use function explode;
use function implode; use function implode;
use function sprintf; use function sprintf;
use function strpos;
/** /**
* The SQLServerPlatform provides the behavior, features and SQL dialect of the * The SQLServerPlatform provides the behavior, features and SQL dialect of the
...@@ -330,13 +332,22 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -330,13 +332,22 @@ class SQLServerPlatform extends AbstractPlatform
*/ */
protected function getCreateColumnCommentSQL($tableName, $columnName, $comment) 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( return $this->getAddExtendedPropertySQL(
'MS_Description', 'MS_Description',
$comment, $comment,
'SCHEMA', 'SCHEMA',
'dbo', $schemaSQL,
'TABLE', 'TABLE',
$tableName, $tableSQL,
'COLUMN', 'COLUMN',
$columnName $columnName
); );
...@@ -678,13 +689,22 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -678,13 +689,22 @@ class SQLServerPlatform extends AbstractPlatform
*/ */
protected function getAlterColumnCommentSQL($tableName, $columnName, $comment) 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( return $this->getUpdateExtendedPropertySQL(
'MS_Description', 'MS_Description',
$comment, $comment,
'SCHEMA', 'SCHEMA',
'dbo', $schemaSQL,
'TABLE', 'TABLE',
$tableName, $tableSQL,
'COLUMN', 'COLUMN',
$columnName $columnName
); );
...@@ -708,12 +728,21 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -708,12 +728,21 @@ class SQLServerPlatform extends AbstractPlatform
*/ */
protected function getDropColumnCommentSQL($tableName, $columnName) 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( return $this->getDropExtendedPropertySQL(
'MS_Description', 'MS_Description',
'SCHEMA', 'SCHEMA',
'dbo', $schemaSQL,
'TABLE', 'TABLE',
$tableName, $tableSQL,
'COLUMN', 'COLUMN',
$columnName $columnName
); );
......
...@@ -198,6 +198,10 @@ class SQLServerSchemaManager extends AbstractSchemaManager ...@@ -198,6 +198,10 @@ class SQLServerSchemaManager extends AbstractSchemaManager
*/ */
protected function _getPortableTableDefinition($table) protected function _getPortableTableDefinition($table)
{ {
if (isset($table['schema_name']) && $table['schema_name'] !== 'dbo') {
return $table['schema_name'] . '.' . $table['name'];
}
return $table['name']; return $table['name'];
} }
......
...@@ -3,11 +3,13 @@ ...@@ -3,11 +3,13 @@
namespace Doctrine\Tests\DBAL\Functional\Schema; namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\Common\EventManager; use Doctrine\Common\EventManager;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Events; use Doctrine\DBAL\Events;
use Doctrine\DBAL\Platforms\OraclePlatform; use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff; use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\SchemaDiff;
use Doctrine\DBAL\Schema\Sequence; use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff;
...@@ -42,6 +44,23 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest ...@@ -42,6 +44,23 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->_sm = $this->_conn->getSchemaManager(); $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 * @group DBAL-1220
*/ */
...@@ -570,6 +589,31 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest ...@@ -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() public function testCreateAndListViews()
{ {
if (!$this->_sm->getDatabasePlatform()->supportsViews()) { 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