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 Doctrine\DBAL\FetchMode; |
23
|
|
|
use Doctrine\DBAL\ParameterType; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Portability wrapper for a Statement. |
27
|
|
|
* |
28
|
|
|
* @link www.doctrine-project.org |
29
|
|
|
* @since 2.0 |
30
|
|
|
* @author Benjamin Eberlei <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class Statement implements \IteratorAggregate, \Doctrine\DBAL\Driver\Statement |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $portability; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \Doctrine\DBAL\Driver\Statement |
41
|
|
|
*/ |
42
|
|
|
private $stmt; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var int |
46
|
|
|
*/ |
47
|
|
|
private $case; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
private $defaultFetchMode = FetchMode::MIXED; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Wraps <tt>Statement</tt> and applies portability measures. |
56
|
|
|
* |
57
|
|
|
* @param \Doctrine\DBAL\Driver\Statement $stmt |
58
|
|
|
* @param \Doctrine\DBAL\Portability\Connection $conn |
59
|
|
|
*/ |
60
|
15 |
|
public function __construct($stmt, Connection $conn) |
61
|
|
|
{ |
62
|
15 |
|
$this->stmt = $stmt; |
63
|
15 |
|
$this->portability = $conn->getPortability(); |
64
|
15 |
|
$this->case = $conn->getFetchCase(); |
65
|
15 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
1 |
|
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) |
71
|
|
|
{ |
72
|
1 |
|
return $this->stmt->bindParam($column, $variable, $type, $length); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public function bindValue($param, $value, $type = ParameterType::STRING) |
79
|
|
|
{ |
80
|
1 |
|
return $this->stmt->bindValue($param, $value, $type); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
1 |
|
public function closeCursor() |
87
|
|
|
{ |
88
|
1 |
|
return $this->stmt->closeCursor(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
1 |
|
public function columnCount() |
95
|
|
|
{ |
96
|
1 |
|
return $this->stmt->columnCount(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
1 |
|
public function errorCode() |
103
|
|
|
{ |
104
|
1 |
|
return $this->stmt->errorCode(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
1 |
|
public function errorInfo() |
111
|
|
|
{ |
112
|
1 |
|
return $this->stmt->errorInfo(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
3 |
|
public function execute($params = null) |
119
|
|
|
{ |
120
|
3 |
|
return $this->stmt->execute($params); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
6 |
|
public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null) |
127
|
|
|
{ |
128
|
6 |
|
$this->defaultFetchMode = $fetchMode; |
129
|
|
|
|
130
|
6 |
|
return $this->stmt->setFetchMode($fetchMode, $arg1, $arg2); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
3 |
|
public function getIterator() |
137
|
|
|
{ |
138
|
3 |
|
$data = $this->fetchAll(); |
139
|
|
|
|
140
|
3 |
|
return new \ArrayIterator($data); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
2 |
|
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
147
|
|
|
{ |
148
|
2 |
|
$fetchMode = $fetchMode ?: $this->defaultFetchMode; |
149
|
|
|
|
150
|
2 |
|
$row = $this->stmt->fetch($fetchMode); |
151
|
|
|
|
152
|
2 |
|
$iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM); |
153
|
2 |
|
$fixCase = ! is_null($this->case) |
154
|
2 |
|
&& ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED) |
155
|
2 |
|
&& ($this->portability & Connection::PORTABILITY_FIX_CASE); |
156
|
|
|
|
157
|
2 |
|
$row = $this->fixRow($row, $iterateRow, $fixCase); |
158
|
|
|
|
159
|
2 |
|
return $row; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
6 |
|
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
166
|
|
|
{ |
167
|
6 |
|
$fetchMode = $fetchMode ?: $this->defaultFetchMode; |
168
|
|
|
|
169
|
6 |
|
if ($fetchArgument) { |
170
|
|
|
$rows = $this->stmt->fetchAll($fetchMode, $fetchArgument); |
171
|
|
|
} else { |
172
|
6 |
|
$rows = $this->stmt->fetchAll($fetchMode); |
173
|
|
|
} |
174
|
|
|
|
175
|
6 |
|
$iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM); |
176
|
6 |
|
$fixCase = ! is_null($this->case) |
177
|
6 |
|
&& ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED) |
178
|
6 |
|
&& ($this->portability & Connection::PORTABILITY_FIX_CASE); |
179
|
|
|
|
180
|
6 |
|
if ( ! $iterateRow && !$fixCase) { |
181
|
1 |
|
return $rows; |
182
|
|
|
} |
183
|
|
|
|
184
|
5 |
|
if ($fetchMode === FetchMode::COLUMN) { |
185
|
3 |
|
foreach ($rows as $num => $row) { |
186
|
3 |
|
$rows[$num] = [$row]; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
5 |
|
foreach ($rows as $num => $row) { |
191
|
5 |
|
$rows[$num] = $this->fixRow($row, $iterateRow, $fixCase); |
192
|
|
|
} |
193
|
|
|
|
194
|
5 |
|
if ($fetchMode === FetchMode::COLUMN) { |
195
|
3 |
|
foreach ($rows as $num => $row) { |
196
|
3 |
|
$rows[$num] = $row[0]; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
5 |
|
return $rows; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param mixed $row |
205
|
|
|
* @param int $iterateRow |
206
|
|
|
* @param bool $fixCase |
207
|
|
|
* |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
5 |
|
protected function fixRow($row, $iterateRow, $fixCase) |
211
|
|
|
{ |
212
|
5 |
|
if ( ! $row) { |
213
|
2 |
|
return $row; |
214
|
|
|
} |
215
|
|
|
|
216
|
5 |
|
if ($fixCase) { |
217
|
|
|
$row = array_change_key_case($row, $this->case); |
218
|
|
|
} |
219
|
|
|
|
220
|
5 |
|
if ($iterateRow) { |
221
|
5 |
|
foreach ($row as $k => $v) { |
222
|
5 |
|
if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') { |
223
|
3 |
|
$row[$k] = null; |
224
|
5 |
|
} elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) { |
225
|
5 |
|
$row[$k] = rtrim($v); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
5 |
|
return $row; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* {@inheritdoc} |
235
|
|
|
*/ |
236
|
|
|
public function fetchColumn($columnIndex = 0) |
237
|
|
|
{ |
238
|
|
|
$value = $this->stmt->fetchColumn($columnIndex); |
239
|
|
|
|
240
|
|
|
if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) { |
241
|
|
|
if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') { |
242
|
|
|
$value = null; |
243
|
|
|
} elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) { |
244
|
|
|
$value = rtrim($value); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $value; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* {@inheritdoc} |
253
|
|
|
*/ |
254
|
1 |
|
public function rowCount() |
255
|
|
|
{ |
256
|
1 |
|
return $this->stmt->rowCount(); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|