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 Jun 25, 2016
1 parent 94218b4 commit 0a10aaa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
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
46 changes: 46 additions & 0 deletions test/TableGateway/Feature/SequenceFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,50 @@ 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 0a10aaa

Please sign in to comment.