19
19
20
20
namespace Doctrine \DBAL \Driver ;
21
21
22
+ use Doctrine \DBAL \FetchMode ;
22
23
use Doctrine \DBAL \ParameterType ;
24
+ use PDO ;
23
25
24
26
/**
25
27
* The PDO implementation of the Statement interface.
29
31
*/
30
32
class PDOStatement extends \PDOStatement implements Statement
31
33
{
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
+
32
57
/**
33
58
* Protected constructor.
34
59
*/
@@ -41,6 +66,8 @@ protected function __construct()
41
66
*/
42
67
public function setFetchMode ($ fetchMode , $ arg2 = null , $ arg3 = null )
43
68
{
69
+ $ fetchMode = $ this ->convertFetchMode ($ fetchMode );
70
+
44
71
// This thin wrapper is necessary to shield against the weird signature
45
72
// of PDOStatement::setFetchMode(): even if the second and third
46
73
// parameters are optional, PHP will not let us remove it from this
@@ -65,6 +92,8 @@ public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
65
92
*/
66
93
public function bindValue ($ param , $ value , $ type = ParameterType::STRING )
67
94
{
95
+ $ type = $ this ->convertParamType ($ type );
96
+
68
97
try {
69
98
return parent ::bindValue ($ param , $ value , $ type );
70
99
} catch (\PDOException $ exception ) {
@@ -77,6 +106,8 @@ public function bindValue($param, $value, $type = ParameterType::STRING)
77
106
*/
78
107
public function bindParam ($ column , &$ variable , $ type = ParameterType::STRING , $ length = null , $ driverOptions = null )
79
108
{
109
+ $ type = $ this ->convertParamType ($ type );
110
+
80
111
try {
81
112
return parent ::bindParam ($ column , $ variable , $ type , $ length , $ driverOptions );
82
113
} catch (\PDOException $ exception ) {
@@ -115,6 +146,8 @@ public function execute($params = null)
115
146
*/
116
147
public function fetch ($ fetchMode = null , $ cursorOrientation = \PDO ::FETCH_ORI_NEXT , $ cursorOffset = 0 )
117
148
{
149
+ $ fetchMode = $ this ->convertFetchMode ($ fetchMode );
150
+
118
151
try {
119
152
if ($ fetchMode === null && \PDO ::FETCH_ORI_NEXT === $ cursorOrientation && 0 === $ cursorOffset ) {
120
153
return parent ::fetch ();
@@ -139,6 +172,8 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE
139
172
*/
140
173
public function fetchAll ($ fetchMode = null , $ fetchArgument = null , $ ctorArgs = null )
141
174
{
175
+ $ fetchMode = $ this ->convertFetchMode ($ fetchMode );
176
+
142
177
try {
143
178
if ($ fetchMode === null && null === $ fetchArgument && null === $ ctorArgs ) {
144
179
return parent ::fetchAll ();
@@ -169,4 +204,38 @@ public function fetchColumn($columnIndex = 0)
169
204
throw new PDOException ($ exception );
170
205
}
171
206
}
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
+ }
172
241
}
0 commit comments