Skip to content

Commit

Permalink
zendframework#186 Take into account array parameter for sequence name…
Browse files Browse the repository at this point in the history
… to specify schema.
  • Loading branch information
alextech committed Dec 25, 2016
1 parent 4ed6ff2 commit daa9c33
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
13 changes: 9 additions & 4 deletions src/TableGateway/Feature/SequenceFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
56 changes: 30 additions & 26 deletions test/TableGateway/Feature/SequenceFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 [
Expand All @@ -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');
}
}

0 comments on commit daa9c33

Please sign in to comment.