Skip to content

Commit

Permalink
Merge pull request #5883 from derrabus/sa/pdo
Browse files Browse the repository at this point in the history
Make sure only PDO parameter types are passed to PDO methods
  • Loading branch information
derrabus committed Jan 30, 2023
2 parents ee116f2 + 07ad46e commit 8ec4dd6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/Driver/PDO/Connection.php
Expand Up @@ -80,7 +80,7 @@ public function query(string $sql): ResultInterface
*/
public function quote($value, $type = ParameterType::STRING)
{
return $this->connection->quote($value, $type);
return $this->connection->quote($value, ParameterTypeMap::convertParamType($type));
}

/**
Expand Down
47 changes: 47 additions & 0 deletions src/Driver/PDO/ParameterTypeMap.php
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Driver\PDO;

use Doctrine\DBAL\Driver\Exception\UnknownParameterType;
use Doctrine\DBAL\ParameterType;
use PDO;

/** @internal */
final class ParameterTypeMap
{
private const PARAM_TYPE_MAP = [
ParameterType::NULL => PDO::PARAM_NULL,
ParameterType::INTEGER => PDO::PARAM_INT,
ParameterType::STRING => PDO::PARAM_STR,
ParameterType::ASCII => PDO::PARAM_STR,
ParameterType::BINARY => PDO::PARAM_LOB,
ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
ParameterType::BOOLEAN => PDO::PARAM_BOOL,
];

/**
* Converts DBAL parameter type to PDO parameter type
*
* @psalm-return PDO::PARAM_*
*
* @throws UnknownParameterType
*/
public static function convertParamType(int $type): int
{
if (! isset(self::PARAM_TYPE_MAP[$type])) {
throw UnknownParameterType::new($type);
}

return self::PARAM_TYPE_MAP[$type];
}

private function __construct()
{
}

private function __clone()
{
}
}
6 changes: 5 additions & 1 deletion src/Driver/PDO/Result.php
Expand Up @@ -91,7 +91,9 @@ public function free(): void
}

/**
* @return mixed|false
* @psalm-param PDO::FETCH_* $mode
*
* @return mixed
*
* @throws Exception
*/
Expand All @@ -105,6 +107,8 @@ private function fetch(int $mode)
}

/**
* @psalm-param PDO::FETCH_* $mode
*
* @return list<mixed>
*
* @throws Exception
Expand Down
37 changes: 4 additions & 33 deletions src/Driver/PDO/Statement.php
Expand Up @@ -2,13 +2,10 @@

namespace Doctrine\DBAL\Driver\PDO;

use Doctrine\DBAL\Driver\Exception as ExceptionInterface;
use Doctrine\DBAL\Driver\Exception\UnknownParameterType;
use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\Statement as StatementInterface;
use Doctrine\DBAL\ParameterType;
use Doctrine\Deprecations\Deprecation;
use PDO;
use PDOException;
use PDOStatement;

Expand All @@ -18,16 +15,6 @@

final class Statement implements StatementInterface
{
private const PARAM_TYPE_MAP = [
ParameterType::NULL => PDO::PARAM_NULL,
ParameterType::INTEGER => PDO::PARAM_INT,
ParameterType::STRING => PDO::PARAM_STR,
ParameterType::ASCII => PDO::PARAM_STR,
ParameterType::BINARY => PDO::PARAM_LOB,
ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
ParameterType::BOOLEAN => PDO::PARAM_BOOL,
];

private PDOStatement $stmt;

/** @internal The statement can be only instantiated by its driver connection. */
Expand All @@ -50,10 +37,10 @@ public function bindValue($param, $value, $type = ParameterType::STRING)
);
}

$type = $this->convertParamType($type);
$pdoType = ParameterTypeMap::convertParamType($type);

try {
return $this->stmt->bindValue($param, $value, $type);
return $this->stmt->bindValue($param, $value, $pdoType);
} catch (PDOException $exception) {
throw Exception::new($exception);
}
Expand Down Expand Up @@ -101,13 +88,13 @@ public function bindParam(
);
}

$type = $this->convertParamType($type);
$pdoType = ParameterTypeMap::convertParamType($type);

try {
return $this->stmt->bindParam(
$param,
$variable,
$type,
$pdoType,
$length ?? 0,
...array_slice(func_get_args(), 4),
);
Expand Down Expand Up @@ -138,20 +125,4 @@ public function execute($params = null): ResultInterface

return new Result($this->stmt);
}

/**
* Converts DBAL parameter type to PDO parameter type
*
* @param int $type Parameter type
*
* @throws ExceptionInterface
*/
private function convertParamType(int $type): int
{
if (! isset(self::PARAM_TYPE_MAP[$type])) {
throw UnknownParameterType::new($type);
}

return self::PARAM_TYPE_MAP[$type];
}
}

0 comments on commit 8ec4dd6

Please sign in to comment.