Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Fix a sequence name with double quotes for PostgreSQL to preserve nam…
Browse files Browse the repository at this point in the history
…e registry when try to get last sequence id
  • Loading branch information
Aleksandr Kovalenko committed Aug 9, 2016
1 parent 94218b4 commit d31efc6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/TableGateway/Feature/SequenceFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function lastSequenceId()
$sql = 'SELECT ' . $platform->quoteIdentifier($this->sequenceName) . '.CURRVAL as "currval" FROM dual';
break;
case 'PostgreSQL':
$sql = 'SELECT CURRVAL(\'' . $this->sequenceName . '\')';
$sql = 'SELECT CURRVAL(\'"' . $this->sequenceName . '"\')';
break;
default :
return;
Expand Down
2 changes: 1 addition & 1 deletion test/TableGateway/Feature/FeatureSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function testCallMagicCallSucceedsForValidMethodOfAddedFeature()
$statementMock = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$statementMock->expects($this->any())
->method('prepare')
->with('SELECT CURRVAL(\'' . $sequenceName . '\')');
->with('SELECT CURRVAL(\'"' . $sequenceName . '"\')');
$statementMock->expects($this->any())
->method('execute')
->will($this->returnValue($resultMock));
Expand Down
41 changes: 41 additions & 0 deletions test/TableGateway/Feature/SequenceFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,45 @@ public function nextSequenceIdProvider()
return [['PostgreSQL', 'SELECT NEXTVAL(\'"' . $this->sequenceName . '"\')'],
['Oracle', 'SELECT ' . $this->sequenceName . '.NEXTVAL as "nextval" FROM dual']];
}

/**
* @dataProvider lastSequenceIdProvider
*/
public function testLastSequenceId($platformName, $statementSql)
{
$platform = $this->getMockForAbstractClass('Zend\Db\Adapter\Platform\PlatformInterface', ['getName']);
$platform->expects($this->any())
->method('getName')
->will($this->returnValue($platformName));
$platform->expects($this->any())
->method('quoteIdentifier')
->will($this->returnValue($this->sequenceName));
$adapter = $this->getMock('Zend\Db\Adapter\Adapter', ['getPlatform', 'createStatement'], [], '', false);
$adapter->expects($this->any())
->method('getPlatform')
->will($this->returnValue($platform));
$result = $this->getMockForAbstractClass('Zend\Db\Adapter\Driver\ResultInterface', [], '', false, true, true, ['current']);
$result->expects($this->any())
->method('current')
->will($this->returnValue(['currval' => 1]));
$statement = $this->getMockForAbstractClass('Zend\Db\Adapter\Driver\StatementInterface', [], '', false, true, true, ['prepare', 'execute']);
$statement->expects($this->any())
->method('execute')
->will($this->returnValue($result));
$statement->expects($this->any())
->method('prepare')
->with($statementSql);
$adapter->expects($this->once())
->method('createStatement')
->will($this->returnValue($statement));
$this->tableGateway = $this->getMockForAbstractClass('Zend\Db\TableGateway\TableGateway', ['table', $adapter], '', true);
$this->feature->setTableGateway($this->tableGateway);
$this->feature->lastSequenceId();
}

public function lastSequenceIdProvider()
{
return [['PostgreSQL', 'SELECT CURRVAL(\'"' . $this->sequenceName . '"\')'],
['Oracle', 'SELECT ' . $this->sequenceName . '.CURRVAL as "currval" FROM dual']];
}
}

0 comments on commit d31efc6

Please sign in to comment.