Commit 27b78e0a authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge branch 'DBAL-126'

parents 48c78a2d 4c383bd8
...@@ -1124,18 +1124,33 @@ abstract class AbstractPlatform ...@@ -1124,18 +1124,33 @@ abstract class AbstractPlatform
throw new \InvalidArgumentException("Incomplete definition. 'columns' required."); throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
} }
if ($index->isPrimary()) {
return $this->getCreatePrimaryKeySQL($index, $table);
} else {
$type = ''; $type = '';
if ($index->isUnique()) { if ($index->isUnique()) {
$type = 'UNIQUE '; $type = 'UNIQUE ';
} }
$query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table; $query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')'; $query .= ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')';
}
return $query; return $query;
} }
/**
* Get SQL to create an unnamed primary key constraint.
*
* @param Index $index
* @param string|Table $table
* @return string
*/
public function getCreatePrimaryKeySQL(Index $index, $table)
{
return 'ALTER TABLE ' . $table . ' ADD PRIMARY KEY (' . $this->getIndexFieldDeclarationListSQL($index->getColumns()) . ')';
}
/** /**
* Quotes a string so that it can be safely used as a table or column name, * Quotes a string so that it can be safely used as a table or column name,
* even if it is a reserved word of the platform. * even if it is a reserved word of the platform.
......
...@@ -4,6 +4,8 @@ namespace Doctrine\Tests\DBAL\Platforms; ...@@ -4,6 +4,8 @@ namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Schema;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
...@@ -16,7 +18,7 @@ class MySqlPlatformTest extends AbstractPlatformTestCase ...@@ -16,7 +18,7 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
public function testGenerateMixedCaseTableCreate() public function testGenerateMixedCaseTableCreate()
{ {
$table = new \Doctrine\DBAL\Schema\Table("Foo"); $table = new Table("Foo");
$table->addColumn("Bar", "integer"); $table->addColumn("Bar", "integer");
$sql = $this->_platform->getCreateTableSQL($table); $sql = $this->_platform->getCreateTableSQL($table);
...@@ -145,6 +147,32 @@ class MySqlPlatformTest extends AbstractPlatformTestCase ...@@ -145,6 +147,32 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)'; return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)';
} }
/**
* @group DBAL-126
*/
public function testUniquePrimaryKey()
{
$keyTable = new Table("foo");
$keyTable->addColumn("bar", "integer");
$keyTable->addColumn("baz", "string");
$keyTable->setPrimaryKey(array("bar"));
$keyTable->addUniqueIndex(array("baz"));
$oldTable = new Table("foo");
$oldTable->addColumn("bar", "integer");
$oldTable->addColumn("baz", "string");
$c = new \Doctrine\DBAL\Schema\Comparator;
$diff = $c->diffTable($oldTable, $keyTable);
$sql = $this->_platform->getAlterTableSQL($diff);
$this->assertEquals(array(
"ALTER TABLE foo ADD PRIMARY KEY (bar)",
"CREATE UNIQUE INDEX UNIQ_8C73652178240498 ON foo (baz)",
), $sql);
}
public function testModifyLimitQuery() public function testModifyLimitQuery()
{ {
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0); $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
......
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