From 547f8e5c62bc540a73968d959327f25524d425a0 Mon Sep 17 00:00:00 2001 From: Stalin Date: Sun, 4 Mar 2018 17:28:41 +0200 Subject: [PATCH 01/15] Add missed return statement --- src/Adapter/Driver/Mysqli/Connection.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Adapter/Driver/Mysqli/Connection.php b/src/Adapter/Driver/Mysqli/Connection.php index c94e009638..c0e9d8ff74 100644 --- a/src/Adapter/Driver/Mysqli/Connection.php +++ b/src/Adapter/Driver/Mysqli/Connection.php @@ -191,6 +191,8 @@ public function disconnect() $this->resource->close(); } $this->resource = null; + + return $this; } /** From e769e66bcf655577643c53f05c3339ca07bd35c3 Mon Sep 17 00:00:00 2001 From: Stalin Date: Sun, 4 Mar 2018 17:29:07 +0200 Subject: [PATCH 02/15] Add support for reconnection on connection lost --- src/Adapter/Driver/Mysqli/Mysqli.php | 43 +++++++++++++++++++++++++ src/Adapter/Driver/Mysqli/Statement.php | 1 + 2 files changed, 44 insertions(+) diff --git a/src/Adapter/Driver/Mysqli/Mysqli.php b/src/Adapter/Driver/Mysqli/Mysqli.php index c1f3da5177..037091baf4 100644 --- a/src/Adapter/Driver/Mysqli/Mysqli.php +++ b/src/Adapter/Driver/Mysqli/Mysqli.php @@ -61,6 +61,7 @@ public function __construct( $connection = new Connection($connection); } + $this->options = array_merge($this->options, $options); $options = array_intersect_key(array_merge($this->options, $options), $this->options); $this->registerConnection($connection); @@ -259,4 +260,46 @@ public function getLastGeneratedValue() { return $this->getConnection()->getLastGeneratedValue(); } + + /** + * Check connection if not exists -> try to reconnect. + * Depends on configuration value `reconnect_tries` + * + * @param Statement|null $statement + * @return $this + */ + public function checkConnection(Statement $statement = null) + { + + $reconnectTries = array_key_exists('reconnect_tries', $this->options) + ? $this->options['reconnect_tries'] : 0; + + /** + * @var \mysqli $mysqli + */ + $mysqli = $this->connection->getResource(); + + for ($i = 0; $i < $reconnectTries; ++$i) { + if (! $mysqli->ping()) { + + $mysqli = $this->connection + ->disconnect() + ->connect() + ->getResource(); + + if ($mysqli->ping()) { + + if($statement instanceof Statement) { + $statement->initialize($mysqli); + } + + return $this; + } + + } + } + + return $this; + } + } diff --git a/src/Adapter/Driver/Mysqli/Statement.php b/src/Adapter/Driver/Mysqli/Statement.php index 140d4aac58..765db8f1c2 100644 --- a/src/Adapter/Driver/Mysqli/Statement.php +++ b/src/Adapter/Driver/Mysqli/Statement.php @@ -202,6 +202,7 @@ public function prepare($sql = null) } $sql = ($sql) ?: $this->sql; + $this->driver->checkConnection($this); $this->resource = $this->mysqli->prepare($sql); if (! $this->resource instanceof \mysqli_stmt) { From 4e1fea8723f31d4a05b1844d6a6f63a83a60124e Mon Sep 17 00:00:00 2001 From: Stalin Date: Sun, 4 Mar 2018 17:42:15 +0200 Subject: [PATCH 03/15] Fix code style according to build error; --- src/Adapter/Driver/Mysqli/Mysqli.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Adapter/Driver/Mysqli/Mysqli.php b/src/Adapter/Driver/Mysqli/Mysqli.php index 037091baf4..e771c22a74 100644 --- a/src/Adapter/Driver/Mysqli/Mysqli.php +++ b/src/Adapter/Driver/Mysqli/Mysqli.php @@ -281,15 +281,13 @@ public function checkConnection(Statement $statement = null) for ($i = 0; $i < $reconnectTries; ++$i) { if (! $mysqli->ping()) { - $mysqli = $this->connection ->disconnect() ->connect() ->getResource(); if ($mysqli->ping()) { - - if($statement instanceof Statement) { + if( $statement instanceof Statement) { $statement->initialize($mysqli); } @@ -298,8 +296,7 @@ public function checkConnection(Statement $statement = null) } } - return $this; } -} +} \ No newline at end of file From 85e8ca8d8376136399fea193fc8aed14baa4c46d Mon Sep 17 00:00:00 2001 From: Stalin Date: Sun, 4 Mar 2018 17:43:14 +0200 Subject: [PATCH 04/15] Fix code style --- src/Adapter/Driver/Mysqli/Mysqli.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Adapter/Driver/Mysqli/Mysqli.php b/src/Adapter/Driver/Mysqli/Mysqli.php index e771c22a74..501ebfd111 100644 --- a/src/Adapter/Driver/Mysqli/Mysqli.php +++ b/src/Adapter/Driver/Mysqli/Mysqli.php @@ -296,6 +296,7 @@ public function checkConnection(Statement $statement = null) } } + return $this; } From f5c9e8c1c37124db8abb546e6e3fafa315a21227 Mon Sep 17 00:00:00 2001 From: Stalin Date: Sun, 4 Mar 2018 17:44:06 +0200 Subject: [PATCH 05/15] Fix code style --- src/Adapter/Driver/Mysqli/Mysqli.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Adapter/Driver/Mysqli/Mysqli.php b/src/Adapter/Driver/Mysqli/Mysqli.php index 501ebfd111..e771c22a74 100644 --- a/src/Adapter/Driver/Mysqli/Mysqli.php +++ b/src/Adapter/Driver/Mysqli/Mysqli.php @@ -296,7 +296,6 @@ public function checkConnection(Statement $statement = null) } } - return $this; } From 809efd0e25e7bc271e8f92d0b3e95d8bb9c23661 Mon Sep 17 00:00:00 2001 From: Stalin Date: Sun, 4 Mar 2018 17:57:56 +0200 Subject: [PATCH 06/15] Fix code style --- src/Adapter/Driver/Mysqli/Mysqli.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Adapter/Driver/Mysqli/Mysqli.php b/src/Adapter/Driver/Mysqli/Mysqli.php index e771c22a74..ac266077bd 100644 --- a/src/Adapter/Driver/Mysqli/Mysqli.php +++ b/src/Adapter/Driver/Mysqli/Mysqli.php @@ -287,16 +287,14 @@ public function checkConnection(Statement $statement = null) ->getResource(); if ($mysqli->ping()) { - if( $statement instanceof Statement) { + if ($statement instanceof Statement) { $statement->initialize($mysqli); } return $this; } - } } return $this; } - -} \ No newline at end of file +} From b1c64e4ee502e9b42ddc3ae0a5698abcd12dea41 Mon Sep 17 00:00:00 2001 From: Stalin Date: Wed, 7 Mar 2018 01:39:19 +0200 Subject: [PATCH 07/15] Simplify logic. --- src/Adapter/Driver/Mysqli/Mysqli.php | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Adapter/Driver/Mysqli/Mysqli.php b/src/Adapter/Driver/Mysqli/Mysqli.php index ac266077bd..c3b040e02a 100644 --- a/src/Adapter/Driver/Mysqli/Mysqli.php +++ b/src/Adapter/Driver/Mysqli/Mysqli.php @@ -280,20 +280,18 @@ public function checkConnection(Statement $statement = null) $mysqli = $this->connection->getResource(); for ($i = 0; $i < $reconnectTries; ++$i) { - if (! $mysqli->ping()) { - $mysqli = $this->connection - ->disconnect() - ->connect() - ->getResource(); - - if ($mysqli->ping()) { - if ($statement instanceof Statement) { - $statement->initialize($mysqli); - } - - return $this; + if ($mysqli->ping()) { + if ($statement instanceof Statement) { + $statement->initialize($mysqli); } + + return $this; } + + $mysqli = $this->connection + ->disconnect() + ->connect() + ->getResource(); } return $this; } From 2285cff246a72489a66ebb5c18b6555feaaac223 Mon Sep 17 00:00:00 2001 From: Stalin Date: Tue, 13 Mar 2018 02:22:34 +0200 Subject: [PATCH 08/15] Add checkConnection method to DriverInterface. --- src/Adapter/Driver/DriverInterface.php | 13 ++++++++++++- src/Adapter/Driver/IbmDb2/IbmDb2.php | 10 ++++++++++ src/Adapter/Driver/Oci8/Oci8.php | 10 ++++++++++ src/Adapter/Driver/Pdo/Pdo.php | 10 ++++++++++ src/Adapter/Driver/Pgsql/Pgsql.php | 10 ++++++++++ src/Adapter/Driver/Sqlsrv/Sqlsrv.php | 10 ++++++++++ 6 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/Adapter/Driver/DriverInterface.php b/src/Adapter/Driver/DriverInterface.php index 615dbb729f..7bcdb6a57e 100644 --- a/src/Adapter/Driver/DriverInterface.php +++ b/src/Adapter/Driver/DriverInterface.php @@ -9,6 +9,8 @@ namespace Zend\Db\Adapter\Driver; +use Zend\Db\Adapter\Driver\Mysqli\Statement; + interface DriverInterface { const PARAMETERIZATION_POSITIONAL = 'positional'; @@ -65,7 +67,7 @@ public function getPrepareType(); * Format parameter name * * @param string $name - * @param mixed $type + * @param mixed $type * @return string */ public function formatParameterName($name, $type = null); @@ -76,4 +78,13 @@ public function formatParameterName($name, $type = null); * @return mixed */ public function getLastGeneratedValue(); + + /** + * Check current connection to DB. + * If connection lost try reconnect. + * + * @param Statement|null $statement + * @return $this + */ + public function checkConnection(Statement $statement = null); } diff --git a/src/Adapter/Driver/IbmDb2/IbmDb2.php b/src/Adapter/Driver/IbmDb2/IbmDb2.php index 11a064cc91..a38f374958 100644 --- a/src/Adapter/Driver/IbmDb2/IbmDb2.php +++ b/src/Adapter/Driver/IbmDb2/IbmDb2.php @@ -10,6 +10,7 @@ namespace Zend\Db\Adapter\Driver\IbmDb2; use Zend\Db\Adapter\Driver\DriverInterface; +use Zend\Db\Adapter\Driver\Mysqli\Statement; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; @@ -211,4 +212,13 @@ public function getLastGeneratedValue() { return $this->connection->getLastGeneratedValue(); } + + /** + * @inheritdoc + * @param Statement|null $statement + * @return $this + */ + public function checkConnection(Statement $statement = null) { + return $this; + } } diff --git a/src/Adapter/Driver/Oci8/Oci8.php b/src/Adapter/Driver/Oci8/Oci8.php index 3f0ba5af99..a32831626a 100644 --- a/src/Adapter/Driver/Oci8/Oci8.php +++ b/src/Adapter/Driver/Oci8/Oci8.php @@ -10,6 +10,7 @@ namespace Zend\Db\Adapter\Driver\Oci8; use Zend\Db\Adapter\Driver\DriverInterface; +use Zend\Db\Adapter\Driver\Mysqli\Statement; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; use Zend\Db\Adapter\Driver\Feature\AbstractFeature; @@ -299,4 +300,13 @@ public function getLastGeneratedValue() { return $this->getConnection()->getLastGeneratedValue(); } + + /** + * @inheritdoc + * @param Statement|null $statement + * @return $this + */ + public function checkConnection(Statement $statement = null) { + return $this; + } } diff --git a/src/Adapter/Driver/Pdo/Pdo.php b/src/Adapter/Driver/Pdo/Pdo.php index daf4075c5e..84381aa2d2 100644 --- a/src/Adapter/Driver/Pdo/Pdo.php +++ b/src/Adapter/Driver/Pdo/Pdo.php @@ -13,6 +13,7 @@ use Zend\Db\Adapter\Driver\DriverInterface; use Zend\Db\Adapter\Driver\Feature\AbstractFeature; use Zend\Db\Adapter\Driver\Feature\DriverFeatureInterface; +use Zend\Db\Adapter\Driver\Mysqli\Statement; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; @@ -328,4 +329,13 @@ public function getLastGeneratedValue($name = null) { return $this->connection->getLastGeneratedValue($name); } + + /** + * @inheritdoc + * @param Statement|null $statement + * @return $this + */ + public function checkConnection(Statement $statement = null) { + return $this; + } } diff --git a/src/Adapter/Driver/Pgsql/Pgsql.php b/src/Adapter/Driver/Pgsql/Pgsql.php index b99b641e30..3e48f658e6 100644 --- a/src/Adapter/Driver/Pgsql/Pgsql.php +++ b/src/Adapter/Driver/Pgsql/Pgsql.php @@ -10,6 +10,7 @@ namespace Zend\Db\Adapter\Driver\Pgsql; use Zend\Db\Adapter\Driver\DriverInterface; +use Zend\Db\Adapter\Driver\Mysqli\Statement; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; @@ -234,4 +235,13 @@ public function getLastGeneratedValue($name = null) { return $this->connection->getLastGeneratedValue($name); } + + /** + * @inheritdoc + * @param Statement|null $statement + * @return $this + */ + public function checkConnection(Statement $statement = null) { + return $this; + } } diff --git a/src/Adapter/Driver/Sqlsrv/Sqlsrv.php b/src/Adapter/Driver/Sqlsrv/Sqlsrv.php index 9a712905a5..ab29e4f5b6 100644 --- a/src/Adapter/Driver/Sqlsrv/Sqlsrv.php +++ b/src/Adapter/Driver/Sqlsrv/Sqlsrv.php @@ -10,6 +10,7 @@ namespace Zend\Db\Adapter\Driver\Sqlsrv; use Zend\Db\Adapter\Driver\DriverInterface; +use Zend\Db\Adapter\Driver\Mysqli\Statement; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; @@ -212,4 +213,13 @@ public function getLastGeneratedValue() { return $this->getConnection()->getLastGeneratedValue(); } + + /** + * @inheritdoc + * @param Statement|null $statement + * @return $this + */ + public function checkConnection(Statement $statement = null) { + return $this; + } } From f30b1987640a2c4c04f6f95a8e393db49dc02ac1 Mon Sep 17 00:00:00 2001 From: Stalin Date: Tue, 13 Mar 2018 02:35:45 +0200 Subject: [PATCH 09/15] Fix wrong interface import. --- src/Adapter/Driver/DriverInterface.php | 4 ++-- src/Adapter/Driver/IbmDb2/IbmDb2.php | 5 +++-- src/Adapter/Driver/Mysqli/Mysqli.php | 5 +++-- src/Adapter/Driver/Oci8/Oci8.php | 5 +++-- src/Adapter/Driver/Pdo/Pdo.php | 5 +++-- src/Adapter/Driver/Pgsql/Pgsql.php | 5 +++-- src/Adapter/Driver/Sqlsrv/Sqlsrv.php | 5 +++-- 7 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/Adapter/Driver/DriverInterface.php b/src/Adapter/Driver/DriverInterface.php index 7bcdb6a57e..cd76bfd830 100644 --- a/src/Adapter/Driver/DriverInterface.php +++ b/src/Adapter/Driver/DriverInterface.php @@ -83,8 +83,8 @@ public function getLastGeneratedValue(); * Check current connection to DB. * If connection lost try reconnect. * - * @param Statement|null $statement + * @param StatementInterface|null $statement * @return $this */ - public function checkConnection(Statement $statement = null); + public function checkConnection(StatementInterface $statement = null); } diff --git a/src/Adapter/Driver/IbmDb2/IbmDb2.php b/src/Adapter/Driver/IbmDb2/IbmDb2.php index a38f374958..f56b138a98 100644 --- a/src/Adapter/Driver/IbmDb2/IbmDb2.php +++ b/src/Adapter/Driver/IbmDb2/IbmDb2.php @@ -11,6 +11,7 @@ use Zend\Db\Adapter\Driver\DriverInterface; use Zend\Db\Adapter\Driver\Mysqli\Statement; +use Zend\Db\Adapter\Driver\StatementInterface; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; @@ -215,10 +216,10 @@ public function getLastGeneratedValue() /** * @inheritdoc - * @param Statement|null $statement + * @param StatementInterface|null $statement * @return $this */ - public function checkConnection(Statement $statement = null) { + public function checkConnection(StatementInterface $statement = null) { return $this; } } diff --git a/src/Adapter/Driver/Mysqli/Mysqli.php b/src/Adapter/Driver/Mysqli/Mysqli.php index c3b040e02a..c3d56bec18 100644 --- a/src/Adapter/Driver/Mysqli/Mysqli.php +++ b/src/Adapter/Driver/Mysqli/Mysqli.php @@ -11,6 +11,7 @@ use mysqli_stmt; use Zend\Db\Adapter\Driver\DriverInterface; +use Zend\Db\Adapter\Driver\StatementInterface; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; @@ -265,10 +266,10 @@ public function getLastGeneratedValue() * Check connection if not exists -> try to reconnect. * Depends on configuration value `reconnect_tries` * - * @param Statement|null $statement + * @param StatementInterface|null $statement * @return $this */ - public function checkConnection(Statement $statement = null) + public function checkConnection(StatementInterface $statement = null) { $reconnectTries = array_key_exists('reconnect_tries', $this->options) diff --git a/src/Adapter/Driver/Oci8/Oci8.php b/src/Adapter/Driver/Oci8/Oci8.php index a32831626a..79e12b6bab 100644 --- a/src/Adapter/Driver/Oci8/Oci8.php +++ b/src/Adapter/Driver/Oci8/Oci8.php @@ -11,6 +11,7 @@ use Zend\Db\Adapter\Driver\DriverInterface; use Zend\Db\Adapter\Driver\Mysqli\Statement; +use Zend\Db\Adapter\Driver\StatementInterface; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; use Zend\Db\Adapter\Driver\Feature\AbstractFeature; @@ -303,10 +304,10 @@ public function getLastGeneratedValue() /** * @inheritdoc - * @param Statement|null $statement + * @param StatementInterface|null $statement * @return $this */ - public function checkConnection(Statement $statement = null) { + public function checkConnection(StatementInterface $statement = null) { return $this; } } diff --git a/src/Adapter/Driver/Pdo/Pdo.php b/src/Adapter/Driver/Pdo/Pdo.php index 84381aa2d2..812ad08928 100644 --- a/src/Adapter/Driver/Pdo/Pdo.php +++ b/src/Adapter/Driver/Pdo/Pdo.php @@ -14,6 +14,7 @@ use Zend\Db\Adapter\Driver\Feature\AbstractFeature; use Zend\Db\Adapter\Driver\Feature\DriverFeatureInterface; use Zend\Db\Adapter\Driver\Mysqli\Statement; +use Zend\Db\Adapter\Driver\StatementInterface; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; @@ -332,10 +333,10 @@ public function getLastGeneratedValue($name = null) /** * @inheritdoc - * @param Statement|null $statement + * @param StatementInterface|null $statement * @return $this */ - public function checkConnection(Statement $statement = null) { + public function checkConnection(StatementInterface $statement = null) { return $this; } } diff --git a/src/Adapter/Driver/Pgsql/Pgsql.php b/src/Adapter/Driver/Pgsql/Pgsql.php index 3e48f658e6..b84d054c79 100644 --- a/src/Adapter/Driver/Pgsql/Pgsql.php +++ b/src/Adapter/Driver/Pgsql/Pgsql.php @@ -11,6 +11,7 @@ use Zend\Db\Adapter\Driver\DriverInterface; use Zend\Db\Adapter\Driver\Mysqli\Statement; +use Zend\Db\Adapter\Driver\StatementInterface; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; @@ -238,10 +239,10 @@ public function getLastGeneratedValue($name = null) /** * @inheritdoc - * @param Statement|null $statement + * @param StatementInterface|null $statement * @return $this */ - public function checkConnection(Statement $statement = null) { + public function checkConnection(StatementInterface $statement = null) { return $this; } } diff --git a/src/Adapter/Driver/Sqlsrv/Sqlsrv.php b/src/Adapter/Driver/Sqlsrv/Sqlsrv.php index ab29e4f5b6..5dc5f1124c 100644 --- a/src/Adapter/Driver/Sqlsrv/Sqlsrv.php +++ b/src/Adapter/Driver/Sqlsrv/Sqlsrv.php @@ -11,6 +11,7 @@ use Zend\Db\Adapter\Driver\DriverInterface; use Zend\Db\Adapter\Driver\Mysqli\Statement; +use Zend\Db\Adapter\Driver\StatementInterface; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; @@ -216,10 +217,10 @@ public function getLastGeneratedValue() /** * @inheritdoc - * @param Statement|null $statement + * @param StatementInterface|null $statement * @return $this */ - public function checkConnection(Statement $statement = null) { + public function checkConnection(StatementInterface $statement = null) { return $this; } } From b70ac1f18a2262ee8b4b23da58dfb48e60ce82c6 Mon Sep 17 00:00:00 2001 From: Stalin Date: Tue, 13 Mar 2018 02:42:05 +0200 Subject: [PATCH 10/15] Fix wrong interface import. --- src/Adapter/Driver/IbmDb2/IbmDb2.php | 1 - src/Adapter/Driver/Oci8/Oci8.php | 1 - src/Adapter/Driver/Pdo/Pdo.php | 1 - src/Adapter/Driver/Pgsql/Pgsql.php | 1 - src/Adapter/Driver/Sqlsrv/Sqlsrv.php | 1 - 5 files changed, 5 deletions(-) diff --git a/src/Adapter/Driver/IbmDb2/IbmDb2.php b/src/Adapter/Driver/IbmDb2/IbmDb2.php index f56b138a98..673c6ff2f1 100644 --- a/src/Adapter/Driver/IbmDb2/IbmDb2.php +++ b/src/Adapter/Driver/IbmDb2/IbmDb2.php @@ -10,7 +10,6 @@ namespace Zend\Db\Adapter\Driver\IbmDb2; use Zend\Db\Adapter\Driver\DriverInterface; -use Zend\Db\Adapter\Driver\Mysqli\Statement; use Zend\Db\Adapter\Driver\StatementInterface; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; diff --git a/src/Adapter/Driver/Oci8/Oci8.php b/src/Adapter/Driver/Oci8/Oci8.php index 79e12b6bab..c7788a74e2 100644 --- a/src/Adapter/Driver/Oci8/Oci8.php +++ b/src/Adapter/Driver/Oci8/Oci8.php @@ -10,7 +10,6 @@ namespace Zend\Db\Adapter\Driver\Oci8; use Zend\Db\Adapter\Driver\DriverInterface; -use Zend\Db\Adapter\Driver\Mysqli\Statement; use Zend\Db\Adapter\Driver\StatementInterface; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; diff --git a/src/Adapter/Driver/Pdo/Pdo.php b/src/Adapter/Driver/Pdo/Pdo.php index 812ad08928..0b124dbdba 100644 --- a/src/Adapter/Driver/Pdo/Pdo.php +++ b/src/Adapter/Driver/Pdo/Pdo.php @@ -13,7 +13,6 @@ use Zend\Db\Adapter\Driver\DriverInterface; use Zend\Db\Adapter\Driver\Feature\AbstractFeature; use Zend\Db\Adapter\Driver\Feature\DriverFeatureInterface; -use Zend\Db\Adapter\Driver\Mysqli\Statement; use Zend\Db\Adapter\Driver\StatementInterface; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; diff --git a/src/Adapter/Driver/Pgsql/Pgsql.php b/src/Adapter/Driver/Pgsql/Pgsql.php index b84d054c79..456cd888d5 100644 --- a/src/Adapter/Driver/Pgsql/Pgsql.php +++ b/src/Adapter/Driver/Pgsql/Pgsql.php @@ -10,7 +10,6 @@ namespace Zend\Db\Adapter\Driver\Pgsql; use Zend\Db\Adapter\Driver\DriverInterface; -use Zend\Db\Adapter\Driver\Mysqli\Statement; use Zend\Db\Adapter\Driver\StatementInterface; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; diff --git a/src/Adapter/Driver/Sqlsrv/Sqlsrv.php b/src/Adapter/Driver/Sqlsrv/Sqlsrv.php index 5dc5f1124c..58f95ad7ce 100644 --- a/src/Adapter/Driver/Sqlsrv/Sqlsrv.php +++ b/src/Adapter/Driver/Sqlsrv/Sqlsrv.php @@ -10,7 +10,6 @@ namespace Zend\Db\Adapter\Driver\Sqlsrv; use Zend\Db\Adapter\Driver\DriverInterface; -use Zend\Db\Adapter\Driver\Mysqli\Statement; use Zend\Db\Adapter\Driver\StatementInterface; use Zend\Db\Adapter\Exception; use Zend\Db\Adapter\Profiler; From aa10b09651e49a9c7ae4af2d36b93e87d28158f2 Mon Sep 17 00:00:00 2001 From: Stalin Date: Tue, 13 Mar 2018 02:46:52 +0200 Subject: [PATCH 11/15] Apply code style fixes; --- src/Adapter/Driver/IbmDb2/IbmDb2.php | 3 ++- src/Adapter/Driver/Oci8/Oci8.php | 3 ++- src/Adapter/Driver/Pdo/Pdo.php | 3 ++- src/Adapter/Driver/Pgsql/Pgsql.php | 3 ++- src/Adapter/Driver/Sqlsrv/Sqlsrv.php | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Adapter/Driver/IbmDb2/IbmDb2.php b/src/Adapter/Driver/IbmDb2/IbmDb2.php index 673c6ff2f1..79c27342d4 100644 --- a/src/Adapter/Driver/IbmDb2/IbmDb2.php +++ b/src/Adapter/Driver/IbmDb2/IbmDb2.php @@ -218,7 +218,8 @@ public function getLastGeneratedValue() * @param StatementInterface|null $statement * @return $this */ - public function checkConnection(StatementInterface $statement = null) { + public function checkConnection(StatementInterface $statement = null) + { return $this; } } diff --git a/src/Adapter/Driver/Oci8/Oci8.php b/src/Adapter/Driver/Oci8/Oci8.php index c7788a74e2..93db172d2c 100644 --- a/src/Adapter/Driver/Oci8/Oci8.php +++ b/src/Adapter/Driver/Oci8/Oci8.php @@ -306,7 +306,8 @@ public function getLastGeneratedValue() * @param StatementInterface|null $statement * @return $this */ - public function checkConnection(StatementInterface $statement = null) { + public function checkConnection(StatementInterface $statement = null) + { return $this; } } diff --git a/src/Adapter/Driver/Pdo/Pdo.php b/src/Adapter/Driver/Pdo/Pdo.php index 0b124dbdba..15609cc45b 100644 --- a/src/Adapter/Driver/Pdo/Pdo.php +++ b/src/Adapter/Driver/Pdo/Pdo.php @@ -335,7 +335,8 @@ public function getLastGeneratedValue($name = null) * @param StatementInterface|null $statement * @return $this */ - public function checkConnection(StatementInterface $statement = null) { + public function checkConnection(StatementInterface $statement = null) + { return $this; } } diff --git a/src/Adapter/Driver/Pgsql/Pgsql.php b/src/Adapter/Driver/Pgsql/Pgsql.php index 456cd888d5..87755fa85d 100644 --- a/src/Adapter/Driver/Pgsql/Pgsql.php +++ b/src/Adapter/Driver/Pgsql/Pgsql.php @@ -241,7 +241,8 @@ public function getLastGeneratedValue($name = null) * @param StatementInterface|null $statement * @return $this */ - public function checkConnection(StatementInterface $statement = null) { + public function checkConnection(StatementInterface $statement = null) + { return $this; } } diff --git a/src/Adapter/Driver/Sqlsrv/Sqlsrv.php b/src/Adapter/Driver/Sqlsrv/Sqlsrv.php index 58f95ad7ce..228ea8aa96 100644 --- a/src/Adapter/Driver/Sqlsrv/Sqlsrv.php +++ b/src/Adapter/Driver/Sqlsrv/Sqlsrv.php @@ -219,7 +219,8 @@ public function getLastGeneratedValue() * @param StatementInterface|null $statement * @return $this */ - public function checkConnection(StatementInterface $statement = null) { + public function checkConnection(StatementInterface $statement = null) + { return $this; } } From f444d8c435071fb7d2e4b2308b523b4180d564a1 Mon Sep 17 00:00:00 2001 From: Stalin Date: Fri, 13 Apr 2018 00:31:39 +0300 Subject: [PATCH 12/15] Add temporary fixes --- src/Adapter/Adapter.php | 2 +- src/Adapter/Driver/Pgsql/Pgsql.php | 25 +++++++++++++++++++++++++ src/Adapter/Driver/Pgsql/Statement.php | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Adapter/Adapter.php b/src/Adapter/Adapter.php index e356ff9a58..ad89139360 100644 --- a/src/Adapter/Adapter.php +++ b/src/Adapter/Adapter.php @@ -313,7 +313,7 @@ protected function createDriver($parameters) $driver = new Driver\Oci8\Oci8($parameters); break; case 'pgsql': - $driver = new Driver\Pgsql\Pgsql($parameters); + $driver = new Driver\Pgsql\Pgsql($parameters, null, null, $options); break; case 'ibmdb2': $driver = new Driver\IbmDb2\IbmDb2($parameters); diff --git a/src/Adapter/Driver/Pgsql/Pgsql.php b/src/Adapter/Driver/Pgsql/Pgsql.php index 87755fa85d..2b6b97a9a2 100644 --- a/src/Adapter/Driver/Pgsql/Pgsql.php +++ b/src/Adapter/Driver/Pgsql/Pgsql.php @@ -61,6 +61,7 @@ public function __construct( $connection = new Connection($connection); } + $this->options = array_merge($this->options, $options); $this->registerConnection($connection); $this->registerStatementPrototype(($statementPrototype) ?: new Statement()); $this->registerResultPrototype(($resultPrototype) ?: new Result()); @@ -243,6 +244,30 @@ public function getLastGeneratedValue($name = null) */ public function checkConnection(StatementInterface $statement = null) { + + $reconnectTries = array_key_exists('reconnect_tries', $this->options) + ? $this->options['reconnect_tries'] : 0; + + $pg_sql = $this->connection->getResource(); + + for ($i = 0; $i < $reconnectTries; ++$i) { + if (pg_connection_status($pg_sql) == PGSQL_CONNECTION_OK) { + if ($statement instanceof Statement) { + $statement->initialize($pg_sql); + } + + return $this; + } + + try { + $pg_sql = $this->connection + ->disconnect() + ->connect() + ->getResource(); + } catch (Exception\RuntimeException $e) { + print $e->getMessage(); + } + } return $this; } } diff --git a/src/Adapter/Driver/Pgsql/Statement.php b/src/Adapter/Driver/Pgsql/Statement.php index c7e8450626..6a4ec15eec 100644 --- a/src/Adapter/Driver/Pgsql/Statement.php +++ b/src/Adapter/Driver/Pgsql/Statement.php @@ -202,6 +202,7 @@ public function execute($parameters = null) if (! $this->isPrepared()) { $this->prepare(); } + $this->driver->checkConnection($this); /** START Standard ParameterContainer Merging Block */ if (! $this->parameterContainer instanceof ParameterContainer) { From bed0146c4e97be464488f2e30932cc8a39508d09 Mon Sep 17 00:00:00 2001 From: Stalin Date: Wed, 25 Apr 2018 01:42:16 +0300 Subject: [PATCH 13/15] Add implementation for pgsql driver. --- src/Adapter/Driver/Pgsql/Pgsql.php | 4 +++- src/Adapter/Driver/Pgsql/Statement.php | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Adapter/Driver/Pgsql/Pgsql.php b/src/Adapter/Driver/Pgsql/Pgsql.php index 2b6b97a9a2..04b5648e44 100644 --- a/src/Adapter/Driver/Pgsql/Pgsql.php +++ b/src/Adapter/Driver/Pgsql/Pgsql.php @@ -253,7 +253,9 @@ public function checkConnection(StatementInterface $statement = null) for ($i = 0; $i < $reconnectTries; ++$i) { if (pg_connection_status($pg_sql) == PGSQL_CONNECTION_OK) { if ($statement instanceof Statement) { - $statement->initialize($pg_sql); + $statement + ->initialize($pg_sql) + ->prepare(); } return $this; diff --git a/src/Adapter/Driver/Pgsql/Statement.php b/src/Adapter/Driver/Pgsql/Statement.php index 6a4ec15eec..f54a23acf9 100644 --- a/src/Adapter/Driver/Pgsql/Statement.php +++ b/src/Adapter/Driver/Pgsql/Statement.php @@ -88,7 +88,7 @@ public function getProfiler() * Initialize * * @param resource $pgsql - * @return void + * @return $this * @throws Exception\RuntimeException for invalid or missing postgresql connection */ public function initialize($pgsql) @@ -101,6 +101,7 @@ public function initialize($pgsql) )); } $this->pgsql = $pgsql; + return $this; } /** From 60878db9fb520d29576f7b54fc831f337b6a9f85 Mon Sep 17 00:00:00 2001 From: Stalin Date: Wed, 25 Apr 2018 01:48:36 +0300 Subject: [PATCH 14/15] Add is_array check for empty extra options. --- src/Adapter/Driver/Pgsql/Pgsql.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Adapter/Driver/Pgsql/Pgsql.php b/src/Adapter/Driver/Pgsql/Pgsql.php index 04b5648e44..5547d123bc 100644 --- a/src/Adapter/Driver/Pgsql/Pgsql.php +++ b/src/Adapter/Driver/Pgsql/Pgsql.php @@ -61,7 +61,9 @@ public function __construct( $connection = new Connection($connection); } - $this->options = array_merge($this->options, $options); + if (is_array($options)) { + $this->options = array_merge($this->options, $options); + } $this->registerConnection($connection); $this->registerStatementPrototype(($statementPrototype) ?: new Statement()); $this->registerResultPrototype(($resultPrototype) ?: new Result()); From 33be3507f8a40ea37fbdedd337f30071504513e9 Mon Sep 17 00:00:00 2001 From: Stalin Date: Sat, 28 Apr 2018 01:25:09 +0300 Subject: [PATCH 15/15] Change default options value. --- src/Adapter/Driver/Pgsql/Pgsql.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Adapter/Driver/Pgsql/Pgsql.php b/src/Adapter/Driver/Pgsql/Pgsql.php index 5547d123bc..ac68af0cc0 100644 --- a/src/Adapter/Driver/Pgsql/Pgsql.php +++ b/src/Adapter/Driver/Pgsql/Pgsql.php @@ -55,15 +55,13 @@ public function __construct( $connection, Statement $statementPrototype = null, Result $resultPrototype = null, - $options = null + $options = [] ) { if (! $connection instanceof Connection) { $connection = new Connection($connection); } + $this->options = array_merge($this->options, $options); - if (is_array($options)) { - $this->options = array_merge($this->options, $options); - } $this->registerConnection($connection); $this->registerStatementPrototype(($statementPrototype) ?: new Statement()); $this->registerResultPrototype(($resultPrototype) ?: new Result());