1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Portability; |
21
|
|
|
|
22
|
|
|
use PDO; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Portability wrapper for a Statement. |
26
|
|
|
* |
27
|
|
|
* @link www.doctrine-project.org |
28
|
|
|
* @since 2.0 |
29
|
|
|
* @author Benjamin Eberlei <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class Statement implements \IteratorAggregate, \Doctrine\DBAL\Driver\Statement |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var integer |
35
|
|
|
*/ |
36
|
|
|
private $portability; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \Doctrine\DBAL\Driver\Statement |
40
|
|
|
*/ |
41
|
|
|
private $stmt; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var integer |
45
|
|
|
*/ |
46
|
|
|
private $case; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var integer |
50
|
|
|
*/ |
51
|
|
|
private $defaultFetchMode = PDO::FETCH_BOTH; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Wraps <tt>Statement</tt> and applies portability measures. |
55
|
|
|
* |
56
|
|
|
* @param \Doctrine\DBAL\Driver\Statement $stmt |
57
|
|
|
* @param \Doctrine\DBAL\Portability\Connection $conn |
58
|
|
|
*/ |
59
|
15 |
|
public function __construct($stmt, Connection $conn) |
60
|
|
|
{ |
61
|
15 |
|
$this->stmt = $stmt; |
62
|
15 |
|
$this->portability = $conn->getPortability(); |
63
|
15 |
|
$this->case = $conn->getFetchCase(); |
64
|
15 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
1 |
|
public function bindParam($column, &$variable, $type = null, $length = null) |
70
|
|
|
{ |
71
|
1 |
|
return $this->stmt->bindParam($column, $variable, $type, $length); |
72
|
|
|
} |
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
|
77
|
1 |
|
public function bindValue($param, $value, $type = null) |
78
|
|
|
{ |
79
|
1 |
|
return $this->stmt->bindValue($param, $value, $type); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
1 |
|
public function closeCursor() |
86
|
|
|
{ |
87
|
1 |
|
return $this->stmt->closeCursor(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
1 |
|
public function columnCount() |
94
|
|
|
{ |
95
|
1 |
|
return $this->stmt->columnCount(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
1 |
|
public function errorCode() |
102
|
|
|
{ |
103
|
1 |
|
return $this->stmt->errorCode(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
1 |
|
public function errorInfo() |
110
|
|
|
{ |
111
|
1 |
|
return $this->stmt->errorInfo(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
3 |
|
public function execute($params = null) |
118
|
|
|
{ |
119
|
3 |
|
return $this->stmt->execute($params); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
6 |
|
public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null) |
126
|
|
|
{ |
127
|
6 |
|
$this->defaultFetchMode = $fetchMode; |
128
|
|
|
|
129
|
6 |
|
return $this->stmt->setFetchMode($fetchMode, $arg1, $arg2); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
3 |
|
public function getIterator() |
136
|
|
|
{ |
137
|
3 |
|
$data = $this->fetchAll(); |
138
|
|
|
|
139
|
3 |
|
return new \ArrayIterator($data); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
2 |
|
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
146
|
|
|
{ |
147
|
2 |
|
$fetchMode = $fetchMode ?: $this->defaultFetchMode; |
148
|
|
|
$row = $this->stmt->fetch($fetchMode); |
149
|
2 |
|
|
150
|
|
|
return $this->fixRow( |
151
|
2 |
|
$row, |
152
|
2 |
|
$this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL | Connection::PORTABILITY_RTRIM), |
153
|
2 |
|
$this->shouldFixCase($fetchMode) |
154
|
|
|
); |
155
|
|
|
} |
156
|
2 |
|
|
157
|
|
|
private function shouldFixCase(int $fetchMode) : bool |
158
|
|
|
{ |
159
|
|
|
return ! is_null($this->case) && ($fetchMode === PDO::FETCH_ASSOC || $fetchMode === PDO::FETCH_BOTH) |
160
|
|
|
&& ($this->portability & Connection::PORTABILITY_FIX_CASE); |
161
|
|
|
} |
162
|
6 |
|
|
163
|
|
|
/** |
164
|
6 |
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
6 |
|
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
167
|
|
|
{ |
168
|
|
|
$fetchMode = $fetchMode ?: $this->defaultFetchMode; |
169
|
6 |
|
|
170
|
|
|
if ($fetchArgument) { |
|
|
|
|
171
|
|
|
$rows = $this->stmt->fetchAll($fetchMode, $fetchArgument); |
172
|
6 |
|
} else { |
173
|
6 |
|
$rows = $this->stmt->fetchAll($fetchMode); |
174
|
6 |
|
} |
175
|
1 |
|
|
176
|
|
|
$iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL | Connection::PORTABILITY_RTRIM); |
177
|
|
|
$fixCase = $this->shouldFixCase($fetchMode); |
178
|
5 |
|
|
179
|
3 |
|
if ( ! $iterateRow && ! $fixCase) { |
180
|
3 |
|
return $rows; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
View Code Duplication |
if ($fetchMode === PDO::FETCH_COLUMN) { |
|
|
|
|
184
|
5 |
|
foreach ($rows as $num => $row) { |
185
|
5 |
|
$rows[$num] = [$row]; |
186
|
|
|
} |
187
|
|
|
} |
188
|
5 |
|
|
189
|
3 |
|
foreach ($rows as $num => $row) { |
190
|
3 |
|
$rows[$num] = $this->fixRow($row, $iterateRow, $fixCase); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
View Code Duplication |
if ($fetchMode === PDO::FETCH_COLUMN) { |
|
|
|
|
194
|
5 |
|
foreach ($rows as $num => $row) { |
195
|
|
|
$rows[$num] = $row[0]; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $rows; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param mixed $row |
204
|
5 |
|
* @param integer $iterateRow |
205
|
|
|
* @param boolean $fixCase |
206
|
5 |
|
* |
207
|
2 |
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
protected function fixRow($row, $iterateRow, $fixCase) |
210
|
5 |
|
{ |
211
|
|
|
if ( ! $row) { |
212
|
|
|
return $row; |
213
|
|
|
} |
214
|
5 |
|
|
215
|
5 |
|
if ($fixCase) { |
216
|
5 |
|
$row = array_change_key_case($row, $this->case); |
217
|
3 |
|
} |
218
|
5 |
|
|
219
|
5 |
|
if ($iterateRow) { |
220
|
|
|
foreach ($row as $k => $v) { |
221
|
|
View Code Duplication |
if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') { |
|
|
|
|
222
|
|
|
$row[$k] = null; |
223
|
|
|
} elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) { |
224
|
5 |
|
$row[$k] = rtrim($v); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $row; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritdoc} |
234
|
|
|
*/ |
235
|
|
|
public function fetchColumn($columnIndex = 0) |
236
|
|
|
{ |
237
|
|
|
$value = $this->stmt->fetchColumn($columnIndex); |
238
|
|
|
|
239
|
|
|
if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) { |
240
|
|
View Code Duplication |
if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') { |
|
|
|
|
241
|
|
|
$value = null; |
242
|
|
|
} elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) { |
243
|
|
|
$value = rtrim($value); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $value; |
248
|
1 |
|
} |
249
|
|
|
|
250
|
1 |
|
/** |
251
|
|
|
* {@inheritdoc} |
252
|
|
|
*/ |
253
|
|
|
public function rowCount() |
254
|
|
|
{ |
255
|
|
|
return $this->stmt->rowCount(); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: