From 035fa5deba616f0c5e386a509297fd732ac426a4 Mon Sep 17 00:00:00 2001 From: tkonop <31702762+tkonop@users.noreply.github.com> Date: Sun, 5 May 2019 20:43:28 +0200 Subject: [PATCH 1/2] Adding schema for Postgres Postgres sequence can use other schema than public. In this case NEXTVAL and CURRVAL should include Schema name like NEXTVAL("schema"."sequence_name"). In standard approach result of SequenceFeature class was NEXTVAL("schema.sequence_name") which is inproper behavior. I added checking, if the $sequenceName is instance of TableIdentifier. In this case NEXTVAL and CURRVALL is modified. --- src/TableGateway/Feature/SequenceFeature.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/TableGateway/Feature/SequenceFeature.php b/src/TableGateway/Feature/SequenceFeature.php index 014171c24a..cb5afc2587 100644 --- a/src/TableGateway/Feature/SequenceFeature.php +++ b/src/TableGateway/Feature/SequenceFeature.php @@ -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 { @@ -25,6 +26,11 @@ class SequenceFeature extends AbstractFeature */ protected $sequenceName; + /** + * @var string + */ + protected $schemaName = NULL; + /** * @var int */ @@ -39,6 +45,11 @@ public function __construct($primaryKeyField, $sequenceName) { $this->primaryKeyField = $primaryKeyField; $this->sequenceName = $sequenceName; + + if($sequenceName instanceof TableIdentifier) { + $this->sequenceName = $sequenceName->getTable(); + $this->schemaName = $sequenceName->getSchema(); + } } /** @@ -90,6 +101,9 @@ public function nextSequenceId() break; case 'PostgreSQL': $sql = 'SELECT NEXTVAL(\'"' . $this->sequenceName . '"\')'; + if($this->schemaName !== NULL) { + $sql = 'SELECT NEXTVAL(\'"' . $this->schemaName . '"."' . $this->sequenceName . '"\')'; + } break; default: return; @@ -118,6 +132,9 @@ public function lastSequenceId() break; case 'PostgreSQL': $sql = 'SELECT CURRVAL(\'' . $this->sequenceName . '\')'; + if($this->schemaName !== NULL) { + $sql = 'SELECT CURRVAL(\'"' . $this->schemaName . '"."' . $this->sequenceName . '"\')'; + } break; default: return; From d591f6e0fca25d8f897d2579cb7ffbdb54553f86 Mon Sep 17 00:00:00 2001 From: tkonop <31702762+tkonop@users.noreply.github.com> Date: Thu, 14 Nov 2019 13:51:45 +0100 Subject: [PATCH 2/2] Fixed The Travis CI build failed --- src/TableGateway/Feature/SequenceFeature.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/TableGateway/Feature/SequenceFeature.php b/src/TableGateway/Feature/SequenceFeature.php index cb5afc2587..e77ec42507 100644 --- a/src/TableGateway/Feature/SequenceFeature.php +++ b/src/TableGateway/Feature/SequenceFeature.php @@ -29,7 +29,7 @@ class SequenceFeature extends AbstractFeature /** * @var string */ - protected $schemaName = NULL; + protected $schemaName = null; /** * @var int @@ -46,7 +46,7 @@ public function __construct($primaryKeyField, $sequenceName) $this->primaryKeyField = $primaryKeyField; $this->sequenceName = $sequenceName; - if($sequenceName instanceof TableIdentifier) { + if ($sequenceName instanceof TableIdentifier) { $this->sequenceName = $sequenceName->getTable(); $this->schemaName = $sequenceName->getSchema(); } @@ -101,7 +101,7 @@ public function nextSequenceId() break; case 'PostgreSQL': $sql = 'SELECT NEXTVAL(\'"' . $this->sequenceName . '"\')'; - if($this->schemaName !== NULL) { + if ($this->schemaName !== null) { $sql = 'SELECT NEXTVAL(\'"' . $this->schemaName . '"."' . $this->sequenceName . '"\')'; } break; @@ -132,7 +132,7 @@ public function lastSequenceId() break; case 'PostgreSQL': $sql = 'SELECT CURRVAL(\'' . $this->sequenceName . '\')'; - if($this->schemaName !== NULL) { + if ($this->schemaName !== null) { $sql = 'SELECT CURRVAL(\'"' . $this->schemaName . '"."' . $this->sequenceName . '"\')'; } break;