Commit c458fe72 authored by Steve Müller's avatar Steve Müller

fix SQL Server default constraint name generation for quoted identifiers

parent 1a9408c5
...@@ -322,11 +322,13 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -322,11 +322,13 @@ class SQLServerPlatform extends AbstractPlatform
throw new \InvalidArgumentException("Incomplete column definition. 'default' required."); throw new \InvalidArgumentException("Incomplete column definition. 'default' required.");
} }
$columnName = new Identifier($column['name']);
return return
' CONSTRAINT ' . ' CONSTRAINT ' .
$this->generateDefaultConstraintName($table, $column['name']) . $this->generateDefaultConstraintName($table, $column['name']) .
$this->getDefaultValueDeclarationSQL($column) . $this->getDefaultValueDeclarationSQL($column) .
' FOR ' . $column['name']; ' FOR ' . $columnName->getQuotedName($this);
} }
/** /**
...@@ -1519,6 +1521,9 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -1519,6 +1521,9 @@ class SQLServerPlatform extends AbstractPlatform
*/ */
private function generateIdentifierName($identifier) private function generateIdentifierName($identifier)
{ {
return strtoupper(dechex(crc32($identifier))); // Always generate name for unquoted identifiers to ensure consistency.
$identifier = new Identifier($identifier);
return strtoupper(dechex(crc32($identifier->getName())));
} }
} }
...@@ -920,4 +920,185 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas ...@@ -920,4 +920,185 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas
"EXEC sp_RENAME N'[schema].[table].[foo]', N'[bar]', N'INDEX'", "EXEC sp_RENAME N'[schema].[table].[foo]', N'[bar]', N'INDEX'",
); );
} }
/**
* @dataProvider getGeneratesIdentifierNamesInDefaultConstraintDeclarationSQL
* @group DBAL-830
*/
public function testGeneratesIdentifierNamesInDefaultConstraintDeclarationSQL($table, $column, $expectedSql)
{
$this->assertSame($expectedSql, $this->_platform->getDefaultConstraintDeclarationSQL($table, $column));
}
public function getGeneratesIdentifierNamesInDefaultConstraintDeclarationSQL()
{
return array(
// Unquoted identifiers non-reserved keywords.
array('mytable', array('name' => 'mycolumn', 'default' => 'foo'), " CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'foo' FOR mycolumn"),
// Quoted identifiers non-reserved keywords.
array('`mytable`', array('name' => '`mycolumn`', 'default' => 'foo'), " CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'foo' FOR [mycolumn]"),
// Unquoted identifiers reserved keywords.
array('table', array('name' => 'select', 'default' => 'foo'), " CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'foo' FOR [select]"),
// Quoted identifiers reserved keywords.
array('`table`', array('name' => '`select`', 'default' => 'foo'), " CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'foo' FOR [select]"),
);
}
/**
* @dataProvider getGeneratesIdentifierNamesInCreateTableSQL
* @group DBAL-830
*/
public function testGeneratesIdentifierNamesInCreateTableSQL($table, $expectedSql)
{
$this->assertSame($expectedSql, $this->_platform->getCreateTableSQL($table));
}
public function getGeneratesIdentifierNamesInCreateTableSQL()
{
return array(
// Unquoted identifiers non-reserved keywords.
array(
new Table('mytable', array(new Column('mycolumn', Type::getType('string'), array('default' => 'foo')))),
array(
'CREATE TABLE mytable (mycolumn NVARCHAR(255) NOT NULL)',
"ALTER TABLE mytable ADD CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'foo' FOR mycolumn"
)
),
// Quoted identifiers reserved keywords.
array(
new Table('`mytable`', array(new Column('`mycolumn`', Type::getType('string'), array('default' => 'foo')))),
array(
'CREATE TABLE [mytable] ([mycolumn] NVARCHAR(255) NOT NULL)',
"ALTER TABLE [mytable] ADD CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'foo' FOR [mycolumn]"
)
),
// Unquoted identifiers reserved keywords.
array(
new Table('table', array(new Column('select', Type::getType('string'), array('default' => 'foo')))),
array(
'CREATE TABLE [table] ([select] NVARCHAR(255) NOT NULL)',
"ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'foo' FOR [select]"
)
),
// Quoted identifiers reserved keywords.
array(
new Table('`table`', array(new Column('`select`', Type::getType('string'), array('default' => 'foo')))),
array(
'CREATE TABLE [table] ([select] NVARCHAR(255) NOT NULL)',
"ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'foo' FOR [select]"
)
),
);
}
/**
* @dataProvider getGeneratesIdentifierNamesInAlterTableSQL
* @group DBAL-830
*/
public function testGeneratesIdentifierNamesInAlterTableSQL($tableDiff, $expectedSql)
{
$this->assertSame($expectedSql, $this->_platform->getAlterTableSQL($tableDiff));
}
public function getGeneratesIdentifierNamesInAlterTableSQL()
{
return array(
// Unquoted identifiers non-reserved keywords.
array(
new TableDiff(
'mytable',
array(new Column('addcolumn', Type::getType('string'), array('default' => 'foo'))),
array(
'mycolumn' => new ColumnDiff(
'mycolumn',
new Column('mycolumn', Type::getType('string'), array('default' => 'bar')),
array('default'),
new Column('mycolumn', Type::getType('string'), array('default' => 'foo'))
)
),
array(new Column('removecolumn', Type::getType('string'), array('default' => 'foo')))
),
array(
'ALTER TABLE mytable ADD addcolumn NVARCHAR(255) NOT NULL',
"ALTER TABLE mytable ADD CONSTRAINT DF_6B2BD609_4AD86123 DEFAULT 'foo' FOR addcolumn",
'ALTER TABLE mytable DROP COLUMN removecolumn',
'ALTER TABLE mytable DROP CONSTRAINT DF_6B2BD609_9BADD926',
'ALTER TABLE mytable ALTER COLUMN mycolumn NVARCHAR(255) NOT NULL',
"ALTER TABLE mytable ADD CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'bar' FOR mycolumn"
)
),
// Quoted identifiers non-reserved keywords.
array(
new TableDiff(
'`mytable`',
array(new Column('`addcolumn`', Type::getType('string'), array('default' => 'foo'))),
array(
'mycolumn' => new ColumnDiff(
'`mycolumn`',
new Column('`mycolumn`', Type::getType('string'), array('default' => 'bar')),
array('default'),
new Column('`mycolumn`', Type::getType('string'), array('default' => 'foo'))
)
),
array(new Column('`removecolumn`', Type::getType('string'), array('default' => 'foo')))
),
array(
'ALTER TABLE [mytable] ADD [addcolumn] NVARCHAR(255) NOT NULL',
"ALTER TABLE [mytable] ADD CONSTRAINT DF_6B2BD609_4AD86123 DEFAULT 'foo' FOR [addcolumn]",
'ALTER TABLE [mytable] DROP COLUMN [removecolumn]',
'ALTER TABLE [mytable] DROP CONSTRAINT DF_6B2BD609_9BADD926',
'ALTER TABLE [mytable] ALTER COLUMN [mycolumn] NVARCHAR(255) NOT NULL',
"ALTER TABLE [mytable] ADD CONSTRAINT DF_6B2BD609_9BADD926 DEFAULT 'bar' FOR [mycolumn]"
)
),
// Unquoted identifiers reserved keywords.
array(
new TableDiff(
'table',
array(new Column('add', Type::getType('string'), array('default' => 'foo'))),
array(
'select' => new ColumnDiff(
'select',
new Column('select', Type::getType('string'), array('default' => 'bar')),
array('default'),
new Column('select', Type::getType('string'), array('default' => 'foo'))
)
),
array(new Column('drop', Type::getType('string'), array('default' => 'foo')))
),
array(
'ALTER TABLE [table] ADD [add] NVARCHAR(255) NOT NULL',
"ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_FD1A73E7 DEFAULT 'foo' FOR [add]",
'ALTER TABLE [table] DROP COLUMN [drop]',
'ALTER TABLE [table] DROP CONSTRAINT DF_F6298F46_4BF2EAC0',
'ALTER TABLE [table] ALTER COLUMN [select] NVARCHAR(255) NOT NULL',
"ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'bar' FOR [select]"
)
),
// Quoted identifiers reserved keywords.
array(
new TableDiff(
'`table`',
array(new Column('`add`', Type::getType('string'), array('default' => 'foo'))),
array(
'select' => new ColumnDiff(
'`select`',
new Column('`select`', Type::getType('string'), array('default' => 'bar')),
array('default'),
new Column('`select`', Type::getType('string'), array('default' => 'foo'))
)
),
array(new Column('`drop`', Type::getType('string'), array('default' => 'foo')))
),
array(
'ALTER TABLE [table] ADD [add] NVARCHAR(255) NOT NULL',
"ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_FD1A73E7 DEFAULT 'foo' FOR [add]",
'ALTER TABLE [table] DROP COLUMN [drop]',
'ALTER TABLE [table] DROP CONSTRAINT DF_F6298F46_4BF2EAC0',
'ALTER TABLE [table] ALTER COLUMN [select] NVARCHAR(255) NOT NULL',
"ALTER TABLE [table] ADD CONSTRAINT DF_F6298F46_4BF2EAC0 DEFAULT 'bar' FOR [select]"
)
),
);
}
} }
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