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

fix some of AbstractPlatform inheritors that preventing \PDO disconnect #388

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
56 changes: 32 additions & 24 deletions src/Adapter/Platform/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand All @@ -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
*/
Expand All @@ -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'
);
}

Expand All @@ -91,32 +90,41 @@ 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);
}

/**
* {@inheritDoc}
*/
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;
}
}
26 changes: 13 additions & 13 deletions src/Adapter/Platform/Oracle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Oracle extends AbstractPlatform
/**
* @var null|Pdo|Oci8
*/
protected $resource = null;
protected $driver = null;

/**
* @param array $options
Expand Down Expand Up @@ -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'
);
}

Expand All @@ -67,7 +65,7 @@ public function setDriver($driver)
*/
public function getDriver()
{
return $this->resource;
return $this->driver;
}

/**
Expand Down Expand Up @@ -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") . "'";
}
Expand Down
57 changes: 32 additions & 25 deletions src/Adapter/Platform/Postgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
*/
Expand All @@ -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'
);
}

Expand All @@ -81,32 +79,41 @@ 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));
}

/**
* {@inheritDoc}
*/
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;
}
}
38 changes: 21 additions & 17 deletions src/Adapter/Platform/SqlServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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'
);
}

Expand Down Expand Up @@ -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 '
Expand All @@ -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) . '\'';
}
Expand Down
Loading