|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Portability; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Driver\ResultStatement; |
|
8
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
|
9
|
|
|
use Doctrine\DBAL\Driver\StatementIterator; |
|
10
|
|
|
use Doctrine\DBAL\FetchMode; |
|
11
|
|
|
use Doctrine\DBAL\ParameterType; |
|
12
|
|
|
use IteratorAggregate; |
|
13
|
|
|
use function array_change_key_case; |
|
14
|
|
|
use function assert; |
|
15
|
|
|
use function is_string; |
|
16
|
|
|
use function rtrim; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Portability wrapper for a Statement. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Statement implements IteratorAggregate, DriverStatement |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var int */ |
|
24
|
|
|
private $portability; |
|
25
|
|
|
|
|
26
|
|
|
/** @var DriverStatement|ResultStatement */ |
|
27
|
|
|
private $stmt; |
|
28
|
|
|
|
|
29
|
|
|
/** @var int */ |
|
30
|
|
|
private $case; |
|
31
|
|
|
|
|
32
|
|
|
/** @var int */ |
|
33
|
|
|
private $defaultFetchMode = FetchMode::MIXED; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Wraps <tt>Statement</tt> and applies portability measures. |
|
37
|
|
|
* |
|
38
|
343 |
|
* @param DriverStatement|ResultStatement $stmt |
|
39
|
|
|
*/ |
|
40
|
343 |
|
public function __construct($stmt, Connection $conn) |
|
41
|
343 |
|
{ |
|
42
|
343 |
|
$this->stmt = $stmt; |
|
43
|
343 |
|
$this->portability = $conn->getPortability(); |
|
44
|
|
|
$this->case = $conn->getFetchCase(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
217 |
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
217 |
|
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void |
|
51
|
|
|
{ |
|
52
|
217 |
|
assert($this->stmt instanceof DriverStatement); |
|
53
|
217 |
|
|
|
54
|
|
|
$this->stmt->bindParam($column, $variable, $type, $length); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
193 |
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
193 |
|
public function bindValue($param, $value, $type = ParameterType::STRING) : void |
|
61
|
|
|
{ |
|
62
|
193 |
|
assert($this->stmt instanceof DriverStatement); |
|
63
|
193 |
|
|
|
64
|
|
|
$this->stmt->bindValue($param, $value, $type); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
169 |
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
169 |
|
public function closeCursor() : void |
|
71
|
169 |
|
{ |
|
72
|
|
|
$this->stmt->closeCursor(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
145 |
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
145 |
|
public function columnCount() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->stmt->columnCount(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
145 |
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
145 |
|
public function execute($params = null) : void |
|
87
|
|
|
{ |
|
88
|
145 |
|
assert($this->stmt instanceof DriverStatement); |
|
89
|
|
|
|
|
90
|
|
|
$this->stmt->execute($params); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
121 |
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
121 |
|
public function setFetchMode($fetchMode, ...$args) : void |
|
97
|
|
|
{ |
|
98
|
121 |
|
$this->defaultFetchMode = $fetchMode; |
|
99
|
|
|
|
|
100
|
|
|
$this->stmt->setFetchMode($fetchMode, ...$args); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
331 |
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
331 |
|
public function getIterator() |
|
107
|
|
|
{ |
|
108
|
331 |
|
return new StatementIterator($this); |
|
109
|
331 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
334 |
|
public function fetch($fetchMode = null, ...$args) |
|
115
|
|
|
{ |
|
116
|
334 |
|
$fetchMode = $fetchMode ?: $this->defaultFetchMode; |
|
117
|
|
|
|
|
118
|
334 |
|
$row = $this->stmt->fetch($fetchMode, ...$args); |
|
119
|
334 |
|
|
|
120
|
|
|
$iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM); |
|
121
|
|
|
$fixCase = $this->case !== null |
|
122
|
|
|
&& ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED) |
|
123
|
|
|
&& ($this->portability & Connection::PORTABILITY_FIX_CASE); |
|
124
|
381 |
|
|
|
125
|
|
|
$row = $this->fixRow($row, $iterateRow, $fixCase); |
|
126
|
381 |
|
|
|
127
|
|
|
return $row; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
331 |
|
*/ |
|
133
|
|
|
public function fetchAll($fetchMode = null, ...$args) |
|
134
|
331 |
|
{ |
|
135
|
|
|
$fetchMode = $fetchMode ?: $this->defaultFetchMode; |
|
136
|
331 |
|
|
|
137
|
|
|
$rows = $this->stmt->fetchAll($fetchMode, ...$args); |
|
138
|
331 |
|
|
|
139
|
331 |
|
$iterateRow = $this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM); |
|
140
|
331 |
|
$fixCase = $this->case !== null |
|
141
|
331 |
|
&& ($fetchMode === FetchMode::ASSOCIATIVE || $fetchMode === FetchMode::MIXED) |
|
142
|
|
|
&& ($this->portability & Connection::PORTABILITY_FIX_CASE); |
|
143
|
331 |
|
|
|
144
|
|
|
if (! $iterateRow && ! $fixCase) { |
|
145
|
331 |
|
return $rows; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if ($fetchMode === FetchMode::COLUMN) { |
|
149
|
|
|
foreach ($rows as $num => $row) { |
|
150
|
|
|
$rows[$num] = [$row]; |
|
151
|
333 |
|
} |
|
152
|
|
|
} |
|
153
|
333 |
|
|
|
154
|
|
|
foreach ($rows as $num => $row) { |
|
155
|
333 |
|
$rows[$num] = $this->fixRow($row, $iterateRow, $fixCase); |
|
156
|
|
|
} |
|
157
|
333 |
|
|
|
158
|
333 |
|
if ($fetchMode === FetchMode::COLUMN) { |
|
159
|
333 |
|
foreach ($rows as $num => $row) { |
|
160
|
333 |
|
$rows[$num] = $row[0]; |
|
161
|
|
|
} |
|
162
|
333 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
return $rows; |
|
165
|
|
|
} |
|
166
|
333 |
|
|
|
167
|
283 |
|
/** |
|
168
|
283 |
|
* @param mixed $row |
|
169
|
|
|
* @param int $iterateRow |
|
170
|
|
|
* @param bool $fixCase |
|
171
|
|
|
* |
|
172
|
333 |
|
* @return mixed |
|
173
|
333 |
|
*/ |
|
174
|
|
|
protected function fixRow($row, $iterateRow, $fixCase) |
|
175
|
|
|
{ |
|
176
|
333 |
|
if (! $row) { |
|
177
|
283 |
|
return $row; |
|
178
|
283 |
|
} |
|
179
|
|
|
|
|
180
|
|
|
if ($fixCase) { |
|
181
|
|
|
$row = array_change_key_case($row, $this->case); |
|
182
|
333 |
|
} |
|
183
|
|
|
|
|
184
|
|
|
if ($iterateRow) { |
|
185
|
|
|
foreach ($row as $k => $v) { |
|
186
|
|
|
if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $v === '') { |
|
187
|
|
|
$row[$k] = null; |
|
188
|
|
|
} elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($v)) { |
|
189
|
|
|
$row[$k] = rtrim($v); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
334 |
|
} |
|
193
|
|
|
|
|
194
|
334 |
|
return $row; |
|
195
|
331 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
334 |
|
* {@inheritdoc} |
|
199
|
125 |
|
*/ |
|
200
|
|
|
public function fetchColumn($columnIndex = 0) |
|
201
|
|
|
{ |
|
202
|
334 |
|
$value = $this->stmt->fetchColumn($columnIndex); |
|
203
|
333 |
|
|
|
204
|
333 |
|
if ($this->portability & (Connection::PORTABILITY_EMPTY_TO_NULL|Connection::PORTABILITY_RTRIM)) { |
|
205
|
328 |
|
if (($this->portability & Connection::PORTABILITY_EMPTY_TO_NULL) && $value === '') { |
|
206
|
333 |
|
$value = null; |
|
207
|
144 |
|
} elseif (($this->portability & Connection::PORTABILITY_RTRIM) && is_string($value)) { |
|
208
|
|
|
$value = rtrim($value); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
334 |
|
return $value; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* {@inheritdoc} |
|
217
|
|
|
*/ |
|
218
|
|
|
public function rowCount() : int |
|
219
|
|
|
{ |
|
220
|
|
|
assert($this->stmt instanceof DriverStatement); |
|
221
|
|
|
|
|
222
|
|
|
return $this->stmt->rowCount(); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|