Skip to content

Commit

Permalink
Mysql and Pgsql adapters \PDO reference holding fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jadrovski committed Jan 14, 2020
1 parent b4975fc commit a86d46e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 40 deletions.
48 changes: 28 additions & 20 deletions src/Adapter/Platform/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class Mysql extends AbstractPlatform
protected $quoteIdentifierTo = '``';

/**
* @var \mysqli|\PDO
* @var \mysqli|\PDO|Pdo\Pdo|Mysqli\Mysqli
*/
protected $resource = null;
protected $driver = null;

/**
* NOTE: Include dashes for MySQL only, need tests for others platforms
Expand Down Expand Up @@ -60,7 +60,7 @@ public function setDriver($driver)
|| ($driver instanceof \mysqli)
|| ($driver instanceof \PDO && $driver->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'mysql')
) {
$this->resource = $driver;
$this->driver = $driver;
return $this;
}

Expand Down Expand Up @@ -90,32 +90,40 @@ 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);
$quotedViaDriverValue = $this->quoteViaDriver($value);

return $quotedViaDriverValue !== null ? $quotedViaDriverValue : parent::quoteValue($value);
}

/**
* {@inheritDoc}
*/
public function quoteTrustedValue($value)
{
if ($this->resource instanceof DriverInterface) {
$this->resource = $this->resource->getConnection()->getResource();
$quotedViaDriverValue = $this->quoteViaDriver($value);

return $quotedViaDriverValue !== null ? $quotedViaDriverValue : parent::quoteTrustedValue($value);
}

/**
* @param string $value
* @return string|null
*/
protected function quoteViaDriver($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;
}
}
48 changes: 28 additions & 20 deletions src/Adapter/Platform/Postgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Postgresql extends AbstractPlatform
protected $quoteIdentifierTo = '""';

/**
* @var resource|\PDO
* @var resource|\PDO|Pdo\Pdo|Pgsql\Pgsql
*/
protected $resource = null;
protected $driver = null;

/**
* @param null|\Laminas\Db\Adapter\Driver\Pgsql\Pgsql|\Laminas\Db\Adapter\Driver\Pdo\Pdo|resource|\PDO $driver
Expand All @@ -49,7 +49,7 @@ public function setDriver($driver)
|| (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;
}

Expand Down Expand Up @@ -80,32 +80,40 @@ 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);
$quotedViaDriverValue = $this->quoteViaDriver($value);

return $quotedViaDriverValue !== null ? $quotedViaDriverValue : ('E' . parent::quoteValue($value));
}

/**
* {@inheritDoc}
*/
public function quoteTrustedValue($value)
{
if ($this->resource instanceof DriverInterface) {
$this->resource = $this->resource->getConnection()->getResource();
$quotedViaDriverValue = $this->quoteViaDriver($value);

return $quotedViaDriverValue !== null ? $quotedViaDriverValue : ('E' . parent::quoteTrustedValue($value));
}

/**
* @param string $value
* @return string|null
*/
protected function quoteViaDriver($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;
}
}

0 comments on commit a86d46e

Please sign in to comment.