diff --git a/src/TableGateway/Feature/SequenceFeature.php b/src/TableGateway/Feature/SequenceFeature.php index c3d1779355..ca7c8d6c82 100644 --- a/src/TableGateway/Feature/SequenceFeature.php +++ b/src/TableGateway/Feature/SequenceFeature.php @@ -49,14 +49,19 @@ public function __construct($sequencedColumn, $sequenceName = null) */ public function getSequenceName() { - if ($this->sequenceName !== null) { - return $this->sequenceName; - } - //@TODO move to PostgreSQL specific class (possibly decorator) /** @var Adapter $adapter */ $adapter = $this->tableGateway->getAdapter(); $platform = $adapter->getPlatform(); + + if ($this->sequenceName !== null) { + if (is_array($this->sequenceName)) { + $this->sequenceName = $platform->quoteIdentifierChain($this->sequenceName); + } + + return $this->sequenceName; + } + $tableIdentifier = $this->tableGateway->getTable(); // need to preserve table name in case have to query postgres metadata // (case for large resultant identifier names) diff --git a/test/TableGateway/Feature/SequenceFeatureTest.php b/test/TableGateway/Feature/SequenceFeatureTest.php index aef9900521..8436e72831 100644 --- a/test/TableGateway/Feature/SequenceFeatureTest.php +++ b/test/TableGateway/Feature/SequenceFeatureTest.php @@ -29,20 +29,34 @@ class SequenceFeatureTest extends PHPUnit_Framework_TestCase protected $sequenceName = 'sequence_name'; /** - * @dataProvider tableIdentifierProvider + * @dataProvider identifierProvider */ - public function testSequenceNameGenerated($tableIdentifier, $sequenceName) + public function testSequenceNameGenerated($platform, $tableIdentifier, $sequenceName, $expectedSequenceName) { $adapter = $this->getMock('Zend\Db\Adapter\Adapter', ['getPlatform', 'createStatement'], [], '', false); $adapter->expects($this->any()) ->method('getPlatform') - ->will($this->returnValue(new TrustingPostgresqlPlatform())); + ->will($this->returnValue($platform)); $this->tableGateway = $this->getMockForAbstractClass('Zend\Db\TableGateway\TableGateway', [$tableIdentifier, $adapter], '', true); - $sequence = new SequenceFeature('serial_column'); + $sequence = new SequenceFeature('serial_column', $sequenceName); $sequence->setTableGateway($this->tableGateway); - $sequence->getSequenceName(); + $this->assertEquals($expectedSequenceName, $sequence->getSequenceName()); + } + + public function identifierProvider() + { + return [ + [new TrustingPostgresqlPlatform(), + 'table', null, '"table_serial_column_seq"', ], + [new TrustingPostgresqlPlatform(), + ['schema', 'table'], null, '"schema"."table_serial_column_seq"', ], + [new TrustingPostgresqlPlatform(), + new TableIdentifier('table', 'schema'), null, '"schema"."table_serial_column_seq"', ], + [new TrustingPostgresqlPlatform(), + new TableIdentifier('table', 'schema'), ['schema', 'sequence_name'], '"schema"."sequence_name"', ], + ]; } public function testSequenceNameQueriedWhenTooLong() @@ -142,6 +156,13 @@ public function testNextSequenceIdByPlatform($platform, $statementSql, $statemen $feature->nextSequenceId(); } + public function nextSequenceIdProvider() + { + return [ + [new TrustingPostgresqlPlatform(), 'SELECT NEXTVAL( :sequence_name )', ['sequence_name' => $this->sequenceName]], + [new TrustingOraclePlatform(), 'SELECT "'.$this->sequenceName.'".NEXTVAL as "nextval" FROM dual', []], + ]; + } /** * @dataProvider lastSequenceIdProvider */ @@ -173,21 +194,6 @@ public function testLastSequenceIdByPlatform($platform, $statementSql, $statemen $feature->lastSequenceId(); } - public function testDoNotReactToDifferentColumnName() - { - $sequence1 = new SequenceFeature('col_1', 'seq_1'); - $this->assertEquals($sequence1->lastSequenceId('col_2'), null, 'Sequence should not react to foreign column name'); - $this->assertEquals($sequence1->nextSequenceId('col_2'), null, 'Sequence should not react to foreign column name'); - } - - public function nextSequenceIdProvider() - { - return [ - [new TrustingPostgresqlPlatform(), 'SELECT NEXTVAL( :sequence_name )', ['sequence_name' => $this->sequenceName]], - [new TrustingOraclePlatform(), 'SELECT "'.$this->sequenceName.'".NEXTVAL as "nextval" FROM dual', []], - ]; - } - public function lastSequenceIdProvider() { return [ @@ -196,12 +202,10 @@ public function lastSequenceIdProvider() ]; } - public function tableIdentifierProvider() + public function testDoNotReactToDifferentColumnName() { - return [ - ['table', 'table_serial_column_seq'], - [['schema', 'table'], '"schema"."table_serial_column_seq"'], - [new TableIdentifier('table', 'schema'), '"schema"."table_serial_column_seq"'], - ]; + $sequence1 = new SequenceFeature('col_1', 'seq_1'); + $this->assertEquals($sequence1->lastSequenceId('col_2'), null, 'Sequence should not react to foreign column name'); + $this->assertEquals($sequence1->nextSequenceId('col_2'), null, 'Sequence should not react to foreign column name'); } }