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

Fix nextval for sequence from outer schema #172

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/TableGateway/Feature/SequenceFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Zend\Db\Sql\Insert;
use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\Adapter\Driver\StatementInterface;
use Zend\Db\Sql\TableIdentifier;

class SequenceFeature extends AbstractFeature
{
Expand Down Expand Up @@ -89,7 +90,15 @@ public function nextSequenceId()
$sql = 'SELECT ' . $platform->quoteIdentifier($this->sequenceName) . '.NEXTVAL as "nextval" FROM dual';
break;
case 'PostgreSQL':
$sql = 'SELECT NEXTVAL(\'"' . $this->sequenceName . '"\')';
$table = $this->tableGateway->getTable();
if ($table instanceof TableIdentifier) {
$table = $table->hasSchema() ? [$table->getSchema(), $table->getTable()] : $table->getTable();
}
$sql = sprintf(
'SELECT NEXTVAL(pg_get_serial_sequence(%s, %s))',
$platform->quoteValue($platform->quoteIdentifierChain($table)),
$platform->quoteValue($this->primaryKeyField)
);
break;
default :
return;
Expand Down Expand Up @@ -117,7 +126,15 @@ public function lastSequenceId()
$sql = 'SELECT ' . $platform->quoteIdentifier($this->sequenceName) . '.CURRVAL as "currval" FROM dual';
break;
case 'PostgreSQL':
$sql = 'SELECT CURRVAL(\'' . $this->sequenceName . '\')';
$table = $this->tableGateway->getTable();
if ($table instanceof TableIdentifier) {
$table = $table->hasSchema() ? [$table->getSchema(), $table->getTable()] : $table->getTable();
}
$sql = sprintf(
'SELECT CURRVAL(pg_get_serial_sequence(%s, %s))',
$platform->quoteValue($platform->quoteIdentifierChain($table)),
$platform->quoteValue($this->primaryKeyField)
);
break;
default :
return;
Expand Down