Commit d2845256 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #428 from deeky666/DBAL-563

[DBAL-563] Add definition of sequence emulated identity platforms
parents a92784a1 6b4ba50f
......@@ -2618,6 +2618,37 @@ abstract class AbstractPlatform
return false;
}
/**
* Whether the platform emulates identity columns through sequences.
*
* Some platforms that do not support identity columns natively
* but support sequences can emulate identity columns by using
* sequences.
*
* @return boolean
*/
public function usesSequenceEmulatedIdentityColumns()
{
return false;
}
/**
* Returns the name of the sequence for a particular identity column in a particular table.
*
* @param string $tableName The name of the table to return the sequence name for.
* @param string $columnName The name of the identity column in the table to return the sequence name for.
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*
* @see usesSequenceEmulatedIdentityColumns
*/
public function getIdentitySequenceName($tableName, $columnName)
{
throw DBALException::notSupported(__METHOD__);
}
/**
* Whether the platform supports indexes.
*
......
......@@ -448,7 +448,7 @@ BEGIN
END IF;
END;';
$sequenceName = $table . '_' . $name . '_SEQ';
$sequenceName = $this->getIdentitySequenceName($table, $name);
$sequence = new Sequence($sequenceName, $start);
$sql[] = $this->getCreateSequenceSQL($sequence);
......@@ -695,6 +695,22 @@ LEFT JOIN user_cons_columns r_cols
return true;
}
/**
* {@inheritdoc}
*/
public function usesSequenceEmulatedIdentityColumns()
{
return true;
}
/**
* {@inheritdoc}
*/
public function getIdentitySequenceName($tableName, $columnName)
{
return $tableName . '_' . $columnName . '_SEQ';
}
/**
* {@inheritDoc}
*/
......
......@@ -180,6 +180,22 @@ class PostgreSqlPlatform extends AbstractPlatform
return true;
}
/**
* {@inheritdoc}
*/
public function usesSequenceEmulatedIdentityColumns()
{
return true;
}
/**
* {@inheritdoc}
*/
public function getIdentitySequenceName($tableName, $columnName)
{
return $tableName . '_' . $columnName . '_seq';
}
/**
* {@inheritDoc}
*/
......@@ -466,7 +482,7 @@ class PostgreSqlPlatform extends AbstractPlatform
if ($columnDiff->hasChanged('autoincrement')) {
if ($column->getAutoincrement()) {
// add autoincrement
$seqName = $diff->name . '_' . $oldColumnName . '_seq';
$seqName = $this->getIdentitySequenceName($diff->name, $oldColumnName);
$sql[] = "CREATE SEQUENCE " . $seqName;
$sql[] = "SELECT setval('" . $seqName . "', (SELECT MAX(" . $oldColumnName . ") FROM " . $diff->getName()->getQuotedName($this) . "))";
......
......@@ -510,4 +510,21 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
{
$this->_platform->schemaNeedsCreation('schema');
}
/**
* @group DBAL-563
*/
public function testUsesSequenceEmulatedIdentityColumns()
{
$this->assertFalse($this->_platform->usesSequenceEmulatedIdentityColumns());
}
/**
* @group DBAL-563
* @expectedException \Doctrine\DBAL\DBALException
*/
public function testReturnsIdentitySequenceName()
{
$this->_platform->getIdentitySequenceName('mytable', 'mycolumn');
}
}
......@@ -330,4 +330,20 @@ class OraclePlatformTest extends AbstractPlatformTestCase
);
$this->assertEquals($expectedSql, $this->_platform->getAlterTableSQL($tableDiff));
}
/**
* @group DBAL-563
*/
public function testUsesSequenceEmulatedIdentityColumns()
{
$this->assertTrue($this->_platform->usesSequenceEmulatedIdentityColumns());
}
/**
* @group DBAL-563
*/
public function testReturnsIdentitySequenceName()
{
$this->assertSame('mytable_mycolumn_SEQ', $this->_platform->getIdentitySequenceName('mytable', 'mycolumn'));
}
}
......@@ -410,4 +410,20 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase
$this->assertEquals($expectedSql, $sql);
}
/**
* @group DBAL-563
*/
public function testUsesSequenceEmulatedIdentityColumns()
{
$this->assertTrue($this->_platform->usesSequenceEmulatedIdentityColumns());
}
/**
* @group DBAL-563
*/
public function testReturnsIdentitySequenceName()
{
$this->assertSame('mytable_mycolumn_seq', $this->_platform->getIdentitySequenceName('mytable', 'mycolumn'));
}
}
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