From 6b4ba50f7eab49a49c00a21e19ce6081ead79b2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steve=20M=C3=BCller?= Date: Mon, 25 Nov 2013 19:39:41 +0100 Subject: [PATCH] add definition of sequence emulated identity column platforms --- .../DBAL/Platforms/AbstractPlatform.php | 31 +++++++++++++++++++ .../DBAL/Platforms/OraclePlatform.php | 18 ++++++++++- .../DBAL/Platforms/PostgreSqlPlatform.php | 20 ++++++++++-- .../Platforms/AbstractPlatformTestCase.php | 17 ++++++++++ .../DBAL/Platforms/OraclePlatformTest.php | 16 ++++++++++ .../DBAL/Platforms/PostgreSqlPlatformTest.php | 16 ++++++++++ 6 files changed, 115 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php index fc0704f7aa2..f2e086fdf46 100644 --- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php @@ -2618,6 +2618,37 @@ public function supportsIdentityColumns() 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. * diff --git a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php index b5929223549..7a9b6bc06af 100644 --- a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php @@ -448,7 +448,7 @@ public function getCreateAutoincrementSql($name, $table, $start = 1) 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 @@ public function prefersSequences() return true; } + /** + * {@inheritdoc} + */ + public function usesSequenceEmulatedIdentityColumns() + { + return true; + } + + /** + * {@inheritdoc} + */ + public function getIdentitySequenceName($tableName, $columnName) + { + return $tableName . '_' . $columnName . '_SEQ'; + } + /** * {@inheritDoc} */ diff --git a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php index f2c2fee6241..7b1a7b68413 100644 --- a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php @@ -113,7 +113,7 @@ public function getDateAddHourExpression($date, $hours) */ public function getDateSubHourExpression($date, $hours) { - return "(" . $date ." - (" . $hours . " || ' hour')::interval)"; + return "(" . $date ." - (" . $hours . " || ' hour')::interval)"; } /** @@ -180,6 +180,22 @@ public function supportsIdentityColumns() return true; } + /** + * {@inheritdoc} + */ + public function usesSequenceEmulatedIdentityColumns() + { + return true; + } + + /** + * {@inheritdoc} + */ + public function getIdentitySequenceName($tableName, $columnName) + { + return $tableName . '_' . $columnName . '_seq'; + } + /** * {@inheritDoc} */ @@ -466,7 +482,7 @@ public function getAlterTableSQL(TableDiff $diff) 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) . "))"; diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php index f3be680f888..bd58189457a 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php @@ -510,4 +510,21 @@ public function testSchemaNeedsCreation() { $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'); + } } diff --git a/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php index 91ef0d111bb..14f050d27bd 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php @@ -330,4 +330,20 @@ public function testAlterTableNotNULL() ); $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')); + } } diff --git a/tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php index d6eb6e9fdab..fdfca6ed912 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php @@ -410,4 +410,20 @@ public function testDroppingConstraintsBeforeColumns() $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')); + } }