From 0e15727116cda2c1978f81ab9d53d9546e486ea7 Mon Sep 17 00:00:00 2001 From: Roman Jadrovski Date: Fri, 19 Jul 2019 12:35:01 +0300 Subject: [PATCH 1/3] fix some of AbstractPlatform inheritors that preventing \PDO disconnect --- src/Adapter/Platform/Mysql.php | 56 ++++++++++-------- src/Adapter/Platform/Oracle.php | 26 ++++----- src/Adapter/Platform/Postgresql.php | 57 +++++++++++-------- src/Adapter/Platform/SqlServer.php | 38 +++++++------ src/Adapter/Platform/Sqlite.php | 20 +++---- .../Adapter/Platform/MysqlTest.php | 3 - 6 files changed, 107 insertions(+), 93 deletions(-) diff --git a/src/Adapter/Platform/Mysql.php b/src/Adapter/Platform/Mysql.php index 3aae7bbc40..c984e42d14 100644 --- a/src/Adapter/Platform/Mysql.php +++ b/src/Adapter/Platform/Mysql.php @@ -27,9 +27,9 @@ class Mysql extends AbstractPlatform protected $quoteIdentifierTo = '``'; /** - * @var \mysqli|\PDO + * @var \mysqli|Mysqli\Mysqli|Pdo\Pdo */ - protected $resource = null; + protected $driver = null; /** * NOTE: Include dashes for MySQL only, need tests for others platforms @@ -39,7 +39,7 @@ class Mysql extends AbstractPlatform protected $quoteIdentifierFragmentPattern = '/([^0-9,a-z,A-Z$_\-:])/i'; /** - * @param null|\Zend\Db\Adapter\Driver\Mysqli\Mysqli|\Zend\Db\Adapter\Driver\Pdo\Pdo|\mysqli|\PDO $driver + * @param null|\Zend\Db\Adapter\Driver\Mysqli\Mysqli|\Zend\Db\Adapter\Driver\Pdo\Pdo|\mysqli $driver */ public function __construct($driver = null) { @@ -49,7 +49,7 @@ public function __construct($driver = null) } /** - * @param \Zend\Db\Adapter\Driver\Mysqli\Mysqli|\Zend\Db\Adapter\Driver\Pdo\Pdo|\mysqli|\PDO $driver + * @param \Zend\Db\Adapter\Driver\Mysqli\Mysqli|\Zend\Db\Adapter\Driver\Pdo\Pdo|\mysqli $driver * @return self Provides a fluent interface * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException */ @@ -59,14 +59,13 @@ public function setDriver($driver) if ($driver instanceof Mysqli\Mysqli || ($driver instanceof Pdo\Pdo && $driver->getDatabasePlatformName() == 'Mysql') || ($driver instanceof \mysqli) - || ($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'mysql') ) { - $this->resource = $driver; + $this->driver = $driver; return $this; } throw new Exception\InvalidArgumentException( - '$driver must be a Mysqli or Mysql PDO Zend\Db\Adapter\Driver, Mysqli instance or MySQL PDO instance' + '$driver must be a Mysqli, Mysql PDO Zend\Db\Adapter\Driver or Mysqli instance' ); } @@ -91,16 +90,9 @@ public function quoteIdentifierChain($identifierChain) */ public function quoteValue($value) { - if ($this->resource instanceof DriverInterface) { - $this->resource = $this->resource->getConnection()->getResource(); - } - if ($this->resource instanceof \mysqli) { - return '\'' . $this->resource->real_escape_string($value) . '\''; - } - if ($this->resource instanceof \PDO) { - return $this->resource->quote($value); - } - return parent::quoteValue($value); + $quotedViaResource = $this->quoteViaResource($value); + + return $quotedViaResource !== null ? $quotedViaResource : parent::quoteValue($value); } /** @@ -108,15 +100,31 @@ public function quoteValue($value) */ public function quoteTrustedValue($value) { - if ($this->resource instanceof DriverInterface) { - $this->resource = $this->resource->getConnection()->getResource(); + $quotedViaResource = $this->quoteViaResource($value); + + return $quotedViaResource !== null ? $quotedViaResource : parent::quoteTrustedValue($value); + } + + /** + * @param string $value + * + * @return null|string + */ + protected function quoteViaResource($value) + { + if ($this->driver instanceof DriverInterface) { + $resource = $this->driver->getConnection()->getResource(); + } else { + $resource = $this->driver; } - if ($this->resource instanceof \mysqli) { - return '\'' . $this->resource->real_escape_string($value) . '\''; + + if ($resource instanceof \mysqli) { + return '\'' . $resource->real_escape_string($value) . '\''; } - if ($this->resource instanceof \PDO) { - return $this->resource->quote($value); + if ($resource instanceof \PDO) { + return $resource->quote($value); } - return parent::quoteTrustedValue($value); + + return null; } } diff --git a/src/Adapter/Platform/Oracle.php b/src/Adapter/Platform/Oracle.php index 7d66f9372b..708abdd15f 100644 --- a/src/Adapter/Platform/Oracle.php +++ b/src/Adapter/Platform/Oracle.php @@ -19,7 +19,7 @@ class Oracle extends AbstractPlatform /** * @var null|Pdo|Oci8 */ - protected $resource = null; + protected $driver = null; /** * @param array $options @@ -50,15 +50,13 @@ public function setDriver($driver) || ($driver instanceof Pdo && $driver->getDatabasePlatformName() == 'Oracle') || ($driver instanceof Pdo && $driver->getDatabasePlatformName() == 'Sqlite') || ($driver instanceof \oci8) - || ($driver instanceof PDO && $driver->getAttribute(PDO::ATTR_DRIVER_NAME) == 'oci') ) { - $this->resource = $driver; + $this->driver = $driver; return $this; } throw new InvalidArgumentException( - '$driver must be a Oci8 or Oracle PDO Zend\Db\Adapter\Driver, ' - . 'Oci8 instance, or Oci PDO instance' + '$driver must be a Oci8, Oracle PDO Zend\Db\Adapter\Driver or Oci8 instance' ); } @@ -67,7 +65,7 @@ public function setDriver($driver) */ public function getDriver() { - return $this->resource; + return $this->driver; } /** @@ -95,17 +93,19 @@ public function quoteIdentifierChain($identifierChain) */ public function quoteValue($value) { - if ($this->resource instanceof DriverInterface) { - $this->resource = $this->resource->getConnection()->getResource(); + if ($this->driver instanceof DriverInterface) { + $resource = $this->driver->getConnection()->getResource(); + } else { + $resource = $this->driver; } - if ($this->resource) { - if ($this->resource instanceof PDO) { - return $this->resource->quote($value); + if ($resource) { + if ($resource instanceof PDO) { + return $resource->quote($value); } - if (get_resource_type($this->resource) == 'oci8 connection' - || get_resource_type($this->resource) == 'oci8 persistent connection' + if (get_resource_type($resource) == 'oci8 connection' + || get_resource_type($resource) == 'oci8 persistent connection' ) { return "'" . addcslashes(str_replace("'", "''", $value), "\x00\n\r\"\x1a") . "'"; } diff --git a/src/Adapter/Platform/Postgresql.php b/src/Adapter/Platform/Postgresql.php index b844f2effc..e07e86e162 100644 --- a/src/Adapter/Platform/Postgresql.php +++ b/src/Adapter/Platform/Postgresql.php @@ -24,12 +24,12 @@ class Postgresql extends AbstractPlatform protected $quoteIdentifierTo = '""'; /** - * @var resource|\PDO + * @var resource */ - protected $resource = null; + protected $driver = null; /** - * @param null|\Zend\Db\Adapter\Driver\Pgsql\Pgsql|\Zend\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver + * @param null|\Zend\Db\Adapter\Driver\Pgsql\Pgsql|\Zend\Db\Adapter\Driver\Pdo\Pdo|resource $driver */ public function __construct($driver = null) { @@ -39,7 +39,7 @@ public function __construct($driver = null) } /** - * @param \Zend\Db\Adapter\Driver\Pgsql\Pgsql|\Zend\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver + * @param \Zend\Db\Adapter\Driver\Pgsql\Pgsql|\Zend\Db\Adapter\Driver\Pdo\Pdo|resource $driver * @return self Provides a fluent interface * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException */ @@ -48,15 +48,13 @@ public function setDriver($driver) if ($driver instanceof Pgsql\Pgsql || ($driver instanceof Pdo\Pdo && $driver->getDatabasePlatformName() == 'Postgresql') || (is_resource($driver) && (in_array(get_resource_type($driver), ['pgsql link', 'pgsql link persistent']))) - || ($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'pgsql') ) { - $this->resource = $driver; + $this->driver = $driver; return $this; } throw new Exception\InvalidArgumentException( - '$driver must be a Pgsql or Postgresql PDO Zend\Db\Adapter\Driver, pgsql link resource or Postgresql PDO ' - . 'instance' + '$driver must be a Pgsql, Postgresql PDO Zend\Db\Adapter\Driver or pgsql link resource' ); } @@ -81,16 +79,9 @@ public function quoteIdentifierChain($identifierChain) */ public function quoteValue($value) { - if ($this->resource instanceof DriverInterface) { - $this->resource = $this->resource->getConnection()->getResource(); - } - if (is_resource($this->resource)) { - return '\'' . pg_escape_string($this->resource, $value) . '\''; - } - if ($this->resource instanceof \PDO) { - return $this->resource->quote($value); - } - return 'E' . parent::quoteValue($value); + $quotedViaResource = $this->quoteViaResource($value); + + return $quotedViaResource !== null ? $quotedViaResource : ('E' . parent::quoteValue($value)); } /** @@ -98,15 +89,31 @@ public function quoteValue($value) */ public function quoteTrustedValue($value) { - if ($this->resource instanceof DriverInterface) { - $this->resource = $this->resource->getConnection()->getResource(); + $quotedViaResource = $this->quoteViaResource($value); + + return $quotedViaResource !== null ? $quotedViaResource : ('E' . parent::quoteTrustedValue($value)); + } + + /** + * @param string $value + * + * @return null|string + */ + protected function quoteViaResource($value) + { + if ($this->driver instanceof DriverInterface) { + $resource = $this->driver->getConnection()->getResource(); + } else { + $resource = $this->driver; } - if (is_resource($this->resource)) { - return '\'' . pg_escape_string($this->resource, $value) . '\''; + + if (is_resource($resource)) { + return '\'' . pg_escape_string($resource, $value) . '\''; } - if ($this->resource instanceof \PDO) { - return $this->resource->quote($value); + if ($resource instanceof \PDO) { + return $resource->quote($value); } - return 'E' . parent::quoteTrustedValue($value); + + return null; } } diff --git a/src/Adapter/Platform/SqlServer.php b/src/Adapter/Platform/SqlServer.php index f6272a5b5f..ad0d35def8 100644 --- a/src/Adapter/Platform/SqlServer.php +++ b/src/Adapter/Platform/SqlServer.php @@ -26,12 +26,12 @@ class SqlServer extends AbstractPlatform protected $quoteIdentifierTo = '\\'; /** - * @var resource|\PDO + * @var resource */ - protected $resource = null; + protected $driver = null; /** - * @param null|\Zend\Db\Adapter\Driver\Sqlsrv\Sqlsrv|\Zend\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver + * @param null|\Zend\Db\Adapter\Driver\Sqlsrv\Sqlsrv|\Zend\Db\Adapter\Driver\Pdo\Pdo|resource $driver */ public function __construct($driver = null) { @@ -41,22 +41,20 @@ public function __construct($driver = null) } /** - * @param \Zend\Db\Adapter\Driver\Sqlsrv\Sqlsrv|\Zend\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver + * @param \Zend\Db\Adapter\Driver\Sqlsrv\Sqlsrv|\Zend\Db\Adapter\Driver\Pdo\Pdo|resource $driver * @return self Provides a fluent interface * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException */ public function setDriver($driver) { // handle Zend\Db drivers - if (($driver instanceof Pdo\Pdo && in_array($driver->getDatabasePlatformName(), ['SqlServer', 'Dblib'])) - || ($driver instanceof \PDO && in_array($driver->getAttribute(\PDO::ATTR_DRIVER_NAME), ['sqlsrv', 'dblib'])) - ) { - $this->resource = $driver; + if ($driver instanceof Pdo\Pdo && in_array($driver->getDatabasePlatformName(), ['SqlServer', 'Dblib'])) { + $this->driver = $driver; return $this; } throw new Exception\InvalidArgumentException( - '$driver must be a Sqlsrv PDO Zend\Db\Adapter\Driver or Sqlsrv PDO instance' + '$driver must be a Sqlsrv PDO Zend\Db\Adapter\Driver' ); } @@ -89,11 +87,14 @@ public function quoteIdentifierChain($identifierChain) */ public function quoteValue($value) { - if ($this->resource instanceof DriverInterface) { - $this->resource = $this->resource->getConnection()->getResource(); + if ($this->driver instanceof DriverInterface) { + $resource = $this->driver->getConnection()->getResource(); + } else { + $resource = $this->driver; } - if ($this->resource instanceof \PDO) { - return $this->resource->quote($value); + + if ($resource instanceof \PDO) { + return $resource->quote($value); } trigger_error( 'Attempting to quote a value in ' . __CLASS__ . ' without extension/driver support ' @@ -108,11 +109,14 @@ public function quoteValue($value) */ public function quoteTrustedValue($value) { - if ($this->resource instanceof DriverInterface) { - $this->resource = $this->resource->getConnection()->getResource(); + if ($this->driver instanceof DriverInterface) { + $resource = $this->driver->getConnection()->getResource(); + } else { + $resource = $this->driver; } - if ($this->resource instanceof \PDO) { - return $this->resource->quote($value); + + if ($resource instanceof \PDO) { + return $resource->quote($value); } return '\'' . str_replace('\'', '\'\'', $value) . '\''; } diff --git a/src/Adapter/Platform/Sqlite.php b/src/Adapter/Platform/Sqlite.php index 9253baba1a..9ccec35858 100644 --- a/src/Adapter/Platform/Sqlite.php +++ b/src/Adapter/Platform/Sqlite.php @@ -26,12 +26,12 @@ class Sqlite extends AbstractPlatform protected $quoteIdentifierTo = '\''; /** - * @var \PDO + * @var Pdo\Pdo */ - protected $resource = null; + protected $driver = null; /** - * @param null|\Zend\Db\Adapter\Driver\Pdo\Pdo||\PDO $driver + * @param null|\Zend\Db\Adapter\Driver\Pdo\Pdo */ public function __construct($driver = null) { @@ -41,21 +41,19 @@ public function __construct($driver = null) } /** - * @param \Zend\Db\Adapter\Driver\Pdo\Pdo|\PDO $driver + * @param \Zend\Db\Adapter\Driver\Pdo\Pdo $driver * @return self Provides a fluent interface * @throws \Zend\Db\Adapter\Exception\InvalidArgumentException */ public function setDriver($driver) { - if (($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'sqlite') - || ($driver instanceof Pdo\Pdo && $driver->getDatabasePlatformName() == 'Sqlite') - ) { - $this->resource = $driver; + if ($driver instanceof Pdo\Pdo && $driver->getDatabasePlatformName() == 'Sqlite') { + $this->driver = $driver; return $this; } throw new Exception\InvalidArgumentException( - '$driver must be a Sqlite PDO Zend\Db\Adapter\Driver, Sqlite PDO instance' + '$driver must be a Sqlite PDO Zend\Db\Adapter\Driver' ); } @@ -72,7 +70,7 @@ public function getName() */ public function quoteValue($value) { - $resource = $this->resource; + $resource = $this->driver; if ($resource instanceof DriverInterface) { $resource = $resource->getConnection()->getResource(); @@ -90,7 +88,7 @@ public function quoteValue($value) */ public function quoteTrustedValue($value) { - $resource = $this->resource; + $resource = $this->driver; if ($resource instanceof DriverInterface) { $resource = $resource->getConnection()->getResource(); diff --git a/test/integration/Adapter/Platform/MysqlTest.php b/test/integration/Adapter/Platform/MysqlTest.php index bba0e8005f..08e900ec95 100644 --- a/test/integration/Adapter/Platform/MysqlTest.php +++ b/test/integration/Adapter/Platform/MysqlTest.php @@ -64,9 +64,6 @@ public function testQuoteValueWithPdoMysql() if (! $this->adapters['pdo_mysql'] instanceof \PDO) { $this->markTestSkipped('MySQL (PDO_Mysql) not configured in unit test configuration file'); } - $mysql = new Mysql($this->adapters['pdo_mysql']); - $value = $mysql->quoteValue('value'); - self::assertEquals('\'value\'', $value); $mysql = new Mysql(new Pdo\Pdo(new Pdo\Connection($this->adapters['pdo_mysql']))); $value = $mysql->quoteValue('value'); From aa556a713d920b3132d8750022112c7415976966 Mon Sep 17 00:00:00 2001 From: Roman Jadrovski Date: Fri, 19 Jul 2019 12:51:11 +0300 Subject: [PATCH 2/3] tests fixed --- test/integration/Adapter/Platform/PostgresqlTest.php | 3 --- test/integration/Adapter/Platform/SqlServerTest.php | 3 ++- test/integration/Adapter/Platform/SqliteTest.php | 3 --- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/test/integration/Adapter/Platform/PostgresqlTest.php b/test/integration/Adapter/Platform/PostgresqlTest.php index e690d561f6..c3d5100d0f 100644 --- a/test/integration/Adapter/Platform/PostgresqlTest.php +++ b/test/integration/Adapter/Platform/PostgresqlTest.php @@ -64,9 +64,6 @@ public function testQuoteValueWithPdoPgsql() if (! $this->adapters['pdo_pgsql'] instanceof \PDO) { $this->markTestSkipped('Postgres (PDO_PGSQL) not configured in unit test configuration file'); } - $pgsql = new Postgresql($this->adapters['pdo_pgsql']); - $value = $pgsql->quoteValue('value'); - self::assertEquals('\'value\'', $value); $pgsql = new Postgresql(new Pdo\Pdo(new Pdo\Connection($this->adapters['pdo_pgsql']))); $value = $pgsql->quoteValue('value'); diff --git a/test/integration/Adapter/Platform/SqlServerTest.php b/test/integration/Adapter/Platform/SqlServerTest.php index 1cd97214da..e5b1d75b3c 100644 --- a/test/integration/Adapter/Platform/SqlServerTest.php +++ b/test/integration/Adapter/Platform/SqlServerTest.php @@ -10,6 +10,7 @@ namespace ZendIntegrationTest\Db\Adapter\Platform; use PHPUnit\Framework\TestCase; +use Zend\Db\Adapter\Driver\Pdo; use Zend\Db\Adapter\Platform\SqlServer; /** @@ -54,7 +55,7 @@ public function testQuoteValueWithSqlServer() if (! $this->adapters['pdo_sqlsrv']) { $this->markTestSkipped('SQLServer (pdo_sqlsrv) not configured in unit test configuration file'); } - $sqlite = new SqlServer($this->adapters['pdo_sqlsrv']); + $sqlite = new SqlServer(new Pdo\Pdo($this->adapters['pdo_sqlsrv'])); $value = $sqlite->quoteValue('value'); self::assertEquals('\'value\'', $value); } diff --git a/test/integration/Adapter/Platform/SqliteTest.php b/test/integration/Adapter/Platform/SqliteTest.php index 92b059bc1c..415881215b 100644 --- a/test/integration/Adapter/Platform/SqliteTest.php +++ b/test/integration/Adapter/Platform/SqliteTest.php @@ -38,9 +38,6 @@ public function testQuoteValueWithPdoSqlite() if (! $this->adapters['pdo_sqlite'] instanceof \PDO) { $this->markTestSkipped('SQLite (PDO_SQLITE) not configured in unit test configuration file'); } - $sqlite = new Sqlite($this->adapters['pdo_sqlite']); - $value = $sqlite->quoteValue('value'); - self::assertEquals('\'value\'', $value); $sqlite = new Sqlite(new Pdo\Pdo(new Pdo\Connection($this->adapters['pdo_sqlite']))); $value = $sqlite->quoteValue('value'); From 850e1f7a71776e7845ee98adb659787a9bf12111 Mon Sep 17 00:00:00 2001 From: Roman Jadrovski Date: Fri, 19 Jul 2019 12:56:10 +0300 Subject: [PATCH 3/3] fix oracle test --- test/unit/Adapter/Platform/OracleTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/Adapter/Platform/OracleTest.php b/test/unit/Adapter/Platform/OracleTest.php index ed2d326f16..ae540949af 100644 --- a/test/unit/Adapter/Platform/OracleTest.php +++ b/test/unit/Adapter/Platform/OracleTest.php @@ -84,7 +84,7 @@ public function testSetDriverInvalid() { $this->expectException('Zend\Db\Adapter\Exception\InvalidArgumentException'); $this->expectExceptionMessage( - '$driver must be a Oci8 or Oracle PDO Zend\Db\Adapter\Driver, Oci8 instance, or Oci PDO instance' + '$driver must be a Oci8, Oracle PDO Zend\Db\Adapter\Driver or Oci8 instance' ); $this->platform->setDriver(null); }