Commit 558cbf17 authored by Marco Pivetta's avatar Marco Pivetta

Merge pull request #741 from deeky666/DBAL-1051

[DBAL-1051] Quote index name in inline index declaration SQL
parents 80f08a64 356b5291
......@@ -2308,12 +2308,13 @@ abstract class AbstractPlatform
public function getUniqueConstraintDeclarationSQL($name, Index $index)
{
$columns = $index->getQuotedColumns($this);
$name = new Identifier($name);
if (count($columns) === 0) {
throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
}
return 'CONSTRAINT ' . $name . ' UNIQUE ('
return 'CONSTRAINT ' . $name->getQuotedName($this) . ' UNIQUE ('
. $this->getIndexFieldDeclarationListSQL($columns)
. ')' . $this->getPartialIndexSQL($index);
}
......@@ -2332,12 +2333,13 @@ abstract class AbstractPlatform
public function getIndexDeclarationSQL($name, Index $index)
{
$columns = $index->getQuotedColumns($this);
$name = new Identifier($name);
if (count($columns) === 0) {
throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
}
return $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name . ' ('
return $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name->getQuotedName($this) . ' ('
. $this->getIndexFieldDeclarationListSQL($columns)
. ')' . $this->getPartialIndexSQL($index);
}
......
......@@ -19,6 +19,7 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Identifier;
......@@ -438,7 +439,8 @@ class DB2Platform extends AbstractPlatform
*/
public function getIndexDeclarationSQL($name, Index $index)
{
return $this->getUniqueConstraintDeclarationSQL($name, $index);
// Index declaration in statements like CREATE TABLE is not supported.
throw DBALException::notSupported(__METHOD__);
}
/**
......
......@@ -1358,7 +1358,8 @@ class SQLAnywherePlatform extends AbstractPlatform
$flags = '';
if ( ! empty($name)) {
$sql .= 'CONSTRAINT ' . $name . ' ';
$name = new Identifier($name);
$sql .= 'CONSTRAINT ' . $name->getQuotedName($this) . ' ';
}
if ($constraint->hasFlag('clustered')) {
......
......@@ -641,4 +641,20 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
"COMMENT ON COLUMN `select`.`from` IS 'comment'",
);
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
{
return 'CONSTRAINT `select` UNIQUE (foo)';
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInIndexDeclarationSQL()
{
return 'INDEX `select` (foo)';
}
}
......@@ -167,12 +167,17 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
{
$where = 'test IS NULL AND test2 IS NOT NULL';
$indexDef = new \Doctrine\DBAL\Schema\Index('name', array('test', 'test2'), false, false, array(), array('where' => $where));
$uniqueIndex = new \Doctrine\DBAL\Schema\Index('name', array('test', 'test2'), true, false, array(), array('where' => $where));
$expected = ' WHERE ' . $where;
$actuals = array();
if ($this->supportsInlineIndexDeclaration()) {
$actuals []= $this->_platform->getIndexDeclarationSQL('name', $indexDef);
$actuals []= $this->_platform->getUniqueConstraintDeclarationSQL('name', $indexDef);
}
$actuals []= $this->_platform->getUniqueConstraintDeclarationSQL('name', $uniqueIndex);
$actuals []= $this->_platform->getCreateIndexSQL($indexDef, 'table');
foreach ($actuals as $actual) {
......@@ -587,6 +592,54 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
$this->assertEquals($this->getQuotedColumnInForeignKeySQL(), $sql);
}
/**
* @group DBAL-1051
*/
public function testQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
{
$index = new Index('select', array('foo'), true);
$this->assertSame(
$this->getQuotesReservedKeywordInUniqueConstraintDeclarationSQL(),
$this->_platform->getUniqueConstraintDeclarationSQL('select', $index)
);
}
/**
* @return string
*/
abstract protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL();
/**
* @group DBAL-1051
*/
public function testQuotesReservedKeywordInIndexDeclarationSQL()
{
$index = new Index('select', array('foo'));
if (! $this->supportsInlineIndexDeclaration()) {
$this->setExpectedException('Doctrine\DBAL\DBALException');
}
$this->assertSame(
$this->getQuotesReservedKeywordInIndexDeclarationSQL(),
$this->_platform->getIndexDeclarationSQL('select', $index)
);
}
/**
* @return string
*/
abstract protected function getQuotesReservedKeywordInIndexDeclarationSQL();
/**
* @return boolean
*/
protected function supportsInlineIndexDeclaration()
{
return true;
}
/**
* @expectedException \Doctrine\DBAL\DBALException
*/
......
......@@ -739,4 +739,20 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
$this->_platform->getAlterTableSQL($tableDiff)
);
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
{
return 'CONSTRAINT "select" UNIQUE (foo)';
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInIndexDeclarationSQL()
{
return 'INDEX "select" (foo)';
}
}
......@@ -1186,4 +1186,20 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas
array('CaScAdE', 'CASCADE'),
);
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
{
return 'CONSTRAINT [select] UNIQUE (foo) WHERE foo IS NOT NULL';
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInIndexDeclarationSQL()
{
return 'INDEX [select] (foo)';
}
}
......@@ -615,4 +615,28 @@ class DB2PlatformTest extends AbstractPlatformTestCase
),
);
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
{
return 'CONSTRAINT "select" UNIQUE (foo)';
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInIndexDeclarationSQL()
{
return ''; // not supported by this platform
}
/**
* {@inheritdoc}
*/
protected function supportsInlineIndexDeclaration()
{
return false;
}
}
......@@ -642,4 +642,20 @@ EOD;
$this->assertEquals($createTriggerStatement, $sql[3]);
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
{
return 'CONSTRAINT "select" UNIQUE (foo)';
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInIndexDeclarationSQL()
{
return 'INDEX "select" (foo)';
}
}
......@@ -817,11 +817,6 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
);
}
public function testGeneratesPartialIndexesSqlOnlyWhenSupportingPartialIndexes()
{
$this->markTestSkipped('Index declaration in statements like CREATE TABLE is not supported.');
}
/**
* {@inheritdoc}
*/
......@@ -932,4 +927,28 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
array('CaScAdE', 'CASCADE'),
);
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
{
return 'CONSTRAINT "select" UNIQUE (foo)';
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInIndexDeclarationSQL()
{
return ''; // not supported by this platform
}
/**
* {@inheritdoc}
*/
protected function supportsInlineIndexDeclaration()
{
return false;
}
}
......@@ -608,4 +608,20 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
'COMMENT ON COLUMN "select"."from" IS \'comment\'',
);
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInUniqueConstraintDeclarationSQL()
{
return 'CONSTRAINT "select" UNIQUE (foo)';
}
/**
* {@inheritdoc}
*/
protected function getQuotesReservedKeywordInIndexDeclarationSQL()
{
return 'INDEX "select" (foo)';
}
}
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