Commit 0397f760 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge branch 'DBAL-126' into 2.0.x

parents fea10ddb ad6b0cca
......@@ -959,18 +959,33 @@ abstract class AbstractPlatform
if (count($columns) == 0) {
throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
}
if ($index->isPrimary()) {
return $this->getCreatePrimaryKeySQL($index, $table);
} else {
$type = '';
if ($index->isUnique()) {
$type = 'UNIQUE ';
}
$type = '';
if ($index->isUnique()) {
$type = 'UNIQUE ';
$query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')';
}
$query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')';
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,
......
......@@ -20,7 +20,9 @@
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException,
Doctrine\DBAL\Schema\TableDiff;
Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Schema\Index,
Doctrine\DBAL\Schema\Table;
/**
* The MySqlPlatform provides the behavior, features and SQL dialect of the
......@@ -523,20 +525,37 @@ class MySqlPlatform extends AbstractPlatform
* @override
*/
public function getDropIndexSQL($index, $table=null)
{
if($index instanceof \Doctrine\DBAL\Schema\Index) {
$index = $index->getQuotedName($this);
} else if(!is_string($index)) {
{
if($index instanceof Index) {
$indexName = $index->getQuotedName($this);
} else if(is_string($index)) {
$indexName = $index;
} else {
throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
}
if($table instanceof \Doctrine\DBAL\Schema\Table) {
if($table instanceof Table) {
$table = $table->getQuotedName($this);
} else if(!is_string($table)) {
throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
}
if ($index instanceof Index && $index->isPrimary()) {
// mysql primary keys are always named "PRIMARY",
// so we cannot use them in statements because of them being keyword.
return $this->getDropPrimaryKeySQL($table);
}
return 'DROP INDEX ' . $index . ' ON ' . $table;
return 'DROP INDEX ' . $indexName . ' ON ' . $table;
}
/**
* @param Index $index
* @param Table $table
*/
protected function getDropPrimaryKeySQL($table)
{
return 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY';
}
/**
......
......@@ -4,6 +4,8 @@ namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Schema;
require_once __DIR__ . '/../../TestInit.php';
......@@ -16,7 +18,7 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
public function testGenerateMixedCaseTableCreate()
{
$table = new \Doctrine\DBAL\Schema\Table("Foo");
$table = new Table("Foo");
$table->addColumn("Bar", "integer");
$sql = $this->_platform->getCreateTableSQL($table);
......@@ -144,6 +146,32 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
{
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()
{
......
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