Skip to content

Commit 4c80584

Browse files
committed
Added conversion of DBAL constants to PDO ones for PDOStatement
1 parent b24fe97 commit 4c80584

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

lib/Doctrine/DBAL/Driver/PDOStatement.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
namespace Doctrine\DBAL\Driver;
2121

22+
use Doctrine\DBAL\FetchMode;
2223
use Doctrine\DBAL\ParameterType;
24+
use PDO;
2325

2426
/**
2527
* The PDO implementation of the Statement interface.
@@ -29,6 +31,29 @@
2931
*/
3032
class PDOStatement extends \PDOStatement implements Statement
3133
{
34+
/**
35+
* @var int[]
36+
*/
37+
private const PARAM_TYPE_MAP = [
38+
ParameterType::NULL => PDO::PARAM_NULL,
39+
ParameterType::INTEGER => PDO::PARAM_INT,
40+
ParameterType::STRING => PDO::PARAM_STR,
41+
ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
42+
ParameterType::BOOLEAN => PDO::PARAM_BOOL,
43+
];
44+
45+
/**
46+
* @var int[]
47+
*/
48+
private const FETCH_MODE_MAP = [
49+
FetchMode::ASSOCIATIVE => PDO::FETCH_ASSOC,
50+
FetchMode::NUMERIC => PDO::FETCH_NUM,
51+
FetchMode::MIXED => PDO::FETCH_BOTH,
52+
FetchMode::STANDARD_OBJECT => PDO::FETCH_OBJ,
53+
FetchMode::COLUMN => PDO::FETCH_COLUMN,
54+
FetchMode::CUSTOM_OBJECT => PDO::FETCH_CLASS,
55+
];
56+
3257
/**
3358
* Protected constructor.
3459
*/
@@ -41,6 +66,8 @@ protected function __construct()
4166
*/
4267
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
4368
{
69+
$fetchMode = $this->convertFetchMode($fetchMode);
70+
4471
// This thin wrapper is necessary to shield against the weird signature
4572
// of PDOStatement::setFetchMode(): even if the second and third
4673
// parameters are optional, PHP will not let us remove it from this
@@ -65,6 +92,8 @@ public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
6592
*/
6693
public function bindValue($param, $value, $type = ParameterType::STRING)
6794
{
95+
$type = $this->convertParamType($type);
96+
6897
try {
6998
return parent::bindValue($param, $value, $type);
7099
} catch (\PDOException $exception) {
@@ -77,6 +106,8 @@ public function bindValue($param, $value, $type = ParameterType::STRING)
77106
*/
78107
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null)
79108
{
109+
$type = $this->convertParamType($type);
110+
80111
try {
81112
return parent::bindParam($column, $variable, $type, $length, $driverOptions);
82113
} catch (\PDOException $exception) {
@@ -115,6 +146,8 @@ public function execute($params = null)
115146
*/
116147
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
117148
{
149+
$fetchMode = $this->convertFetchMode($fetchMode);
150+
118151
try {
119152
if ($fetchMode === null && \PDO::FETCH_ORI_NEXT === $cursorOrientation && 0 === $cursorOffset) {
120153
return parent::fetch();
@@ -139,6 +172,8 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE
139172
*/
140173
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
141174
{
175+
$fetchMode = $this->convertFetchMode($fetchMode);
176+
142177
try {
143178
if ($fetchMode === null && null === $fetchArgument && null === $ctorArgs) {
144179
return parent::fetchAll();
@@ -169,4 +204,38 @@ public function fetchColumn($columnIndex = 0)
169204
throw new PDOException($exception);
170205
}
171206
}
207+
208+
/**
209+
* Converts DBAL parameter type to PDO parameter type
210+
*
211+
* @param int $type Parameter type
212+
* @return int
213+
*/
214+
private function convertParamType(int $type) : int
215+
{
216+
if ( ! isset(self::PARAM_TYPE_MAP[$type])) {
217+
throw new \InvalidArgumentException('Invalid parameter type: ' . $type);
218+
}
219+
220+
return self::PARAM_TYPE_MAP[$type];
221+
}
222+
223+
/**
224+
* Converts DBAL fetch mode to PDO fetch mode
225+
*
226+
* @param int|null $fetchMode Fetch mode
227+
* @return int|null
228+
*/
229+
private function convertFetchMode(?int $fetchMode) : ?int
230+
{
231+
if ($fetchMode === null) {
232+
return null;
233+
}
234+
235+
if ( ! isset(self::FETCH_MODE_MAP[$fetchMode])) {
236+
throw new \InvalidArgumentException('Invalid fetch mode: ' . $fetchMode);
237+
}
238+
239+
return self::FETCH_MODE_MAP[$fetchMode];
240+
}
172241
}

0 commit comments

Comments
 (0)