Skip to content

Commit 8ec4dd6

Browse files
authored
Merge pull request #5883 from derrabus/sa/pdo
Make sure only PDO parameter types are passed to PDO methods
2 parents ee116f2 + 07ad46e commit 8ec4dd6

File tree

4 files changed

+57
-35
lines changed

4 files changed

+57
-35
lines changed

src/Driver/PDO/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function query(string $sql): ResultInterface
8080
*/
8181
public function quote($value, $type = ParameterType::STRING)
8282
{
83-
return $this->connection->quote($value, $type);
83+
return $this->connection->quote($value, ParameterTypeMap::convertParamType($type));
8484
}
8585

8686
/**

src/Driver/PDO/ParameterTypeMap.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Driver\PDO;
6+
7+
use Doctrine\DBAL\Driver\Exception\UnknownParameterType;
8+
use Doctrine\DBAL\ParameterType;
9+
use PDO;
10+
11+
/** @internal */
12+
final class ParameterTypeMap
13+
{
14+
private const PARAM_TYPE_MAP = [
15+
ParameterType::NULL => PDO::PARAM_NULL,
16+
ParameterType::INTEGER => PDO::PARAM_INT,
17+
ParameterType::STRING => PDO::PARAM_STR,
18+
ParameterType::ASCII => PDO::PARAM_STR,
19+
ParameterType::BINARY => PDO::PARAM_LOB,
20+
ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
21+
ParameterType::BOOLEAN => PDO::PARAM_BOOL,
22+
];
23+
24+
/**
25+
* Converts DBAL parameter type to PDO parameter type
26+
*
27+
* @psalm-return PDO::PARAM_*
28+
*
29+
* @throws UnknownParameterType
30+
*/
31+
public static function convertParamType(int $type): int
32+
{
33+
if (! isset(self::PARAM_TYPE_MAP[$type])) {
34+
throw UnknownParameterType::new($type);
35+
}
36+
37+
return self::PARAM_TYPE_MAP[$type];
38+
}
39+
40+
private function __construct()
41+
{
42+
}
43+
44+
private function __clone()
45+
{
46+
}
47+
}

src/Driver/PDO/Result.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ public function free(): void
9191
}
9292

9393
/**
94-
* @return mixed|false
94+
* @psalm-param PDO::FETCH_* $mode
95+
*
96+
* @return mixed
9597
*
9698
* @throws Exception
9799
*/
@@ -105,6 +107,8 @@ private function fetch(int $mode)
105107
}
106108

107109
/**
110+
* @psalm-param PDO::FETCH_* $mode
111+
*
108112
* @return list<mixed>
109113
*
110114
* @throws Exception

src/Driver/PDO/Statement.php

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
namespace Doctrine\DBAL\Driver\PDO;
44

5-
use Doctrine\DBAL\Driver\Exception as ExceptionInterface;
6-
use Doctrine\DBAL\Driver\Exception\UnknownParameterType;
75
use Doctrine\DBAL\Driver\Result as ResultInterface;
86
use Doctrine\DBAL\Driver\Statement as StatementInterface;
97
use Doctrine\DBAL\ParameterType;
108
use Doctrine\Deprecations\Deprecation;
11-
use PDO;
129
use PDOException;
1310
use PDOStatement;
1411

@@ -18,16 +15,6 @@
1815

1916
final class Statement implements StatementInterface
2017
{
21-
private const PARAM_TYPE_MAP = [
22-
ParameterType::NULL => PDO::PARAM_NULL,
23-
ParameterType::INTEGER => PDO::PARAM_INT,
24-
ParameterType::STRING => PDO::PARAM_STR,
25-
ParameterType::ASCII => PDO::PARAM_STR,
26-
ParameterType::BINARY => PDO::PARAM_LOB,
27-
ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
28-
ParameterType::BOOLEAN => PDO::PARAM_BOOL,
29-
];
30-
3118
private PDOStatement $stmt;
3219

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

53-
$type = $this->convertParamType($type);
40+
$pdoType = ParameterTypeMap::convertParamType($type);
5441

5542
try {
56-
return $this->stmt->bindValue($param, $value, $type);
43+
return $this->stmt->bindValue($param, $value, $pdoType);
5744
} catch (PDOException $exception) {
5845
throw Exception::new($exception);
5946
}
@@ -101,13 +88,13 @@ public function bindParam(
10188
);
10289
}
10390

104-
$type = $this->convertParamType($type);
91+
$pdoType = ParameterTypeMap::convertParamType($type);
10592

10693
try {
10794
return $this->stmt->bindParam(
10895
$param,
10996
$variable,
110-
$type,
97+
$pdoType,
11198
$length ?? 0,
11299
...array_slice(func_get_args(), 4),
113100
);
@@ -138,20 +125,4 @@ public function execute($params = null): ResultInterface
138125

139126
return new Result($this->stmt);
140127
}
141-
142-
/**
143-
* Converts DBAL parameter type to PDO parameter type
144-
*
145-
* @param int $type Parameter type
146-
*
147-
* @throws ExceptionInterface
148-
*/
149-
private function convertParamType(int $type): int
150-
{
151-
if (! isset(self::PARAM_TYPE_MAP[$type])) {
152-
throw UnknownParameterType::new($type);
153-
}
154-
155-
return self::PARAM_TYPE_MAP[$type];
156-
}
157128
}

0 commit comments

Comments
 (0)