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